Implement user-config

This commit is contained in:
Tim Van Baak 2020-01-17 06:58:11 -08:00
parent 759d7d77f9
commit b041d5d784
3 changed files with 38 additions and 7 deletions

View File

@ -79,7 +79,11 @@ def get_parser(valid_commands):
parser.add_argument("-n", parser.add_argument("-n",
metavar="LEXICON", metavar="LEXICON",
dest="lexicon", dest="lexicon",
help="The name of the lexicon to operate on") help="Specify a lexicon to operate on")
parser.add_argument("-u",
metavar="USERNAME",
dest="username",
help="Specify a user to operate on")
parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help()) parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help())
subp = parser.add_subparsers( subp = parser.add_subparsers(
metavar="COMMAND", metavar="COMMAND",

View File

@ -43,6 +43,7 @@ def requires(argument, verify=lambda a: a is not None):
config.logger.error( config.logger.error(
"This command requires specifying {}".format(argument)) "This command requires specifying {}".format(argument))
return -1 return -1
if type(cmd_args) is not ArgumentParser or second_layer:
command(cmd_args) command(cmd_args)
augmented_command.__dict__['wrapper'] = True augmented_command.__dict__['wrapper'] = True
return augmented_command return augmented_command
@ -79,13 +80,16 @@ def config_set(cfg, set_tuple):
config is from a with json_rw context config is from a with json_rw context
set_tuple is a tuple of the pathspec and the value set_tuple is a tuple of the pathspec and the value
""" """
import json
from config import logger from config import logger
pathspec, value = set_tuple pathspec, value = set_tuple
if not pathspec: if not pathspec:
logger.error("Path must be non-empty") logger.error("Path must be non-empty")
path = pathspec.split('.') path = pathspec.split('.')
if not value: try:
value = None value = json.loads(value)
except:
pass # Leave value as string
for spec in path[:-1]: for spec in path[:-1]:
if spec not in cfg: if spec not in cfg:
logger.error("Path not found") logger.error("Path not found")

View File

@ -1,4 +1,4 @@
from cli.helpers import add_argument, no_argument from cli.helpers import add_argument, no_argument, requires, config_get, config_set, CONFIG_GET_ROOT_VALUE
@add_argument("--username", help="User's login handle") @add_argument("--username", help="User's login handle")
@add_argument("--displayname", help="User's publicly displayed name") @add_argument("--displayname", help="User's publicly displayed name")
@ -71,12 +71,35 @@ def command_list(args):
for user in users: for user in users:
print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username'])) print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username']))
@no_argument @add_argument("--get", metavar="PATHSPEC", dest="get",
nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key")
@add_argument("--set", metavar=("PATHSPEC", "VALUE"), dest="set",
nargs=2, help="Set the value of a config key")
@requires("username")
def command_config(args): def command_config(args):
""" """
Interact with a user's config Interact with a user's config
""" """
raise NotImplementedError() import json
import config
import user
if args.get and args.set:
config.logger.error("Specify one of --get and --set")
return -1
uid = user.uid_from_username(args.username)
if not uid:
config.logger.error("User not found")
return -1
if args.get:
with config.json_ro('user', uid, 'config.json') as cfg:
config_get(cfg, args.get)
if args.set:
with config.json_rw('user', uid, 'config.json') as cfg:
config_set(cfg, args.set)
@add_argument("--username", help="The user to change password for") @add_argument("--username", help="The user to change password for")
def command_passwd(args): def command_passwd(args):