Skip to content

Commit

Permalink
support for config file arguments via conf.py
Browse files Browse the repository at this point in the history
  • Loading branch information
devanshshukla99 committed Jul 24, 2024
1 parent ee6c82e commit 500c9fc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
18 changes: 9 additions & 9 deletions sphinx_versioned/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,28 @@ 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}

# 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()

Expand Down
30 changes: 24 additions & 6 deletions sphinx_versioned/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/test_branch_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit 500c9fc

Please sign in to comment.