/
config.py
201 lines (163 loc) · 6.31 KB
/
config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'''
Show or edit MicroDrop configuration.
.. versionadded:: 2.13
'''
import argparse
import io
import json
import re
import sys
import configobj
import microdrop as md
import microdrop.config
import pydash
import yaml
def _config_parser():
parser = argparse.ArgumentParser(parents=[md.MICRODROP_PARSER],
add_help=False)
subparsers = parser.add_subparsers(dest='command', help='commands')
subparsers.add_parser('locate', help='Show path to configuration '
'source')
show = subparsers.add_parser('show', help='Show configuration')
show.add_argument('--get', metavar='KEY')
show_format = show.add_mutually_exclusive_group()
show_format.add_argument('--json', action='store_true')
show_format.add_argument('--yaml', action='store_true')
edit = subparsers.add_parser('edit', help='Modify configuration')
edit.add_argument('-n', '--dry-run', action='store_true')
edit_args = edit.add_mutually_exclusive_group(required=True)
edit_args.add_argument('--append', nargs=2, metavar=('KEY', 'VALUE'),
help='Add one configuration value to the beginning '
'of a list key.')
edit_args.add_argument('--prepend', nargs=2, metavar=('KEY', 'VALUE'),
help='Add one configuration value to the end of a '
'list key.')
edit_args.add_argument('--set', nargs=2, metavar=('KEY', 'VALUE'),
help='Set a boolean or string key')
edit_args.add_argument('--remove', nargs=2, metavar=('KEY', 'VALUE'),
help='Remove all instances of configuration value '
'from a list key.')
edit_args.add_argument('--remove-key', metavar='KEY', help='Remove a '
'configuration key (and all its values).')
return parser
CONFIG_PARSER = _config_parser()
def parse_args(args=None):
"""Parses arguments, returns (options, args)."""
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(parents=[CONFIG_PARSER])
args = parser.parse_args(args)
return args
def main(args=None):
'''
Wrap :func:`config` with integer return code.
Parameters
----------
args : argparse.Namespace, optional
Arguments as parsed by :func:`parse_args`.
See also
--------
:func:`parse_args`
'''
config(args)
# Return
return 0
def config(args=None):
'''
Parameters
----------
args : argparse.Namespace, optional
Arguments as parsed by :func:`parse_args`.
See also
--------
:func:`parse_args`
Returns
-------
configobj.ConfigObj
Parsed (and potentially modified) configuration.
'''
if args is None:
args = parse_args()
config = md.config.Config(args.config)
if args.command == 'locate':
print config.filename
elif args.command == 'show':
if args.get:
data = pydash.get(config.data.dict(), args.get)
else:
data = config.data.dict()
if args.json:
# Output in JSON.
json.dump(obj=data, fp=sys.stdout, indent=4)
elif args.yaml:
# Output in YAML format.
print yaml.dump(data, default_flow_style=False),
elif isinstance(data, dict):
# Output in `ini` format.
output = io.BytesIO()
configobj.ConfigObj(data).write(output)
print output.getvalue(),
else:
print data
elif args.command == 'edit':
for action_i in ('append', 'prepend', 'set', 'remove', 'remove_key'):
if getattr(args, action_i):
action = action_i
break
if action in ('append', 'prepend', 'set', 'remove'):
# Unpack key and new value.
key, new_value = getattr(args, action)
# Look up existing value.
config_value = pydash.get(config.data, key)
if action == 'set':
# Set a key to a string value.
# Create dictionary structure containing only the specified key
# and value.
nested_value = pydash.set_({}, key, new_value)
# Merge nested value into existing configuration structure.
pydash.merge(config.data, nested_value)
else:
# Action is a list action.
if config_value is None:
# Create dictionary structure containing only empty list for
# specified key.
config_value = []
nested_value = pydash.set_({}, key, config_value)
# Merge nested value into existing configuration structure.
pydash.merge(config.data, nested_value)
elif not isinstance(config_value, list):
print >> sys.stderr, 'Value at %s is not a list.' % key
raise SystemExit(1)
if new_value in config_value:
# Remove value even if we are appending or prepending to
# avoid duplicate values.
config_value.remove(new_value)
if args.append:
config_value.append(new_value)
elif args.prepend:
config_value.insert(0, new_value)
elif action == 'remove_key':
key = getattr(args, action)
if pydash.get(config.data, key) is not None:
# Key exists.
# Split key into levels.
# Use [negative lookbehind assertion][1] to only split on
# non-escaped '.' characters.
#
# [1]: https://stackoverflow.com/a/21107911/345236
levels = re.split(r'(?<!\\)\.', key)
parents = levels[:-1]
parent = config.data
for parent_i in parents:
parent = parent[parent_i]
# Delete key from deepest parent.
del parent[levels[-1]]
if args.dry_run:
output = io.BytesIO()
config.data.write(output)
print output.getvalue(),
else:
config.save()
return config
if __name__ == '__main__':
config_ = config(parse_args())