Package: eyed3 / 0.8.8-1

support-debian-python-magic Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Support debian version of python-magic along with pypi python-magic used
upstream.
--- a/src/eyed3/utils/__init__.py
+++ b/src/eyed3/utils/__init__.py
@@ -48,19 +48,37 @@
 ID3_MIME_TYPE_EXTENSIONS = (".id3", ".tag")
 
 
-class MagicTypes(magic.Magic):
-    def __init__(self):
-        magic.Magic.__init__(self, mime=True, mime_encoding=False,
-                             keep_going=False)
-
-    def guess_type(self, filename):
-        if os.path.splitext(filename)[1] in ID3_MIME_TYPE_EXTENSIONS:
-            return ID3_MIME_TYPE
-        try:
-            return self.from_file(filename)
-        except UnicodeEncodeError:
+def _retry_with_surrogate_encode(f, arg):
+    try:
+        return f(arg)
+    except UnicodeEncodeError:
+        return f(arg.encode("utf-8", 'surrogateescape'))
+
+
+# handle magic name conflict
+if hasattr(magic, 'from_file'):  # pypi python-magic
+    class MagicTypes(magic.Magic):
+        def __init__(self):
+            magic.Magic.__init__(self, mime=True, mime_encoding=False,
+                                 keep_going=False)
+
+        def guess_type(self, filename):
+            if os.path.splitext(filename)[1] in ID3_MIME_TYPE_EXTENSIONS:
+                return ID3_MIME_TYPE
             # https://github.com/ahupp/python-magic/pull/144
-            return self.from_file(filename.encode("utf-8", 'surrogateescape'))
+            return _retry_with_surrogate_encode(self.from_file, filename)
+
+else:                            # debian python-magic
+    class MagicTypes(object):
+        def __init__(self):
+            flags = magic.MIME_TYPE
+            self._magic = magic.open(flags)
+            self._magic.load()
+
+        def guess_type(self, filename):
+            if os.path.splitext(filename)[1] in ID3_MIME_TYPE_EXTENSIONS:
+                return ID3_MIME_TYPE
+            return _retry_with_surrogate_encode(self._magic.file, filename)
 
 
 _mime_types = MagicTypes()