summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2013-09-27 09:49:24 -0700
committerAdam Hupp <adam@hupp.org>2013-09-27 09:49:24 -0700
commit2b2f1fba280a2a8a37228417ede3228021da5a68 (patch)
tree6fc3c70778f8f176c333b8534f2992a60cdd9bb3
parent20e713777b8a005bcb81ae7e29db8735a9dbc054 (diff)
parentd529597e3d26e89f7f86c39a7959f582fd1b3c9e (diff)
downloadpython-magic-2b2f1fba280a2a8a37228417ede3228021da5a68.tar.gz
Merge branch 'master' of https://github.com/aromanovich/python-magic into aromanovich-master
Conflicts: test.py
-rw-r--r--.travis.yml7
-rw-r--r--magic.py7
-rw-r--r--test.py119
-rw-r--r--testdata/keep-going.jpgbin0 -> 124365 bytes
-rw-r--r--testdata/λ1
5 files changed, 70 insertions, 64 deletions
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..0ddbd99
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,7 @@
+language: python
+python:
+ - "2.6"
+ - "2.7"
+ - "3.2"
+script:
+ - python test.py
diff --git a/magic.py b/magic.py
index 740a5b3..d64253e 100644
--- a/magic.py
+++ b/magic.py
@@ -33,20 +33,23 @@ class Magic:
"""
- def __init__(self, mime=False, magic_file=None, mime_encoding=False):
+ def __init__(self, mime=False, magic_file=None, mime_encoding=False,
+ keep_going=False):
"""
Create a new libmagic wrapper.
mime - if True, mimetypes are returned instead of textual descriptions
mime_encoding - if True, codec is returned
magic_file - use a mime database other than the system default
-
+ keep_going - don't stop at the first match, keep going
"""
flags = MAGIC_NONE
if mime:
flags |= MAGIC_MIME
elif mime_encoding:
flags |= MAGIC_MIME_ENCODING
+ if keep_going:
+ flags |= MAGIC_CONTINUE
self.cookie = magic_open(flags)
diff --git a/test.py b/test.py
index 9e83708..6412045 100644
--- a/test.py
+++ b/test.py
@@ -1,78 +1,73 @@
-
import os.path
import unittest
-import random
-import magic
-from os import path
-
-testfile = [
- ("magic.pyc", "python 2.4 byte-compiled", "application/octet-stream"),
- ("test.pdf", "PDF document, version 1.2", "application/pdf"),
- ("test.gz", 'gzip compressed data, was "test", from Unix, last modified: '
- 'Sat Jun 28 18:32:52 2008', "application/x-gzip"),
- ("text.txt", "ASCII text", "text/plain"),
- # is there no better way to encode a unicode literal across python2/3.[01]/3.3?
- ("\xce\xbb".decode('utf-8'), "empty", "application/x-empty")
- ]
-
-testFileEncoding = [('text-iso8859-1.txt', 'iso-8859-1')]
+import magic
-class TestMagic(unittest.TestCase):
+class MagicTest(unittest.TestCase):
+ TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata')
- mime = False
-
- def setUp(self):
- self.m = magic.Magic(mime=self.mime)
+ def assert_values(self, m, expected_values):
+ for filename, expected_value in expected_values.items():
+ filename = os.path.join(self.TESTDATA_DIR, filename)
- def testFileTypes(self):
- for filename, desc, mime in testfile:
- filename = path.join(path.dirname(__file__),
- "testdata",
- filename)
- if self.mime:
- target = mime
- else:
- target = desc
-
- snippet = open(filename, 'rb').read(1024)
-
- # do this rather than b"" literals since those aren't supported in 2.4
- target_bytes = target.encode('utf-8')
- self.assertEqual(target_bytes, self.m.from_buffer(snippet))
- self.assertEqual(target_bytes, self.m.from_file(filename), filename)
+ value = m.from_buffer(open(filename, 'rb').read())
+ expected_value_bytes = expected_value.encode('utf-8')
+ self.assertEqual(value, expected_value_bytes)
- self.assertEqual(target_bytes, magic.from_buffer(snippet, mime=self.mime))
- self.assertEqual(target_bytes, magic.from_file(filename, mime=self.mime),
- filename)
+ value = m.from_file(filename)
+ self.assertEqual(value, expected_value_bytes)
+ def test_mime_types(self):
+ m = magic.Magic(mime=True)
+ self.assert_values(m, {
+ 'magic.pyc': 'application/octet-stream',
+ 'test.pdf': 'application/pdf',
+ 'test.gz': 'application/x-gzip',
+ 'text.txt': 'text/plain',
+ b'\xce\xbb'.decode('utf-8'): 'text/plain',
+ })
- def testErrors(self):
- self.assertRaises(IOError, self.m.from_file, "nonexistent")
- self.assertRaises(IOError, lambda: magic.from_file("nonexistent", mime=self.mime))
+ def test_descriptions(self):
+ m = magic.Magic()
+ os.environ['TZ'] = 'UTC' # To get the last modified date of test.gz in UTC
+ try:
+ self.assert_values(m, {
+ 'magic.pyc': 'python 2.4 byte-compiled',
+ 'test.pdf': 'PDF document, version 1.2',
+ 'test.gz': 'gzip compressed data, was "test", from Unix, '
+ 'last modified: Sun Jun 29 01:32:52 2008',
+ 'text.txt': 'ASCII text',
+ })
+ finally:
+ del os.environ['TZ']
- self.assertRaises(magic.MagicException, magic.Magic, magic_file="noneexistent")
- os.environ['MAGIC'] = '/nonexistent'
- self.assertRaises(magic.MagicException, magic.Magic)
- del os.environ['MAGIC']
+ def test_mime_encodings(self):
+ m = magic.Magic(mime_encoding=True)
+ self.assert_values(m, {
+ 'text-iso8859-1.txt': 'iso-8859-1',
+ 'text.txt': 'us-ascii',
+ })
-class TestMagicMime(TestMagic):
- mime = True
+ def test_errors(self):
+ m = magic.Magic()
+ self.assertRaises(IOError, m.from_file, 'nonexistent')
+ self.assertRaises(magic.MagicException, magic.Magic,
+ magic_file='nonexistent')
+ os.environ['MAGIC'] = 'nonexistent'
+ try:
+ self.assertRaises(magic.MagicException, magic.Magic)
+ finally:
+ del os.environ['MAGIC']
-class TestMagicMimeEncoding(unittest.TestCase):
- def setUp(self):
- self.m = magic.Magic(mime_encoding=True)
+ def test_keep_going(self):
+ filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg')
- def testFileEncoding(self):
- for filename, encoding in testFileEncoding:
- filename = path.join(path.dirname(__file__),
- "testdata",
- filename)
- snippet = open(filename, 'rb').read(1024)
-
- self.assertEqual(encoding, self.m.from_buffer(snippet))
- self.assertEqual(encoding, self.m.from_file(filename), filename)
+ m = magic.Magic(mime=True)
+ self.assertEqual(m.from_file(filename),
+ 'application/octet-stream'.encode('utf-8'))
+
+ m = magic.Magic(mime=True, keep_going=True)
+ self.assertEqual(m.from_file(filename), 'image/jpeg'.encode('utf-8'))
if __name__ == '__main__':
unittest.main()
-
diff --git a/testdata/keep-going.jpg b/testdata/keep-going.jpg
new file mode 100644
index 0000000..c15171d
--- /dev/null
+++ b/testdata/keep-going.jpg
Binary files differ
diff --git a/testdata/λ b/testdata/λ
index e69de29..9daeafb 100644
--- a/testdata/λ
+++ b/testdata/λ
@@ -0,0 +1 @@
+test