Refactor lexicon loading into wrapper

This commit is contained in:
Tim Van Baak 2020-01-28 13:24:01 -08:00
parent fe65ec2696
commit 1e70b95548
2 changed files with 18 additions and 15 deletions

View File

@ -5,7 +5,8 @@ import traceback
# Module imports # Module imports
import amanuensis.cli as cli import amanuensis.cli as cli
from amanuensis.cli.helpers import USER_ARGS, USER_KWARGS from amanuensis.cli.helpers import (
USER_ARGS, USER_KWARGS, LEXICON_ARGS, LEXICON_KWARGS)
import amanuensis.config as config import amanuensis.config as config
@ -80,10 +81,7 @@ def get_parser(valid_commands):
default=os.environ.get(config.ENV_LOG_FILE_NUM), default=os.environ.get(config.ENV_LOG_FILE_NUM),
help="Maximum rolling file count") help="Maximum rolling file count")
# Lexicon settings. # Lexicon settings.
parser.add_argument("-n", parser.add_argument(*LEXICON_ARGS, **LEXICON_KWARGS)
metavar="LEXICON",
dest="tl_lexicon",
help="Specify a lexicon to operate on")
parser.add_argument(*USER_ARGS, **USER_KWARGS) parser.add_argument(*USER_ARGS, **USER_KWARGS)
parser.set_defaults( parser.set_defaults(
func=lambda args: repl(args) func=lambda args: repl(args)

View File

@ -49,14 +49,17 @@ def no_argument(command):
# Wrappers for commands requiring lexicon or username options # Wrappers for commands requiring lexicon or username options
LEXICON_ARGS = ['--lexicon']
LEXICON_KWARGS = {
'metavar': 'LEXICON',
'dest': 'lexicon',
'help': 'Specify a user to operate on'}
def requires_lexicon(command): def requires_lexicon(command):
@wraps(command) @wraps(command)
def augmented_command(cmd_args): def augmented_command(cmd_args):
# Add lexicon argument in parser pass # Add lexicon argument in parser pass
if isinstance(cmd_args, ArgumentParser): if isinstance(cmd_args, ArgumentParser):
cmd_args.add_argument( cmd_args.add_argument(*LEXICON_ARGS, **LEXICON_KWARGS)
"-n", metavar="LEXICON", dest="lexicon",
help="Specify a lexicon to operate on")
# If there are more command wrappers, pass through to them # If there are more command wrappers, pass through to them
if command.__dict__.get('wrapper', False): if command.__dict__.get('wrapper', False):
command(cmd_args) command(cmd_args)
@ -64,17 +67,19 @@ def requires_lexicon(command):
return None return None
# Verify lexicon argument in execute pass # Verify lexicon argument in execute pass
base_val = (hasattr(cmd_args, "tl_lexicon") val = ((hasattr(cmd_args, 'lexicon')
and getattr(cmd_args, "tl_lexicon")) and getattr(cmd_args, 'lexicon'))
subp_val = (hasattr(cmd_args, "lexicon") or None)
and getattr(cmd_args, "lexicon"))
val = subp_val or base_val or None
if not val: if not val:
from amanuensis.config import logger from amanuensis.config import logger
logger.error("This command requires specifying a lexicon") logger.error("This command requires specifying a lexicon")
return -1 return -1
# from amanuensis.lexicon import LexiconModel from amanuensis.lexicon import LexiconModel
cmd_args.lexicon = val#LexiconModel.by(name=val).name TODO cmd_args.lexicon = LexiconModel.by(name=val) #TODO catch specific exceptions
if cmd_args.lexicon is None:
from amanuensis.config import logger
logger.error('Could not find lexicon "{}"'.format(val))
return -1
return command(cmd_args) return command(cmd_args)
augmented_command.__dict__['wrapper'] = True augmented_command.__dict__['wrapper'] = True