How to use puremagic - 10 common examples

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 / 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 / test / test_common_extensions.py View on Github external
def test_office(self):
        """Test common office document formats """
        # Office files have very similar magic numbers, and may overlap
        for item in os.listdir(OFFICE_DIR):
            puremagic.from_file(os.path.join(OFFICE_DIR, item))
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_file(self):
        """File identification """
        mp4file = NamedTemporaryFile(delete=False)
        mp4file.write(self.mp4magic)
        mp4file.close()
        ext = puremagic.from_file(mp4file.name)
        os.unlink(mp4file.name)
        self.assertEqual(self.expect_ext, ext)
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_hex_string(self):
        """Hex string identification """
        ext = puremagic.from_string(self.mp4magic)
        self.assertEqual(self.expect_ext, ext)
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_not_found(self):
        """Bad file type via string """
        try:
            with self.assertRaises(puremagic.PureError):
                puremagic.from_string("not applicable string")
        except TypeError:
            # Python 2.6 doesn't support using
            # assertRaises as a context manager
            pass
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_string(self):
        """String identification """
        ext = puremagic.from_string(bytes(self.mp4magic))
        self.assertEqual(self.expect_ext, ext)
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_string_with_confidence(self):
        """String identification: magic_string """
        ext = puremagic.magic_string(bytes(self.mp4magic))
        self.assertEqual(self.expect_ext, ext[0].extension)
        self.assertRaises(ValueError, puremagic.magic_string, "")
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_magic_string_with_filename_hint(self):
        """String identification: magic_string with hint """
        filename = os.path.join(OFFICE_DIR, "test.xlsx")
        with open(filename, "rb") as f:
            data = f.read()
        ext = puremagic.magic_string(data, filename=filename)
        self.assertEqual(".xlsx", ext[0].extension)
github cdgriffith / puremagic / test / test_common_extensions.py View on Github external
def test_string_with_confidence(self):
        """String identification: magic_string """
        ext = puremagic.magic_string(bytes(self.mp4magic))
        self.assertEqual(self.expect_ext, ext[0].extension)
        self.assertRaises(ValueError, puremagic.magic_string, "")