2828)
2929from typing import Any , Dict , List , Optional
3030
31- from docutils .parsers .rst import Directive
31+ from docutils .parsers .rst import Directive , directives
3232from docutils .statemachine import StringList , ViewList
3333
3434from sphinx import addnodes , project
@@ -397,10 +397,17 @@ class CylcGlobalDirective(SphinxDirective):
397397 """Represent a Cylc Global Config.
398398 """
399399 optional_arguments = 1
400+ option_spec = {
401+ 'show' : directives .split_escaped_whitespace
402+ }
400403
401404 def run (self ):
405+ display_these = None
406+ if 'show' in self .options :
407+ display_these = [
408+ i .strip () for i in self .options ['show' ][0 ].split (',' )]
402409 src = self .arguments [0 ] if self .arguments else None
403- ret = self .config_to_node (load_cfg (src ), src )
410+ ret = self .config_to_node (load_cfg (src ), src , display_these )
404411 node = addnodes .desc_content ()
405412 self .state .nested_parse (
406413 StringList (ret ),
@@ -410,7 +417,11 @@ def run(self):
410417 return [node ]
411418
412419 @staticmethod
413- def config_to_node (config : [Dict , Any ], src : str ) -> List [str ]:
420+ def config_to_node (
421+ config : [Dict , Any ],
422+ src : str ,
423+ display_these = None ,
424+ ) -> List [str ]:
414425 """Take a global config and create a node for display.
415426
416427 * Displays `platform groups` and then `platforms`.
@@ -482,13 +493,13 @@ def config_to_node(config: [Dict, Any], src: str) -> List[str]:
482493 # Custom keys:
483494 section_content += custom_items (meta )
484495
485- # Key list needs a closing space :
486- section_content . append ( '' )
496+ if display_these :
497+ section_content += custom_items ( conf , these = display_these )
487498
488499 # Add description tag.
489500 description = meta .get ('description' , '' )
490501 if description :
491- section_content . append ( description )
502+ section_content += [ '' , description , '' ]
492503
493504 content += directive (
494505 CYLC_CONF , [title ], content = section_content )
@@ -510,11 +521,19 @@ class CylcWorkflowDirective(SphinxDirective):
510521 """Represent a Cylc Workflow Config.
511522 """
512523 required_arguments = 1
524+ option_spec = {
525+ 'show' : directives .split_escaped_whitespace
526+ }
513527
514528 def run (self ):
529+ display_these = None
530+ if 'show' in self .options :
531+ display_these = [
532+ i .strip () for i in self .options ['show' ][0 ].split (',' )]
515533 ret = self .config_to_node (
516534 load_cfg (self .arguments [0 ]),
517- self .arguments [0 ]
535+ self .arguments [0 ],
536+ display_these
518537 )
519538 node = addnodes .desc_content ()
520539 self .state .nested_parse (
@@ -525,7 +544,7 @@ def run(self):
525544 return [node ]
526545
527546 @staticmethod
528- def config_to_node (config , src ):
547+ def config_to_node (config , src , display_these = None ):
529548 """Document Workflow
530549
531550 Additional processing:
@@ -573,6 +592,10 @@ def config_to_node(config, src):
573592 # Custom keys:
574593 task_content += custom_items (task_meta )
575594
595+ # Config keys given
596+ if display_these :
597+ task_content += custom_items (taskdef , display_these )
598+
576599 desc = task_meta .get ('description' , '' )
577600 if desc :
578601 task_content += ['' , desc , '' ]
@@ -651,16 +674,29 @@ def custom_items(
651674 data: The input dictionary.
652675 not_these: Keys to ignore.
653676 these: Keys to include.
677+
678+ Examples:
679+ >>> data = {'foo': 'I cannot believe it!', 'title': 'Hi'}
680+ >>> custom_items(data)
681+ :foo:
682+ I cannot believe it!
683+ >>> custom(data, these=['title'])
684+ :title:
685+ Hi
654686 """
655687 ret = []
656688 if these :
657689 for key in these :
658- value = data .get ('key' , '' )
659- value = value .replace ("\n " , "\n " )
660- ret .append (f':{ key } :\n { value } ' )
690+ value = data .get (key , '' )
691+ if value and isinstance (value , str ):
692+ value = value .replace ("\n " , "\n " )
693+ ret .append (f':{ key } :\n { value } ' )
661694 else :
662695 for key , val in data .items ():
663- if key not in (not_these or ['title' , 'description' , 'URL' ]):
696+ if (
697+ key not in (not_these or ['title' , 'description' , 'URL' ])
698+ and isinstance (val , str )
699+ ):
664700 value = val .replace ("\n " , "\n " )
665701 ret .append (f':{ key } :\n { value } ' )
666702 return ret
0 commit comments