Skip to content

Commit 18c35b2

Browse files
Reading sphinx-config conf.py to get keyword args for sphinx-versioned-docs
Now, selecting branches will take precedence over excluding branches.
1 parent fa5b9da commit 18c35b2

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

sphinx_versioned/build.py

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
import pathlib
44
from sphinx import application
5+
from sphinx.config import Config
56
from sphinx.errors import SphinxError
67
from sphinx.cmd.build import build_main
78

@@ -26,10 +27,25 @@ class VersionedDocs:
2627
config : :class:`dict`
2728
"""
2829

29-
def __init__(self, config: dict) -> None:
30+
def __init__(self, chdir: str, local_conf: str, output_dir: str, git_root: str, config: dict) -> None:
31+
# chdir if required
32+
if chdir:
33+
self.chdir = pathlib.Path(chdir)
34+
if not self.chdir.exists():
35+
log.error(f"Directory not found -- chdir: {chdir}")
36+
raise NotADirectoryError
37+
os.chdir(chdir)
38+
39+
# All paths
40+
self.local_conf = pathlib.Path(local_conf)
41+
self.output_dir = pathlib.Path(output_dir)
42+
self.git_root = pathlib.Path(git_root) if git_root else None
43+
44+
# CLI config variables
3045
self.config = config
31-
self._parse_config(config)
32-
self._handle_paths()
46+
47+
# Read sphinx-conf.py variables
48+
self.read_conf(config)
3349

3450
self._versions_to_pre_build = []
3551
self._versions_to_build = []
@@ -61,70 +77,78 @@ def __init__(self, config: dict) -> None:
6177
print(f"\n\033[92m Successfully built {', '.join([x.name for x in self._built_version])} \033[0m")
6278
return
6379

64-
def _parse_config(self, config: dict) -> bool:
65-
for varname, value in config.items():
66-
setattr(self, varname, value)
67-
68-
self._additional_args = ()
69-
self._additional_args += ("-Q",) if self.quite else ()
70-
self._additional_args += ("-vv",) if self.verbose else ()
71-
return True
72-
73-
def _handle_paths(self) -> None:
74-
"""Method to handle cwd and path for local config, as well as, configure
75-
:class:`~sphinx_versioned.versions.GitVersions` and the output directory.
76-
"""
77-
self.chdir = self.chdir if self.chdir else os.getcwd()
78-
log.debug(f"Working directory {self.chdir}")
79-
80-
self.versions = GitVersions(self.git_root, self.output_dir, self.force_branches)
81-
self.output_dir = pathlib.Path(self.output_dir)
82-
self.local_conf = pathlib.Path(self.local_conf)
83-
80+
def read_conf(self, config) -> bool:
8481
if self.local_conf.name != "conf.py":
8582
self.local_conf = self.local_conf / "conf.py"
8683

84+
# If default conf.py location fails
8785
if not self.local_conf.exists():
8886
log.error(f"conf.py does not exist at {self.local_conf}")
8987
raise FileNotFoundError(f"conf.py not found at {self.local_conf.parent}")
9088

9189
log.success(f"located conf.py")
90+
91+
# Parse sphinx config file i.e. conf.py
92+
self._sphinx_conf = Config.read(self.local_conf.parent.absolute())
93+
sv_conf_values = {
94+
x.replace("sv_", ""): y for x, y in self._sphinx_conf._raw_config.items() if x.startswith("sv_")
95+
}
96+
log.error(sv_conf_values)
97+
log.critical(config)
98+
99+
master_config = config.copy()
100+
for x, y in master_config.items():
101+
if y or x not in sv_conf_values:
102+
continue
103+
master_config[x] = sv_conf_values.get(x)
104+
log.error(master_config)
105+
106+
for varname, value in master_config.items():
107+
setattr(self, varname, value)
108+
109+
# Set additional config for sphinx
110+
self._additional_args = ()
111+
self._additional_args += ("-Q",) if self.quite else ()
112+
self._additional_args += ("-vv",) if self.verbose else ()
113+
114+
# Initialize GitVersions instance
115+
self.versions = GitVersions(self.git_root, self.output_dir, self.force_branches)
92116
return
93117

94-
def _select_branches(self) -> None:
95-
if not self.select_branches:
118+
def _select_branch(self) -> None:
119+
if not self.select_branch:
96120
self._versions_to_pre_build = self._all_branches
121+
self._exclude_branch()
97122
return
98123

99-
for tag in self.select_branches:
124+
for tag in self.select_branch:
100125
if tag in self._lookup_branch.keys():
101126
self._versions_to_pre_build.append(self._lookup_branch.get(tag))
102-
elif self.force_branches:
127+
elif self.force_branch:
103128
log.warning(f"Forcing build for branch `{tag}`, be careful, it may or may not exist!")
104129
self._versions_to_pre_build.append(PseudoBranch(tag))
105130
else:
106131
log.critical(f"Branch not found/selected: `{tag}`, use `--force` to force the build")
107132

108133
return
109134

110-
def _exclude_branches(self) -> None:
111-
if not self.exclude_branches:
135+
def _exclude_branch(self) -> None:
136+
if not self.exclude_branch:
112137
return
113138

114139
_names_versions_to_pre_build = [x.name for x in self._versions_to_pre_build]
115-
for tag in self.exclude_branches:
140+
for tag in self.exclude_branch:
116141
if tag in _names_versions_to_pre_build:
117142
self._versions_to_pre_build.remove(self._lookup_branch.get(tag))
118143

119144
return
120145

121146
def _select_exclude_branches(self) -> list:
122-
log.debug(f"Instructions to select: `{self.select_branches}`")
123-
log.debug(f"Instructions to exclude: `{self.exclude_branches}`")
147+
log.debug(f"Instructions to select: `{self.select_branch}`")
148+
log.debug(f"Instructions to exclude: `{self.exclude_branch}`")
124149
self._versions_to_pre_build = []
125150

126-
self._select_branches()
127-
self._exclude_branches()
151+
self._select_branch()
128152

129153
log.info(f"selected branches: `{[x.name for x in self._versions_to_pre_build]}`")
130154
return

0 commit comments

Comments
 (0)