-
Notifications
You must be signed in to change notification settings - Fork 3
/
shelf_export.py
executable file
·87 lines (69 loc) · 2.42 KB
/
shelf_export.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
#!/usr/bin/env python3
"""Exports a shelf-created dictionary to stdout. Tested with DBM files."""
__version__ = '0.0.3'
__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 force_string(raw_string):
"""Make sure the returned object is a string."""
retval = raw_string
if sys.version_info > (3, 0): #Python 3
if isinstance(raw_string, bytes):
retval = raw_string.decode("utf-8", 'replace')
elif isinstance(raw_string, str):
pass
elif isinstance(raw_string, list):
retval = ' '.join([force_string(listitem) for listitem in raw_string])
#print(str(type(raw_string)))
#print("huh:" + str(raw_string))
#sys.exit()
else:
print(str(type(raw_string)))
print("huh:" + str(raw_string))
sys.exit()
retval = str(raw_string)
else:
retval = str(raw_string)
return retval
default_shelf_file = os.environ["HOME"] + '/.cache/ip_names'
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='shelf_export version ' + str(__version__))
parser.add_argument('-r', '--read', help='Shelf file from which to read dictionary (default: %(default)s)', required=False, default=default_shelf_file)
(parsed, unparsed) = parser.parse_known_args()
cl_args = vars(parsed)
active_shelf = cl_args['read']
try:
persistent_dict = shelve.open(active_shelf, flag='r')
except:
sys.stderr.write('Unable to open ' + active_shelf + ' for reading, exiting.\n')
sys.stderr.flush()
sys.exit(1)
print('{')
is_first = True
for a_key in sorted(persistent_dict):
if a_key in persistent_dict:
key_string = force_string(a_key)
#if "'" in str(persistent_dict[a_key]):
# sys.stderr.write('Quote error for ' + str(persistent_dict[a_key]) + '\n')
# sys.stderr.flush()
#else:
if is_first:
is_first = False
else:
print(',')
#sys.stdout.write('\t"' + key_string + '": ' + str(persistent_dict[a_key]).replace("'", '"').replace('{', '[').replace('}', ']'))
sys.stdout.write('\t"' + key_string + '": ' + json.dumps(list(persistent_dict[a_key])))
sys.stdout.flush()
else:
sys.stderr.write('Key error for ' + force_string(a_key) + '\n')
sys.stderr.flush()
print('\n}')