From 5794ce299e0283ed98e102ee1faaeaf86206f588 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sun, 4 Dec 2022 11:10:40 -0600 Subject: change syntax for accessing config variable --- src/pyssg/builder.py | 18 +++++++++--------- src/pyssg/configuration.py | 10 ++++++---- src/pyssg/md_parser.py | 4 ++-- src/pyssg/page.py | 30 +++++++++++++++--------------- src/pyssg/plt/index.html | 6 +++--- src/pyssg/plt/page.html | 4 ++-- src/pyssg/plt/rss.xml | 18 +++++++++--------- src/pyssg/plt/sitemap.xml | 2 +- src/pyssg/plt/tag.html | 4 ++-- src/pyssg/pyssg.py | 11 ++++++----- 10 files changed, 55 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/pyssg/builder.py b/src/pyssg/builder.py index f0ca802..9834e1d 100644 --- a/src/pyssg/builder.py +++ b/src/pyssg/builder.py @@ -24,7 +24,7 @@ class Builder: # the autoescape option could be a security risk if used in a dynamic # website, as far as i can tell log.debug('initializing the jinja environment') - self.__loader: FSLoader = FSLoader(self.config.get('path', 'plt')) + self.__loader: FSLoader = FSLoader(self.config['path']['plt']) self.env: Environment = Environment(loader=self.__loader, autoescape=False, trim_blocks=True, @@ -43,12 +43,12 @@ class Builder: def build(self) -> None: log.debug('building site') - self.dirs = get_dir_structure(self.config.get('path', 'src'), + self.dirs = get_dir_structure(self.config['path']['src'], ['templates']) - self.md_files = get_file_list(self.config.get('path', 'src'), + self.md_files = get_file_list(self.config['path']['src'], ['.md'], ['templates']) - self.html_files = get_file_list(self.config.get('path', 'src'), + self.html_files = get_file_list(self.config['path']['src'], ['.html'], ['templates']) @@ -82,7 +82,7 @@ class Builder: log.debug('creating dir structure') dir_path: str for d in self.dirs: - dir_path = os.path.join(self.config.get('path', 'dst'), d) + dir_path = os.path.join(self.config['path']['dst'], d) # using silent=True to not print the info create dir msgs for this create_dir(dir_path, True, True) @@ -96,11 +96,11 @@ class Builder: dst_file: str for f in self.html_files: - src_file = os.path.join(self.config.get('path', 'src'), f) - dst_file = os.path.join(self.config.get('path', 'dst'), f) + src_file = os.path.join(self.config['path']['src'], f) + dst_file = os.path.join(self.config['path']['dst'], f) # only copy files if they have been modified (or are new) - if self.db.update(src_file, remove=f'{self.config.get("path", "src")}/'): + if self.db.update(src_file, remove=f'{self.config["path"]["src"]}/'): log.debug('file "%s" has been modified or is new, copying', f) copy_file(src_file, dst_file) else: @@ -166,7 +166,7 @@ class Builder: file_name, template_name) template: Template = self.env.get_template(template_name) content: str = template.render(**template_vars) - dst_path: str = os.path.join(self.config.get('path', 'dst'), file_name) + dst_path: str = os.path.join(self.config['path']['dst'], file_name) log.debug('writing html file to path "%s"', dst_path) with open(dst_path, 'w') as f: diff --git a/src/pyssg/configuration.py b/src/pyssg/configuration.py index d420fe8..895df5c 100644 --- a/src/pyssg/configuration.py +++ b/src/pyssg/configuration.py @@ -1,4 +1,6 @@ import sys +import yaml +import pprint from importlib.metadata import version from importlib.resources import path as rpath from datetime import datetime, timezone @@ -10,14 +12,14 @@ from .utils import get_expanded_path log: Logger = getLogger(__name__) -DEFAULT_CONFIG_PATH: str = '$XDG_CONFIG_HOME/pyssg/config.ini' +DEFAULT_CONFIG_PATH: str = '$XDG_CONFIG_HOME/pyssg/config.yaml' VERSION = version('pyssg') def __expand_all_paths(config: ConfigParser) -> None: log.debug('expanding all path options') for option in config.options('path'): - path: str = config.get('path', option) + path: str = config['path'][option] config.set('path', option, get_expanded_path(path)) @@ -54,8 +56,8 @@ def get_parsed_config(path: str) -> ConfigParser: config.set('fmt', 'sitemap_date', '%%Y-%%m-%%d') config.set('info', 'version', VERSION) config.set('info', 'rss_run_date', datetime.now( - tz=timezone.utc).strftime(config.get('fmt', 'rss_date'))) + tz=timezone.utc).strftime(config['fmt']['rss_date'])) config.set('info', 'sitemap_run_date', datetime.now( - tz=timezone.utc).strftime(config.get('fmt', 'sitemap_date'))) + tz=timezone.utc).strftime(config['fmt']['sitemap_date'])) return config diff --git a/src/pyssg/md_parser.py b/src/pyssg/md_parser.py index bbd22a7..061fcd5 100644 --- a/src/pyssg/md_parser.py +++ b/src/pyssg/md_parser.py @@ -63,10 +63,10 @@ class MDParser: log.debug('parsing all files') for f in self.files: log.debug('parsing file "%s"', f) - src_file: str = os.path.join(self.config.get('path', 'src'), f) + src_file: str = os.path.join(self.config['path']['src'], f) log.debug('path "%s"', src_file) # get flag if update is successful - file_updated: bool = self.db.update(src_file, remove=f'{self.config.get("path", "src")}/') + file_updated: bool = self.db.update(src_file, remove=f'{self.config["path"]["src"]}/') log.debug('parsing md into html') content: str = self.md.reset().convert(open(src_file).read()) diff --git a/src/pyssg/page.py b/src/pyssg/page.py index 264bc92..4f2ee43 100644 --- a/src/pyssg/page.py +++ b/src/pyssg/page.py @@ -67,7 +67,7 @@ class Page: return self.meta[meta][0] except KeyError: log.error('failed to parse mandatory metadata "%s" from file "%s"', - meta, os.path.join(self.config.get('path', 'src'), self.name)) + meta, os.path.join(self.config['path']['src'], self.name)) sys.exit(1) @@ -83,23 +83,23 @@ class Page: log.debug('parsing timestamp') self.cdatetime = datetime.fromtimestamp(self.ctimestamp, tz=timezone.utc) - self.cdate = self.cdatetime.strftime(self.config.get('fmt', 'date')) - self.cdate_list = self.cdatetime.strftime(self.config.get('fmt', 'list_date')) - self.cdate_list_sep = self.cdatetime.strftime(self.config.get('fmt', 'list_sep_date')) - self.cdate_rss = self.cdatetime.strftime(self.config.get('fmt', 'rss_date')) + self.cdate = self.cdatetime.strftime(self.config['fmt']['date']) + self.cdate_list = self.cdatetime.strftime(self.config['fmt']['list_date']) + self.cdate_list_sep = self.cdatetime.strftime(self.config['fmt']['list_sep_date']) + self.cdate_rss = self.cdatetime.strftime(self.config['fmt']['rss_date']) self.cdate_sitemap = \ - self.cdatetime.strftime(self.config.get('fmt', 'sitemap_date')) + self.cdatetime.strftime(self.config['fmt']['sitemap_date']) if self.mtimestamp != 0.0: log.debug('parsing modified timestamp') self.mdatetime = datetime.fromtimestamp(self.mtimestamp, tz=timezone.utc) - self.mdate = self.mdatetime.strftime(self.config.get('fmt', 'date')) - self.mdate_list = self.mdatetime.strftime(self.config.get('fmt', 'list_date')) - self.mdate_list_sep = self.mdatetime.strftime(self.config.get('fmt', 'list_sep_date')) - self.mdate_rss = self.mdatetime.strftime(self.config.get('fmt', 'rss_date')) + self.mdate = self.mdatetime.strftime(self.config['fmt']['date']) + self.mdate_list = self.mdatetime.strftime(self.config['fmt']['list_date']) + self.mdate_list_sep = self.mdatetime.strftime(self.config['fmt']['list_sep_date']) + self.mdate_rss = self.mdatetime.strftime(self.config['fmt']['rss_date']) self.mdate_sitemap = \ - self.mdatetime.strftime(self.config.get('fmt', 'sitemap_date')) + self.mdatetime.strftime(self.config['fmt']['sitemap_date']) else: log.debug('not parsing modified timestamp, hasn\'t been modified') @@ -110,22 +110,22 @@ class Page: for t in tags_only: self.tags.append((t, - f'{self.config.get("url", "main")}/tag/@{t}.html')) + f'{self.config["url"]["main"]}/tag/@{t}.html')) except KeyError: log.debug('not parsing tags, doesn\'t have any') log.debug('parsing url') - self.url = f'{self.config.get("url", "main")}/{self.name.replace(".md", ".html")}' + self.url = f'{self.config["url"]["main"]}/{self.name.replace(".md", ".html")}' log.debug('final url "%s"', self.url) log.debug('parsing image url') try: self.image_url = \ - f'{self.config.get("url", "static")}/{self.meta["image_url"][0]}' + f'{self.config["url"]["static"]}/{self.meta["image_url"][0]}' except KeyError: log.debug('using default image, no image_url metadata found') self.image_url = \ - f'{self.config.get("url", "static")}/{self.config.get("url", "default_image")}' + f'{self.config["url"]["static"]}/{self.config["url"]["default_image"]}' log.debug('final image url "%s"', self.image_url) # if contains open graph elements diff --git a/src/pyssg/plt/index.html b/src/pyssg/plt/index.html index 09ca786..d061625 100644 --- a/src/pyssg/plt/index.html +++ b/src/pyssg/plt/index.html @@ -2,11 +2,11 @@ - - Index -- {{config.get('info', 'title')}} + + Index -- {{config['info']['title']}} -

