-
Notifications
You must be signed in to change notification settings - Fork 3
/
shelf_import.py
executable file
·91 lines (69 loc) · 2.73 KB
/
shelf_import.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
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""imports a json dictionary from stdin and merges all items in it to a shelf. Tested with DBM files."""
__version__ = '0.0.4'
__author__ = 'William Stearns'
__copyright__ = 'Copyright 2021, William Stearns'
__credits__ = ['William Stearns']
__email__ = '[email protected]'
__license__ = 'GPL 3.0'
__maintainer__ = 'William Stearns'
__status__ = 'Development' #Prototype, Development or Production
import os
import sys
import json
import shelve
def debug_out(output_string):
"""Send debuging output to stderr."""
sys.stderr.write(output_string + '\n')
sys.stderr.flush()
def add_items_to_persistent(item_dict, p_shelf):
"""Take all the items in the item_dict and add them to p_shelf. Both are dictionaries."""
#The stdin dictionary _values_ must all be strings (string will be appended to shelf list if new) or lists (all new list items will be appended to shelf list).
for one_key in item_dict:
if item_dict[one_key]:
if one_key in p_shelf:
current_list = p_shelf[one_key]
else:
current_list = []
is_modified = False
if isinstance(item_dict[one_key], str):
if item_dict[one_key] not in current_list:
current_list.append(item_dict[one_key])
is_modified = True
elif isinstance(item_dict[one_key], list):
for one_val in item_dict[one_key]:
if one_val not in current_list:
current_list.append(one_val)
is_modified = True
else:
debug_out("value associated with " + one_key + " is not a string or list, skipping.")
if is_modified:
p_shelf[one_key] = current_list
default_shelf_file = os.environ["HOME"] + '/.cache/ip_names_TEST' #FIXME before release
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='shelf_import version ' + str(__version__))
parser.add_argument('-w', '--write', help='Shelf file to which to write new values (default: %(default)s)', required=False, default=default_shelf_file)
(parsed, unparsed) = parser.parse_known_args()
cl_args = vars(parsed)
active_shelf = cl_args['write']
stdintext = sys.stdin.buffer.read().decode("utf-8", 'replace')
try:
import_items = json.loads(stdintext)
except json.decoder.JSONDecodeError as e:
debug_out("Unable to import stdin as a json object, exiting.")
raise e
if isinstance(import_items, dict):
#Here we have a dictionary of items to add to the shelf.
try:
persistent_dict = shelve.open(active_shelf, writeback=True)
add_items_to_persistent(import_items, persistent_dict)
except:
debug_out("Cannot open " + active_shelf + " for writing, exiting.")
sys.exit(1)
else:
debug_out("Std input is not a dictionary, exiting.")
print(import_items)
print(type(import_items))
sys.exit(1)
sys.exit(0)