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 import, add type hinting #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions filehash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
try:
from filehash import FileHash, SUPPORTED_ALGORITHMS
except ImportError:
from .filehash import FileHash, SUPPORTED_ALGORITHMS
from .filehash import FileHash, SUPPORTED_ALGORITHMS

__all__ = ["FileHash", "SUPPORTED_ALGORITHMS"]
16 changes: 8 additions & 8 deletions filehash/filehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class FileHash:
Class wrapping the hashlib module to facilitate calculating file hashes.
"""

def __init__(self, hash_algorithm='sha256', chunk_size=4096):
def __init__(self, hash_algorithm: str = 'sha256', chunk_size: int = 4096):
"""
Initialize the FileHash class.

Expand All @@ -167,7 +167,7 @@ def __init__(self, hash_algorithm='sha256', chunk_size=4096):
self.chunk_size = chunk_size
self.hash_algorithm = hash_algorithm

def hash_file(self, filename):
def hash_file(self, filename: str) -> str:
"""
Method for calculating the hash of a file.

Expand All @@ -182,7 +182,7 @@ def hash_file(self, filename):
buffer = fp.read(self.chunk_size)
return hash_func.hexdigest()

def hash_files(self, filenames):
def hash_files(self, filenames: list[str]) -> list[FileHashResult]:
"""
Method for calculating the hash of multiple files.

Expand All @@ -192,7 +192,7 @@ def hash_files(self, filenames):
"""
return [FileHashResult(fn, self.hash_file(fn)) for fn in filenames]

def hash_dir(self, path, pattern='*'):
def hash_dir(self, path, pattern='*') -> list[FileHashResult]:
"""
Method for calculating the hash of files in a directory.

Expand All @@ -209,7 +209,7 @@ def hash_dir(self, path, pattern='*'):
os.chdir(saved_dir) # popd
return result

def cathash_files(self, filenames):
def cathash_files(self, filenames: list[str]) -> list[FileHashResult]:
"""
Method for calculating a single hash from multiple files.
Files are sorted by their individual hash values and then traversed in that order to generate a combined hash value.
Expand All @@ -226,7 +226,7 @@ def cathash_files(self, filenames):
buffer = fp.read(self.chunk_size)
return hash_func.hexdigest()

def cathash_dir(self, path, pattern='*'):
def cathash_dir(self, path: str, pattern: str = '*'):
"""
Method for calculating a single hash of files in a directory.
Files are sorted by their individual hash values and then traversed in that order to generate a combined hash value.
Expand All @@ -243,7 +243,7 @@ def cathash_dir(self, path, pattern='*'):
os.chdir(saved_dir) # popd
return result

def verify_checksums(self, checksum_filename):
def verify_checksums(self, checksum_filename: str) -> list[VerifyHashResult]:
"""
Method for verifying the checksums of a file or set of files. The
checksum file is a text file where each line has the hash and filename
Expand All @@ -270,7 +270,7 @@ def verify_checksums(self, checksum_filename):
result.append(VerifyHashResult(filename, expected_hash == actual_hash))
return result

def verify_sfv(self, sfv_filename):
def verify_sfv(self, sfv_filename: str) -> list[VerifyHashResult]:
"""
Method for verifying the checksums of a file or set of files. The
sfv (Simple File Verification) file is a text file where each line has
Expand Down
3 changes: 2 additions & 1 deletion filehash/filehash_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def process_checksum_file(checksum_filename, hasher):


def main():
args = create_parser().parse_args()
parser = create_parser()
args = parser.parse_args()

if not args.algorithm.lower() in SUPPORTED_ALGORITHMS:
print("ERROR: Unknown checksum/hash algorithm: {0}".format(args.algorithm))
Expand Down