From a7470248fed0f67a9b6bfd57e81dcc0a96a75fa6 Mon Sep 17 00:00:00 2001 From: SchoolGuy Date: Wed, 13 Jan 2021 09:14:13 +0100 Subject: Reformat according to PEP I achieved this via the PyCharm Reformat Shortcut --- magic/__init__.py | 109 ++++++++++++++++++++++++++++-------------------------- magic/compat.py | 3 ++ 2 files changed, 59 insertions(+), 53 deletions(-) (limited to 'magic') 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) -- cgit v1.2.1