Skip to content

Commit 28fbde0

Browse files
committed
feat: add files and initial implementation
1 parent 3015118 commit 28fbde0

26 files changed

+1583
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*.{py,rst,txt}]
4+
indent_style = space
5+
trim_trailing_whitespace = true
6+
indent_size = 4
7+
end_of_line = LF
8+
9+
[*.yml]
10+
indent_style = space
11+
indent_size = 2
12+
end_of_line = LF

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: python
2+
3+
python:
4+
- "2.7"
5+
- "3.3"
6+
- "3.4"
7+
- "3.5"
8+
9+
# command to install dependencies
10+
install:
11+
- pip install -r requirements.txt
12+
- pip install coveralls
13+
# Deal with issue on Travis builders re: multiprocessing.Queue :(
14+
- "sudo rm -rf /dev/shm && sudo ln -s /run/shm /dev/shm"
15+
# coverage run --source=moscowdjango,meetup manage.py test
16+
17+
script:
18+
make test
19+
20+
after_success:
21+
coveralls

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
all: check_dependencies unit functional doctest
2+
3+
filename=filetype-`python -c 'import filetype;print filetype.version'`.tar.gz
4+
5+
check_dependencies:
6+
@echo "Checking for dependencies to run tests ..."
7+
@for dependency in `echo $$LETTUCE_DEPENDENCIES`; do \
8+
python -c "import $$dependency" 2>/dev/null || (echo "You must install $$dependency in order to run filetype's tests" && exit 3) ; \
9+
done
10+
11+
unit: clean lint
12+
@echo "Running unit tests ..."
13+
@nosetests -s --verbosity=2 --with-coverage --cover-erase --cover-inclusive tests/unit --cover-package=filetype
14+
15+
doctest: clean
16+
@cd docs && make doctest
17+
18+
documentation:
19+
@cd docs && make html
20+
21+
clean:
22+
@printf "Cleaning up files that are already in .gitignore... "
23+
@for pattern in `cat .gitignore`; do find . -name "$$pattern" -delete; done
24+
@echo "OK!"
25+
26+
deploy-documentation: documentation
27+
@printf 'Deploying documentation ...'
28+
@echo "DONE!"
29+
30+
deploy: deploy-documentation
31+
32+
release: clean doctest deploy-documentation publish
33+
@printf "Exporting to $(filename)... "
34+
@tar czf $(filename) filetype setup.py README.md LICENSE
35+
@echo "DONE!"
36+
37+
lint:
38+
@flake8 .
39+
40+
publish:
41+
@python setup.py sdist register upload

examples/image.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import filetype
2+
3+
4+
def main():
5+
kind = filetype.guess_type('tests/fixtures/sample.jpg')
6+
if kind is None:
7+
print('Cannot guess file type!')
8+
return
9+
10+
print('File extension: %s' % kind.extension)
11+
print('File MIME type: %s' % kind.mime)
12+
13+
14+
if __name__ == '__main__':
15+
main()

filetype/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .filetype import * # noqa

filetype/cmd.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

filetype/filetype.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from filetype.match import match
4+
from filetype.utils import get_signature_bytes
5+
6+
7+
def guess_type(obj):
8+
"""
9+
Infers the type of the given input.
10+
11+
Function is overloaded to accept multiple types in input
12+
and peform the needed type inference based on it.
13+
14+
:param obj str/list/bytes
15+
:rtype Type
16+
"""
17+
if not obj:
18+
return None
19+
20+
buf = None
21+
if type(obj) is bytes:
22+
buf = obj
23+
24+
if type(obj) == str:
25+
buf = get_signature_bytes(obj)
26+
27+
return match(buf)
28+
29+
30+
def guess_mime(obj):
31+
"""
32+
Infers the file type of the given input
33+
and returns its MIME type.
34+
35+
:param obj str/list/bytes
36+
:rtype str
37+
"""
38+
kind = guess_type(obj)
39+
return kind.mime if kind else kind
40+
41+
42+
def guess_extension(obj):
43+
"""
44+
Infers the file type of the given input
45+
and returns its RFC file extension.
46+
47+
:param obj str/list/bytes
48+
:rtype str
49+
"""
50+
kind = guess_type(obj)
51+
return kind.extension if kind else kind
52+
53+
54+
def is_extension_supported(ext):
55+
return None
56+
57+
58+
def is_extension_equals(ext):
59+
return None
60+
61+
62+
def is_mime_supported(mime):
63+
return None
64+
65+
66+
def is_mime_equals(mime):
67+
return None

filetype/match.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from filetype.types import types
4+
5+
6+
def match(buf):
7+
"""
8+
Performs bytes matching comparison agains the
9+
available type matchers.
10+
11+
:param buf list
12+
:rtype bool
13+
"""
14+
for matcher in types:
15+
if matcher.match(buf):
16+
return matcher
17+
18+
return None

filetype/types/__init__.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from filetype.types import image
4+
from filetype.types import video
5+
from filetype.types import audio
6+
from filetype.types import font
7+
from filetype.types import archive
8+
9+
# Supported image types
10+
image_types = (
11+
image.Jpeg(),
12+
image.Png(),
13+
image.Gif(),
14+
image.Webp(),
15+
image.Cr2(),
16+
image.Tiff(),
17+
image.Bmp(),
18+
image.Jxr(),
19+
image.Psd(),
20+
image.Ico(),
21+
)
22+
23+
# Supported video types
24+
video_types = (
25+
video.Mp4(),
26+
video.M4v(),
27+
video.Mkv(),
28+
video.Mov(),
29+
video.Avi(),
30+
video.Wmv(),
31+
video.Mpeg(),
32+
)
33+
34+
# Supported audio types
35+
audio_types = (
36+
audio.Midi(),
37+
audio.Mp3(),
38+
audio.M4a(),
39+
audio.Ogg(),
40+
audio.Flac(),
41+
audio.Wav(),
42+
audio.Amr(),
43+
)
44+
45+
# Supported archive container types
46+
archive_types = (
47+
archive.Epub(),
48+
archive.Zip(),
49+
archive.Tar(),
50+
archive.Rar(),
51+
archive.Gz(),
52+
archive.Bz2(),
53+
archive.SevenZ(),
54+
archive.Pdf(),
55+
archive.Exe(),
56+
archive.Swf(),
57+
archive.Rtf(),
58+
archive.Nes(),
59+
archive.Crx(),
60+
archive.Cab(),
61+
archive.Eot(),
62+
archive.Ps(),
63+
archive.Xz(),
64+
archive.Sqlite(),
65+
archive.Deb(),
66+
archive.Ar(),
67+
archive.Z(),
68+
archive.Lz(),
69+
)
70+
71+
# Supported archive container types
72+
font_types = (
73+
font.Woff(),
74+
font.Woff2(),
75+
font.Ttf(),
76+
font.Otf(),
77+
)
78+
79+
# Expose supported type matchers
80+
types = list(image_types + audio_types +
81+
font_types + video_types +
82+
archive_types)

0 commit comments

Comments
 (0)