| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''This module contains functions for generating a YAML file from help texts.''' |
| 5 |
|
| 6 |
from . import help_parser |
| 7 |
from . import yaml_source |
| 8 |
from .str_utils import indent |
| 9 |
|
| 10 |
|
| 11 |
def fix_description(s): |
| 12 |
'''Fix description.''' |
| 13 |
|
| 14 |
s = s.strip() |
| 15 |
|
| 16 |
# Replace hyphens followed by a newline ('-\n') with a simple hyphen. |
| 17 |
# This prevents words like "Some-word-with-\nhyphens" from being split |
| 18 |
# incorrectly due to the newline. Instead, it will correctly join as |
| 19 |
# "Some-word-with-hyphens". |
| 20 |
s = s.replace('-\n', '-') |
| 21 |
|
| 22 |
s = s.replace('\n', ' ') |
| 23 |
return s |
| 24 |
|
| 25 |
|
| 26 |
def strip_metavar(s): |
| 27 |
'''Strip metavar.''' |
| 28 |
|
| 29 |
if s and s[0] == '[' and s[-1] == ']': |
| 30 |
s = s[1:-1] |
| 31 |
|
| 32 |
if s and s[0] == '=': |
| 33 |
s = s[1:] |
| 34 |
|
| 35 |
if s and s[0] == '<' and s[-1] == '>': |
| 36 |
s = s[1:-1] |
| 37 |
|
| 38 |
return s |
| 39 |
|
| 40 |
|
| 41 |
def complete_for_metavar(string): |
| 42 |
'''Return completion for metavar.''' |
| 43 |
|
| 44 |
string = string.strip('<>') |
| 45 |
lower = string.lower() |
| 46 |
|
| 47 |
equals = { |
| 48 |
'f': ['file'], |
| 49 |
'n': ['integer'], |
| 50 |
'duration': ['integer'], |
| 51 |
'bits': ['integer'], |
| 52 |
'cols': ['integer'], |
| 53 |
'column': ['integer'], |
| 54 |
'digits': ['integer'], |
| 55 |
'size': ['integer'], |
| 56 |
'bytes': ['integer'], |
| 57 |
'pid': ['pid'], |
| 58 |
'user': ['user'], |
| 59 |
'group': ['group'], |
| 60 |
'program': ['command'], |
| 61 |
'command': ['command'], |
| 62 |
'cmd': ['cmd'], |
| 63 |
'signal': ['signal'], |
| 64 |
'sig': ['signal'], |
| 65 |
} |
| 66 |
|
| 67 |
endswith = { |
| 68 |
'file': ['file'], |
| 69 |
'filename': ['file'], |
| 70 |
'dir': ['directory'], |
| 71 |
'directory': ['directory'], |
| 72 |
'seconds': ['integer'], |
| 73 |
'length': ['integer'], |
| 74 |
'width': ['integer'], |
| 75 |
'num': ['integer'], |
| 76 |
'number': ['integer'], |
| 77 |
} |
| 78 |
|
| 79 |
if lower in equals: |
| 80 |
return equals[lower] |
| 81 |
|
| 82 |
for suffix, complete in endswith.items(): |
| 83 |
if lower.endswith(suffix): |
| 84 |
return complete |
| 85 |
|
| 86 |
if string.startswith('{') and string.endswith('}'): |
| 87 |
string = string.strip('{}') |
| 88 |
for sep in ['|', ',']: |
| 89 |
if sep in string: |
| 90 |
return ['choices', [item.strip() for item in string.split(sep)]] |
| 91 |
|
| 92 |
return ['none'] |
| 93 |
|
| 94 |
|
| 95 |
def from_file_to_yaml(file): |
| 96 |
'''Parse help text in a file and return YAML.''' |
| 97 |
|
| 98 |
with open(file, 'r', encoding='utf-8') as fh: |
| 99 |
content = fh.read() |
| 100 |
|
| 101 |
prog = help_parser.get_program_name_from_help(content) |
| 102 |
char_stream = help_parser.CharStream(content) |
| 103 |
parsed = help_parser.parse(char_stream) |
| 104 |
|
| 105 |
output = [] |
| 106 |
|
| 107 |
output.append(f'prog: "{prog}"\nhelp: "<Program description here>"\noptions:') |
| 108 |
|
| 109 |
for obj in parsed: |
| 110 |
if isinstance(obj, help_parser.Unparsed): |
| 111 |
output.append(f"# {obj.text.rstrip()}") |
| 112 |
elif isinstance(obj, help_parser.OptionsWithDescription): |
| 113 |
option_dict = { |
| 114 |
'option_strings': [], |
| 115 |
'metavar': None, |
| 116 |
'optional_arg': False, |
| 117 |
'help': fix_description(obj.description or '') |
| 118 |
} |
| 119 |
|
| 120 |
for option in obj.options: |
| 121 |
option_dict['option_strings'].append(option.option) |
| 122 |
|
| 123 |
if option.optional: |
| 124 |
option_dict['optional_arg'] = True |
| 125 |
|
| 126 |
if option.metavar: |
| 127 |
option_dict['metavar'] = strip_metavar(option.metavar) |
| 128 |
option_dict['complete'] = complete_for_metavar(option_dict['metavar']) |
| 129 |
|
| 130 |
output.append( |
| 131 |
indent(yaml_source.option_to_yaml(option_dict), 2) |
| 132 |
) |
| 133 |
|
| 134 |
output.append('') |
| 135 |
|
| 136 |
return '\n'.join(output) |