From 286b3bcb1e5baf131c89d16905d5d28c72254050 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sat, 27 Jul 2024 01:33:43 -0600 Subject: [PATCH] broke out Context into its own "config" module to avoid circular dependencies --- src/dragonglass/config.py | 32 ++++++++++++++++++++++++++++++++ src/dragonglass/dragonglass.py | 27 ++------------------------- src/dragonglass/mparse.py | 4 ++-- 3 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/dragonglass/config.py diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py new file mode 100644 index 0000000..bab0e1a --- /dev/null +++ b/src/dragonglass/config.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +from argparse import Namespace +from pathlib import Path +from typing import Any + +import yaml + +from .tree import SourceIndex, SourceNode + + +class Context: + def __init__(self) -> None: + self.source_dir: Path | None = None + self.config: dict[str, Any] = {} + self.src_index: SourceIndex | None = None + self.current_node: SourceNode | None = None + + def load_config(self, args: Namespace) -> None: + config_filename: str = str(args.config) if args.config else ".dragonglass" + assert self.source_dir is not None + config_path = self.source_dir / config_filename + if config_path.exists() and config_path.is_file(): + with open(config_path, "r") as f: + self.config = yaml.full_load(f) + + @property + def url_prefix(self) -> str: + rc = self.config.get("url_prefix", "/") + return rc if rc.endswith("/") else rc + '/' + + diff --git a/src/dragonglass/dragonglass.py b/src/dragonglass/dragonglass.py index 4e8dd68..610a049 100644 --- a/src/dragonglass/dragonglass.py +++ b/src/dragonglass/dragonglass.py @@ -2,12 +2,10 @@ import argparse from pathlib import Path -from typing import Any - -import yaml +from .config import Context from .mparse import create_markdown_parser -from .tree import SourceIndex, SourceNode, generate_list +from .tree import SourceIndex, generate_list # The command line parser parser = argparse.ArgumentParser(prog='dragonglass') @@ -16,27 +14,6 @@ parser.add_argument('dest_dir', help='Destination directory for the conversion.' parser.add_argument('-C', '--config', help='Specifies an alternate name for the configuration file.') -class Context: - def __init__(self) -> None: - self.source_dir: Path | None = None - self.config: dict[str, Any] = {} - self.src_index: SourceIndex | None = None - self.current_node: SourceNode | None = None - - def load_config(self, args: argparse.Namespace) -> None: - config_filename: str = str(args.config) if args.config else ".dragonglass" - assert self.source_dir is not None - config_path = self.source_dir / config_filename - if config_path.exists() and config_path.is_file(): - with open(config_path, "r") as f: - self.config = yaml.full_load(f) - - @property - def url_prefix(self) -> str: - rc = self.config.get("url_prefix", "/") - return rc if rc.endswith("/") else rc + '/' - - def main() -> int: args = parser.parse_args() context = Context() diff --git a/src/dragonglass/mparse.py b/src/dragonglass/mparse.py index 698c3cb..f9f8299 100644 --- a/src/dragonglass/mparse.py +++ b/src/dragonglass/mparse.py @@ -1,4 +1,4 @@ -#/usr/bin/env python3 +#!/usr/bin/env python3 import re import xml.etree.ElementTree as etree # noqa: N813 @@ -11,7 +11,7 @@ from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor, SimpleTagInlineProcessor from markdown.preprocessors import Preprocessor -from .dragonglass import Context +from .config import Context PRIO_BASE = 10000 # priority base for our extensions