Skip to content

Commit 3044d67

Browse files
authored
Avoid self.app in builder (#13632)
1 parent acdf4a8 commit 3044d67

File tree

17 files changed

+78
-45
lines changed

17 files changed

+78
-45
lines changed

sphinx/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def __init__(
264264
else:
265265
self.confdir = _StrPath(confdir).resolve()
266266
self.config = Config.read(self.confdir, confoverrides or {}, self.tags)
267+
self.config._verbosity = -1 if self.quiet else self.verbosity
267268

268269
# set up translation infrastructure
269270
self._init_i18n()

sphinx/builders/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def __init__(self, app: Sphinx, env: BuildEnvironment) -> None:
139139

140140
@property
141141
def app(self) -> Sphinx:
142+
cls_module = self.__class__.__module__
142143
cls_name = self.__class__.__qualname__
143-
_deprecation_warning(__name__, f'{cls_name}.app', remove=(10, 0))
144+
_deprecation_warning(cls_module, f'{cls_name}.app', remove=(10, 0))
144145
return self._app
145146

146147
@property

sphinx/builders/_epub_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def copy_image_files_pil(self) -> None:
425425
__('copying images... '),
426426
'brown',
427427
len(self.images),
428-
self.app.verbosity,
428+
self.config.verbosity,
429429
):
430430
dest = self.images[src]
431431
try:

sphinx/builders/changes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ class ChangesBuilder(Builder):
3030

3131
def init(self) -> None:
3232
self.create_template_bridge()
33-
theme_factory = HTMLThemeFactory(self.app)
33+
theme_factory = HTMLThemeFactory(
34+
confdir=self.confdir,
35+
app=self._app,
36+
config=self.config,
37+
registry=self.env._registry,
38+
)
3439
self.theme = theme_factory.create('default')
3540
self.templates.init(self, self.theme)
3641

sphinx/builders/gettext.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class I18nBuilder(Builder):
165165
def init(self) -> None:
166166
super().init()
167167
self.env.set_versioning_method(self.versioning_method, self.config.gettext_uuid)
168-
self.tags = self.app.tags = I18nTags()
168+
self.tags = self._app.tags = I18nTags()
169169
self.catalogs: defaultdict[str, Catalog] = defaultdict(Catalog)
170170

171171
def get_target_uri(self, docname: str, typ: str | None = None) -> str:
@@ -251,7 +251,7 @@ def init(self) -> None:
251251
def _collect_templates(self) -> set[str]:
252252
template_files = set()
253253
for template_path in self.config.templates_path:
254-
tmpl_abs_path = self.app.srcdir / template_path
254+
tmpl_abs_path = self.srcdir / template_path
255255
for dirpath, _dirs, files in walk(tmpl_abs_path):
256256
for fn in files:
257257
if fn.endswith('.html'):
@@ -268,7 +268,11 @@ def _extract_from_template(self) -> None:
268268
extract_translations = self.templates.environment.extract_translations
269269

270270
for template in status_iterator(
271-
files, __('reading templates... '), 'purple', len(files), self.app.verbosity
271+
files,
272+
__('reading templates... '),
273+
'purple',
274+
len(files),
275+
self.config.verbosity,
272276
):
273277
try:
274278
with codecs.open(template, encoding='utf-8') as f:
@@ -307,15 +311,15 @@ def finish(self) -> None:
307311
__('writing message catalogs... '),
308312
'darkgreen',
309313
len(self.catalogs),
310-
self.app.verbosity,
314+
self.config.verbosity,
311315
operator.itemgetter(0),
312316
):
313317
# noop if config.gettext_compact is set
314318
ensuredir(self.outdir / os.path.dirname(textdomain))
315319

316320
context['messages'] = list(catalog)
317321
template_path = [
318-
self.app.srcdir / rel_path for rel_path in self.config.templates_path
322+
self.srcdir / rel_path for rel_path in self.config.templates_path
319323
]
320324
renderer = GettextRenderer(template_path, outdir=self.outdir)
321325
content = renderer.render('message.pot.jinja', context)

sphinx/builders/html/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ def get_theme_config(self) -> tuple[str, dict[str, str | int | bool]]:
228228
return self.config.html_theme, self.config.html_theme_options
229229

230230
def init_templates(self) -> None:
231-
theme_factory = HTMLThemeFactory(self.app)
231+
theme_factory = HTMLThemeFactory(
232+
confdir=self.confdir,
233+
app=self._app,
234+
config=self.config,
235+
registry=self.env._registry,
236+
)
232237
theme_name, theme_options = self.get_theme_config()
233238
self.theme = theme_factory.create(theme_name)
234239
self.theme_options = theme_options
@@ -255,11 +260,6 @@ def init_highlighter(self) -> None:
255260
self.dark_highlighter: PygmentsBridge | None
256261
if dark_style is not None:
257262
self.dark_highlighter = PygmentsBridge('html', dark_style)
258-
self.app.add_css_file(
259-
'pygments_dark.css',
260-
media='(prefers-color-scheme: dark)',
261-
id='pygments_dark_css',
262-
)
263263
else:
264264
self.dark_highlighter = None
265265

