Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def group_test(self, directory):
failures = []
ext_failures = []
for item in os.listdir(directory):
try:
ext = puremagic.from_file(os.path.join(directory, item))
except puremagic.PureError:
failures.append(item)
else:
if not item.endswith(ext):
ext_failures.append((item, ext))
if failures:
raise AssertionError(
"The following items could not be identified from the {} folder: {}".format(
directory, ", ".join(failures)
)
)
if ext_failures:
raise AssertionError(
"The following files did not have the expected extensions: {}".format(
", ".join(['"{}" expected "{}"'.format(item, ext) for item, ext in ext_failures])
)
def _load_magic(name="magic_array.data", location=None):
loc = location if location else os.path.abspath(os.path.dirname(__file__))
magic_file_loc = os.path.join(loc, name)
with open(magic_file_loc, "rb") as mf:
incoming_stream = mf.read()
magic_array = eval(incoming_stream)
if isinstance(magic_array, list):
return magic_array
else:
raise PureError("Magic array data was not loaded properly")
async def get_mimetype(self):
"""Return the mimetype for the file."""
if self._mimetype:
return self._mimetype
try:
results = puremagic.magic_string(await self.get_file_bytes())
except puremagic.PureError:
# If no results return none
return ""
# If for some reason we get a len 0 list
if not results: # pragma: nocover
return ""
# If we only have one result use it.
if len(results) == 1: # pragma: nocover
return results[0].mime_type
# If we have multiple matches with the same confidence, pick one that
# actually has a mime_type.
confidence = results[0].confidence
results = filter(lambda x: x.confidence == confidence, results)
results = list(filter(lambda x: bool(x.mime_type), results))
# That way we do not try to identify bytes that don't exist
length = len(data)
# Iterate through the list of known magic strings
for magic_row in _load_magic():
start = magic_row[1]
if start < 0:
if data[start:] == magic_row[0]:
return magic_row
else:
end = magic_row[1] + len(magic_row[0])
if end > length:
continue
if data[start:end] == magic_row[0]:
return magic_row
raise PureError("Could not identify file")
# Iterate through the items first via the header
for magic_row in _load_magic():
start = magic_row[1]
if start < 0:
if data[start:] == magic_row[0]:
matches.append([magic_row[2], magic_row[3], magic_row[4],
_confidence(magic_row)])
else:
end = magic_row[1] + len(magic_row[0])
if end > length:
continue
if data[start:end] == magic_row[0]:
matches.append([magic_row[2], magic_row[3], magic_row[4],
_confidence(magic_row)])
if not matches:
raise PureError("Could not identify file")
return matches