TEMPLATE = '''
-.. This file is automatically generated. do not modify
+{% set punct_char = '-' %}
+{# add a header if we have multiple commands in this section #}
+{% if commands | length > 1 %}
+{{ section }}
+{{ section | length * '-' }}
+{# and demote the subsection #}
+{% set punct_char = '^' %}
+{% endif %}
{% for command in commands %}
{{ command.prefix }}
-{{ command.prefix | length * '^' }}
+{{ command.prefix | length * punct_char }}
{{ command.help | wordwrap(70)}}
'''
+def group_by_prefix(commands):
+ last_prefix = None
+ grouped = []
+ for cmd in commands:
+ prefix = cmd.prefix.split(' ', 1)[0]
+ if prefix == last_prefix:
+ grouped.append(cmd)
+ elif last_prefix is None:
+ last_prefix = prefix
+ grouped = [cmd]
+ else:
+ yield last_prefix, grouped
+ last_prefix = prefix
+ grouped = [cmd]
+ assert grouped
+ yield last_prefix, grouped
+
+
+def render_commands(commands):
+ rendered = io.StringIO()
+ for section, grouped in group_by_prefix(commands):
+ logger.debug('rendering commands: %s: %d', section, len(grouped))
+ for cmd in grouped:
+ logger.info('%s ==> %s', section, cmd.prefix)
+ rendered.write(Template(TEMPLATE).render(
+ section=section,
+ commands=grouped))
+ return rendered.getvalue().split('\n')
+
+
class CephMgrCommands(Directive):
"""
extracts commands from specified mgr modules
return command
def _render_cmds(self, commands):
- rendered = Template(TEMPLATE).render(commands=list(commands))
- lines = rendered.split("\n")
+ lines = render_commands(commands)
assert lines
lineno = self.lineno - self.state_machine.input_offset - 1
source = self.state_machine.input_lines.source(lineno)
return command
def _render_cmds(self, commands):
- rendered = Template(TEMPLATE).render(commands=list(commands))
- lines = rendered.split("\n")
+ lines = render_commands(commands)
assert lines
lineno = self.lineno - self.state_machine.input_offset - 1
source = self.state_machine.input_lines.source(lineno)