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
107from pathlib import Path
118
9+ from docutils import nodes
10+ from docutils .parsers .rst import Directive , directives
1211
1312ZEPHYR_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