[Reproducible-commits] [diffoscope] 04/11: Make file detection compatible with PyPI python-magic

Jérémy Bobbio lunar at moszumanska.debian.org
Sat Nov 7 16:29:01 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository diffoscope.

commit 4fd5605f12016851250a95d72607bc9835a0e46c
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Sat Nov 7 16:28:17 2015 +0100

    Make file detection compatible with PyPI python-magic
    
    The Python module available in Debian as python-magic is coming from
    the source of file. In Python land, its name is “Magic-file-extensions”.
    
    Meanwhile, on PyPI, there is a module named python-magic which uses
    ctypes to call the system libmagic. This module has a different
    interface than Magic-file-extensions.
    
    We add the necessary compatibility code so diffoscope can eventually
    be published on PyPI.
---
 diffoscope/comparators/binary.py | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index 38ca2f6..cc2d5d9 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -81,19 +81,32 @@ def needs_content(original_method):
     return wrapper
 
 class File(object, metaclass=ABCMeta):
-    @classmethod
-    def guess_file_type(self, path):
-        if not hasattr(self, '_mimedb'):
-            self._mimedb = magic.open(magic.NONE)
-            self._mimedb.load()
-        return self._mimedb.file(path)
-
-    @classmethod
-    def guess_encoding(self, path):
-        if not hasattr(self, '_mimedb_encoding'):
-            self._mimedb_encoding = magic.open(magic.MAGIC_MIME_ENCODING)
-            self._mimedb_encoding.load()
-        return self._mimedb_encoding.file(path)
+    if hasattr(magic, 'open'): # use Magic-file-extensions from file
+        @classmethod
+        def guess_file_type(self, path):
+            if not hasattr(self, '_mimedb'):
+                self._mimedb = magic.open(magic.NONE)
+                self._mimedb.load()
+            return self._mimedb.file(path)
+
+        @classmethod
+        def guess_encoding(self, path):
+            if not hasattr(self, '_mimedb_encoding'):
+                self._mimedb_encoding = magic.open(magic.MAGIC_MIME_ENCODING)
+                self._mimedb_encoding.load()
+            return self._mimedb_encoding.file(path)
+    else: # use python-magic
+        @classmethod
+        def guess_file_type(self, path):
+            if not hasattr(self, '_mimedb'):
+                self._mimedb = magic.Magic()
+            return self._mimedb.from_file(path).decode('utf-8')
+
+        @classmethod
+        def guess_encoding(self, path):
+            if not hasattr(self, '_mimedb_encoding'):
+                self._mimedb_encoding = magic.Magic(mime_encoding=True)
+            return self._mimedb_encoding.from_file(path).decode('utf-8')
 
     def __repr__(self):
         return '<%s %s %s>' % (self.__class__, self.name, self.path)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list