Skip to content

Commit

Permalink
- Adding new verbose output to command line with -v or --verbose
Browse files Browse the repository at this point in the history
- Adding #95 README clarification for Faster Import (thanks to mara004)
  • Loading branch information
cdgriffith committed Aug 8, 2024
1 parent e80d58a commit 0165f93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

Version 1.27
------------

- Adding new verbose output to command line with `-v` or `--verbose`
- Adding #92 include py.typed in sdist (thanks to Nicholas Bollweg - bollwyvl)
- Adding #93 Improve PDF file detection, fix json description (thanks to Péter - peterekepeter)
- Adding #95 README clarification for Faster Import (thanks to mara004)
- Fixing #96 #86 stream does not work properly on opened small files (thanks to Felipe Lema and Andy - NebularNerd)

Version 1.26
------------

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ others might.

Advantages over using a wrapper for 'file' or 'libmagic':

- Faster
- Faster Import
- Lightweight
- Cross platform compatible
- No dependencies
Expand Down
28 changes: 25 additions & 3 deletions puremagic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from itertools import chain

__author__ = "Chris Griffith"
__version__ = "1.26"
__version__ = "1.27"
__all__ = [
"magic_file",
"magic_string",
Expand Down Expand Up @@ -229,8 +229,8 @@ def _stream_details(stream):
try:
stream.seek(-max_foot, os.SEEK_END)
except OSError:
stream.seek(stream.tell(), os.SEEK_END)
stream.seek(-max_foot, os.SEEK_END)
# File is smaller than the max_foot size, jump to beginning
stream.seek(0)
foot = stream.read()
stream.seek(0)
return head, foot
Expand Down Expand Up @@ -378,13 +378,35 @@ def command_line_entry(*args):
dest="mime",
help="Return the mime type instead of file type",
)
parser.add_argument("-v", "--v", action="store_true", dest="verbose", help="Print verbose output")
parser.add_argument("files", nargs="+")
args = parser.parse_args(args if args else sys.argv[1:])

for fn in args.files:
if not os.path.exists(fn):
print(f"File '{fn}' does not exist!")
continue
if args.verbose:
try:
matches = magic_file(fn)
except PureError:
print(f"'{fn}' : could not be Identified")
continue
print("")
print(f"File: {fn}")
print(f"Total Possible Matches: {len(matches)}")
for i, result in enumerate(matches):
if i == 0:
print("\n\tBest Match")
else:
print(f"\tAlertnative Match {i}")
print(f"\tname: {result.name}")
print(f"\tconfidence: {result.confidence}")
print(f"\textension: {result.extension}")
print(f"\tmime_type: {result.mime_type}")
print(f"\tbyte_match: {result.byte_match}")
print(f"\toffset: {result.offset}")
print("")
try:
print(f"'{fn}' : {from_file(fn, args.mime)}")
except PureError:
Expand Down

0 comments on commit 0165f93

Please sign in to comment.