2
2
import shutil
3
3
import pathlib
4
4
from sphinx import application
5
+ from sphinx .config import Config
5
6
from sphinx .errors import SphinxError
6
7
from sphinx .cmd .build import build_main
7
8
@@ -26,10 +27,25 @@ class VersionedDocs:
26
27
config : :class:`dict`
27
28
"""
28
29
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
30
45
self .config = config
31
- self ._parse_config (config )
32
- self ._handle_paths ()
46
+
47
+ # Read sphinx-conf.py variables
48
+ self .read_conf (config )
33
49
34
50
self ._versions_to_pre_build = []
35
51
self ._versions_to_build = []
@@ -61,70 +77,78 @@ def __init__(self, config: dict) -> None:
61
77
print (f"\n \033 [92m Successfully built { ', ' .join ([x .name for x in self ._built_version ])} \033 [0m" )
62
78
return
63
79
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 :
84
81
if self .local_conf .name != "conf.py" :
85
82
self .local_conf = self .local_conf / "conf.py"
86
83
84
+ # If default conf.py location fails
87
85
if not self .local_conf .exists ():
88
86
log .error (f"conf.py does not exist at { self .local_conf } " )
89
87
raise FileNotFoundError (f"conf.py not found at { self .local_conf .parent } " )
90
88
91
89
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 )
92
116
return
93
117
94
- def _select_branches (self ) -> None :
95
- if not self .select_branches :
118
+ def _select_branch (self ) -> None :
119
+ if not self .select_branch :
96
120
self ._versions_to_pre_build = self ._all_branches
121
+ self ._exclude_branch ()
97
122
return
98
123
99
- for tag in self .select_branches :
124
+ for tag in self .select_branch :
100
125
if tag in self ._lookup_branch .keys ():
101
126
self ._versions_to_pre_build .append (self ._lookup_branch .get (tag ))
102
- elif self .force_branches :
127
+ elif self .force_branch :
103
128
log .warning (f"Forcing build for branch `{ tag } `, be careful, it may or may not exist!" )
104
129
self ._versions_to_pre_build .append (PseudoBranch (tag ))
105
130
else :
106
131
log .critical (f"Branch not found/selected: `{ tag } `, use `--force` to force the build" )
107
132
108
133
return
109
134
110
- def _exclude_branches (self ) -> None :
111
- if not self .exclude_branches :
135
+ def _exclude_branch (self ) -> None :
136
+ if not self .exclude_branch :
112
137
return
113
138
114
139
_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 :
116
141
if tag in _names_versions_to_pre_build :
117
142
self ._versions_to_pre_build .remove (self ._lookup_branch .get (tag ))
118
143
119
144
return
120
145
121
146
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 } `" )
124
149
self ._versions_to_pre_build = []
125
150
126
- self ._select_branches ()
127
- self ._exclude_branches ()
151
+ self ._select_branch ()
128
152
129
153
log .info (f"selected branches: `{ [x .name for x in self ._versions_to_pre_build ]} `" )
130
154
return
0 commit comments