Index -- {{config.get('info', 'title')}}

+

Index -- {{config['info']['title']}}

Some text here.

Tags: diff --git a/src/pyssg/plt/page.html b/src/pyssg/plt/page.html index 15663fa..39101c4 100644 --- a/src/pyssg/plt/page.html +++ b/src/pyssg/plt/page.html @@ -2,8 +2,8 @@ - - {{page.title}} -- {{config.get('info', 'title')}} + + {{page.title}} -- {{config['info']['title']}}

{{page.title}}

diff --git a/src/pyssg/plt/rss.xml b/src/pyssg/plt/rss.xml index be6ddf0..31abd48 100644 --- a/src/pyssg/plt/rss.xml +++ b/src/pyssg/plt/rss.xml @@ -3,24 +3,24 @@ xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"> - {{config.get('info', 'title')}} - {{config.get('url', 'main')}} - + {{config['info']['title']}} + {{config['url']['main']}} + Short site description. en-us Blog Copyright 2021 Somebody some@one.com (Sombody) some@one.com (Sombody) - {{config.get('info', 'rss_run_date')}} - {{config.get('info', 'rss_run_date')}} - pyssg v{{config.get('info', 'version')}} + {{config['info']['rss_run_date']}} + {{config['info']['rss_run_date']}} + pyssg v{{config['info']['version']}} https://validator.w3.org/feed/docs/rss2.html 30 - {{config.get('url', 'static')}}/images/blog.png - {{config.get('info', 'title')}} - {{config.get('url', 'main')}} + {{config['url']['static']}}/images/blog.png + {{config['info']['title']}} + {{config['url']['main']}} {%for p in all_pages%} diff --git a/src/pyssg/plt/sitemap.xml b/src/pyssg/plt/sitemap.xml index af1212a..d9ff21b 100644 --- a/src/pyssg/plt/sitemap.xml +++ b/src/pyssg/plt/sitemap.xml @@ -14,7 +14,7 @@ {%for t in all_tags%} {{t[1]}} - {{config.get('info', 'sitemap_run_date')}} + {{config['info']['sitemap_run_date']}} daily 0.5 diff --git a/src/pyssg/plt/tag.html b/src/pyssg/plt/tag.html index ffd1956..eadfb95 100644 --- a/src/pyssg/plt/tag.html +++ b/src/pyssg/plt/tag.html @@ -2,8 +2,8 @@ - - Posts filtered by {{tag[0]}} -- {{config.get('info', 'title')}} + + Posts filtered by {{tag[0]}} -- {{config['info']['title']}}

