-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save record of failed submission (#411)
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> --------- Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f5596e0
commit 77bd939
Showing
4 changed files
with
94 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import List | ||
|
||
|
||
class Record: | ||
"""Record failed or canceled submissions.""" | ||
|
||
def __init__(self) -> None: | ||
self.record_directory = Path.home() / ".dpdispatcher" / "submission" | ||
self.record_directory.mkdir(parents=True, exist_ok=True) | ||
|
||
def get_submissions(self) -> List[str]: | ||
"""Get all stored submission hashes. | ||
Returns | ||
------- | ||
list[str] | ||
List of submission hashes. | ||
""" | ||
return [ | ||
f.stem | ||
for f in self.record_directory.iterdir() | ||
if (f.is_file() and f.suffix == ".json") | ||
] | ||
|
||
def write(self, submission) -> Path: | ||
"""Write submission data to file. | ||
Parameters | ||
---------- | ||
submission : dpdispatcher.Submission | ||
Submission data. | ||
Returns | ||
------- | ||
pathlib.Path | ||
Path to submission data. | ||
""" | ||
submission_path = self.record_directory / f"{submission.submission_hash}.json" | ||
submission_path.write_text(json.dumps(submission.serialize(), indent=2)) | ||
return submission_path | ||
|
||
def get_submission(self, hash: str, not_exist_ok: bool = False) -> Path: | ||
"""Get submission data by hash. | ||
Parameters | ||
---------- | ||
hash : str | ||
Hash of submission data. | ||
Returns | ||
------- | ||
pathlib.Path | ||
Path to submission data. | ||
""" | ||
submission_file = self.record_directory / f"{hash}.json" | ||
if not not_exist_ok and not submission_file.is_file(): | ||
raise FileNotFoundError(f"Submission file not found: {submission_file}") | ||
return submission_file | ||
|
||
def remove(self, hash: str): | ||
"""Remove submission data by hash. | ||
Call this method when the remote directory is cleaned. | ||
Parameters | ||
---------- | ||
hash : str | ||
Hash of submission data. | ||
""" | ||
path = self.get_submission(hash, not_exist_ok=True) | ||
if path.is_file(): | ||
path.unlink() | ||
|
||
|
||
# the record object can be globally used | ||
record = Record() | ||
__all__ = ["record"] |
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