How to use the puremagic.PureError function in puremagic

To help you get started, we’ve selected a few puremagic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
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])
                )
github cdgriffith / puremagic / puremagic.py View on Github external
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")
github opsdroid / opsdroid / opsdroid / events.py View on Github external
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))
github cdgriffith / puremagic / puremagic.py View on Github external
# 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")
github cdgriffith / puremagic / puremagic.py View on Github external
# 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