| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import pprint |
| 5 |
from collections import defaultdict |
| 6 |
|
| 7 |
import yaml |
| 8 |
from pygments import highlight |
| 9 |
from pygments.lexers import YamlLexer |
| 10 |
from pygments.lexers import MarkdownLexer |
| 11 |
from pygments.formatters import TerminalFormatter |
| 12 |
|
| 13 |
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
| 14 |
|
| 15 |
COMMANDS_INFILE = 'commands.yaml' |
| 16 |
COMMANDS_OUTFILE = 'commands.md' |
| 17 |
OPTIONS_INFILE = 'options.yaml' |
| 18 |
OPTIONS_OUTFILE = 'options.md' |
| 19 |
DOCUMENTATION_INFILE = 'documentation.md.in' |
| 20 |
DOCUMENTATION_OUTFILE = 'documentation.md' |
| 21 |
MANUAL_INFILE = 'manual.in.py' |
| 22 |
MANUAL_OUTFILE = '../crazy_complete/manual.py' |
| 23 |
|
| 24 |
OPTIONS = [] |
| 25 |
COMMANDS = [] |
| 26 |
BY_CATEGORY = defaultdict(list) |
| 27 |
|
| 28 |
def escape_underscore(s): |
| 29 |
return s.replace('_', '\\_') |
| 30 |
|
| 31 |
class Command: |
| 32 |
def __init__(self, dictionary): |
| 33 |
self.command = None |
| 34 |
self.category = None |
| 35 |
self.synopsis = None |
| 36 |
self.short = None |
| 37 |
self.long = None |
| 38 |
self.notes = [] |
| 39 |
self.definition = None |
| 40 |
self.output = None |
| 41 |
self.also = None |
| 42 |
self.implemented = None |
| 43 |
|
| 44 |
for key, value in dictionary.items(): |
| 45 |
if key == 'command': |
| 46 |
assert isinstance(value, str), key |
| 47 |
self.command = value |
| 48 |
elif key == 'synopsis': |
| 49 |
assert isinstance(value, str), key |
| 50 |
self.synopsis = value |
| 51 |
elif key == 'short': |
| 52 |
assert isinstance(value, str), key |
| 53 |
self.short = value |
| 54 |
elif key == 'long': |
| 55 |
assert isinstance(value, str), key |
| 56 |
self.long = value |
| 57 |
elif key == 'definition': |
| 58 |
assert isinstance(value, str), key |
| 59 |
self.definition = value |
| 60 |
elif key == 'category': |
| 61 |
assert isinstance(value, str), key |
| 62 |
self.category = value |
| 63 |
elif key == 'output': |
| 64 |
assert isinstance(value, str), key |
| 65 |
self.output = value |
| 66 |
elif key == 'notes': |
| 67 |
assert isinstance(value, list), key |
| 68 |
self.notes = value |
| 69 |
elif key == 'also': |
| 70 |
assert isinstance(value, dict), key |
| 71 |
self.also = value |
| 72 |
elif key == 'implemented': |
| 73 |
assert isinstance(value, list), key |
| 74 |
self.implemented = value |
| 75 |
else: |
| 76 |
raise Exception(f"Unknown key: {key}") |
| 77 |
|
| 78 |
if self.command is None: |
| 79 |
raise Exception("No command") |
| 80 |
|
| 81 |
if self.category is None: |
| 82 |
raise Exception(f"No category: {self.command}") |
| 83 |
|
| 84 |
if self.category not in ('meta', 'custom', 'basic', 'bonus'): |
| 85 |
raise Exception(f"No such category: {self.category}") |
| 86 |
|
| 87 |
if self.short is None: |
| 88 |
raise Exception(f"No short: {self.command}") |
| 89 |
|
| 90 |
if self.short.endswith('.'): |
| 91 |
raise Exception(f"short ends with '.': {self.command}") |
| 92 |
|
| 93 |
if self.definition is None: |
| 94 |
raise Exception(f"No definition: {self.command}") |
| 95 |
|
| 96 |
yaml.safe_load(self.definition) |
| 97 |
|
| 98 |
if self.synopsis is None: |
| 99 |
self.synopsis = f"['{self.command}']" |
| 100 |
|
| 101 |
if self.output is None: |
| 102 |
raise Exception(f"No output: {self.command}") |
| 103 |
|
| 104 |
if '<TAB>' not in self.output: |
| 105 |
raise Exception(f"No <TAB> in output: {self.command}") |
| 106 |
|
| 107 |
if self.implemented: |
| 108 |
for implemented in self.implemented: |
| 109 |
if not implemented in ('Bash', 'Fish', 'Zsh'): |
| 110 |
raise Exception(f"Implemented: Wrong shell {implemented}") |
| 111 |
|
| 112 |
class Option: |
| 113 |
def __init__(self, dictionary): |
| 114 |
self.options = None |
| 115 |
self.metavar = None |
| 116 |
self.choices = None |
| 117 |
self.default = None |
| 118 |
self.short = None |
| 119 |
self.long = None |
| 120 |
self.optional = False |
| 121 |
|
| 122 |
for key, value in dictionary.items(): |
| 123 |
if key == 'options': |
| 124 |
assert isinstance(value, list), key |
| 125 |
self.options = value |
| 126 |
elif key == 'short': |
| 127 |
assert isinstance(value, str), key |
| 128 |
self.short = value |
| 129 |
elif key == 'long': |
| 130 |
assert isinstance(value, str), key |
| 131 |
self.long = value |
| 132 |
elif key == 'metavar': |
| 133 |
assert isinstance(value, str), key |
| 134 |
self.metavar = value |
| 135 |
elif key == 'default': |
| 136 |
assert isinstance(value, str), key |
| 137 |
self.default= value |
| 138 |
elif key == 'choices': |
| 139 |
assert isinstance(value, (list, dict)), key |
| 140 |
self.choices = value |
| 141 |
elif key == 'optional': |
| 142 |
assert isinstance(value, bool), key |
| 143 |
self.optional = value |
| 144 |
else: |
| 145 |
raise Exception(f"Unknown key: {key}") |
| 146 |
|
| 147 |
if self.options is None: |
| 148 |
raise Exception("No options") |
| 149 |
|
| 150 |
if self.short is None: |
| 151 |
raise Exception(f"No short: {self.options}") |
| 152 |
|
| 153 |
|
| 154 |
def make_markup_table(header, rows): |
| 155 |
# Ensure everything is string |
| 156 |
header = [str(h) for h in header] |
| 157 |
rows = [[str(c) for c in row] for row in rows] |
| 158 |
|
| 159 |
# Determine column widths (max of header + rows) |
| 160 |
num_cols = len(header) |
| 161 |
col_widths = [ |
| 162 |
max(len(header[i]), max((len(row[i]) for row in rows), default=0)) |
| 163 |
for i in range(num_cols) |
| 164 |
] |
| 165 |
|
| 166 |
# Helper to format a row |
| 167 |
def fmt_row(row): |
| 168 |
return "| " + " | ".join( |
| 169 |
row[i].ljust(col_widths[i]) for i in range(num_cols) |
| 170 |
) + " |" |
| 171 |
|
| 172 |
# Header separator |
| 173 |
separator = "| " + " | ".join("-" * col_widths[i] for i in range(num_cols)) + " |" |
| 174 |
|
| 175 |
# Build table |
| 176 |
table_lines = [fmt_row(header), separator] |
| 177 |
for row in rows: |
| 178 |
table_lines.append(fmt_row(row)) |
| 179 |
|
| 180 |
return "\n".join(table_lines) |
| 181 |
|
| 182 |
|
| 183 |
def make_english_listing(words): |
| 184 |
if len(words) == 1: |
| 185 |
return words[0] |
| 186 |
elif len(words) == 2: |
| 187 |
return f'{words[0]} and {words[1]}' |
| 188 |
elif len(words) == 3: |
| 189 |
return f'{words[0]}, {words[1]} and {words[2]}' |
| 190 |
|
| 191 |
raise AssertionError("Not reached") |
| 192 |
|
| 193 |
|
| 194 |
def make_implemented_sentence(shells): |
| 195 |
shells_formatted = [f'**{w}**' for w in sorted(shells)] |
| 196 |
r = f'This completer is currently only implemented in {make_english_listing(shells_formatted)}.' |
| 197 |
return r |
| 198 |
|
| 199 |
|
| 200 |
def make_markup_link(text, link): |
| 201 |
return f'[{escape_underscore(text)}](#{link})' |
| 202 |
|
| 203 |
|
| 204 |
def make_markup_summary_table(header, commands, pre_text=None): |
| 205 |
r = [f'### {escape_underscore(header)}'] |
| 206 |
|
| 207 |
if pre_text: |
| 208 |
r += [pre_text] |
| 209 |
|
| 210 |
header = ['Command', 'Description'] |
| 211 |
rows = [] |
| 212 |
for c in commands: |
| 213 |
rows.append([make_markup_link(c.command, c.command), escape_underscore(c.short)]) |
| 214 |
|
| 215 |
r += [make_markup_table(header, rows)] |
| 216 |
|
| 217 |
return '\n\n'.join(r) |
| 218 |
|
| 219 |
def make_markup_command(command): |
| 220 |
r = [f'### {escape_underscore(command.command)}'] |
| 221 |
r += [f'> {command.short}'] |
| 222 |
|
| 223 |
r += ['**SYNOPSIS**'] |
| 224 |
r += [f'```yaml\n{command.synopsis.strip()}\n```'] |
| 225 |
|
| 226 |
if command.long: |
| 227 |
r += ['**PARAMETERS**'] |
| 228 |
r += [command.long] |
| 229 |
|
| 230 |
notes = list(command.notes) |
| 231 |
if command.implemented: |
| 232 |
notes += [make_implemented_sentence(command.implemented)] |
| 233 |
|
| 234 |
if notes: |
| 235 |
r += ['**NOTES**'] |
| 236 |
r += [f'- {l}' for l in notes] |
| 237 |
|
| 238 |
r += ['**EXAMPLE**'] |
| 239 |
r += [f'```yaml\n{command.definition.strip()}\n```'] |
| 240 |
if command.output: |
| 241 |
r += [f'```\n{command.output.strip()}\n```'] |
| 242 |
if command.also: |
| 243 |
r += ['**SEE ALSO**'] |
| 244 |
r += [f'- {make_markup_link(cmd, cmd)}: {desc}' for cmd, desc in command.also.items()] |
| 245 |
|
| 246 |
return '\n\n'.join(r) |
| 247 |
|
| 248 |
|
| 249 |
def make_markup(by_category): |
| 250 |
r = [] |
| 251 |
r += [make_markup_summary_table('Meta commands', BY_CATEGORY['meta'])] |
| 252 |
r += [make_markup_summary_table('Built-in commands', BY_CATEGORY['basic'])] |
| 253 |
r += [make_markup_summary_table('User-defined commands', BY_CATEGORY['custom'])] |
| 254 |
r += [make_markup_summary_table('Bonus commands', BY_CATEGORY['bonus'])] |
| 255 |
|
| 256 |
for commands in by_category.values(): |
| 257 |
for command in commands: |
| 258 |
r += [make_markup_command(command)] |
| 259 |
|
| 260 |
return '\n\n---\n\n'.join(r) |
| 261 |
|
| 262 |
|
| 263 |
def make_option_markup(option): |
| 264 |
option_strings = '|'.join(option.options) |
| 265 |
|
| 266 |
r = f'**{option_strings}' |
| 267 |
if option.metavar: |
| 268 |
r += '=' |
| 269 |
if option.optional: |
| 270 |
r += '[' |
| 271 |
if option.metavar: |
| 272 |
r += option.metavar |
| 273 |
if option.optional: |
| 274 |
r += ']' |
| 275 |
r += '**' |
| 276 |
|
| 277 |
if option.choices: |
| 278 |
choices = ', '.join(option.choices) |
| 279 |
r += f' *({choices})*' |
| 280 |
|
| 281 |
r += f'\n\n> {option.short}' |
| 282 |
|
| 283 |
if option.long: |
| 284 |
r += f'\n\n{option.long.strip()}' |
| 285 |
|
| 286 |
if isinstance(option.choices, dict): |
| 287 |
r += '\n' |
| 288 |
for key, value in option.choices.items(): |
| 289 |
r += f'\n- {key}: {value}' |
| 290 |
|
| 291 |
if option.default: |
| 292 |
r += f'\n\nThis option defaults to `{option.default}`.' |
| 293 |
|
| 294 |
return r |
| 295 |
|
| 296 |
def options_markup(options): |
| 297 |
r = [] |
| 298 |
for option in options: |
| 299 |
r.append(make_option_markup(option)) |
| 300 |
|
| 301 |
return '\n\n---\n\n'.join(r) |
| 302 |
|
| 303 |
|
| 304 |
def make_python_commands(commands): |
| 305 |
data = [] |
| 306 |
|
| 307 |
for command in commands: |
| 308 |
def_colored = highlight(command.definition, YamlLexer(), TerminalFormatter()) |
| 309 |
|
| 310 |
if command.long: |
| 311 |
long_colored = highlight(command.long, MarkdownLexer(), TerminalFormatter()) |
| 312 |
else: |
| 313 |
long_colored = None |
| 314 |
|
| 315 |
data.append({ |
| 316 |
'command': command.command, |
| 317 |
'category': command.category, |
| 318 |
'short': command.short, |
| 319 |
'long': command.long, |
| 320 |
'long_colored': long_colored, |
| 321 |
'notes': command.notes, |
| 322 |
'implemented': command.implemented, |
| 323 |
'definition': command.definition, |
| 324 |
'definition_colored': def_colored, |
| 325 |
'output': command.output, |
| 326 |
'also': command.also |
| 327 |
}) |
| 328 |
|
| 329 |
return 'COMMANDS = ' + pprint.pformat(data) |
| 330 |
|
| 331 |
# ============================================================================= |
| 332 |
# Read commands and options |
| 333 |
# ============================================================================= |
| 334 |
|
| 335 |
with open(COMMANDS_INFILE, 'r', encoding='utf-8') as fh: |
| 336 |
for command_def in yaml.safe_load_all(fh): |
| 337 |
COMMANDS.append(Command(command_def)) |
| 338 |
|
| 339 |
with open(OPTIONS_INFILE, 'r', encoding='utf-8') as fh: |
| 340 |
for option_def in yaml.safe_load_all(fh): |
| 341 |
OPTIONS.append(Option(option_def)) |
| 342 |
|
| 343 |
COMMANDS = sorted(COMMANDS, key=lambda c: c.command) |
| 344 |
|
| 345 |
# ============================================================================= |
| 346 |
# Generate command and option files |
| 347 |
# ============================================================================= |
| 348 |
|
| 349 |
for command in COMMANDS: |
| 350 |
BY_CATEGORY[command.category].append(command) |
| 351 |
|
| 352 |
commands_markup = make_markup(BY_CATEGORY) |
| 353 |
options_markup = options_markup(OPTIONS) |
| 354 |
|
| 355 |
with open(COMMANDS_OUTFILE, 'w', encoding='utf-8') as fh: |
| 356 |
fh.write(commands_markup) |
| 357 |
|
| 358 |
with open(OPTIONS_OUTFILE, 'w', encoding='utf-8') as fh: |
| 359 |
fh.write(options_markup) |
| 360 |
|
| 361 |
with open(DOCUMENTATION_INFILE, 'r', encoding='utf-8') as fh: |
| 362 |
content = fh.read() |
| 363 |
|
| 364 |
content = content.replace('%COMMANDS%', commands_markup) |
| 365 |
content = content.replace('%OPTIONS%', options_markup) |
| 366 |
|
| 367 |
with open(DOCUMENTATION_OUTFILE, 'w', encoding='utf-8') as fh: |
| 368 |
fh.write(content) |
| 369 |
|
| 370 |
# ============================================================================= |
| 371 |
# Generate python manual data |
| 372 |
# ============================================================================= |
| 373 |
|
| 374 |
with open(MANUAL_INFILE, 'r', encoding='utf-8') as fh: |
| 375 |
content = fh.read() |
| 376 |
|
| 377 |
commands_python = make_python_commands(COMMANDS) |
| 378 |
content = content.replace('%COMMANDS%', commands_python) |
| 379 |
|
| 380 |
with open(MANUAL_OUTFILE, 'w', encoding='utf-8') as fh: |
| 381 |
fh.write(content) |