-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.py
executable file
·80 lines (54 loc) · 1.71 KB
/
p.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
75
76
77
78
79
80
#! /usr/bin/env python
import os
import os.path
import json
import sys
def get_database_path():
return os.path.expanduser('~/.p/database')
def read_data(path):
if not os.path.exists(path):
return {}
with open(path, 'r') as ftr:
return json.load(ftr)
def write_data(path, data):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path, 'w') as ftr:
json.dump(data, ftr, sort_keys=True, indent=4, separators=(',', ': '))
def show(key):
database_path = get_database_path()
data = read_data(database_path)
print(data.get(key, {}).get('value', ''))
def list_key_value():
database_path = get_database_path()
data = read_data(database_path)
print(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
def delete_key(key):
database_path = get_database_path()
data = read_data(database_path)
del data[key]
write_data(database_path, data)
def set_key_value(key, value, comment=''):
database_path = get_database_path()
data = read_data(database_path)
data[key] = {'value': value, 'comment': comment}
write_data(database_path, data)
action_map = {'-s': set_key_value, '-l': list_key_value, '-d': delete_key,
'': show}
def get_element_from_list(index, lst):
if index >= len(lst):
return ''
return lst[index]
def get_command_from_argv(argv):
action = argv[0]
if action in action_map:
argv = argv[1:]
else:
action = ''
return action, argv
def main():
system_argv = sys.argv[1:]
action, args = get_command_from_argv(system_argv)
action_map.get(action, show)(*args)
if __name__ == '__main__':
main()