| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- Subject: Format with ruff
- Origin: upstream, commit 0.4.27-41-g62bd3c6 <https://github.com/ahupp/python-magic/commit/0.4.27-41-g62bd3c6>
- Author: Adam Hupp <adam@hupp.org>
- Date: Sat Mar 1 17:10:13 2025 -0800
- --- a/magic/__init__.py
- +++ b/magic/__init__.py
- @@ -38,12 +38,27 @@
- Magic is a wrapper around the libmagic C library.
- """
-
- - def __init__(self, mime=False, magic_file=None, mime_encoding=False,
- - keep_going=False, uncompress=False, raw=False, extension=False,
- - follow_symlinks=False, check_tar=True, check_soft=True,
- - check_apptype=True, check_elf=True, check_text=True,
- - check_cdf=True, check_csv=True, check_encoding=True,
- - check_json=True, check_simh=True):
- + def __init__(
- + self,
- + mime=False,
- + magic_file=None,
- + mime_encoding=False,
- + keep_going=False,
- + uncompress=False,
- + raw=False,
- + extension=False,
- + follow_symlinks=False,
- + check_tar=True,
- + check_soft=True,
- + check_apptype=True,
- + check_elf=True,
- + check_text=True,
- + check_cdf=True,
- + check_csv=True,
- + check_encoding=True,
- + check_json=True,
- + check_simh=True,
- + ):
- """
- Create a new libmagic wrapper.
-
- @@ -101,7 +116,9 @@
- # MAGIC_EXTENSION was added in 523 or 524, so bail if
- # it doesn't appear to be available
- if extension and (not _has_version or version() < 524):
- - raise NotImplementedError('MAGIC_EXTENSION is not supported in this version of libmagic')
- + raise NotImplementedError(
- + "MAGIC_EXTENSION is not supported in this version of libmagic"
- + )
-
- # For https://github.com/ahupp/python-magic/issues/190
- # libmagic has fixed internal limits that some files exceed, causing
- @@ -128,7 +145,7 @@
- # which is not what libmagic expects
- # NEXTBREAK: only take bytes
- 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)
- @@ -176,7 +193,7 @@
- # incorrect fix for a threading problem, however I'm leaving
- # it in because it's harmless and I'm slightly afraid to
- # remove it.
- - if hasattr(self, 'cookie') and self.cookie and magic_close:
- + if hasattr(self, "cookie") and self.cookie and magic_close:
- magic_close(self.cookie)
- self.cookie = None
-
- @@ -192,7 +209,7 @@
-
-
- def from_file(filename, mime=False):
- - """"
- + """
- Accepts a filename and returns the detected filetype. Return
- value is the mimetype if mime=True, otherwise a human readable
- name.
- @@ -230,7 +247,9 @@
- m = _get_magic_type(mime)
- return m.from_descriptor(fd)
-
- +
- from . import loader
- +
- libmagic = loader.load_lib()
-
- magic_t = ctypes.c_void_p
- @@ -261,20 +280,23 @@
- else:
- # backslashreplace here because sometimes libmagic will return metadata in the charset
- # of the file, which is unknown to us (e.g the title of a Word doc)
- - return s.decode('utf-8', 'backslashreplace')
- + return s.decode("utf-8", "backslashreplace")
-
-
- try:
- from os import PathLike
- +
- def unpath(filename):
- if isinstance(filename, PathLike):
- return filename.__fspath__()
- else:
- return filename
- except ImportError:
- +
- def unpath(filename):
- return filename
-
- +
- def coerce_filename(filename):
- if filename is None:
- return None
- @@ -286,12 +308,11 @@
- # then you'll get inconsistent behavior (crashes) depending on the user's
- # LANG environment variable
- # NEXTBREAK: remove
- - is_unicode = (sys.version_info[0] <= 2 and
- - isinstance(filename, unicode)) or \
- - (sys.version_info[0] >= 3 and
- - isinstance(filename, str))
- + is_unicode = (sys.version_info[0] <= 2 and isinstance(filename, unicode)) or (
- + sys.version_info[0] >= 3 and isinstance(filename, str)
- + )
- if is_unicode:
- - return filename.encode('utf-8', 'surrogateescape')
- + return filename.encode("utf-8", "surrogateescape")
- else:
- return filename
-
- @@ -370,7 +391,7 @@
- magic_compile.argtypes = [magic_t, c_char_p]
-
- _has_param = False
- -if hasattr(libmagic, 'magic_setparam') and hasattr(libmagic, 'magic_getparam'):
- +if hasattr(libmagic, "magic_setparam") and hasattr(libmagic, "magic_getparam"):
- _has_param = True
- _magic_setparam = libmagic.magic_setparam
- _magic_setparam.restype = c_int
- @@ -443,8 +464,8 @@
- MAGIC_NO_CHECK_CDF = 0x0040000 # Don't check for CDF files
- MAGIC_NO_CHECK_CSV = 0x0080000 # Don't check for CSV files
- MAGIC_NO_CHECK_ENCODING = 0x0200000 # Don't check text encodings
- -MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
- -MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
- +MAGIC_NO_CHECK_JSON = 0x0400000 # Don't check for JSON files
- +MAGIC_NO_CHECK_SIMH = 0x0800000 # Don't check for SIMH tape files
-
- MAGIC_PARAM_INDIR_MAX = 0 # Recursion limit for indirect magic
- MAGIC_PARAM_NAME_MAX = 1 # Use count limit for name/use magic
- @@ -468,22 +489,20 @@
- warnings.warn(
- "Using compatibility mode with libmagic's python binding. "
- "See https://github.com/ahupp/python-magic/blob/master/COMPAT.md for details.",
- - PendingDeprecationWarning)
- + PendingDeprecationWarning,
- + )
-
- return fn(*args, **kwargs)
-
- return _
-
- - fn = ['detect_from_filename',
- - 'detect_from_content',
- - 'detect_from_fobj',
- - 'open']
- + fn = ["detect_from_filename", "detect_from_content", "detect_from_fobj", "open"]
- for fname in fn:
- to_module[fname] = deprecation_wrapper(compat.__dict__[fname])
-
- # copy constants over, ensuring there's no conflicts
- is_const_re = re.compile("^[A-Z_]+$")
- - allowed_inconsistent = set(['MAGIC_MIME'])
- + allowed_inconsistent = set(["MAGIC_MIME"])
- for name, value in compat.__dict__.items():
- if is_const_re.match(name):
- if name in to_module:
- --- a/magic/__init__.pyi
- +++ b/magic/__init__.pyi
- @@ -11,7 +11,25 @@
- flags: int = ...
- cookie: Any = ...
- lock: threading.Lock = ...
- - def __init__(self, mime: bool = ..., magic_file: Optional[Any] = ..., mime_encoding: bool = ..., keep_going: bool = ..., uncompress: bool = ..., raw: bool = ..., extension: bool = ..., follow_symlinks: bool = ..., check_tar: bool = ..., check_soft: bool = ..., check_apptype: bool = ..., check_elf: bool = ..., check_text: bool = ..., check_encoding: bool = ..., check_json: bool = ..., check_simh: bool = ...) -> None: ...
- + def __init__(
- + self,
- + mime: bool = ...,
- + magic_file: Optional[Any] = ...,
- + mime_encoding: bool = ...,
- + keep_going: bool = ...,
- + uncompress: bool = ...,
- + raw: bool = ...,
- + extension: bool = ...,
- + follow_symlinks: bool = ...,
- + check_tar: bool = ...,
- + check_soft: bool = ...,
- + check_apptype: bool = ...,
- + check_elf: bool = ...,
- + check_text: bool = ...,
- + check_encoding: bool = ...,
- + check_json: bool = ...,
- + check_simh: bool = ...,
- + ) -> None: ...
- def from_buffer(self, buf: Union[bytes, str]) -> Text: ...
- def from_file(self, filename: Union[bytes, str, PathLike]) -> Text: ...
- def from_descriptor(self, fd: int, mime: bool = ...) -> Text: ...
- --- /dev/null
- +++ b/ruff.toml
- @@ -0,0 +1,3 @@
- +exclude = ["magic/compat.py"]
- +
- +
- --- a/test/libmagic_test.py
- +++ b/test/libmagic_test.py
- @@ -6,16 +6,20 @@
- import os.path
-
- # magic_descriptor is broken (?) in centos 7, so don't run those tests
- -SKIP_FROM_DESCRIPTOR = bool(os.environ.get('SKIP_FROM_DESCRIPTOR'))
- +SKIP_FROM_DESCRIPTOR = bool(os.environ.get("SKIP_FROM_DESCRIPTOR"))
-
- -TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'testdata'))
- +TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testdata"))
-
-
- class MagicTestCase(unittest.TestCase):
- - filename = os.path.join(TESTDATA_DIR, 'test.pdf')
- - expected_mime_type = 'application/pdf'
- - expected_encoding = 'us-ascii'
- - expected_name = ('PDF document, version 1.2', 'PDF document, version 1.2, 2 pages', 'PDF document, version 1.2, 2 page(s)')
- + filename = os.path.join(TESTDATA_DIR, "test.pdf")
- + expected_mime_type = "application/pdf"
- + expected_encoding = "us-ascii"
- + expected_name = (
- + "PDF document, version 1.2",
- + "PDF document, version 1.2, 2 pages",
- + "PDF document, version 1.2, 2 page(s)",
- + )
-
- def assert_result(self, result):
- self.assertEqual(result.mime_type, self.expected_mime_type)
- @@ -27,11 +31,9 @@
- self.assert_result(result)
-
- def test_detect_from_fobj(self):
- -
- if SKIP_FROM_DESCRIPTOR:
- self.skipTest("magic_descriptor is broken in this version of libmagic")
-
- -
- with open(self.filename) as fobj:
- result = magic.detect_from_fobj(fobj)
- self.assert_result(result)
- @@ -41,10 +43,10 @@
- # this avoids hitting a bug in python3+libfile bindings
- # see https://github.com/ahupp/python-magic/issues/152
- # for a similar issue
- - with open(self.filename, 'rb') as fobj:
- + with open(self.filename, "rb") as fobj:
- result = magic.detect_from_content(fobj.read(4096))
- self.assert_result(result)
-
-
- -if __name__ == '__main__':
- +if __name__ == "__main__":
- unittest.main()
|