MCPcopy Create free account
hub / github.com/ahupp/python-magic / __init__

Method __init__

magic/__init__.py:43–92  ·  view source on GitHub ↗

Create a new libmagic wrapper. mime - if True, mimetypes are returned instead of textual descriptions mime_encoding - if True, codec is returned magic_file - use a mime database other than the system default keep_going - don't stop at the first match, keep g

(self, mime=False, magic_file=None, mime_encoding=False,
                 keep_going=False, uncompress=False, raw=False, extension=False)

Source from the content-addressed store, hash-verified

41 """
42
43 def __init__(self, mime=False, magic_file=None, mime_encoding=False,
44 keep_going=False, uncompress=False, raw=False, extension=False):
45 """
46 Create a new libmagic wrapper.
47
48 mime - if True, mimetypes are returned instead of textual descriptions
49 mime_encoding - if True, codec is returned
50 magic_file - use a mime database other than the system default
51 keep_going - don't stop at the first match, keep going
52 uncompress - Try to look inside compressed files.
53 raw - Do not try to decode "non-printable" chars.
54 extension - Print a slash-separated list of valid extensions for the file type found.
55 """
56 self.flags = MAGIC_NONE
57 if mime:
58 self.flags |= MAGIC_MIME_TYPE
59 if mime_encoding:
60 self.flags |= MAGIC_MIME_ENCODING
61 if keep_going:
62 self.flags |= MAGIC_CONTINUE
63 if uncompress:
64 self.flags |= MAGIC_COMPRESS
65 if raw:
66 self.flags |= MAGIC_RAW
67 if extension:
68 self.flags |= MAGIC_EXTENSION
69
70 self.cookie = magic_open(self.flags)
71 self.lock = threading.Lock()
72
73 magic_load(self.cookie, magic_file)
74
75 # MAGIC_EXTENSION was added in 523 or 524, so bail if
76 # it doesn't appear to be available
77 if extension and (not _has_version or version() < 524):
78 raise NotImplementedError('MAGIC_EXTENSION is not supported in this version of libmagic')
79
80 # For https://github.com/ahupp/python-magic/issues/190
81 # libmagic has fixed internal limits that some files exceed, causing
82 # an error. We can avoid this (at least for the sample file given)
83 # by bumping the limit up. It's not clear if this is a general solution
84 # or whether other internal limits should be increased, but given
85 # the lack of other reports I'll assume this is rare.
86 if _has_param:
87 try:
88 self.setparam(MAGIC_PARAM_NAME_MAX, 64)
89 except MagicException as e:
90 # some versions of libmagic fail this call,
91 # so rather than fail hard just use default behavior
92 pass
93
94 def from_buffer(self, buf):
95 """

Callers 1

__init__Method · 0.45

Calls 3

setparamMethod · 0.95
magic_loadFunction · 0.85
versionFunction · 0.85

Tested by

no test coverage detected