summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSchoolGuy <matrixfueller@gmail.com>2021-01-13 09:14:13 +0100
committerSchoolGuy <matrixfueller@gmail.com>2021-01-13 09:14:13 +0100
commita7470248fed0f67a9b6bfd57e81dcc0a96a75fa6 (patch)
treeb987fdedc4c0334436bd58396d4f4ea514b4fb21
parentb59f6c8ce99e208e128911550b259306a65edcd7 (diff)
downloadpython-magic-a7470248fed0f67a9b6bfd57e81dcc0a96a75fa6.tar.gz
Reformat according to PEP
I achieved this via the PyCharm Reformat Shortcut
-rw-r--r--magic/__init__.py109
-rw-r--r--magic/compat.py3
-rw-r--r--setup.py52
-rw-r--r--test/libmagic_test.py1
-rw-r--r--test/run.py4
-rwxr-xr-xtest/test.py38
6 files changed, 109 insertions, 98 deletions
diff --git a/magic/__init__.py b/magic/__init__.py
index 66e33de..5fe6068 100644
--- a/magic/__init__.py
+++ b/magic/__init__.py
@@ -28,9 +28,10 @@ from ctypes import c_char_p, c_int, c_size_t, c_void_p, byref, POINTER
# avoid shadowing the real open with the version from compat.py
_real_open = open
+
class MagicException(Exception):
def __init__(self, message):
- super(MagicException, self).__init__(message)
+ super(Exception, self).__init__(message)
self.message = message
@@ -162,6 +163,7 @@ class Magic:
magic_close(self.cookie)
self.cookie = None
+
_instances = {}
@@ -215,10 +217,10 @@ def from_descriptor(fd, mime=False):
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') \
- or ctypes.util.find_library('libmagic-1') \
- or ctypes.util.find_library('msys-magic-1') #for MSYS2
+ or ctypes.util.find_library('magic1') \
+ or ctypes.util.find_library('cygmagic-1') \
+ or ctypes.util.find_library('libmagic-1') \
+ or ctypes.util.find_library('msys-magic-1') # for MSYS2
# necessary because find_library returns None if it doesn't find the library
if dll:
@@ -228,12 +230,13 @@ if not libmagic or not libmagic._name:
windows_dlls = ['magic1.dll', 'cygmagic-1.dll', 'libmagic-1.dll', 'msys-magic-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'), # flake8:noqa
+ # 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 # flake8:noqa
- }
+ '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, []):
try:
@@ -280,15 +283,7 @@ 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
- # then you'll get inconsistent behavior (crashes) depending on the user's
- # LANG environment variable
- is_unicode = (sys.version_info[0] <= 2 and
- isinstance(filename, unicode)) or \
- (sys.version_info[0] >= 3 and
- isinstance(filename, str))
- if is_unicode:
+ if isinstance(filename, str):
return filename.encode('utf-8', 'surrogateescape')
else:
return filename
@@ -329,6 +324,7 @@ _magic_buffer.errcheck = errorcheck_null
def magic_buffer(cookie, buf):
return _magic_buffer(cookie, buf, len(buf))
+
magic_descriptor = libmagic.magic_descriptor
magic_descriptor.restype = c_char_p
magic_descriptor.argtypes = [magic_t, c_int]
@@ -379,12 +375,14 @@ if hasattr(libmagic, 'magic_setparam') and hasattr(libmagic, 'magic_getparam'):
_magic_getparam.argtypes = [magic_t, c_int, POINTER(c_size_t)]
_magic_getparam.errcheck = errorcheck_negative_one
+
def magic_setparam(cookie, param, val):
if not _has_param:
raise NotImplementedError("magic_setparam not implemented")
v = c_size_t(val)
return _magic_setparam(cookie, param, byref(v))
+
def magic_getparam(cookie, param):
if not _has_param:
raise NotImplementedError("magic_getparam not implemented")
@@ -392,6 +390,7 @@ def magic_getparam(cookie, param):
_magic_getparam(cookie, param, byref(val))
return val.value
+
_has_version = False
if hasattr(libmagic, "magic_version"):
_has_version = True
@@ -399,53 +398,55 @@ if hasattr(libmagic, "magic_version"):
magic_version.restype = c_int
magic_version.argtypes = []
+
def version():
if not _has_version:
raise NotImplementedError("magic_version not implemented")
return magic_version()
-MAGIC_NONE = 0x000000 # No flags
-MAGIC_DEBUG = 0x000001 # Turn on debugging
-MAGIC_SYMLINK = 0x000002 # Follow symlinks
-MAGIC_COMPRESS = 0x000004 # Check inside compressed files
-MAGIC_DEVICES = 0x000008 # Look at the contents of devices
-MAGIC_MIME_TYPE = 0x000010 # Return a mime string
-MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding
+
+MAGIC_NONE = 0x000000 # No flags
+MAGIC_DEBUG = 0x000001 # Turn on debugging
+MAGIC_SYMLINK = 0x000002 # Follow symlinks
+MAGIC_COMPRESS = 0x000004 # Check inside compressed files
+MAGIC_DEVICES = 0x000008 # Look at the contents of devices
+MAGIC_MIME_TYPE = 0x000010 # Return a mime string
+MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding
# TODO: should be
# MAGIC_MIME = MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING
-MAGIC_MIME = 0x000010 # Return a mime string
-MAGIC_EXTENSION = 0x1000000 # Return a /-separated list of extensions
-
-MAGIC_CONTINUE = 0x000020 # Return all matches
-MAGIC_CHECK = 0x000040 # Print warnings to stderr
-MAGIC_PRESERVE_ATIME = 0x000080 # Restore access time on exit
-MAGIC_RAW = 0x000100 # Don't translate unprintable chars
-MAGIC_ERROR = 0x000200 # Handle ENOENT etc as real errors
-
-MAGIC_NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files
-MAGIC_NO_CHECK_TAR = 0x002000 # Don't check for tar files
-MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
-MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
-MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
-MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
-MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
-MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
-MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
-
-MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
-MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
-MAGIC_PARAM_ELF_PHNUM_MAX = 2 # Max ELF notes processed
-MAGIC_PARAM_ELF_SHNUM_MAX = 3 # Max ELF program sections processed
-MAGIC_PARAM_ELF_NOTES_MAX = 4 # # Max ELF sections processed
-MAGIC_PARAM_REGEX_MAX = 5 # Length limit for regex searches
-MAGIC_PARAM_BYTES_MAX = 6 # Max number of bytes to read from file
+MAGIC_MIME = 0x000010 # Return a mime string
+MAGIC_EXTENSION = 0x1000000 # Return a /-separated list of extensions
+
+MAGIC_CONTINUE = 0x000020 # Return all matches
+MAGIC_CHECK = 0x000040 # Print warnings to stderr
+MAGIC_PRESERVE_ATIME = 0x000080 # Restore access time on exit
+MAGIC_RAW = 0x000100 # Don't translate unprintable chars
+MAGIC_ERROR = 0x000200 # Handle ENOENT etc as real errors
+
+MAGIC_NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files
+MAGIC_NO_CHECK_TAR = 0x002000 # Don't check for tar files
+MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
+MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
+MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
+MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
+MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
+MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
+MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
+
+MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
+MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
+MAGIC_PARAM_ELF_PHNUM_MAX = 2 # Max ELF notes processed
+MAGIC_PARAM_ELF_SHNUM_MAX = 3 # Max ELF program sections processed
+MAGIC_PARAM_ELF_NOTES_MAX = 4 # # Max ELF sections processed
+MAGIC_PARAM_REGEX_MAX = 5 # Length limit for regex searches
+MAGIC_PARAM_BYTES_MAX = 6 # Max number of bytes to read from file
+
# This package name conflicts with the one provided by upstream
# libmagic. This is a common source of confusion for users. To
# resolve, We ship a copy of that module, and expose it's functions
# wrapped in deprecation warnings.
def add_compat(to_module):
-
import warnings, re
from magic import compat
@@ -456,6 +457,7 @@ def add_compat(to_module):
DeprecationWarning)
return compat[fn](*args, **kwargs)
+
return _
fn = [('detect_from_filename', 'magic.from_file'),
@@ -466,7 +468,7 @@ def add_compat(to_module):
# for now, disable the deprecation warning until theres clarity on
# what the merged module should look like
to_module[fname] = compat.__dict__.get(fname)
- #to_module[fname] = deprecation_wrapper(compat.__dict__, fname, alternate)
+ # to_module[fname] = deprecation_wrapper(compat.__dict__, fname, alternate)
# copy constants over, ensuring there's no conflicts
is_const_re = re.compile("^[A-Z_]+$")
@@ -483,4 +485,5 @@ def add_compat(to_module):
else:
to_module[name] = value
+
add_compat(globals())
diff --git a/magic/compat.py b/magic/compat.py
index 662569e..7b39691 100644
--- a/magic/compat.py
+++ b/magic/compat.py
@@ -19,6 +19,7 @@ def _init():
"""
return ctypes.cdll.LoadLibrary(find_library('magic'))
+
_libraries = {}
_libraries['magic'] = _init()
@@ -55,6 +56,8 @@ FileMagic = namedtuple('FileMagic', ('mime_type', 'encoding', 'name'))
class magic_set(Structure):
pass
+
+
magic_set._fields_ = []
magic_t = POINTER(magic_set)
diff --git a/setup.py b/setup.py
index 43c2011..234c8a3 100644
--- a/setup.py
+++ b/setup.py
@@ -12,29 +12,29 @@ def read(file_name):
encoding='utf-8') as f:
return f.read()
-setup(name='python-magic',
- description='File type identification using libmagic',
- author='Adam Hupp',
- author_email='adam@hupp.org',
- url="http://github.com/ahupp/python-magic",
- version='0.4.18',
- py_modules=['magic'],
- long_description=read('README.md'),
- long_description_content_type='text/markdown',
- keywords="mime magic file",
- license="MIT",
- python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',
- classifiers=[
- 'Intended Audience :: Developers',
- 'License :: OSI Approved :: MIT License',
- 'Programming Language :: Python',
- 'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.7',
- 'Programming Language :: Python :: 3',
- 'Programming Language :: Python :: 3.5',
- 'Programming Language :: Python :: 3.6',
- 'Programming Language :: Python :: 3.7',
- 'Programming Language :: Python :: 3.8',
- 'Programming Language :: Python :: Implementation :: CPython',
- ],
- )
+
+setup(
+ name='python-magic',
+ description='File type identification using libmagic',
+ author='Adam Hupp',
+ author_email='adam@hupp.org',
+ url="http://github.com/ahupp/python-magic",
+ version='0.4.18',
+ py_modules=['magic'],
+ long_description=read('README.md'),
+ long_description_content_type='text/markdown',
+ keywords="mime magic file",
+ license="MIT",
+ python_requires='>=3.0',
+ classifiers=[
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3.5',
+ 'Programming Language :: Python :: 3.6',
+ 'Programming Language :: Python :: 3.7',
+ 'Programming Language :: Python :: 3.8',
+ 'Programming Language :: Python :: Implementation :: CPython',
+ ],
+)
diff --git a/test/libmagic_test.py b/test/libmagic_test.py
index 4c0a80d..e1623b4 100644
--- a/test/libmagic_test.py
+++ b/test/libmagic_test.py
@@ -34,5 +34,6 @@ class MagicTestCase(unittest.TestCase):
result = magic.detect_from_content(fobj.read(4096))
self.assert_result(result)
+
if __name__ == '__main__':
unittest.main()
diff --git a/test/run.py b/test/run.py
index a9dd39a..48203df 100644
--- a/test/run.py
+++ b/test/run.py
@@ -1,4 +1,3 @@
-
import subprocess
import os.path
import sys
@@ -10,12 +9,13 @@ new_env = {
'PYTHONPATH': os.path.join(this_dir, ".."),
}
+
def has_py(version):
ret = subprocess.run("which %s" % version, shell=True, stdout=subprocess.DEVNULL)
return ret.returncode == 0
-def run_test(versions):
+def run_test(versions):
found = False
for i in versions:
if not has_py(i):
diff --git a/test/test.py b/test/test.py
index 2865a66..ec479f0 100755
--- a/test/test.py
+++ b/test/test.py
@@ -1,8 +1,9 @@
import os
+
# for output which reports a local time
os.environ['TZ'] = 'GMT'
-if os.environ.get('LC_ALL','') != 'en_US.UTF-8':
+if os.environ.get('LC_ALL', '') != 'en_US.UTF-8':
# this ensure we're in a utf-8 default filesystem encoding which is
# necessary for some tests
raise Exception("must run `export LC_ALL=en_US.UTF-8` before running test suite")
@@ -14,6 +15,7 @@ import unittest
import magic
import sys
+
class MagicTest(unittest.TestCase):
TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testdata')
@@ -57,7 +59,8 @@ class MagicTest(unittest.TestCase):
magic.from_file(filename.encode('utf-8'), mime=True))
def test_from_descriptor_str_and_bytes(self):
- with open(os.path.join(self.TESTDATA_DIR, "test.pdf")) as f:
+ filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
+ with open(filename) as f:
self.assertEqual('application/pdf',
magic.from_descriptor(f.fileno(), mime=True))
self.assertEqual('application/pdf',
@@ -73,11 +76,10 @@ class MagicTest(unittest.TestCase):
m.from_buffer(b'#!/usr/bin/env python\nprint("foo")')
in ("text/x-python", "text/x-script.python"))
-
-
def test_open_file(self):
m = magic.Magic(mime=True)
- with open(os.path.join(self.TESTDATA_DIR, "test.pdf")) as f:
+ filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
+ with open(filename) as f:
self.assertEqual("application/pdf", m.from_open_file(f))
def test_mime_types(self):
@@ -106,18 +108,18 @@ class MagicTest(unittest.TestCase):
'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", last modified'
- ': Sun Jun 29 01:32:52 2008, from Unix, original size 15',
- 'gzip compressed data, was "test", '
- 'last modified: Sun Jun 29 01:32:52 2008, '
- 'from Unix, original size modulo 2^32 15',
- 'gzip compressed data, was "test", last modified'
- ': Sun Jun 29 01:32:52 2008, from Unix, truncated'
- ),
+ ('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", last modified'
+ ': Sun Jun 29 01:32:52 2008, from Unix, original size 15',
+ 'gzip compressed data, was "test", '
+ 'last modified: Sun Jun 29 01:32:52 2008, '
+ 'from Unix, original size modulo 2^32 15',
+ 'gzip compressed data, was "test", last modified'
+ ': Sun Jun 29 01:32:52 2008, from Unix, truncated'
+ ),
'text.txt': 'ASCII text',
'test.snappy.parquet': ('Apache Parquet', 'Par archive data'),
}, buf_equals_file=False)
@@ -194,6 +196,7 @@ class MagicTest(unittest.TestCase):
try:
def t(x, y):
raise magic.MagicException("passthrough")
+
magic.magic_buffer = t
with self.assertRaises(magic.MagicException):
@@ -214,5 +217,6 @@ class MagicTest(unittest.TestCase):
with open(os.path.join(self.TESTDATA_DIR, 'name_use.jpg'), 'rb') as f:
m.from_buffer(f.read())
+
if __name__ == '__main__':
unittest.main()