How to use the puremagic.from_string 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 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 btimby / fulltext / fulltext / magicwrap.py View on Github external
def from_buffer(header, mime=True):
        if not mime:
            raise ValueError("mime=False arg is not supported")
        if not header:
            # ...or else puremagic throws ValueError
            return "text/plain"
        try:
            ret = puremagic.from_string(header, mime=True)
        except puremagic.PureError:
            return guess_header(header)
        if not ret or ret == "application/octet-stream":
            ret = guess_header(header)
        return ret
github iandennismiller / gthnk / gthnk / models / day.py View on Github external
def attach(self, binary):
        # determine the format of the file
        ext = puremagic.from_string(binary)

        page = None

        # if the attachment is a PDF
        if ext == ".pdf":
            # use PyPDF2 to read the stream
            pdf = PdfFileReader(StringIO(binary))
            # if it is a multi-page PDF
            if pdf.getNumPages() > 1:
                # add the pages individually
                for pdf_page in pdf.pages:
                    output = PdfFileWriter()
                    output.addPage(pdf_page)

                    pdf_page_buf = StringIO()
                    output.write(pdf_page_buf)