Skip to content

Commit cf005fe

Browse files
kartbennashif
authored andcommitted
doc: _extensions: apply ruff lint rules
This makes all Python scripts in doc/_extensions compliant w.r.t current Ruff rules Signed-off-by: Benjamin Cabé <[email protected]>
1 parent e4539aa commit cf005fe

File tree

12 files changed

+156
-233
lines changed

12 files changed

+156
-233
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -43,78 +43,6 @@
4343
"./boards/microchip/mec172xevb_assy6906/support/mec172x_remote_flasher.py" = [
4444
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
4545
]
46-
"./doc/_extensions/zephyr/api_overview.py" = [
47-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
48-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
49-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
50-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
51-
]
52-
"./doc/_extensions/zephyr/application.py" = [
53-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
54-
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
55-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
56-
]
57-
"./doc/_extensions/zephyr/domain/__init__.py" = [
58-
"B023", # https://docs.astral.sh/ruff/rules/function-uses-loop-variable
59-
"B026", # https://docs.astral.sh/ruff/rules/star-arg-unpacking-after-keyword-arg
60-
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
61-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
62-
"F401", # https://docs.astral.sh/ruff/rules/unused-import
63-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
64-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
65-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
66-
]
67-
"./doc/_extensions/zephyr/doxybridge.py" = [
68-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
69-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
70-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
71-
]
72-
"./doc/_extensions/zephyr/doxyrunner.py" = [
73-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
74-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
75-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
76-
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
77-
"UP024", # https://docs.astral.sh/ruff/rules/os-error-alias
78-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
79-
]
80-
"./doc/_extensions/zephyr/doxytooltip/__init__.py" = [
81-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
82-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
83-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
84-
]
85-
"./doc/_extensions/zephyr/external_content.py" = [
86-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
87-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
88-
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
89-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
90-
]
91-
"./doc/_extensions/zephyr/gh_utils.py" = [
92-
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
93-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
94-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
95-
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
96-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
97-
]
98-
"./doc/_extensions/zephyr/kconfig/__init__.py" = [
99-
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
100-
"SIM112", # https://docs.astral.sh/ruff/rules/uncapitalized-environment-variables
101-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
102-
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
103-
"UP028", # https://docs.astral.sh/ruff/rules/yield-in-for-loop
104-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
105-
]
106-
"./doc/_extensions/zephyr/link-roles.py" = [
107-
"B006", # https://docs.astral.sh/ruff/rules/mutable-argument-default
108-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
109-
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
110-
"UP010", # https://docs.astral.sh/ruff/rules/unnecessary-future-import
111-
]
112-
"./doc/_extensions/zephyr/manifest_projects_table.py" = [
113-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
114-
"SIM114", # https://docs.astral.sh/ruff/rules/if-with-same-arms
115-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
116-
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
117-
]
11846
"./doc/_scripts/gen_boards_catalog.py" = [
11947
"E401", # https://docs.astral.sh/ruff/rules/multiple-imports-on-one-line
12048
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports

doc/_extensions/zephyr/api_overview.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Copyright (c) 2023 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33

4-
import doxmlparser
4+
from pathlib import Path
5+
from typing import Any
56

7+
import doxmlparser
68
from docutils import nodes
79
from doxmlparser.compound import DoxCompoundKind
8-
from pathlib import Path
910
from sphinx.application import Sphinx
1011
from sphinx.util.docutils import SphinxDirective
11-
from typing import Any, Dict
1212

1313

1414
class ApiOverview(SphinxDirective):
@@ -56,7 +56,9 @@ def visit_group(app, group, all_groups, rows, indent=0):
5656

5757
if since:
5858
since_url = nodes.inline()
59-
reference = nodes.reference(text=f"v{since.strip()}.0", refuri=f"{github_uri}/v{since.strip()}.0")
59+
reference = nodes.reference(
60+
text=f"v{since.strip()}.0", refuri=f"{github_uri}/v{since.strip()}.0"
61+
)
6062
reference.attributes["internal"] = True
6163
since_url += reference
6264
else:
@@ -161,7 +163,7 @@ def sync_contents(app: Sphinx) -> None:
161163
app.builder.env.api_overview_table = generate_table(app, toplevel, groups)
162164

163165

164-
def setup(app) -> Dict[str, Any]:
166+
def setup(app) -> dict[str, Any]:
165167
app.add_config_value("api_overview_doxygen_xml_dir", "html/doxygen/xml", "env")
166168
app.add_config_value("api_overview_doxygen_base_url", "../../doxygen/html", "env")
167169

doc/_extensions/zephyr/application.py

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
'''Sphinx extensions related to managing Zephyr applications.'''
66

7-
from docutils import nodes
8-
from docutils.parsers.rst import Directive
9-
from docutils.parsers.rst import directives
107
from pathlib import Path
118

9+
from docutils import nodes
10+
from docutils.parsers.rst import Directive, directives
1211

1312
ZEPHYR_BASE = Path(__file__).parents[3]
1413

@@ -79,8 +78,7 @@ def run(self):
7978
flash_args = self.options.get('flash-args', None)
8079

8180
if tool not in self.TOOLS:
82-
raise self.error('Unknown tool {}; choose from: {}'.format(
83-
tool, self.TOOLS))
81+
raise self.error(f'Unknown tool {tool}; choose from: {self.TOOLS}')
8482

8583
if app and zephyr_app:
8684
raise self.error('Both app and zephyr-app options were given.')
@@ -92,25 +90,22 @@ def run(self):
9290
raise self.error('build-dir-fmt is only supported for the west build tool.')
9391

9492
if generator not in self.GENERATORS:
95-
raise self.error('Unknown generator {}; choose from: {}'.format(
96-
generator, self.GENERATORS))
93+
raise self.error(f'Unknown generator {generator}; choose from: {self.GENERATORS}')
9794

9895
if host_os not in self.HOST_OS:
99-
raise self.error('Unknown host-os {}; choose from: {}'.format(
100-
host_os, self.HOST_OS))
96+
raise self.error(f'Unknown host-os {host_os}; choose from: {self.HOST_OS}')
10197

10298
if compact and skip_config:
10399
raise self.error('Both compact and maybe-skip-config options were given.')
104100

105-
if zephyr_app:
106-
# as folks might use "<...>" notation to indicate a variable portion of the path, we
107-
# deliberately don't check for the validity of such paths.
108-
if not any([x in zephyr_app for x in ["<", ">"]]):
109-
app_path = ZEPHYR_BASE / zephyr_app
110-
if not app_path.is_dir():
111-
raise self.error(
112-
f"zephyr-app: {zephyr_app} is not a valid folder in the zephyr tree."
113-
)
101+
# as folks might use "<...>" notation to indicate a variable portion of the path, we
102+
# deliberately don't check for the validity of such paths.
103+
if zephyr_app and not any([x in zephyr_app for x in ["<", ">"]]):
104+
app_path = ZEPHYR_BASE / zephyr_app
105+
if not app_path.is_dir():
106+
raise self.error(
107+
f"zephyr-app: {zephyr_app} is not a valid folder in the zephyr tree."
108+
)
114109

115110
app = app or zephyr_app
116111
in_tree = self.IN_TREE_STR if zephyr_app else None
@@ -168,7 +163,7 @@ def run(self):
168163
if tool_comment:
169164
paragraph = nodes.paragraph()
170165
paragraph += nodes.Text(tool_comment.format(
171-
'CMake and {}'.format(generator)))
166+
f'CMake and {generator}'))
172167
content.append(paragraph)
173168
content.append(self._lit_block(c))
174169
else:
@@ -208,30 +203,30 @@ def _generate_west(self, **kwargs):
208203
# west always defaults to ninja
209204
gen_arg = ' -G\'Unix Makefiles\'' if generator == 'make' else ''
210205
cmake_args = gen_arg + self._cmake_args(**kwargs)
211-
cmake_args = ' --{}'.format(cmake_args) if cmake_args != '' else ''
206+
cmake_args = f' --{cmake_args}' if cmake_args != '' else ''
212207
build_args = "".join(f" -o {b}" for b in build_args) if build_args else ""
213-
west_args = ' {}'.format(west_args) if west_args else ''
214-
flash_args = ' {}'.format(flash_args) if flash_args else ''
208+
west_args = f' {west_args}' if west_args else ''
209+
flash_args = f' {flash_args}' if flash_args else ''
215210
snippet_args = ''.join(f' -S {s}' for s in snippets) if snippets else ''
216211
shield_args = ''.join(f' --shield {s}' for s in shield) if shield else ''
217212
# ignore zephyr_app since west needs to run within
218213
# the installation. Instead rely on relative path.
219-
src = ' {}'.format(app) if app and not cd_into else ''
214+
src = f' {app}' if app and not cd_into else ''
220215

