broke out Context into its own "config" module to avoid circular dependencies

This commit is contained in:
Amy G. Bowersox 2024-07-27 01:33:43 -06:00
parent 1660d91a84
commit 286b3bcb1e
3 changed files with 36 additions and 27 deletions

32
src/dragonglass/config.py Normal file
View File

@ -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 + '/'

View File

@ -2,12 +2,10 @@
import argparse import argparse
from pathlib import Path from pathlib import Path
from typing import Any
import yaml
from .config import Context
from .mparse import create_markdown_parser from .mparse import create_markdown_parser
from .tree import SourceIndex, SourceNode, generate_list from .tree import SourceIndex, generate_list
# The command line parser # The command line parser
parser = argparse.ArgumentParser(prog='dragonglass') 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.') 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: def main() -> int:
args = parser.parse_args() args = parser.parse_args()
context = Context() context = Context()

View File

@ -1,4 +1,4 @@
#/usr/bin/env python3 #!/usr/bin/env python3
import re import re
import xml.etree.ElementTree as etree # noqa: N813 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.inlinepatterns import InlineProcessor, SimpleTagInlineProcessor
from markdown.preprocessors import Preprocessor from markdown.preprocessors import Preprocessor
from .dragonglass import Context from .config import Context
PRIO_BASE = 10000 # priority base for our extensions PRIO_BASE = 10000 # priority base for our extensions