-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JSDatabaseManager to use shared database utilities
- Moved hash calculation and TOML file operations to a new module for better modularity and reuse. - Updated to utilize the new utility functions , , and . - Replaced direct operations with calls to the shared utility functions to handle hash and timestamp management. - Improved code organization by separating concerns and reducing redundancy. - Updated import statements to reflect the new module structure. - Ensured backward compatibility with existing functionality and improved maintainability.
- Loading branch information
1 parent
b740467
commit 2144c41
Showing
4 changed files
with
57 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import toml | ||
from typing import Optional, Dict | ||
import hashlib | ||
|
||
def calculate_hash(data: str) -> str: | ||
"""Calculate the SHA-256 hash of the given data.""" | ||
return hashlib.sha256(data.encode("utf-8")).hexdigest() | ||
|
||
|
||
def load_hash_and_timestamp(hash_file_path, pattern_key: str, pattern_file: str) -> Optional[Dict[str, str]]: | ||
"""Load the hash and timestamp for a specific pattern from the specified TOML file.""" | ||
try: | ||
with open(hash_file_path, "r") as f: | ||
hash_data = toml.load(f) | ||
# Access the specific structure using the provided keys | ||
return hash_data.get(pattern_key, {}).get(pattern_file) | ||
except FileNotFoundError: | ||
return None | ||
|
||
def save_hash_and_timestamp(hash_file_path, pattern_key: str, pattern_file: str, source: str, hash_value: str, timestamp: str) -> None: | ||
"""Save the hash and timestamp for a specific pattern to the specified TOML file.""" | ||
try: | ||
with open(hash_file_path, "r") as f: | ||
hash_data = toml.load(f) | ||
except FileNotFoundError: | ||
hash_data = {} | ||
|
||
# Define the new data structure | ||
new_data = { | ||
pattern_key: { | ||
pattern_file: { | ||
"source": source, | ||
"hash": hash_value, | ||
"timestamp": timestamp, | ||
} | ||
} | ||
} | ||
|
||
# Update the existing data with the new data | ||
if pattern_key in hash_data: | ||
hash_data[pattern_key].update(new_data[pattern_key]) | ||
else: | ||
hash_data.update(new_data) | ||
|
||
# Write the updated data back to the TOML file | ||
with open(hash_file_path, "w") as f: | ||
toml.dump(hash_data, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters