-
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.
feat: Enhance JS database management with hashing and timestamp funct…
…ionality (#327) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ryan Mast <[email protected]>
- Loading branch information
1 parent
28ca5c0
commit a10799e
Showing
3 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
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,60 @@ | ||
# Copyright 2025 Lawrence Livermore National Security, LLC | ||
# See the top-level LICENSE file for details. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# Copyright 2025 Lawrence Livermore National Security, LLC | ||
# See the top-level LICENSE file for details. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import hashlib | ||
from typing import Dict, Optional | ||
|
||
import tomlkit | ||
|
||
|
||
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 = tomlkit.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_info: Dict[str, 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 = tomlkit.load(f) | ||
except FileNotFoundError: | ||
hash_data = {} | ||
|
||
# Define the new data structure | ||
new_data = { | ||
pattern_info["pattern_key"]: { | ||
pattern_info["pattern_file"]: { | ||
"source": pattern_info["source"], | ||
"hash": pattern_info["hash_value"], | ||
"timestamp": pattern_info["timestamp"], | ||
} | ||
} | ||
} | ||
|
||
# Update the existing data with the new data | ||
if pattern_info["pattern_key"] in hash_data: | ||
hash_data[pattern_info["pattern_key"]].update(new_data[pattern_info["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: | ||
tomlkit.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