Skip to content

Commit 500c9fc

Browse files
support for config file arguments via conf.py
1 parent ee6c82e commit 500c9fc

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

sphinx_versioned/__main__.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -131,28 +131,28 @@ def main(
131131
select_branch, exclude_branch = parse_branch_selection(branches)
132132

133133
config = {
134-
"reset_intersphinx_mapping": reset_intersphinx_mapping,
135-
"sphinx_compatibility": sphinx_compatibility,
134+
"quite": quite,
135+
"verbose": verbose,
136+
"prebuild": prebuild,
137+
"main_branch": main_branch,
136138
"force_branch": force_branch,
139+
"select_branch": select_branch,
137140
"exclude_branch": exclude_branch,
138141
"floating_badge": floating_badge,
139-
"select_branch": select_branch,
140-
"prebuild": prebuild,
141-
"main_branch": main_branch,
142-
"verbose": verbose,
143-
"quite": quite,
142+
"sphinx_compatibility": sphinx_compatibility,
143+
"reset_intersphinx_mapping": reset_intersphinx_mapping,
144144
}
145145
# Filtered config dict, containing only variables which are `True`
146146
filtered_config = {x: y for x, y in config.items() if y}
147147

148148
# VersionedDocs instance
149149
DocsBuilder = VersionedDocs(
150150
chdir=chdir,
151+
git_root=git_root,
151152
local_conf=local_conf,
152153
output_dir=output_dir,
153-
git_root=git_root,
154-
ignore_conf=ignore_conf,
155154
config=filtered_config,
155+
ignore_conf=ignore_conf,
156156
)
157157
return DocsBuilder.run()
158158

sphinx_versioned/build.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ class VersionedDocs:
3939
CLI configuration arguments.
4040
"""
4141

42-
def __init__(self, config: dict, debug: bool = False) -> None:
43-
self.config = config
42+
def __init__(
43+
self,
44+
chdir: str,
45+
output_dir: str,
46+
git_root: str,
47+
local_conf: str,
48+
config: dict,
49+
ignore_conf: bool = False,
50+
debug: bool = False,
51+
) -> None:
52+
if chdir:
53+
log.debug(f"chdir: {chdir}")
54+
os.chdir(chdir)
55+
56+
self.local_conf = pathlib.Path(local_conf)
57+
self.output_dir = pathlib.Path(output_dir)
58+
self.git_root = git_root
59+
60+
self._raw_cli_config = config
61+
self.ignore_conf = ignore_conf
4462

4563
# Read sphinx-conf.py variables
4664
self.read_conf()
@@ -144,11 +162,11 @@ def _select_branch(self) -> None:
144162
self._exclude_branch()
145163
return
146164

147-
for tag in self.select_branches:
165+
for tag in self.config.get("select_branch"):
148166
filtered_tags = fnmatch.filter(self._lookup_branch.keys(), tag)
149167
if filtered_tags:
150168
self._versions_to_pre_build.extend([self._lookup_branch.get(x) for x in filtered_tags])
151-
elif self.force_branches:
169+
elif self.config.get("force_branch"):
152170
log.warning(f"Forcing build for branch `{tag}`, be careful, it may or may not exist!")
153171
self._versions_to_pre_build.append(PseudoBranch(tag))
154172
else:
@@ -161,7 +179,7 @@ def _exclude_branch(self) -> None:
161179
return
162180

163181
_names_versions_to_pre_build = [x.name for x in self._versions_to_pre_build]
164-
for tag in self.exclude_branches:
182+
for tag in self.config.get("exclude_branch"):
165183
filtered_tags = fnmatch.filter(_names_versions_to_pre_build, tag)
166184
for x in filtered_tags:
167185
self._versions_to_pre_build.remove(self._lookup_branch.get(x))
@@ -264,7 +282,7 @@ def prebuild(self) -> None:
264282
log.critical(f"Pre-build failed for {tag}")
265283
finally:
266284
# restore to active branch
267-
self.versions.checkout(self._active_branch.name)
285+
self.versions.checkout(self._active_branch)
268286

269287
log.success(f"Prebuilding successful for {', '.join([x.name for x in self._versions_to_build])}")
270288
return

tests/test_branch_selection.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ def test_parse_branch_selection_regex(branches, select, exclude):
4343
parsed_select, parsed_exclude = parse_branch_selection(branches)
4444

4545
ver = VersionedDocs(
46-
{
47-
"chdir": ".",
48-
"output_dir": OUTPATH,
49-
"git_root": BASEPATH.parent,
50-
"local_conf": "docs/conf.py",
51-
"select_branches": parsed_select,
52-
"exclude_branches": parsed_exclude,
53-
"main_branch": "main",
46+
chdir=".",
47+
output_dir=OUTPATH,
48+
git_root=BASEPATH.parent,
49+
local_conf="docs/conf.py",
50+
config={
5451
"quite": False,
5552
"verbose": True,
53+
"main_branch": "main",
5654
"force_branches": True,
55+
"select_branch": parsed_select,
56+
"exclude_branch": parsed_exclude,
5757
},
5858
debug=True,
5959
)

0 commit comments

Comments
 (0)