@@ -273,6 +273,13 @@ def css_files(self) -> list[_CascadingStyleSheet]:
273273
def init_css_files(self) -> None:
274274
self._css_files = []
275275
self.add_css_file('pygments.css', priority=200)
276+
if self.dark_highlighter is not None:
277+
self.add_css_file(
278+
'pygments_dark.css',
279+
priority=200,
280+
media='(prefers-color-scheme: dark)',
281+
id='pygments_dark_css',
282+
)
276283

277284
for filename in self._get_style_filenames():
278285
self.add_css_file(filename, priority=200)
@@ -780,7 +787,7 @@ def copy_image_files(self) -> None:
780787
__('copying images... '),
781788
'brown',
782789
len(self.images),
783-
self.app.verbosity,
790+
self.config.verbosity,
784791
stringify_func=stringify_func,
785792
):
786793
dest = self.images[src]
@@ -807,7 +814,7 @@ def to_relpath(f: str) -> str:
807814
__('copying downloadable files... '),
808815
'brown',
809816
len(self.env.dlfiles),
810-
self.app.verbosity,
817+
self.config.verbosity,
811818
stringify_func=to_relpath,
812819
):
813820
try:
@@ -1128,7 +1135,7 @@ def hasdoc(name: str) -> bool:
11281135
# 'blah.html' should have content_root = './' not ''.
11291136
ctx['content_root'] = (f'..{SEP}' * default_baseuri.count(SEP)) or f'.{SEP}'
11301137

1131-
outdir = self.app.outdir
1138+
outdir = self.outdir
11321139

11331140
def css_tag(css: _CascadingStyleSheet) -> str:
11341141
attrs = [

sphinx/builders/latex/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def init(self) -> None:
132132
self.context: dict[str, Any] = {}
133133
self.docnames: Iterable[str] = {}
134134
self.document_data: list[tuple[str, str, str, str, str, bool]] = []
135-
self.themes = ThemeFactory(self.app)
135+
self.themes = ThemeFactory(srcdir=self.srcdir, config=self.config)
136136
texescape.init()
137137

138138
self.init_context()
@@ -481,7 +481,7 @@ def copy_image_files(self) -> None:
481481
__('copying images... '),
482482
'brown',
483483
len(self.images),
484-
self.app.verbosity,
484+
self.config.verbosity,
485485
stringify_func=stringify_func,
486486
):
487487
dest = self.images[src]

sphinx/builders/latex/theming.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
if TYPE_CHECKING:
1313
from pathlib import Path
1414

15-
from sphinx.application import Sphinx
1615
from sphinx.config import Config
1716

1817
logger = logging.getLogger(__name__)
@@ -102,11 +101,11 @@ def __init__(self, name: str, filename: Path) -> None:
102101
class ThemeFactory:
103102
"""A factory class for LaTeX Themes."""
104103

105-
def __init__(self, app: Sphinx) -> None:
104+
def __init__(self, *, srcdir: Path, config: Config) -> None:
106105
self.themes: dict[str, Theme] = {}
107-
self.theme_paths = [app.srcdir / p for p in app.config.latex_theme_path]
108-
self.config = app.config
109-
self.load_builtin_themes(app.config)
106+
self.theme_paths = [srcdir / p for p in config.latex_theme_path]
107+
self.config = config
108+
self.load_builtin_themes(config)
110109

111110
def load_builtin_themes(self, config: Config) -> None:
112111
"""Load built-in themes."""

sphinx/builders/linkcheck.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def finish(self) -> None:
9898
self.process_result(result)
9999

100100
if self.broken_hyperlinks or self.timed_out_hyperlinks:
101-
self.app.statuscode = 1
101+
self._app.statuscode = 1
102102

103103
def process_result(self, result: CheckResult) -> None:
104104
filename = self.env.doc2path(result.docname, False)
@@ -130,7 +130,7 @@ def process_result(self, result: CheckResult) -> None:
130130
case _Status.WORKING:
131131
logger.info(darkgreen('ok ') + f'{res_uri}{result.message}') # NoQA: G003
132132
case _Status.TIMEOUT:
133-
if self.app.quiet:
133+
if self.config.verbosity < 0:
134134
msg = 'timeout ' + f'{res_uri}{result.message}'
135135
logger.warning(msg, location=(result.docname, result.lineno))
136136
else:
@@ -145,7 +145,7 @@ def process_result(self, result: CheckResult) -> None:
145145
)
146146
self.timed_out_hyperlinks += 1
147147
case _Status.BROKEN:
148-
if self.app.quiet:
148+
if self.config.verbosity < 0:
149149
logger.warning(
150150
__('broken link: %s (%s)'),
151151
res_uri,

sphinx/builders/texinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def copy_image_files(self, targetname: str) -> None:
198198
__('copying images... '),
199199
'brown',
200200
len(self.images),
201-
self.app.verbosity,
201+
self.config.verbosity,
202202
stringify_func=stringify_func,
203203
):
204204
dest = self.images[src]

0 commit comments

Comments
 (0)