-
Notifications
You must be signed in to change notification settings - Fork 1
/
taste.py
executable file
·74 lines (57 loc) · 1.86 KB
/
taste.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
73
74
#!/usr/bin/env python3
import json
import platform
import pprint
import sys
def usage() -> str:
prompt = "$"
if platform.system() == "Windows":
prompt = "C:\..>"
usage_msg = f"""
Tool for tasting (inspecting) a JSON cookbook.
Usage:
{prompt} taste.py [cookbook.json] [attribute]
List [attribute] in all the recipes. Returns None if unavailable
{prompt} taste.py [cookbook.json] INDEX
List the entire JSON of the recipe at that index number INDEX
(Note: index begins at zero)
{prompt} taste.py [cookbook.json] attrs
List the attributes.
{prompt} taste.py [cookbook.json] length
Display the number of recipes in the cookbook.
"""
return usage_msg
def main():
pp = pprint.PrettyPrinter()
if len(sys.argv) < 3:
print(usage())
sys.exit(0)
with open(sys.argv[1], "r") as fp:
cookbook = json.load(fp)
if sys.argv[2] == "attrs":
attrs = set()
for recipe in cookbook:
attrs = attrs.union(tuple(recipe.keys()))
attr_str = ", ".join(attrs)
# TODO: print this nicer
pp.pprint(f"Attributes: {attrs}")
sys.exit(0)
elif sys.argv[2] == "length":
print(f"Number of recipes in cookbook: {len(cookbook)}")
sys.exit(0)
if sys.argv[2].isdigit() is True:
idx = int(sys.argv[2])
if idx >= len(cookbook):
print(f"Cookbook only has {len(cookbook)} recipes. Try a smaller number.")
sys.exit(1)
elif idx < 0:
print(
f"Negative numbered recipes are not valid input. The number of recipes starts at 0."
)
sys.exit(1)
pp.pprint(cookbook[idx])
sys.exit(0)
results = [recipe.get(sys.argv[2]) for recipe in cookbook]
pp.pprint(results)
if __name__ == "__main__":
main()