blob: 54536113fd2ffa35639ce56ca87d0ab5cd25cd01 (
plain)
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
|
import dataclasses
import json
import sys
from typing import Any
import questionary
import rich
import rich.table
from sbdata.repo import Item
from sbdata.task import Arguments, tasks
class ObjectEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
if isinstance(o, Item):
return o.internalname
if dataclasses.is_dataclass(o):
return o.__dict__
return super().default(o)
def render_thing(i):
if isinstance(i, Item):
return i.internalname
return str(i)
def main():
args = Arguments(sys.argv)
task = args.get_value(
'Task', tasks.get(args.task),
questionary.select('Which task do you want to execute?', choices=[
questionary.Choice(task.label, task) for task in tasks.values()
]))
print("Selected task: " + task.label)
data = task.run(args)
if args.has_flag('json'):
print(json.dumps(data, cls=ObjectEncoder))
if args.has_flag('explore'):
if not (isinstance(data, list) and len(data) > 0 and dataclasses.is_dataclass(data[0])):
print('Cannot explore this')
return
console = rich.get_console()
keys = list(data[0].__dict__.keys())
query = ''
while True:
table = rich.table.Table()
for k in keys:
table.add_column(k)
for item in data:
if any(query in render_thing(val).casefold() for val in item.__dict__.values()):
table.add_row(*[render_thing(getattr(item, k)) for k in keys])
console.print(table)
query = console.input("Search: ")
if __name__ == '__main__':
main()
|