-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve logging and add dry-run feature.
- Loading branch information
Showing
5 changed files
with
161 additions
and
45 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
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
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 |
---|---|---|
@@ -1,18 +1,96 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from pathlib import Path | ||
from typing import TypeAlias | ||
|
||
|
||
@dataclass | ||
class DumpLog: | ||
"""Represents a single dump log entry.""" | ||
|
||
path: Path | ||
time: datetime | ||
|
||
|
||
DumpDict: TypeAlias = dict[str, DumpLog] | ||
|
||
|
||
class DumpLogger: | ||
def __init__(self): | ||
self.log_dict: dict[str, dict[str, Path]] = {'calculations': {}, 'workflows': {}} | ||
"""Main logger class using dataclasses for better structure.""" | ||
|
||
def update_calculations(self, new_calculations: dict[str, Path]): | ||
"""Update the log with new calculations.""" | ||
self.log_dict['calculations'].update(new_calculations) | ||
DUMP_FILE: str = '.dump_log.json' | ||
|
||
def update_workflows(self, new_workflows: dict[str, Path]): | ||
"""Update the log with new workflows.""" | ||
self.log_dict['workflows'].update(new_workflows) | ||
def __init__( | ||
self, | ||
dump_parent_path: Path, | ||
calculations: DumpDict | None = None, | ||
workflows: DumpDict | None = None, | ||
counter: int = 0, | ||
) -> None: | ||
self.dump_parent_path = dump_parent_path | ||
self.calculations = calculations or {} | ||
self.workflows = workflows or {} | ||
self.counter = 0 | ||
|
||
def get_logs(self): | ||
@property | ||
def dump_file(self) -> Path: | ||
"""Get the path to the dump file.""" | ||
return self.dump_parent_path / self.DUMP_FILE | ||
|
||
def update_calculations(self, new_calculations: DumpDict) -> None: | ||
"""Update the calculations log.""" | ||
self.calculations.update(new_calculations) | ||
self.counter += len(new_calculations) | ||
|
||
def update_workflows(self, new_workflows: DumpDict) -> None: | ||
"""Update the workflows log.""" | ||
self.workflows.update(new_workflows) | ||
self.counter += len(new_workflows) | ||
|
||
def get_log(self) -> dict[str, DumpDict]: | ||
"""Retrieve the current state of the log.""" | ||
return self.log_dict | ||
return {'calculations': self.calculations, 'workflows': self.workflows} | ||
|
||
def save_log(self) -> None: | ||
"""Save the log to a JSON file.""" | ||
|
||
def serialize_logs(logs: DumpDict) -> dict: | ||
serialized = {} | ||
for uuid, entry in logs.items(): | ||
serialized[uuid] = {'path': str(entry.path), 'time': entry.time.isoformat()} | ||
return serialized | ||
|
||
log_dict = { | ||
'calculations': serialize_logs(self.calculations), | ||
'workflows': serialize_logs(self.workflows), | ||
} | ||
|
||
with self.dump_file.open('w', encoding='utf-8') as f: | ||
json.dump(log_dict, f, indent=4) | ||
|
||
@classmethod | ||
def from_file(cls, dump_parent_path: Path) -> 'DumpLogger': | ||
"""Alternative constructor to load from an existing JSON file.""" | ||
instance = cls(dump_parent_path=dump_parent_path) | ||
|
||
if not instance.dump_file.exists(): | ||
return instance | ||
|
||
try: | ||
with instance.dump_file.open('r', encoding='utf-8') as f: | ||
data = json.load(f) | ||
|
||
def deserialize_logs(category_data: dict) -> DumpDict: | ||
deserialized = {} | ||
for uuid, entry in category_data.items(): | ||
deserialized[uuid] = DumpLog(path=Path(entry['path']), time=datetime.fromisoformat(entry['time'])) | ||
return deserialized | ||
|
||
instance.calculations = deserialize_logs(data['calculations']) | ||
instance.workflows = deserialize_logs(data['workflows']) | ||
|
||
except (json.JSONDecodeError, OSError): | ||
raise | ||
|
||
return instance |
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
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