added support for directory event matching

This commit is contained in:
PatchOfScotland
2023-03-31 13:51:14 +02:00
parent 14dec78756
commit 5952b02be4
10 changed files with 253 additions and 50 deletions

View File

@ -5,12 +5,15 @@ Author(s): David Marchant
"""
from hashlib import sha256
from os import listdir
from os.path import isfile
from meow_base.core.correctness.vars import HASH_BUFFER_SIZE, SHA256
from meow_base.core.correctness.validation import check_type, \
valid_existing_file_path
valid_existing_file_path, valid_existing_dir_path
def _get_file_sha256(file_path):
def _get_file_sha256(file_path:str)->str:
sha256_hash = sha256()
with open(file_path, 'rb') as file_to_hash:
@ -22,7 +25,16 @@ def _get_file_sha256(file_path):
return sha256_hash.hexdigest()
def get_file_hash(file_path:str, hash:str, hint:str=""):
# TODO update this to be a bit more robust
def _get_dir_sha256(dir_path:str)->str:
sha256_hash = sha256()
buffer = str(listdir(dir_path)).encode()
sha256_hash.update(buffer)
return sha256_hash.hexdigest()
def get_file_hash(file_path:str, hash:str, hint:str="")->str:
check_type(hash, str, hint=hint)
valid_existing_file_path(file_path)
@ -35,3 +47,24 @@ def get_file_hash(file_path:str, hash:str, hint:str=""):
f"'{list(valid_hashes.keys())}")
return valid_hashes[hash](file_path)
# TODO inspect this a bit more fully
def get_dir_hash(file_path:str, hash:str, hint:str="")->str:
check_type(hash, str, hint=hint)
valid_existing_dir_path(file_path)
valid_hashes = {
SHA256: _get_dir_sha256
}
if hash not in valid_hashes:
raise KeyError(f"Cannot use hash '{hash}'. Valid are "
f"'{list(valid_hashes.keys())}")
return valid_hashes[hash](file_path)
def get_hash(path:str, hash:str, hint:str="")->str:
if isfile(path):
return get_file_hash(path, hash, hint=hint)
else:
return get_dir_hash(path, hash, hint=hint)