diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..136b399 Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..500bc70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.pylintEnabled": true +} \ No newline at end of file diff --git a/Makefile b/Makefile index b05592b..96c9fa4 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ virtualenv: @$(SYSTEM_PYTHON) -m venv venv @echo "Installing all dependencies..." $(VENV_PIP) install --upgrade pip + $(VENV_PIP) install --upgrade setuptools wheel $(VENV_PIP) install -r $(REQUIREMENTS) all: uninstall install diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000..a2e2c00 Binary files /dev/null and b/docs/.DS_Store differ diff --git a/example.py b/example.py index 883723d..5d5d2a0 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,5 @@ -import sniffpy import requests +import sniffpy r = requests.get("https://httpbin.org/image/jpeg") mime_type = sniffpy.sniff(r.content) #returns a MIMEType object diff --git a/makefile~ b/makefile~ new file mode 100644 index 0000000..b05592b --- /dev/null +++ b/makefile~ @@ -0,0 +1,71 @@ + +ROOT_DIR:=(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +REQUIREMENTS=requirements.txt +# Detects which OS is being used +# Only relevant for virtual environment creation +ifeq ($(OS), Windows_NT) + SYSTEM_PYTHON=py +else + SYSTEM_PYTHON=python3 +endif + +VENV_ROOT=venv +VENV_BIN=$(VENV_ROOT)/bin +VENV_PIP=$(VENV_BIN)/pip3 +VENV_PYTHON=$(VENV_BIN)/python + +virtualenv: + @echo "Making virtual environment..." + @$(SYSTEM_PYTHON) -m venv venv + @echo "Installing all dependencies..." + $(VENV_PIP) install --upgrade pip + $(VENV_PIP) install -r $(REQUIREMENTS) + +all: uninstall install + +install: virtualenv + @echo "Installing sniffpy in the system" + @$(VENV_PIP) install -e . + @echo " " + @echo "/////////////////////////////////////// " + @echo " " + @echo " _____ _ __ __ " + @echo " / ____| (_)/ _|/ _| " + @echo " | (___ _ __ _| |_| |_ _ __ _ _ " + @echo " \___ \| '_ \| | _| _| '_ \| | | | " + @echo " ____) | | | | | | | | | |_) | |_| | " + @echo " |_____/|_| |_|_|_| |_| | .__/ \__, | " + @echo " | | __/ | " + @echo " |_| |___/ " + @echo " " + @echo "/////////////////////////////////////// " + @echo " " + @echo " " + + +uninstall: + @echo "Uninstalling sniffpy in the system" + $(VENV_PIP) uninstall sniffpy + @echo "Succesfully uninstalled sniffpy" + +stylecheck: base_stylecheck test_stylecheck + @echo "Done with stylecheck" + +base_stylecheck: + @echo "Checking codebase coding style" + @$(VENV_BIN)/pylint -v sniffpy + @echo " " + @echo " " + @echo " " + +test_stylecheck: + @echo "Checking coding style of tests" + @$(VENV_BIN)/pylint --rcfile .pylintrctest tests + @echo " " + @echo " " + @echo " " + +test: FORCE + $(VENV_BIN)/py.test + +FORCE: ; diff --git a/setup.py b/setup.py index 3af5175..ed34cf2 100644 --- a/setup.py +++ b/setup.py @@ -5,13 +5,14 @@ setuptools.setup( name="sniffpy", - version="0.1-dev", + version="1.0.0", author="Codeprentice", author_email="nb2838@columbia.edu", description="A package for mime-sniffing", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/codeprentice-org/sniffpy", + download_url='https://github.com/woberton/sniffpy/archive/1.0.0.tar.gz', packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", diff --git a/sniffpy/ref.py b/sniffpy/ref.py index 9f910e1..4a1fcf9 100644 --- a/sniffpy/ref.py +++ b/sniffpy/ref.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Tuple def check_condition(code_point: str, condition: List[str]) -> bool: @@ -12,8 +12,7 @@ def check_condition(code_point: str, condition: List[str]) -> bool: def collect_code_points(str_input: str, condition: List[str], pos: int, - exclusion: bool = True) -> (str, - int): + exclusion: bool = True) -> Tuple[str, int]: result = [] while pos != len(str_input) and check_condition( str_input[pos], condition) ^ exclusion: @@ -23,7 +22,7 @@ def collect_code_points(str_input: str, def collect_http_quoted_string( - str_input: str, pos: int, exact_value: bool = False) -> (str, int): + str_input: str, pos: int, exact_value: bool = False) -> Tuple[str, int]: position_start = pos value = "" assert str_input[pos] == '"' diff --git a/sniffpy/sniff.py b/sniffpy/sniff.py index 15ef553..9e65a13 100644 --- a/sniffpy/sniff.py +++ b/sniffpy/sniff.py @@ -1,3 +1,4 @@ +from typing import Tuple from . import match from . import mimetype from .mimetype import MIMEType @@ -6,7 +7,7 @@ #The following functions are helper functions for sniff_mislabeled_feed #TODO: Rewrite the skip_* functions into one single neat parameterized function -def skip_comment(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_comment(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML in the sequence (resource) They are in the form @@ -21,7 +22,7 @@ def skip_comment(sequence: bytes, i: int, length: int) -> (int, bool): i += 1 return i, False -def skip_markup_declaration(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_markup_declaration(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML markup declarations in the sequence (resource) in the form @@ -36,7 +37,7 @@ def skip_markup_declaration(sequence: bytes, i: int, length: int) -> (int, bool) i += 1 return i, False -def skip_processing_instruction(sequence: bytes, i: int, length: int) -> (int, bool): +def skip_processing_instruction(sequence: bytes, i: int, length: int) -> Tuple[int, bool]: """ Skips XML processing instruction in the sequence (resource) They are in the form @@ -51,7 +52,7 @@ def skip_processing_instruction(sequence: bytes, i: int, length: int) -> (int, b i += 1 return i, False -def handle_rdf(sequence: bytes, i: int, length: int) -> (int, MIMEType): +def handle_rdf(sequence: bytes, i: int, length: int) -> Tuple[int, MIMEType]: """ Handles Resource Description Framework First checks whether it is an RDF diff --git a/sniffpy/utils.py b/sniffpy/utils.py index 30c4e2f..5f85d5a 100644 --- a/sniffpy/utils.py +++ b/sniffpy/utils.py @@ -1,8 +1,8 @@ """ Moudule with functions or utilities""" +from typing import Tuple import sniffpy.constants as const - -def parse_vint(sequence: bytes, index: int) -> (int, int): +def parse_vint(sequence: bytes, index: int) -> Tuple[int, int]: """ Implementation of https://mimesniff.spec.whatwg.org/#parse-a-vint""" mask = 128 max_len = 8 @@ -112,9 +112,9 @@ def match_padded_sequence( return False i = 0 - print(len(sequence)) + #print(len(sequence)) while i + offset + len(pattern) < len(sequence): - print(sequence[offset + i: offset + i + len(pattern)]) + #print(sequence[offset + i: offset + i + len(pattern)]) if sequence[offset + i: offset + i + len(pattern)] == pattern: return True if sequence[offset + i] != 0: diff --git a/tests/test_match.py b/tests/test_match.py index dd5b027..5b56ee5 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -31,7 +31,7 @@ def test_match_pattern(): class TestImageMatching: - """ Class to test pattern matching of image Mimetypes""" + """Class to test pattern matching of image Mimetypes""" mime_types = ['image/gif', 'image/png', 'image/jpeg', 'undefined/undefined'] content = [ @@ -43,28 +43,28 @@ class TestImageMatching: @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) def test_match_image_pattern(self, mime, resource): - """ Tests the most importnat image MIMEs with simulated content""" + """ Tests the most important image MIMEs with simulated content""" computed_type = match.match_image_type_pattern(resource) actual_type = parse_mime_type(mime) assert computed_type == actual_type @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["image"])) - def test_match_image_pattern_wfile(self, expected_type, resource): + def test_match_image_pattern_file(self, expected_type, resource): computed_type = match.match_image_type_pattern(resource) assert computed_type == expected_type class TestAudioVideoMatching: - + "Testing for pattern matching of audio and video MIME types" def test_is_mp4_pattern(self): mp4_file_path = os.path.join(TEST_FILES_PATH, 'video/mp4.mp4') - with open(mp4_file_path, 'rb') as f: - resource = f.read() + with open(mp4_file_path, 'rb') as file: + resource = file.read() assert match.is_mp4_pattern(resource) def test_is_webm_pattern(self): webm_file_path = os.path.join(TEST_FILES_PATH, 'video/webm.webm') - with open(webm_file_path, 'rb') as f: - resource = f.read() + with open(webm_file_path, 'rb') as file: + resource = file.read() assert match.is_webm_pattern(resource) @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["audio", "video"])) @@ -76,3 +76,31 @@ def test_match_video_audio_type_pattern(self, expected_type, resource): def test_match_font_type_pattern(self, expected_type, resource): computed_type = match.match_font_type_pattern(resource) assert computed_type == expected_type + +class FontTypeMatching: + """Class to test pattern matching of font MIME types""" + + mime_types = ['application/vnd.ms-fontobject', 'font/tff', 'font/otf', 'font/collection', + 'font/woff', 'font/woff2'] + content = [ + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x4C\x50', + b'\x00\x01\x00\x00', + b'\x4F\x54\x54\x4F', + b'\x74\x74\x63\x66', + b'\x77\x4F\x46\x46', + b'\x77\x4F\x46\x32' + ] + + @pytest.mark.parametrize('mime, resource', list(zip(mime_types, content))) + def test_match_font_pattern(self, mime, resource): + computed_type = match.match_font_type_pattern(resource) + actual_type = parse_mime_type(mime) + assert computed_type == actual_type + + @pytest.mark.parametrize('expected_type, resource', get_resource_test_list(["font"])) + def test_match_font_pattern_file(self, expected_type, resource): + computed_type = match.match_font_type_pattern(resource) + assert computed_type == expected_type diff --git a/tests/test_sniff.py b/tests/test_sniff.py index 834ddaf..0b78377 100644 --- a/tests/test_sniff.py +++ b/tests/test_sniff.py @@ -7,6 +7,7 @@ from tests.resources import get_resource_test_list class TestSniffing: + """Implments tests pertaining to the sniff module""" mime_types = [ 'text/plain', 'text/plain',