Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI params parser #141

Merged
merged 6 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 17 additions & 9 deletions filetype/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import filetype


Expand All @@ -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__':
Expand Down
2 changes: 1 addition & 1 deletion filetype/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 6 additions & 8 deletions filetype/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from io import BufferedIOBase
# Python 2.7 workaround
try:
import pathlib
Expand Down Expand Up @@ -73,14 +72,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))
19 changes: 15 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
# 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
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
Expand Down