-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·72 lines (58 loc) · 2.43 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/local/bin/python3
# ruff: noqa: T201, SLF001
import argparse
import os
from pathlib import Path
from hardverapro_pp.utils.database import ItemDatabase
def list_item(database_id: str, item_id: str) -> None:
file_selector = '*' if database_id is None else database_id
database_folder = os.environ.get('HA_DATABASE_FOLDER', '.')
pkl_files = list(Path(database_folder).glob(file_selector + '.pkl'))
for file in pkl_files:
database = ItemDatabase(file.stem)
if item_id:
print(f'Database Id: {database._query_id}')
print(f'Database path: {database._database_path}')
print(f'Database new: {database._database_newly_created}')
i = 0
for element in database._database:
if element.id == item_id:
element_str = str(element)
print(f'[{i}]:{os.linesep}')
print('\t' + element_str.replace(os.linesep, f'{os.linesep}\t'))
print()
i += 1
else:
print(database)
def delete_item(database_id: str, item_id: str) -> None:
if database_id is None and item_id is None:
print('At least database id or item id must be defined!')
return
file_selector = '*' if database_id is None else database_id
database_folder = os.environ.get('HA_DATABASE_FOLDER', '.')
pkl_files = list(Path(database_folder).glob(file_selector + '.pkl'))
for file in pkl_files:
if item_id:
database = ItemDatabase(file.stem)
succeed = database.delete(item_id)
if succeed:
print(f'{file.stem}.{item_id} is successfully deleted.')
else:
file.unlink()
print(f'{file.stem} is successfully deleted.')
def main() -> None:
parser = argparse.ArgumentParser(description='HardverApro++ CLI tool')
parser.add_argument(
'action',
choices=['list', 'delete'],
help='Action to perform on the database: list or delete',
)
parser.add_argument('-i', '--item_id', type=str, help='Id of the item to interact with')
parser.add_argument('-d', '--database_id', type=str, help='Database Id to use for delete action')
args = parser.parse_args()
if args.action == 'list':
list_item(args.database_id, args.item_id)
elif args.action == 'delete':
delete_item(args.database_id, args.item_id)
if __name__ == '__main__':
main()