-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
config.py
66 lines (54 loc) · 2.04 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
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2021, Vanessa Sochat"
__license__ = "MPL 2.0"
import shpc.defaults as defaults
from shpc.logger import logger
import sys
def main(args, parser, extra, subparser):
from shpc.main import get_client
# If nothing provided, show help
if not args.params:
print(subparser.format_help())
sys.exit(0)
# The first "param" is either set of get
command = args.params.pop(0)
# If the user wants the central config file
if args.central:
args.settings_file = defaults.default_settings_file
validate = True if not command == "edit" else False
cli = get_client(
quiet=args.quiet, settings_file=args.settings_file, validate=validate
)
# For each new setting, update and save!
if command == "inituser":
return cli.settings.inituser()
if command == "edit":
return cli.settings.edit()
elif command in ["set", "add", "remove"]:
for param in args.params:
if ":" not in param:
logger.warning(
"Param %s is missing a :, should be key:value pair. Skipping."
% param
)
continue
key, value = param.split(":", 1)
if command == "set":
cli.settings.set(key, value)
logger.info("Updated %s to be %s" % (key, value))
elif command == "add":
cli.settings.add(key, value)
logger.info("Added %s to %s" % (key, value))
elif command == "remove":
cli.settings.remove(key, value)
logger.info("Removed %s from %s" % (key, value))
# Save settings
cli.settings.save()
# For each get request, print the param pair
elif command == "get":
for key in args.params:
value = cli.settings.get(key)
value = value or "is unset"
logger.info("%s %s" % (key.ljust(30), value))
else:
logger.error("%s is not a recognized command." % command)