Fix a bunch of config-related imports

This commit is contained in:
Tim Van Baak 2020-04-23 19:41:52 -07:00
parent ab25d5174a
commit da34711974
9 changed files with 51 additions and 35 deletions

View File

@ -6,8 +6,10 @@ import sys
# Module imports # Module imports
from amanuensis.cli import describe_commands, get_commands from amanuensis.cli import describe_commands, get_commands
from amanuensis.config.context import RootConfigDirectoryContext from amanuensis.config import (
import amanuensis.config as config RootConfigDirectoryContext,
ENV_CONFIG_DIR,
ENV_LOG_FILE)
from amanuensis.errors import AmanuensisError from amanuensis.errors import AmanuensisError
from amanuensis.log import init_logging from amanuensis.log import init_logging
from amanuensis.models import ModelFactory from amanuensis.models import ModelFactory
@ -28,7 +30,7 @@ def get_parser(valid_commands):
# The config directory. # The config directory.
parser.add_argument("--config-dir", parser.add_argument("--config-dir",
dest="config_dir", dest="config_dir",
default=os.environ.get(config.ENV_CONFIG_DIR, "./config"), default=os.environ.get(ENV_CONFIG_DIR, "./config"),
help="The config directory for Amanuensis") help="The config directory for Amanuensis")
# Logging settings. # Logging settings.
parser.add_argument("--verbose", "-v", parser.add_argument("--verbose", "-v",
@ -37,7 +39,7 @@ def get_parser(valid_commands):
help="Enable verbose console logging") help="Enable verbose console logging")
parser.add_argument("--log-file", parser.add_argument("--log-file",
dest="log_file", dest="log_file",
default=os.environ.get(config.ENV_LOG_FILE), default=os.environ.get(ENV_LOG_FILE),
help="Enable verbose file logging") help="Enable verbose file logging")
parser.set_defaults(func=lambda args: parser.print_help()) parser.set_defaults(func=lambda args: parser.print_help())
subp = parser.add_subparsers( subp = parser.add_subparsers(
@ -89,7 +91,7 @@ def main(argv):
try: try:
args.func(args) args.func(args)
except AmanuensisError as e: except AmanuensisError as e:
config.logger.error('Unexpected internal {}: {}'.format( logger.error('Unexpected internal {}: {}'.format(
type(e).__name__, str(e))) type(e).__name__, str(e)))

View File

@ -4,6 +4,7 @@ from amanuensis.cli.helpers import (
add_argument, no_argument, alias, add_argument, no_argument, alias,
config_get, config_set, CONFIG_GET_ROOT_VALUE) config_get, config_set, CONFIG_GET_ROOT_VALUE)
@alias('i') @alias('i')
@add_argument( @add_argument(
"--refresh", action="store_true", "--refresh", action="store_true",
@ -13,8 +14,7 @@ def command_init(args):
Initialize a config directory at --config-dir Initialize a config directory at --config-dir
A clean config directory will contain a config.json, a A clean config directory will contain a config.json, a
pidfile, a lexicon config directory, and a user config lexicon config directory, and a user config directory.
directory.
Refreshing an existing directory will add keys to the global config that Refreshing an existing directory will add keys to the global config that
are present in the default configs. Users and lexicons that are missing are present in the default configs. Users and lexicons that are missing

View File

@ -1,6 +1,10 @@
# Module imports # Module imports
from amanuensis.config.dict import AttrOrderedDict, ReadOnlyOrderedDict from amanuensis.config.dict import AttrOrderedDict, ReadOnlyOrderedDict
from amanuensis.config.directory import RootConfigDirectoryContext, is_guid from amanuensis.config.directory import (
RootConfigDirectoryContext,
UserConfigDirectoryContext,
LexiconConfigDirectoryContext,
is_guid)
# Environment variable name constants # Environment variable name constants
ENV_SECRET_KEY = "AMANUENSIS_SECRET_KEY" ENV_SECRET_KEY = "AMANUENSIS_SECRET_KEY"
@ -13,5 +17,7 @@ __all__ = [
AttrOrderedDict.__name__, AttrOrderedDict.__name__,
ReadOnlyOrderedDict.__name__, ReadOnlyOrderedDict.__name__,
RootConfigDirectoryContext.__name__, RootConfigDirectoryContext.__name__,
UserConfigDirectoryContext.__name__,
LexiconConfigDirectoryContext.__name__,
is_guid.__name__, is_guid.__name__,
] ]

View File

@ -2,10 +2,10 @@ from amanuensis.models.factory import ModelFactory
from amanuensis.models.lexicon import LexiconModel from amanuensis.models.lexicon import LexiconModel
from amanuensis.models.user import UserModelBase, UserModel, AnonymousUserModel from amanuensis.models.user import UserModelBase, UserModel, AnonymousUserModel
__all__ = [ __all__ = [member.__name__ for member in [
'ModelFactory', ModelFactory,
'LexiconModel', LexiconModel,
'UserModelBase', UserModelBase,
'UserModel', UserModel,
'AnonymousUserModel', AnonymousUserModel,
] ]]

View File

@ -1,8 +1,7 @@
from amanuensis.config import is_guid, RootConfigDirectoryContext
from amanuensis.errors import ArgumentError
from amanuensis.models.user import UserModel from amanuensis.models.user import UserModel
from amanuensis.models.lexicon import LexiconModel from amanuensis.models.lexicon import LexiconModel
from amanuensis.config import is_guid
from amanuensis.config.context import RootConfigDirectoryContext
from amanuensis.errors import ArgumentError
class ModelFactory(): class ModelFactory():

View File

@ -1,9 +1,11 @@
import time import time
from typing import cast
from amanuensis.config.context import ( from amanuensis.config import (
RootConfigDirectoryContext, RootConfigDirectoryContext,
LexiconConfigDirectoryContext) LexiconConfigDirectoryContext,
from amanuensis.config.loader import ReadOnlyOrderedDict, json_rw ReadOnlyOrderedDict)
from amanuensis.config.context import json_rw
class LexiconModel(): class LexiconModel():
@ -13,7 +15,7 @@ class LexiconModel():
# Creating the config context implicitly checks for existence # Creating the config context implicitly checks for existence
self._ctx: LexiconConfigDirectoryContext = root.lexicon[lid] self._ctx: LexiconConfigDirectoryContext = root.lexicon[lid]
with self._ctx.config(edit=False) as config: with self._ctx.config(edit=False) as config:
self._cfg: ReadOnlyOrderedDict = config self._cfg: ReadOnlyOrderedDict = cast(ReadOnlyOrderedDict, config)
def __str__(self) -> str: def __str__(self) -> str:
return f'<Lexicon {self.cfg.name}>' return f'<Lexicon {self.cfg.name}>'
@ -45,7 +47,7 @@ class LexiconModel():
return self.cfg.get('title', f'Lexicon {self.cfg.name}') return self.cfg.get('title', f'Lexicon {self.cfg.name}')
def edit(self) -> json_rw: def edit(self) -> json_rw:
return self.ctx.config(edit=True) return cast(json_rw, self.ctx.config(edit=True))
def log(self, message: str) -> None: def log(self, message: str) -> None:
now = int(time.time()) now = int(time.time())

View File

@ -1,9 +1,12 @@
from typing import cast
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
from amanuensis.config.context import ( from amanuensis.config import (
RootConfigDirectoryContext, RootConfigDirectoryContext,
UserConfigDirectoryContext) UserConfigDirectoryContext,
from amanuensis.config.loader import ReadOnlyOrderedDict ReadOnlyOrderedDict)
from amanuensis.config.context import json_rw
class UserModelBase(): class UserModelBase():
@ -51,7 +54,7 @@ class UserModel(UserModelBase):
# Creating the config context implicitly checks for existence # Creating the config context implicitly checks for existence
self._ctx: UserConfigDirectoryContext = root.user[uid] self._ctx: UserConfigDirectoryContext = root.user[uid]
with self._ctx.config(edit=False) as config: with self._ctx.config(edit=False) as config:
self._cfg: ReadOnlyOrderedDict = config self._cfg: ReadOnlyOrderedDict = cast(ReadOnlyOrderedDict, config)
def __str__(self) -> str: def __str__(self) -> str:
return f'<{self.cfg.username}>' return f'<{self.cfg.username}>'
@ -61,9 +64,12 @@ class UserModel(UserModelBase):
# Utility methods # Utility methods
def edit(self) -> json_rw:
return cast(json_rw, self.ctx.config(edit=True))
def set_password(self, password: str) -> None: def set_password(self, password: str) -> None:
pw_hash = generate_password_hash(password) pw_hash = generate_password_hash(password)
with self.ctx.config(edit=True) as cfg: with self.edit() as cfg:
cfg['password'] = pw_hash cfg['password'] = pw_hash
def check_password(self, password) -> bool: def check_password(self, password) -> bool:

View File

@ -8,11 +8,11 @@ from amanuensis.parser.parsing import parse_raw_markdown
from amanuensis.parser.render import PreviewHtmlRenderer, HtmlRenderer from amanuensis.parser.render import PreviewHtmlRenderer, HtmlRenderer
__all__ = [ __all__ = [
'FeatureCounter', FeatureCounter.__name__,
'GetCitations', GetCitations.__name__,
'titlesort', titlesort.__name__,
'filesafe_title', filesafe_title.__name__,
'parse_raw_markdown', parse_raw_markdown.__name__,
'PreviewHtmlRenderer', PreviewHtmlRenderer.__name__,
'HtmlRenderer', HtmlRenderer.__name__,
] ]

View File

@ -1,5 +1,6 @@
import pkg_resources import pkg_resources
def get_stream(*path): def get_stream(*path):
rs_path = "/".join(path) rs_path = "/".join(path)
return pkg_resources.resource_stream(__name__, rs_path) return pkg_resources.resource_stream(__name__, rs_path)