From 500c9fc8d201d236780b2c89788caf6a025870ca Mon Sep 17 00:00:00 2001 From: Devansh Shukla Date: Wed, 24 Jul 2024 16:20:57 +0530 Subject: [PATCH] support for config file arguments via `conf.py` --- sphinx_versioned/__main__.py | 18 +++++++++--------- sphinx_versioned/build.py | 30 ++++++++++++++++++++++++------ tests/test_branch_selection.py | 16 ++++++++-------- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/sphinx_versioned/__main__.py b/sphinx_versioned/__main__.py index f5cbafa..8a63f1e 100755 --- a/sphinx_versioned/__main__.py +++ b/sphinx_versioned/__main__.py @@ -131,16 +131,16 @@ def main( select_branch, exclude_branch = parse_branch_selection(branches) config = { - "reset_intersphinx_mapping": reset_intersphinx_mapping, - "sphinx_compatibility": sphinx_compatibility, + "quite": quite, + "verbose": verbose, + "prebuild": prebuild, + "main_branch": main_branch, "force_branch": force_branch, + "select_branch": select_branch, "exclude_branch": exclude_branch, "floating_badge": floating_badge, - "select_branch": select_branch, - "prebuild": prebuild, - "main_branch": main_branch, - "verbose": verbose, - "quite": quite, + "sphinx_compatibility": sphinx_compatibility, + "reset_intersphinx_mapping": reset_intersphinx_mapping, } # Filtered config dict, containing only variables which are `True` filtered_config = {x: y for x, y in config.items() if y} @@ -148,11 +148,11 @@ def main( # VersionedDocs instance DocsBuilder = VersionedDocs( chdir=chdir, + git_root=git_root, local_conf=local_conf, output_dir=output_dir, - git_root=git_root, - ignore_conf=ignore_conf, config=filtered_config, + ignore_conf=ignore_conf, ) return DocsBuilder.run() diff --git a/sphinx_versioned/build.py b/sphinx_versioned/build.py index 36bfc64..b02a1d4 100644 --- a/sphinx_versioned/build.py +++ b/sphinx_versioned/build.py @@ -39,8 +39,26 @@ class VersionedDocs: CLI configuration arguments. """ - def __init__(self, config: dict, debug: bool = False) -> None: - self.config = config + def __init__( + self, + chdir: str, + output_dir: str, + git_root: str, + local_conf: str, + config: dict, + ignore_conf: bool = False, + debug: bool = False, + ) -> None: + if chdir: + log.debug(f"chdir: {chdir}") + os.chdir(chdir) + + self.local_conf = pathlib.Path(local_conf) + self.output_dir = pathlib.Path(output_dir) + self.git_root = git_root + + self._raw_cli_config = config + self.ignore_conf = ignore_conf # Read sphinx-conf.py variables self.read_conf() @@ -144,11 +162,11 @@ def _select_branch(self) -> None: self._exclude_branch() return - for tag in self.select_branches: + for tag in self.config.get("select_branch"): filtered_tags = fnmatch.filter(self._lookup_branch.keys(), tag) if filtered_tags: self._versions_to_pre_build.extend([self._lookup_branch.get(x) for x in filtered_tags]) - elif self.force_branches: + elif self.config.get("force_branch"): log.warning(f"Forcing build for branch `{tag}`, be careful, it may or may not exist!") self._versions_to_pre_build.append(PseudoBranch(tag)) else: @@ -161,7 +179,7 @@ def _exclude_branch(self) -> None: return _names_versions_to_pre_build = [x.name for x in self._versions_to_pre_build] - for tag in self.exclude_branches: + for tag in self.config.get("exclude_branch"): filtered_tags = fnmatch.filter(_names_versions_to_pre_build, tag) for x in filtered_tags: self._versions_to_pre_build.remove(self._lookup_branch.get(x)) @@ -264,7 +282,7 @@ def prebuild(self) -> None: log.critical(f"Pre-build failed for {tag}") finally: # restore to active branch - self.versions.checkout(self._active_branch.name) + self.versions.checkout(self._active_branch) log.success(f"Prebuilding successful for {', '.join([x.name for x in self._versions_to_build])}") return diff --git a/tests/test_branch_selection.py b/tests/test_branch_selection.py index f3b970b..7cc8a47 100644 --- a/tests/test_branch_selection.py +++ b/tests/test_branch_selection.py @@ -43,17 +43,17 @@ def test_parse_branch_selection_regex(branches, select, exclude): parsed_select, parsed_exclude = parse_branch_selection(branches) ver = VersionedDocs( - { - "chdir": ".", - "output_dir": OUTPATH, - "git_root": BASEPATH.parent, - "local_conf": "docs/conf.py", - "select_branches": parsed_select, - "exclude_branches": parsed_exclude, - "main_branch": "main", + chdir=".", + output_dir=OUTPATH, + git_root=BASEPATH.parent, + local_conf="docs/conf.py", + config={ "quite": False, "verbose": True, + "main_branch": "main", "force_branches": True, + "select_branch": parsed_select, + "exclude_branch": parsed_exclude, }, debug=True, )