Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_hex_string(self):
"""Hex string identification """
ext = puremagic.from_string(self.mp4magic)
self.assertEqual(self.expect_ext, ext)
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
def test_string(self):
"""String identification """
ext = puremagic.from_string(bytes(self.mp4magic))
self.assertEqual(self.expect_ext, ext)
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
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)