diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index 81a6d63..6bde370 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -6,8 +6,10 @@ import sys # Module imports from amanuensis.cli import describe_commands, get_commands -from amanuensis.config.context import RootConfigDirectoryContext -import amanuensis.config as config +from amanuensis.config import ( + RootConfigDirectoryContext, + ENV_CONFIG_DIR, + ENV_LOG_FILE) from amanuensis.errors import AmanuensisError from amanuensis.log import init_logging from amanuensis.models import ModelFactory @@ -28,7 +30,7 @@ def get_parser(valid_commands): # The config directory. parser.add_argument("--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") # Logging settings. parser.add_argument("--verbose", "-v", @@ -37,7 +39,7 @@ def get_parser(valid_commands): help="Enable verbose console logging") parser.add_argument("--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") parser.set_defaults(func=lambda args: parser.print_help()) subp = parser.add_subparsers( @@ -89,7 +91,7 @@ def main(argv): try: args.func(args) except AmanuensisError as e: - config.logger.error('Unexpected internal {}: {}'.format( + logger.error('Unexpected internal {}: {}'.format( type(e).__name__, str(e))) diff --git a/amanuensis/cli/server.py b/amanuensis/cli/server.py index 3eae23f..c9a0d30 100644 --- a/amanuensis/cli/server.py +++ b/amanuensis/cli/server.py @@ -4,6 +4,7 @@ from amanuensis.cli.helpers import ( add_argument, no_argument, alias, config_get, config_set, CONFIG_GET_ROOT_VALUE) + @alias('i') @add_argument( "--refresh", action="store_true", @@ -13,8 +14,7 @@ def command_init(args): Initialize a config directory at --config-dir A clean config directory will contain a config.json, a - pidfile, a lexicon config directory, and a user config - directory. + lexicon config directory, and a user config directory. Refreshing an existing directory will add keys to the global config that are present in the default configs. Users and lexicons that are missing diff --git a/amanuensis/config/__init__.py b/amanuensis/config/__init__.py index 5de44c7..d4c7949 100644 --- a/amanuensis/config/__init__.py +++ b/amanuensis/config/__init__.py @@ -1,6 +1,10 @@ # Module imports 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 ENV_SECRET_KEY = "AMANUENSIS_SECRET_KEY" @@ -13,5 +17,7 @@ __all__ = [ AttrOrderedDict.__name__, ReadOnlyOrderedDict.__name__, RootConfigDirectoryContext.__name__, + UserConfigDirectoryContext.__name__, + LexiconConfigDirectoryContext.__name__, is_guid.__name__, ] diff --git a/amanuensis/models/__init__.py b/amanuensis/models/__init__.py index 0f73559..194beb0 100644 --- a/amanuensis/models/__init__.py +++ b/amanuensis/models/__init__.py @@ -2,10 +2,10 @@ from amanuensis.models.factory import ModelFactory from amanuensis.models.lexicon import LexiconModel from amanuensis.models.user import UserModelBase, UserModel, AnonymousUserModel -__all__ = [ - 'ModelFactory', - 'LexiconModel', - 'UserModelBase', - 'UserModel', - 'AnonymousUserModel', -] +__all__ = [member.__name__ for member in [ + ModelFactory, + LexiconModel, + UserModelBase, + UserModel, + AnonymousUserModel, +]] diff --git a/amanuensis/models/factory.py b/amanuensis/models/factory.py index 2400886..53ef68b 100644 --- a/amanuensis/models/factory.py +++ b/amanuensis/models/factory.py @@ -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.lexicon import LexiconModel -from amanuensis.config import is_guid -from amanuensis.config.context import RootConfigDirectoryContext -from amanuensis.errors import ArgumentError class ModelFactory(): diff --git a/amanuensis/models/lexicon.py b/amanuensis/models/lexicon.py index 5990653..3d9bfac 100644 --- a/amanuensis/models/lexicon.py +++ b/amanuensis/models/lexicon.py @@ -1,9 +1,11 @@ import time +from typing import cast -from amanuensis.config.context import ( +from amanuensis.config import ( RootConfigDirectoryContext, - LexiconConfigDirectoryContext) -from amanuensis.config.loader import ReadOnlyOrderedDict, json_rw + LexiconConfigDirectoryContext, + ReadOnlyOrderedDict) +from amanuensis.config.context import json_rw class LexiconModel(): @@ -13,7 +15,7 @@ class LexiconModel(): # Creating the config context implicitly checks for existence self._ctx: LexiconConfigDirectoryContext = root.lexicon[lid] with self._ctx.config(edit=False) as config: - self._cfg: ReadOnlyOrderedDict = config + self._cfg: ReadOnlyOrderedDict = cast(ReadOnlyOrderedDict, config) def __str__(self) -> str: return f'' @@ -45,7 +47,7 @@ class LexiconModel(): return self.cfg.get('title', f'Lexicon {self.cfg.name}') 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: now = int(time.time()) diff --git a/amanuensis/models/user.py b/amanuensis/models/user.py index bef15d5..59ec47f 100644 --- a/amanuensis/models/user.py +++ b/amanuensis/models/user.py @@ -1,9 +1,12 @@ +from typing import cast + from werkzeug.security import generate_password_hash, check_password_hash -from amanuensis.config.context import ( +from amanuensis.config import ( RootConfigDirectoryContext, - UserConfigDirectoryContext) -from amanuensis.config.loader import ReadOnlyOrderedDict + UserConfigDirectoryContext, + ReadOnlyOrderedDict) +from amanuensis.config.context import json_rw class UserModelBase(): @@ -51,7 +54,7 @@ class UserModel(UserModelBase): # Creating the config context implicitly checks for existence self._ctx: UserConfigDirectoryContext = root.user[uid] with self._ctx.config(edit=False) as config: - self._cfg: ReadOnlyOrderedDict = config + self._cfg: ReadOnlyOrderedDict = cast(ReadOnlyOrderedDict, config) def __str__(self) -> str: return f'<{self.cfg.username}>' @@ -61,9 +64,12 @@ class UserModel(UserModelBase): # Utility methods + def edit(self) -> json_rw: + return cast(json_rw, self.ctx.config(edit=True)) + def set_password(self, password: str) -> None: pw_hash = generate_password_hash(password) - with self.ctx.config(edit=True) as cfg: + with self.edit() as cfg: cfg['password'] = pw_hash def check_password(self, password) -> bool: diff --git a/amanuensis/parser/__init__.py b/amanuensis/parser/__init__.py index b30c929..eb73e64 100644 --- a/amanuensis/parser/__init__.py +++ b/amanuensis/parser/__init__.py @@ -8,11 +8,11 @@ from amanuensis.parser.parsing import parse_raw_markdown from amanuensis.parser.render import PreviewHtmlRenderer, HtmlRenderer __all__ = [ - 'FeatureCounter', - 'GetCitations', - 'titlesort', - 'filesafe_title', - 'parse_raw_markdown', - 'PreviewHtmlRenderer', - 'HtmlRenderer', + FeatureCounter.__name__, + GetCitations.__name__, + titlesort.__name__, + filesafe_title.__name__, + parse_raw_markdown.__name__, + PreviewHtmlRenderer.__name__, + HtmlRenderer.__name__, ] diff --git a/amanuensis/resources/__init__.py b/amanuensis/resources/__init__.py index 7341f8a..4690d32 100644 --- a/amanuensis/resources/__init__.py +++ b/amanuensis/resources/__init__.py @@ -1,5 +1,6 @@ import pkg_resources + def get_stream(*path): rs_path = "/".join(path) return pkg_resources.resource_stream(__name__, rs_path)