| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
""" |
| 5 |
This module provides functions for creating CommandLine objects from |
| 6 |
dictionaries and vice versa. |
| 7 |
""" |
| 8 |
|
| 9 |
from collections import OrderedDict |
| 10 |
|
| 11 |
from .errors import CrazyError, CrazyTypeError |
| 12 |
from .cli import CommandLine, INHERIT |
| 13 |
from .str_utils import validate_prog |
| 14 |
from . import compat |
| 15 |
|
| 16 |
|
| 17 |
def _validate_keys(dictionary, allowed_keys): |
| 18 |
for key in dictionary.keys(): |
| 19 |
if key not in allowed_keys: |
| 20 |
raise CrazyError(f'Unknown key: {key}') |
| 21 |
|
| 22 |
|
| 23 |
def dictionary_to_commandline(dictionary, prog=None): |
| 24 |
'''Convert a single dictionary to a cli.CommandLine object.''' |
| 25 |
|
| 26 |
_validate_keys(dictionary, |
| 27 |
['prog', 'help', 'aliases', 'wraps', |
| 28 |
'abbreviate_commands', 'abbreviate_options', |
| 29 |
'inherit_options', 'options', 'positionals']) |
| 30 |
|
| 31 |
options = dictionary.get('options', []) |
| 32 |
if not isinstance(options, list): |
| 33 |
raise CrazyTypeError('options', 'list', options) |
| 34 |
|
| 35 |
positionals = dictionary.get('positionals', []) |
| 36 |
if not isinstance(positionals, list): |
| 37 |
raise CrazyTypeError('positionals', 'list', positionals) |
| 38 |
|
| 39 |
commandline = CommandLine( |
| 40 |
prog or dictionary['prog'], |
| 41 |
parent = None, |
| 42 |
help = dictionary.get('help', None), |
| 43 |
aliases = dictionary.get('aliases', []), |
| 44 |
wraps = dictionary.get('wraps', None), |
| 45 |
abbreviate_commands = dictionary.get('abbreviate_commands', INHERIT), |
| 46 |
abbreviate_options = dictionary.get('abbreviate_options', INHERIT), |
| 47 |
inherit_options = dictionary.get('inherit_options', INHERIT)) |
| 48 |
|
| 49 |
for option in options: |
| 50 |
_validate_keys(option, |
| 51 |
['option_strings', 'metavar', 'help', 'optional_arg', 'groups', |
| 52 |
'repeatable', 'final', 'hidden', 'complete', 'nosort', |
| 53 |
'when', 'capture', 'long_opt_arg_sep']) |
| 54 |
|
| 55 |
commandline.add_option( |
| 56 |
option.get('option_strings', None), |
| 57 |
metavar = option.get('metavar', None), |
| 58 |
help = option.get('help', None), |
| 59 |
optional_arg = option.get('optional_arg', False), |
| 60 |
groups = option.get('groups', None), |
| 61 |
repeatable = option.get('repeatable', INHERIT), |
| 62 |
final = option.get('final', False), |
| 63 |
hidden = option.get('hidden', False), |
| 64 |
complete = option.get('complete', None), |
| 65 |
nosort = option.get('nosort', False), |
| 66 |
when = option.get('when', None), |
| 67 |
capture = option.get('capture', None), |
| 68 |
long_opt_arg_sep = option.get('long_opt_arg_sep', INHERIT) |
| 69 |
) |
| 70 |
|
| 71 |
for positional in positionals: |
| 72 |
_validate_keys(positional, |
| 73 |
['number', 'metavar', 'help', 'repeatable', 'complete', 'nosort', |
| 74 |
'when', 'capture']) |
| 75 |
|
| 76 |
commandline.add_positional( |
| 77 |
positional.get('number', None), |
| 78 |
metavar = positional.get('metavar', None), |
| 79 |
help = positional.get('help', None), |
| 80 |
repeatable = positional.get('repeatable', False), |
| 81 |
complete = positional.get('complete', None), |
| 82 |
nosort = positional.get('nosort', False), |
| 83 |
when = positional.get('when', None), |
| 84 |
capture = positional.get('capture', None)) |
| 85 |
|
| 86 |
return commandline |
| 87 |
|
| 88 |
|
| 89 |
def _check_prog_in_dictionary(dictionary): |
| 90 |
if 'prog' not in dictionary: |
| 91 |
raise CrazyError('Missing `prog` field') |
| 92 |
|
| 93 |
if not isinstance(dictionary['prog'], str): |
| 94 |
raise CrazyTypeError('prog', 'str', dictionary["prog"]) |
| 95 |
|
| 96 |
try: |
| 97 |
validate_prog(dictionary['prog']) |
| 98 |
except CrazyError as e: |
| 99 |
raise CrazyError(f'prog: {e}') from e |
| 100 |
|
| 101 |
|
| 102 |
def _get_commandline_by_path(root, path): |
| 103 |
current = root |
| 104 |
|
| 105 |
for i, name in enumerate(path): |
| 106 |
path_str = ' '.join(path[0:i + 1]) |
| 107 |
|
| 108 |
if not current.get_subcommands(): |
| 109 |
raise CrazyError(f"Missing definition of program `{path_str}`") |
| 110 |
|
| 111 |
current = current.get_subcommands().get_subcommand_by_name(name) |
| 112 |
if not current: |
| 113 |
raise CrazyError(f"Missing definition of program `{path_str}`") |
| 114 |
|
| 115 |
return current |
| 116 |
|
| 117 |
|
| 118 |
def replace_defines_in_documents(documents): |
| 119 |
'''Replaces defines in a list of documents. |
| 120 |
|
| 121 |
Exmaple: |
| 122 |
prog: '%defines%' |
| 123 |
my_completer: ['choices', ['foo', 'bar']] |
| 124 |
--- |
| 125 |
prog: 'example' |
| 126 |
options: |
| 127 |
- option_strings: ['-o'] |
| 128 |
complete: 'my_completer' |
| 129 |
''' |
| 130 |
|
| 131 |
defines = None |
| 132 |
|
| 133 |
for document in documents: |
| 134 |
if not isinstance(document, dict): |
| 135 |
continue |
| 136 |
|
| 137 |
if 'prog' in document and document['prog'] == '%defines%': |
| 138 |
defines = document |
| 139 |
break |
| 140 |
|
| 141 |
if defines is None: |
| 142 |
return documents |
| 143 |
|
| 144 |
documents.remove(defines) |
| 145 |
defines.pop('prog') |
| 146 |
|
| 147 |
def replace_defines(obj): |
| 148 |
if isinstance(obj, str): |
| 149 |
return defines.get(obj, obj) |
| 150 |
if isinstance(obj, list): |
| 151 |
return [replace_defines(sub) for sub in obj] |
| 152 |
if isinstance(obj, dict): |
| 153 |
return {key: replace_defines(val) for key, val in obj.items()} |
| 154 |
return obj |
| 155 |
|
| 156 |
new = [] |
| 157 |
for document in documents: |
| 158 |
new.append(replace_defines(document)) |
| 159 |
return new |
| 160 |
|
| 161 |
|
| 162 |
def dictionaries_to_commandline(dictionaries): |
| 163 |
'''Convert a list of dictionaries to a cli.CommandLine object.''' |
| 164 |
|
| 165 |
dictionaries = replace_defines_in_documents(dictionaries) |
| 166 |
compat.fix_commandline_dictionaries(dictionaries) |
| 167 |
|
| 168 |
root = CommandLine('root') |
| 169 |
|
| 170 |
for dictionary in dictionaries: |
| 171 |
_check_prog_in_dictionary(dictionary) |
| 172 |
|
| 173 |
path = dictionary['prog'].split() |
| 174 |
progname = path.pop() |
| 175 |
|
| 176 |
cmdline = _get_commandline_by_path(root, path) |
| 177 |
|
| 178 |
subcommands = cmdline.get_subcommands() |
| 179 |
if not subcommands: |
| 180 |
subcommands = cmdline.add_subcommands() |
| 181 |
|
| 182 |
new_cmdline = dictionary_to_commandline(dictionary, progname) |
| 183 |
|
| 184 |
try: |
| 185 |
subcommands.add_commandline_object(new_cmdline) |
| 186 |
except CrazyError as e: |
| 187 |
raise CrazyError(f"Multiple definition of program `{progname}`") from e |
| 188 |
|
| 189 |
if not root.get_subcommands(): |
| 190 |
raise CrazyError("No programs defined") |
| 191 |
|
| 192 |
if len(root.get_subcommands().subcommands) > 1: |
| 193 |
progs = [c.prog for c in root.get_subcommands().subcommands] |
| 194 |
raise CrazyError(f"Too many main programs defined: {progs}") |
| 195 |
|
| 196 |
cmdline = root.get_subcommands().subcommands[0] |
| 197 |
cmdline.parent = None |
| 198 |
return cmdline |
| 199 |
|
| 200 |
|
| 201 |
def option_to_dictionary(self): |
| 202 |
'''Convert a cli.Option object to a dictionary.''' |
| 203 |
|
| 204 |
r = OrderedDict() |
| 205 |
|
| 206 |
r['option_strings'] = self.option_strings |
| 207 |
|
| 208 |
if self.metavar is not None: |
| 209 |
r['metavar'] = self.metavar |
| 210 |
|
| 211 |
if self.help is not None: |
| 212 |
r['help'] = self.help |
| 213 |
|
| 214 |
if self.optional_arg is not False: |
| 215 |
r['optional_arg'] = self.optional_arg |
| 216 |
|
| 217 |
if self.groups is not None: |
| 218 |
r['groups'] = self.groups |
| 219 |
|
| 220 |
if self.repeatable != INHERIT: |
| 221 |
r['repeatable'] = self.repeatable |
| 222 |
|
| 223 |
if self.final is True: |
| 224 |
r['final'] = self.final |
| 225 |
|
| 226 |
if self.hidden is True: |
| 227 |
r['hidden'] = self.hidden |
| 228 |
|
| 229 |
if self.complete: |
| 230 |
r['complete'] = self.complete |
| 231 |
|
| 232 |
if self.nosort is True: |
| 233 |
r['nosort'] = self.nosort |
| 234 |
|
| 235 |
if self.when is not None: |
| 236 |
r['when'] = self.when |
| 237 |
|
| 238 |
if self.capture is not None: |
| 239 |
r['capture'] = self.capture |
| 240 |
|
| 241 |
if self.long_opt_arg_sep != INHERIT: |
| 242 |
r['long_opt_arg_sep'] = self.long_opt_arg_sep |
| 243 |
|
| 244 |
return r |
| 245 |
|
| 246 |
|
| 247 |
def positional_to_dictionary(self): |
| 248 |
'''Convert a cli.Positional object to a dictionary.''' |
| 249 |
|
| 250 |
r = OrderedDict() |
| 251 |
|
| 252 |
r['number'] = self.number |
| 253 |
|
| 254 |
if self.metavar is not None: |
| 255 |
r['metavar'] = self.metavar |
| 256 |
|
| 257 |
if self.help is not None: |
| 258 |
r['help'] = self.help |
| 259 |
|
| 260 |
if self.repeatable is not False: |
| 261 |
r['repeatable'] = self.repeatable |
| 262 |
|
| 263 |
if self.complete: |
| 264 |
r['complete'] = self.complete |
| 265 |
|
| 266 |
if self.nosort is True: |
| 267 |
r['nosort'] = self.nosort |
| 268 |
|
| 269 |
if self.when is not None: |
| 270 |
r['when'] = self.when |
| 271 |
|
| 272 |
if self.capture is not None: |
| 273 |
r['capture'] = self.capture |
| 274 |
|
| 275 |
return r |
| 276 |
|
| 277 |
|
| 278 |
def commandline_to_dictionary(commandline): |
| 279 |
'''Convert a cli.CommandLine object to a dictionary.''' |
| 280 |
|
| 281 |
r = OrderedDict() |
| 282 |
|
| 283 |
r['prog'] = commandline.get_command_path() |
| 284 |
|
| 285 |
if commandline.aliases: |
| 286 |
r['aliases'] = commandline.aliases |
| 287 |
|
| 288 |
if commandline.help is not None: |
| 289 |
r['help'] = commandline.help |
| 290 |
|
| 291 |
if commandline.wraps is not None: |
| 292 |
r['wraps'] = commandline.wraps |
| 293 |
|
| 294 |
if commandline.abbreviate_commands != INHERIT: |
| 295 |
r['abbreviate_commands'] = commandline.abbreviate_commands |
| 296 |
|
| 297 |
if commandline.abbreviate_options != INHERIT: |
| 298 |
r['abbreviate_options'] = commandline.abbreviate_options |
| 299 |
|
| 300 |
if commandline.inherit_options != INHERIT: |
| 301 |
r['inherit_options'] = commandline.inherit_options |
| 302 |
|
| 303 |
if commandline.options: |
| 304 |
r['options'] = options = [] |
| 305 |
for option in commandline.options: |
| 306 |
options.append(option_to_dictionary(option)) |
| 307 |
|
| 308 |
if commandline.positionals: |
| 309 |
r['positionals'] = positionals = [] |
| 310 |
for positional in commandline.positionals: |
| 311 |
positionals.append(positional_to_dictionary(positional)) |
| 312 |
|
| 313 |
return r |
| 314 |
|
| 315 |
|
| 316 |
def commandline_to_dictionaries(commandline): |
| 317 |
'''Convert a cli.CommandLine object to dictionaries.''' |
| 318 |
|
| 319 |
dictionaries = [] |
| 320 |
|
| 321 |
commandline.visit_commandlines( |
| 322 |
lambda c: dictionaries.append(commandline_to_dictionary(c))) |
| 323 |
|
| 324 |
return dictionaries |