221216
if build_dir_fmt is None:
222-
dst = ' -d {}'.format(build_dir) if build_dir != 'build' else ''
217+
dst = f' -d {build_dir}' if build_dir != 'build' else ''
223218
build_dst = dst
224219
else:
225220
app_name = app.split('/')[-1]
226221
build_dir_formatted = build_dir_fmt.format(app=app_name, board=board, source_dir=app)
227-
dst = ' -d {}'.format(build_dir_formatted)
222+
dst = f' -d {build_dir_formatted}'
228223
build_dst = ''
229224

230225
if in_tree and not compact:
231226
content.append(in_tree)
232227

233228
if cd_into and app:
234-
content.append('cd {}'.format(app))
229+
content.append(f'cd {app}')
235230

236231
# We always have to run west build.
237232
#
@@ -252,48 +247,49 @@ def _generate_west(self, **kwargs):
252247
# etc. commands can use the signed file which must be created
253248
# in this step.
254249
if 'sign' in goals:
255-
content.append('west sign{}'.format(dst))
250+
content.append(f'west sign{dst}')
256251

257252
for goal in goals:
258253
if goal in {'build', 'sign'}:
259254
continue
260255
elif goal == 'flash':
261-
content.append('west flash{}{}'.format(flash_args, dst))
256+
content.append(f'west flash{flash_args}{dst}')
262257
elif goal == 'debug':
263-
content.append('west debug{}'.format(dst))
258+
content.append(f'west debug{dst}')
264259
elif goal == 'debugserver':
265-
content.append('west debugserver{}'.format(dst))
260+
content.append(f'west debugserver{dst}')
266261
elif goal == 'attach':
267-
content.append('west attach{}'.format(dst))
262+
content.append(f'west attach{dst}')
268263
else:
269-
content.append('west build -t {}{}'.format(goal, dst))
264+
content.append(f'west build -t {goal}{dst}')
270265

