-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: manage recording database (#451)
* implemented db module * use click + loguru
- Loading branch information
Showing
17 changed files
with
144 additions
and
48 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 @@ | ||
"""Package for interacting with the OpenAdapt database.""" |
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
File renamed without changes.
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,34 @@ | ||
"""Lists all recordings in the database. | ||
Usage: python -m openadapt.db.list | ||
""" | ||
|
||
from sys import stdout | ||
|
||
from loguru import logger | ||
|
||
from openadapt.db.crud import get_all_recordings | ||
|
||
|
||
def main() -> None: | ||
"""Prints all recordings in the database.""" | ||
logger.remove() | ||
logger.add( | ||
stdout, | ||
colorize=True, | ||
format="<blue>[DB] </blue><yellow>{message}</yellow>", | ||
) | ||
print() # newline | ||
|
||
if not get_all_recordings(): | ||
logger.info("No recordings found.") | ||
|
||
for idx, recording in enumerate(get_all_recordings()[::-1], start=1): | ||
logger.info( | ||
f"[{idx}]: {recording.task_description} | {recording.timestamp}" | ||
+ (" [latest]" if idx == len(get_all_recordings()) else "") | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,69 @@ | ||
"""Removes recordings from the database. | ||
Usage: python -m openadapt.db.remove [--all | --latest | --id <recording_id>] | ||
""" | ||
from sys import stdout | ||
|
||
from loguru import logger | ||
import click | ||
|
||
from openadapt.db.crud import delete_recording, get_all_recordings | ||
|
||
print() # newline | ||
|
||
|
||
@click.command() | ||
@click.option("--all", is_flag=True, help="Remove all recordings.") | ||
@click.option("--latest", is_flag=True, help="Remove the latest recording.") | ||
@click.option("--id", "recording_id", type=int, help="Remove recording by ID.") | ||
def remove(all: str, latest: str, recording_id: int) -> int: | ||
"""Removes a recording from the database.""" | ||
recordings = get_all_recordings()[::-1] | ||
|
||
logger.remove() | ||
logger.add( | ||
stdout, | ||
colorize=True, | ||
format="<blue>[DB] </blue><yellow>{message}</yellow>", | ||
) | ||
|
||
if sum([all, latest, recording_id is not None]) > 1: | ||
logger.error("Invalid usage.") | ||
logger.error("Use --help for more information.") | ||
return 1 | ||
|
||
if not recordings: | ||
logger.error("No recordings found.") | ||
return 1 | ||
|
||
if all: | ||
if click.confirm("Are you sure you want to delete all recordings?"): | ||
for r in recordings: | ||
logger.info(f"Deleting {r.task_description} | {r.timestamp}...") | ||
delete_recording(r.timestamp) | ||
logger.info("All recordings deleted.") | ||
else: | ||
logger.info("Aborting...") | ||
return 0 | ||
|
||
if latest: | ||
recording_id = len(recordings) | ||
|
||
elif recording_id is None or not (1 <= recording_id <= len(recordings)): | ||
logger.error("Invalid recording ID.") | ||
return 1 | ||
recording_to_delete = recordings[recording_id - 1] | ||
|
||
if click.confirm( | ||
"Are you sure you want to delete recording" | ||
f" {recording_to_delete.task_description} | {recording_to_delete.timestamp}?" | ||
): | ||
delete_recording(recording_to_delete.timestamp) | ||
logger.info("Recording deleted.") | ||
else: | ||
logger.info("Aborting...") | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
remove() |
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
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
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
Oops, something went wrong.