Skip to content

Commit

Permalink
Optimizing grabbing head and footer length
Browse files Browse the repository at this point in the history
Removing old winzip sig
  • Loading branch information
cdgriffith committed Aug 8, 2024
1 parent 558b572 commit 2a324df
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
5 changes: 4 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ A big thank you to everyone that has helped!
- Andy (NebularNerd)
- Raphaël Vinot (Rafiot)
- Sebastian Kreft (sk-)
- William Bonnaventure (Aztorius)
- William Bonnaventure (Aztorius)
- Nicholas Bollweg (bollwyvl)
- Péter (peterekepeter)
- mara004
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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)
- Removing expected invalid WinZip signature

Version 1.26
------------
Expand Down
6 changes: 3 additions & 3 deletions 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 Import
- Faster
- Lightweight
- Cross platform compatible
- No dependencies
Expand All @@ -35,7 +35,7 @@ Disadvantages:
Compatibility
~~~~~~~~~~~~~

- Python 3.8+
- Python 3.7+

Using github ci to run continuous integration tests on listed platforms.

Expand Down Expand Up @@ -174,7 +174,7 @@ http://www.garykessler.net/library/file_sigs.html

Freedesktop.org

For use of their shared-mime-info file (even if they do use XML, blea), available at:
For use of their shared-mime-info file, available at:
https://cgit.freedesktop.org/xdg/shared-mime-info/

License
Expand Down
1 change: 0 additions & 1 deletion puremagic/magic_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,6 @@
["564350434830", 0, ".pch", "", "Visual C PreCompiled header"],
["554641c6d2c1", 0, ".ufa", "", "UFA compressed archive"],
["ac9ebd8f0000", 0, ".qdf", "", "Quicken data"],
["57696e5a6970", 29152, ".zip", "application/zip", "WinZip compressed archive"],
["504147454455", 0, ".dmp", "", "Windows memory dump"],
["4d444d5093a7", 0, ".dmp", "", "Windows dump file"],
["458600000600", 0, ".qbb", "", "QuickBooks backup"],
Expand Down
6 changes: 3 additions & 3 deletions puremagic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def _max_lengths() -> tuple[int, int]:
return max_header_length, max_footer_length


max_head, max_foot = _max_lengths()


def _confidence(matches, ext=None) -> list[PureMagicWithConfidence]:
"""Rough confidence based on string length and file extension"""
results = []
Expand Down Expand Up @@ -205,7 +208,6 @@ def _magic(header: bytes, footer: bytes, mime: bool, ext=None) -> str:

def _file_details(filename: os.PathLike | str) -> tuple[bytes, bytes]:
"""Grab the start and end of the file"""
max_head, max_foot = _max_lengths()
with open(filename, "rb") as fin:
head = fin.read(max_head)
try:
Expand All @@ -218,13 +220,11 @@ def _file_details(filename: os.PathLike | str) -> tuple[bytes, bytes]:

def _string_details(string):
"""Grab the start and end of the string"""
max_head, max_foot = _max_lengths()
return string[:max_head], string[-max_foot:]


def _stream_details(stream):
"""Grab the start and end of the stream"""
max_head, max_foot = _max_lengths()
head = stream.read(max_head)
try:
stream.seek(-max_foot, os.SEEK_END)
Expand Down

0 comments on commit 2a324df

Please sign in to comment.