Posts filtered by {{tag[0]}}

diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index a496b34..2734a99 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -52,6 +52,7 @@ def main() -> None: log.debug('changed logging level to DEBUG') config_path: str = str(args['config']) if args['config'] else DEFAULT_CONFIG_PATH + # only needed for the 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) @@ -74,9 +75,9 @@ def main() -> None: if args['init']: log.info('initializing the directory structure and copying over templates') - create_dir(config.get('path', 'src')) - create_dir(os.path.join(config.get('path', 'dst'), 'tag'), True) - create_dir(config.get('path', 'plt')) + create_dir(config['path']['src']) + create_dir(os.path.join(config['path']['dst'], 'tag'), True) + create_dir(config['path']['plt']) files: list[str] = ['index.html', 'page.html', 'tag.html', @@ -84,7 +85,7 @@ def main() -> None: '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) + plt_file: str = os.path.join(config['path']['plt'], f) with rpath('pyssg.plt', f) as p: copy_file(str(p), plt_file) log.info('finished initialization') @@ -93,7 +94,7 @@ def main() -> None: if args['build']: log.info('building the html files') - db_path: str = os.path.join(config.get('path', 'src'), '.files') + db_path: str = os.path.join(config['path']['src'], '.files') db: Database = Database(db_path, config) db.read() -- cgit v1.2.3-70-g09d2