From 3cffd0cd30ae0b67de8a8e4cd3427f3ea048648b Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Thu, 25 Aug 2022 06:25:31 +0800 Subject: [PATCH 1/6] fix: CLI params parser --- filetype/__main__.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/filetype/__main__.py b/filetype/__main__.py index 58cc70c..7df5c4f 100644 --- a/filetype/__main__.py +++ b/filetype/__main__.py @@ -1,3 +1,5 @@ +import sys + import filetype @@ -12,17 +14,23 @@ def guess(path): def main(): import argparse - parser = argparse.ArgumentParser(description='Determine type of FILEs.') - parser.add_argument("file", nargs='+') - parser.add_argument('-v', '--version', action='store_true', - help='output version information and exit') + parser = argparse.ArgumentParser( + prog='filetype', description='Determine type of FILEs.' + ) + parser.add_argument('-f', '--file', nargs='+') + parser.add_argument( + '-v', '--version', action='version', + version='%(prog)s ' + filetype.version, + help='output version information and exit' + ) + args = parser.parse_args() + if len(sys.argv) < 2: + parser.print_help() + sys.exit(1) - if args.version: - print(filetype.version) - else: - for i in args.file: - guess(i) + for i in args.file: + guess(i) if __name__ == '__main__': From 508d290ddee823a10df0712b9687ccf4b46858c5 Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Thu, 25 Aug 2022 06:34:04 +0800 Subject: [PATCH 2/6] test: ignore E501 error for flake8 check --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9bbd805..09efaa3 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,7 @@ commands = pytest --ignore=tests/test_benchmark.py basepython = python3 deps = flake8 -commands = flake8 {toxinidir} --extend-exclude tests,docs,build,dist,venv +commands = flake8 {toxinidir} --extend-exclude tests,docs,build,dist,venv --extend-ignore=E501 [testenv:doc] basepython = python3 From cc29965feab27ef9b2404cf4ffff6a4e1b1fe4f6 Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Thu, 25 Aug 2022 06:51:31 +0800 Subject: [PATCH 3/6] test: Use tox pipeline instead of pytest --- .github/workflows/test.yml | 17 ++++------------- tox.ini | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6147e38..90c5527 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,19 +45,10 @@ jobs: run: filetype --help - name: Install test framework dependencies - run: python -m pip install pytest pytest-random-order pytest-cov pytest-html - - - name: UnitTest with pytest and coverage - run: | - pytest \ - --random-order \ - --random-order-bucket=global \ - --ignore=examples \ - --ignore=tests/test_benchmark.py \ - --cov=filetype \ - --cov-report html:coverage/ \ - tests \ - ; + run: python -m pip install tox + + - name: Run tox pipeline + run: tox - name: HTML coverage reports if: always() diff --git a/tox.ini b/tox.ini index 09efaa3..261975f 100644 --- a/tox.ini +++ b/tox.ini @@ -4,12 +4,23 @@ # and then run "tox" from this directory. [tox] -envlist = py{27,35,36,37,38,39}, lint, doc, clean +envlist = py{27,35,36,37,38,39}, lint, test, doc, clean skip_missing_interpreters = true [testenv:test] -deps = pytest -commands = pytest --ignore=tests/test_benchmark.py +deps = + pytest + pytest-random-order + pytest-cov + pytest-html +commands = pytest \ + --random-order \ + --random-order-bucket=global \ + --ignore=examples \ + --ignore=tests/test_benchmark.py \ + --cov=filetype \ + --cov-report html:coverage/ \ + tests [testenv:lint] basepython = python3 From ee75f0b072dea4ae959c93f4c5393ccc9b923651 Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Thu, 25 Aug 2022 06:55:50 +0800 Subject: [PATCH 4/6] test: fix E275 missing whitespace after keyword --- filetype/types/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filetype/types/image.py b/filetype/types/image.py index 5213ce4..39cb630 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -196,7 +196,7 @@ def match(self, buf): buf[2] == 0x2A and buf[3] == 0x0) or (buf[0] == 0x4D and buf[1] == 0x4D and buf[2] == 0x0 and buf[3] == 0x2A)) - and not(buf[8] == 0x43 and buf[9] == 0x52)) + and not (buf[8] == 0x43 and buf[9] == 0x52)) class Bmp(Type): From bc06b43ec301ea6d1e9cbfb6dd9b284bc7a8fd0f Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Wed, 14 Sep 2022 12:41:28 +0800 Subject: [PATCH 5/6] refactor: duck-typing reading magic string and try to restore the reader position if possible --- filetype/utils.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/filetype/utils.py b/filetype/utils.py index 493b1e2..09a9e51 100644 --- a/filetype/utils.py +++ b/filetype/utils.py @@ -73,14 +73,13 @@ def get_bytes(obj): if isinstance(obj, pathlib.PurePath): return get_signature_bytes(obj) - if isinstance(obj, BufferedIOBase): - start_pos = obj.tell() - obj.seek(0) - magic_bytes = obj.read(_NUM_SIGNATURE_BYTES) - obj.seek(start_pos) # restore reader position - return get_bytes(magic_bytes) - if hasattr(obj, 'read'): + if hasattr(obj, 'tell') and hasattr(obj, 'seek'): + start_pos = obj.tell() + obj.seek(0) + magic_bytes = obj.read(_NUM_SIGNATURE_BYTES) + obj.seek(start_pos) + return get_bytes(magic_bytes) return get_bytes(obj.read(_NUM_SIGNATURE_BYTES)) raise TypeError('Unsupported type as file input: %s' % type(obj)) From d3fdddf00d00787e9a4b8ac58125c89059aef48b Mon Sep 17 00:00:00 2001 From: Zhang Jianfei Date: Wed, 14 Sep 2022 12:45:27 +0800 Subject: [PATCH 6/6] test: remove unused imported(F401) --- filetype/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/filetype/utils.py b/filetype/utils.py index 09a9e51..c954876 100644 --- a/filetype/utils.py +++ b/filetype/utils.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -from io import BufferedIOBase # Python 2.7 workaround try: import pathlib