diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-11-27 06:44:07 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-11-27 06:44:07 -0600 |
commit | 98bfded1b407431ad62642d7f029e4e5f3534c07 (patch) | |
tree | 8f38dd229fe54889d130eb0838c9c47619e96376 /src/pyssg/pyssg.py | |
parent | 9ea43f16d6440dce54ae7f78d46618d25e52f6ec (diff) |
refactor code and fix type checks
still need to refactor more code before migrating to yaml config file
Diffstat (limited to 'src/pyssg/pyssg.py')
-rw-r--r-- | src/pyssg/pyssg.py | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index eb042b6..a496b34 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -4,9 +4,10 @@ from importlib.resources import path as rpath from typing import Union from configparser import ConfigParser from logging import Logger, getLogger, DEBUG +from argparse import ArgumentParser +from .arg_parser import get_parser from .utils import create_dir, copy_file, get_expanded_path -from .arg_parser import get_parsed_arguments from .configuration import get_parsed_config, DEFAULT_CONFIG_PATH, VERSION from .database import Database from .builder import Builder @@ -15,7 +16,30 @@ log: Logger = getLogger(__name__) def main() -> None: - args: dict[str, Union[str, bool]] = vars(get_parsed_arguments()) + arg_parser: ArgumentParser = get_parser() + args: dict[str, Union[str, bool]] = vars(arg_parser.parse_args()) + + # too messy to place at utils.py, don't want to be + # passing the arg parser around + def _log_perror(message: str) -> None: + arg_parser.print_usage() + # even if it's an error, print it as info + # as it is not critical, only config related + log.info(message) + sys.exit(1) + + # -1 as first argument is program path + num_args = len(sys.argv) - 1 + if num_args == 2 and args['config']: + _log_perror('pyssg: error: only config argument passed, --help for more') + elif not num_args > 0 or (num_args == 1 and args['debug']): + _log_perror('pyssg: error: no arguments passed, --help for more') + elif num_args == 3 and (args['debug'] and args['config']): + _log_perror("pyssg: error: no arguments passed other than 'debug' and 'config', --help for more") + + if args['version']: + log.info('pyssg v%s', VERSION) + sys.exit(0) if args['debug']: # need to modify the root logger specifically, @@ -27,15 +51,7 @@ def main() -> None: handler.setLevel(DEBUG) log.debug('changed logging level to DEBUG') - if not len(sys.argv) > 1 or (len(sys.argv) == 2 and args['debug']): - log.info('pyssg v%s - no arguments passed, --help for more', VERSION) - sys.exit(0) - - if args['version']: - log.info('pyssg v%s', VERSION) - sys.exit(0) - - config_path: str = args['config'] if args['config'] else DEFAULT_CONFIG_PATH + config_path: str = str(args['config']) if args['config'] else DEFAULT_CONFIG_PATH config_path = get_expanded_path(config_path) config_dir, _ = os.path.split(config_path) log.debug('checked config file path, final config path "%s"', config_path) @@ -44,7 +60,7 @@ def main() -> None: log.info('copying default config file') create_dir(config_dir) with rpath('pyssg.plt', 'default.ini') as p: - copy_file(p, config_path) + copy_file(str(p), config_path) sys.exit(0) if not os.path.exists(config_path): @@ -61,27 +77,19 @@ def main() -> None: create_dir(config.get('path', 'src')) create_dir(os.path.join(config.get('path', 'dst'), 'tag'), True) create_dir(config.get('path', 'plt')) - files: list[str] = ('index.html', + files: list[str] = ['index.html', 'page.html', 'tag.html', 'rss.xml', - 'sitemap.xml') + 'sitemap.xml'] log.debug('list of files to copy over: (%s)', ', '.join(files)) for f in files: plt_file: str = os.path.join(config.get('path', 'plt'), f) with rpath('pyssg.plt', f) as p: - copy_file(p, plt_file) + copy_file(str(p), plt_file) + log.info('finished initialization') sys.exit(0) - if args['add_checksum_to_db']: - log.info('adding checksum column to existing db') - db_path: str = os.path.join(config.get('path', 'src'), '.files') - db: Database = Database(db_path, config) - # needs to be read_old instead of read - db.read_old() - db.write() - - sys.exit(0) if args['build']: log.info('building the html files') @@ -93,4 +101,5 @@ def main() -> None: builder.build() db.write() + log.info('finished building the html files') sys.exit(0) |