python-python-magic/magic-pep8.patch
Tomáš Chvátal 070d16a1c8 - Version update to 0.4.15:
* Few test tweaks
- Enable tests
- Add patch to fix gzip detection in tests:
  * magic-tests.patch
- Add patch to fix working with new file:
  * magic-new-file.patch
- Add patch to work with new mimedb:
  * magic-new-mime.patch
- Add patch magic-pep8.patch to have other patches apply cleanly

OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-python-magic?expand=0&rev=9
2018-08-21 10:50:28 +00:00

208 lines
7.6 KiB
Diff

From 828ff0289642a95ec00fe4f9a55da51c455277c8 Mon Sep 17 00:00:00 2001
From: "Guido A.J. Stevens" <guido.stevens@cosent.nl>
Date: Thu, 25 Jan 2018 08:29:18 +0000
Subject: [PATCH] PEP8
---
magic.py | 30 ++++++++++++++++--------------
test/test.py | 29 ++++++++++++++++++-----------
2 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/magic.py b/magic.py
index dd86389..83b906d 100644
--- a/magic.py
+++ b/magic.py
@@ -19,7 +19,6 @@
import sys
import glob
-import os.path
import ctypes
import ctypes.util
import threading
@@ -63,7 +62,7 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
self.cookie = magic_open(self.flags)
self.lock = threading.Lock()
-
+
magic_load(self.cookie, magic_file)
def from_buffer(self, buf):
@@ -76,7 +75,7 @@ def from_buffer(self, buf):
# otherwise this string is passed as wchar*
# which is not what libmagic expects
if type(buf) == str and str != bytes:
- buf = buf.encode('utf-8', errors='replace')
+ buf = buf.encode('utf-8', errors='replace')
return maybe_decode(magic_buffer(self.cookie, buf))
except MagicException as e:
return self._handle509Bug(e)
@@ -99,7 +98,7 @@ def _handle509Bug(self, e):
return "application/octet-stream"
else:
raise e
-
+
def __del__(self):
# no _thread_check here because there can be no other
# references to this object at this point.
@@ -117,12 +116,14 @@ def __del__(self):
_instances = {}
+
def _get_magic_type(mime):
i = _instances.get(mime)
if i is None:
i = _instances[mime] = Magic(mime=mime)
return i
+
def from_file(filename, mime=False):
""""
Accepts a filename and returns the detected filetype. Return
@@ -135,6 +136,7 @@ def from_file(filename, mime=False):
m = _get_magic_type(mime)
return m.from_file(filename)
+
def from_buffer(buffer, mime=False):
"""
Accepts a binary string and returns the detected filetype. Return
@@ -148,25 +150,25 @@ def from_buffer(buffer, mime=False):
return m.from_buffer(buffer)
-
-
libmagic = None
# Let's try to find magic or magic1
-dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1') or ctypes.util.find_library('cygmagic-1')
+dll = ctypes.util.find_library('magic') \
+ or ctypes.util.find_library('magic1') \
+ or ctypes.util.find_library('cygmagic-1')
-# This is necessary because find_library returns None if it doesn't find the library
+# necessary because find_library returns None if it doesn't find the library
if dll:
libmagic = ctypes.CDLL(dll)
if not libmagic or not libmagic._name:
- windows_dlls = ['magic1.dll','cygmagic-1.dll']
+ windows_dlls = ['magic1.dll', 'cygmagic-1.dll']
platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib',
'/usr/local/lib/libmagic.dylib'] +
- # Assumes there will only be one version installed
- glob.glob('/usr/local/Cellar/libmagic/*/lib/libmagic.dylib'),
+ # Assumes there will only be one version installed
+ glob.glob('/usr/local/Cellar/libmagic/*/lib/libmagic.dylib'), # flake8:noqa
'win32': windows_dlls,
'cygwin': windows_dlls,
- 'linux': ['libmagic.so.1'], # fallback for some Linuxes (e.g. Alpine) where library search does not work
+ 'linux': ['libmagic.so.1'], # fallback for some Linuxes (e.g. Alpine) where library search does not work # flake8:noqa
}
platform = 'linux' if sys.platform.startswith('linux') else sys.platform
for dll in platform_to_lib.get(platform, []):
@@ -204,13 +206,13 @@ def maybe_decode(s):
return s
else:
return s.decode('utf-8')
-
+
def coerce_filename(filename):
if filename is None:
return None
# ctypes will implicitly convert unicode strings to bytes with
- # .encode('ascii'). If you use the filesystem encoding
+ # .encode('ascii'). If you use the filesystem encoding
# then you'll get inconsistent behavior (crashes) depending on the user's
# LANG environment variable
is_unicode = (sys.version_info[0] <= 2 and
diff --git a/test/test.py b/test/test.py
index c6e2d9c..a92972b 100755
--- a/test/test.py
+++ b/test/test.py
@@ -1,4 +1,4 @@
-import os, sys
+import os
# for output which reports a local time
os.environ['TZ'] = 'GMT'
import shutil
@@ -7,6 +7,7 @@
import magic
+
class MagicTest(unittest.TestCase):
TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata')
@@ -15,8 +16,8 @@ def assert_values(self, m, expected_values):
try:
filename = os.path.join(self.TESTDATA_DIR, filename)
except TypeError:
- filename = os.path.join(self.TESTDATA_DIR.encode('utf-8'), filename)
-
+ filename = os.path.join(
+ self.TESTDATA_DIR.encode('utf-8'), filename)
if type(expected_value) is not tuple:
expected_value = (expected_value,)
@@ -39,7 +40,8 @@ def test_from_buffer_str_and_bytes(self):
self.assertEqual("text/x-python", m.from_buffer(b))
def test_mime_types(self):
- dest = os.path.join(MagicTest.TESTDATA_DIR, b'\xce\xbb'.decode('utf-8'))
+ dest = os.path.join(MagicTest.TESTDATA_DIR,
+ b'\xce\xbb'.decode('utf-8'))
shutil.copyfile(os.path.join(MagicTest.TESTDATA_DIR, 'lambda'), dest)
try:
m = magic.Magic(mime=True)
@@ -56,14 +58,16 @@ def test_mime_types(self):
def test_descriptions(self):
m = magic.Magic()
- os.environ['TZ'] = 'UTC' # To get the last modified date of test.gz in UTC
+ os.environ['TZ'] = 'UTC' # To get last modified date of test.gz in UTC
try:
self.assert_values(m, {
'magic._pyc_': 'python 2.4 byte-compiled',
'test.pdf': 'PDF document, version 1.2',
'test.gz':
- ('gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
- 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix'),
+ ('gzip compressed data, was "test", from Unix, last '
+ 'modified: Sun Jun 29 01:32:52 2008',
+ 'gzip compressed data, was "test", last modified'
+ ': Sun Jun 29 01:32:52 2008, from Unix'),
'text.txt': 'ASCII text',
})
finally:
@@ -94,18 +98,21 @@ def test_keep_going(self):
self.assertEqual(m.from_file(filename), 'image/jpeg')
m = magic.Magic(mime=True, keep_going=True)
- self.assertEqual(m.from_file(filename), 'image/jpeg\\012- application/octet-stream')
-
+ self.assertEqual(m.from_file(filename),
+ 'image/jpeg\\012- application/octet-stream')
def test_rethrow(self):
old = magic.magic_buffer
try:
- def t(x,y):
+ def t(x, y):
raise magic.MagicException("passthrough")
magic.magic_buffer = t
- self.assertRaises(magic.MagicException, magic.from_buffer, "hello", True)
+ with self.assertRaises(magic.MagicException):
+ magic.from_buffer("hello", True)
finally:
magic.magic_buffer = old
+
+
if __name__ == '__main__':
unittest.main()