271266
return content
272267

273268
@staticmethod
274269
def _mkdir(mkdir, build_dir, host_os, skip_config):
275270
content = []
276271
if skip_config:
277-
content.append("# If you already made a build directory ({}) and ran cmake, just 'cd {}' instead.".format(build_dir, build_dir)) # noqa: E501
272+
content.append(f"# If you already made a build directory ({build_dir}) and ran cmake, "
273+
f"just 'cd {build_dir}' instead.")
278274
if host_os == 'all':
279-
content.append('mkdir {} && cd {}'.format(build_dir, build_dir))
275+
content.append(f'mkdir {build_dir} && cd {build_dir}')
280276
if host_os == "unix":
281-
content.append('{} {} && cd {}'.format(mkdir, build_dir, build_dir))
277+
content.append(f'{mkdir} {build_dir} && cd {build_dir}')
282278
elif host_os == "win":
283279
build_dir = build_dir.replace('/', '\\')
284-
content.append('mkdir {} & cd {}'.format(build_dir, build_dir))
280+
content.append(f'mkdir {build_dir} & cd {build_dir}')
285281
return content
286282

287283
@staticmethod
288284
def _cmake_args(**kwargs):
289285
board = kwargs['board']
290286
conf = kwargs['conf']
291287
gen_args = kwargs['gen_args']
292-
board_arg = ' -DBOARD={}'.format(board) if board else ''
293-
conf_arg = ' -DCONF_FILE={}'.format(conf) if conf else ''
294-
gen_args = ' {}'.format(gen_args) if gen_args else ''
288+
board_arg = f' -DBOARD={board}' if board else ''
289+
conf_arg = f' -DCONF_FILE={conf}' if conf else ''
290+
gen_args = f' {gen_args}' if gen_args else ''
295291

