[Git][reproducible-builds/diffoscope][master] Prefer to use magic.Magic over the magic.open compatibility interface....

Chris Lamb gitlab at salsa.debian.org
Thu Jan 28 12:22:44 UTC 2021



Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
c72c30f2 by Chris Lamb at 2021-01-28T12:22:17+00:00
Prefer to use magic.Magic over the magic.open compatibility interface. (Closes: reproducible-builds/diffoscope#236)

See <https://github.com/ahupp/python-magic/blob/master/COMPAT.md> for more info.

- - - - -


1 changed file:

- diffoscope/comparators/utils/file.py


Changes:

=====================================
diffoscope/comparators/utils/file.py
=====================================
@@ -65,37 +65,37 @@ def _run_tests(fold, tests):
 
 
 class File(metaclass=abc.ABCMeta):
-    if hasattr(magic, "open"):  # use Magic-file-extensions from file
+    if hasattr(magic, "Magic"):  # use python-magic
 
         @classmethod
         def guess_file_type(cls, path):
             if not hasattr(cls, "_mimedb"):
-                cls._mimedb = magic.open(magic.NONE)
-                cls._mimedb.load()
-            return cls._mimedb.file(
-                path.encode("utf-8", errors="surrogateescape")
-            )
+                cls._mimedb = magic.Magic()
+            return maybe_decode(cls._mimedb.from_file(path))
 
         @classmethod
         def guess_encoding(cls, path):
             if not hasattr(cls, "_mimedb_encoding"):
-                cls._mimedb_encoding = magic.open(magic.MAGIC_MIME_ENCODING)
-                cls._mimedb_encoding.load()
-            return cls._mimedb_encoding.file(path)
+                cls._mimedb_encoding = magic.Magic(mime_encoding=True)
+            return maybe_decode(cls._mimedb_encoding.from_file(path))
 
-    else:  # use python-magic
+    else:  # use Magic-file-extensions from file
 
         @classmethod
         def guess_file_type(cls, path):
             if not hasattr(cls, "_mimedb"):
-                cls._mimedb = magic.Magic()
-            return maybe_decode(cls._mimedb.from_file(path))
+                cls._mimedb = magic.open(magic.NONE)
+                cls._mimedb.load()
+            return cls._mimedb.file(
+                path.encode("utf-8", errors="surrogateescape")
+            )
 
         @classmethod
         def guess_encoding(cls, path):
             if not hasattr(cls, "_mimedb_encoding"):
-                cls._mimedb_encoding = magic.Magic(mime_encoding=True)
-            return maybe_decode(cls._mimedb_encoding.from_file(path))
+                cls._mimedb_encoding = magic.open(magic.MAGIC_MIME_ENCODING)
+                cls._mimedb_encoding.load()
+            return cls._mimedb_encoding.file(path)
 
     def __init__(self, container=None):
         self._comments = []



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/c72c30f29ea3760eb4c785644dc7cd4c26833740

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/c72c30f29ea3760eb4c785644dc7cd4c26833740
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20210128/b42e5cb3/attachment.htm>


More information about the rb-commits mailing list