diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-04-17 02:13:36 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-04-17 02:13:36 -0600 |
commit | 3c0691df1b7ff1fd04e22ab6055e84cc2137504e (patch) | |
tree | dded20edb6c37f2b6904447684db693fcddf16a7 /src/pyssg/pyssg.py | |
parent | f203d11cff37d6809396d0ea39bf53abe9bad182 (diff) |
refactor config handler and morev0.6.0
Refactored the configuration handling to use configparser; also the argument parser; generally added more 'logging'; updated template resources; and more minor things
Diffstat (limited to 'src/pyssg/pyssg.py')
-rw-r--r-- | src/pyssg/pyssg.py | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index e694565..9b82231 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -1,7 +1,8 @@ import os -import shutil -from importlib.resources import path +import sys +from importlib.resources import path as rpath from typing import Union +from configparser import ConfigParser from jinja2 import Environment, FileSystemLoader from markdown import Markdown @@ -9,61 +10,65 @@ from yafg import YafgExtension from MarkdownHighlight.highlight import HighlightExtension from markdown_checklist.extension import ChecklistExtension +from .utils import create_dir, copy_file, sanity_check_path from .arg_parser import get_parsed_arguments -from .configuration import Configuration +from .configuration import get_parsed_config, DEFAULT_CONFIG_PATH, VERSION from .database import Database from .builder import Builder def main() -> None: - opts: dict[str, Union[str, bool]] = vars(get_parsed_arguments()) - conf_path: str = opts['config'] - conf_path = os.path.expandvars(conf_path) - - - config: Configuration = None - if os.path.exists('pyssgrc'): - config = Configuration('pyssgrc') - else: - config = Configuration(conf_path) - - config.read() - config.fill_missing(opts) - - if opts['version']: - print(f'pyssg v{config.version}') - return - - if opts['init']: - try: - os.mkdir(config.src) - os.makedirs(os.path.join(config.dst, 'tag')) - os.mkdir(config.plt) - except FileExistsError: - pass - - # copy basic template files + args: dict[str, Union[str, bool]] = vars(get_parsed_arguments()) + if not len(sys.argv) > 1: + print(f'pyssg v{VERSION} - no arguments passed, --help for more') + sys.exit(0) + + if args['version']: + print(f'pyssg v{VERSION}') + sys.exit(0) + + config_path: str = args['config'] if args['config'] else DEFAULT_CONFIG_PATH + config_path = os.path.normpath(os.path.expandvars(config_path)) + sanity_check_path(config_path) + config_dir, _ = os.path.split(config_path) + + if args['copy_default_config']: + create_dir(config_dir) + with rpath('pyssg.plt', 'default.ini') as p: + copy_file(p, config_path) + sys.exit(0) + + if not os.path.exists(config_path): + print(f'''config file does't exist in path "{config_path}"; make sure + the path is correct; use --copy-default-config to if you + haven't already''') + sys.exit(1) + + config: ConfigParser = get_parsed_config(config_path) + + if args['init']: + 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', 'page.html', 'tag.html', 'rss.xml', 'sitemap.xml') for f in files: - plt_file: str = os.path.join(config.plt, f) - with path('pyssg.plt', f) as p: - if not os.path.exists(plt_file): - shutil.copy(p, plt_file) - - return + plt_file: str = os.path.join(config.get('path', 'plt'), f) + with rpath('pyssg.plt', f) as p: + copy_file(p, plt_file) + sys.exit(0) - if opts['build']: + if args['build']: # start the db - db: Database = Database(os.path.join(config.src, '.files')) + db: Database = Database(os.path.join(config.get('path', 'src'), '.files')) db.read() # the autoescape option could be a security risk if used in a dynamic # website, as far as i can tell - env: Environment = Environment(loader=FileSystemLoader(config.plt), + env: Environment = Environment(loader=FileSystemLoader(config.get('path', 'plt')), autoescape=False, trim_blocks=True, lstrip_blocks=True) @@ -92,4 +97,4 @@ def main() -> None: builder.build() db.write() - return + sys.exit(0) |