296-
return '{}{}{}'.format(board_arg, conf_arg, gen_args)
292+
return f'{board_arg}{conf_arg}{gen_args}'
297293

298294
def _cd_into(self, mkdir, **kwargs):
299295
app = kwargs['app']
@@ -319,13 +315,13 @@ def _cd_into(self, mkdir, **kwargs):
319315
if os_comment:
320316
content.append(os_comment.format('Linux/macOS'))
321317
if app:
322-
content.append('cd {}'.format(app))
318+
content.append(f'cd {app}')
323319
elif host == "win":
324320
if os_comment:
325321
content.append(os_comment.format('Windows'))
326322
if app:
327323
backslashified = app.replace('/', '\\')
328-
content.append('cd {}'.format(backslashified))
324+
content.append(f'cd {backslashified}')
329325
if mkdir:
330326
content.extend(self._mkdir(mkdir, build_dir, host, skip_config))
331327
if not compact:
@@ -359,39 +355,36 @@ def _generate_cmake(self, **kwargs):
359355
cmake_build_dir = ''
360356
tool_build_dir = ''
361357
else:
362-
source_dir = ' {}'.format(app) if app else ' .'
363-
cmake_build_dir = ' -B{}'.format(build_dir)
364-
tool_build_dir = ' -C{}'.format(build_dir)
358+
source_dir = f' {app}' if app else ' .'
359+
cmake_build_dir = f' -B{build_dir}'
360+
tool_build_dir = f' -C{build_dir}'
365361

366362
# Now generate the actual cmake and make/ninja commands
367363
gen_arg = ' -GNinja' if generator == 'ninja' else ''
368-
build_args = ' {}'.format(build_args) if build_args else ''
364+
build_args = f' {build_args}' if build_args else ''
369365
snippet_args = ' -DSNIPPET="{}"'.format(';'.join(snippets)) if snippets else ''
370366
shield_args = ' -DSHIELD="{}"'.format(';'.join(shield)) if shield else ''
371367
cmake_args = self._cmake_args(**kwargs)
372368

373369
if not compact:
374370
if not cd_into and skip_config:
375-
content.append("# If you already ran cmake with -B{}, you " \
376-
"can skip this step and run {} directly.".
377-
format(build_dir, generator)) # noqa: E501
371+
content.append(f'# If you already ran cmake with -B{build_dir}, you '
372+
f'can skip this step and run {generator} directly.')
378373
else:
379-
content.append('# Use cmake to configure a {}-based build' \
380-
'system:'.format(generator.capitalize())) # noqa: E501
374+
content.append(f'# Use cmake to configure a {generator.capitalize()}-based build'
375+
'system:')
381376

382-
content.append('cmake{}{}{}{}{}{}'.format(cmake_build_dir, gen_arg,
383-
cmake_args, snippet_args, shield_args, source_dir))
377+
content.append(f'cmake{cmake_build_dir}{gen_arg}{cmake_args}{snippet_args}{shield_args}{source_dir}')
384378
if not compact:
385379
content.extend(['',
386380
'# Now run the build tool on the generated build system:'])
387381

388382
if 'build' in goals:
389-
content.append('{}{}{}'.format(generator, tool_build_dir,
390-
build_args))
383+
content.append(f'{generator}{tool_build_dir}{build_args}')
391384
for goal in goals:
392385
if goal == 'build':
393386
continue
394-
content.append('{}{} {}'.format(generator, tool_build_dir, goal))
387+
content.append(f'{generator}{tool_build_dir} {goal}')
395388

396389
return content
397390

0 commit comments

Comments
 (0)