| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''This module contains code for validating completing definitions.''' |
| 5 |
|
| 6 |
# Unlike the `scheme_validator`, this validator operates directly on Python |
| 7 |
# objects and therefore does not have access to line and column information. |
| 8 |
# To save code and maintain consistency, we reuse the validation logic |
| 9 |
# from `scheme_validator` by wrapping the objects in `ValueWithoutTrace`. |
| 10 |
# This provides the same structure as used in `scheme_validator`, except that |
| 11 |
# we use `ValueWithoutTrace` instead of `ValueWithTrace`. |
| 12 |
|
| 13 |
from types import NoneType |
| 14 |
|
| 15 |
from . import scheme_validator |
| 16 |
from .value_with_trace import ValueWithOutTrace |
| 17 |
|
| 18 |
|
| 19 |
def _mk_value_without_trace(value): |
| 20 |
if isinstance(value, ValueWithOutTrace): |
| 21 |
return value |
| 22 |
|
| 23 |
if isinstance(value, (NoneType, str, int, float, bool)): |
| 24 |
return ValueWithOutTrace(value) |
| 25 |
|
| 26 |
mk = _mk_value_without_trace |
| 27 |
|
| 28 |
if isinstance(value, (list, tuple)): |
| 29 |
return ValueWithOutTrace(list(map(mk, value))) |
| 30 |
|
| 31 |
if isinstance(value, dict): |
| 32 |
return ValueWithOutTrace({mk(k): mk(v) for k, v in value.items()}) |
| 33 |
|
| 34 |
raise AssertionError(f"Not reached: {value!r}") |
| 35 |
|
| 36 |
|
| 37 |
def _option_to_definition(option): |
| 38 |
return { |
| 39 |
'option_strings': option.option_strings, |
| 40 |
'metavar': option.metavar, |
| 41 |
'help': option.help, |
| 42 |
'complete': option.complete, |
| 43 |
'nosort': option.nosort, |
| 44 |
'groups': option.groups, |
| 45 |
'optional_arg': option.optional_arg, |
| 46 |
'repeatable': option.repeatable, |
| 47 |
'final': option.final, |
| 48 |
'hidden': option.hidden, |
| 49 |
'when': option.when, |
| 50 |
'capture': option.capture, |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def _positional_to_definition(positional): |
| 55 |
return { |
| 56 |
'number': positional.number, |
| 57 |
'metavar': positional.metavar, |
| 58 |
'help': positional.help, |
| 59 |
'repeatable': positional.repeatable, |
| 60 |
'complete': positional.complete, |
| 61 |
'nosort': positional.nosort, |
| 62 |
'when': positional.when, |
| 63 |
'capture': positional.capture, |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
def _commandline_to_definition(cmdline): |
| 68 |
options = list(map(_option_to_definition, cmdline.options)) |
| 69 |
positionals = list(map(_positional_to_definition, cmdline.positionals)) |
| 70 |
|
| 71 |
definition = { |
| 72 |
'prog': cmdline.get_command_path(), |
| 73 |
'help': cmdline.help, |
| 74 |
'aliases': cmdline.aliases, |
| 75 |
'wraps': cmdline.wraps, |
| 76 |
'abbreviate_commands': cmdline.abbreviate_commands, |
| 77 |
'abbreviate_options': cmdline.abbreviate_options, |
| 78 |
'inherit_options': cmdline.inherit_options, |
| 79 |
'options': options, |
| 80 |
'positionals': positionals, |
| 81 |
} |
| 82 |
|
| 83 |
return _mk_value_without_trace(definition) |
| 84 |
|
| 85 |
|
| 86 |
def _commandlines_to_definition_list(commandline): |
| 87 |
result = [] |
| 88 |
|
| 89 |
def callback(cmdline): |
| 90 |
result.append(_commandline_to_definition(cmdline)) |
| 91 |
|
| 92 |
commandline.visit_commandlines(callback) |
| 93 |
|
| 94 |
return result |
| 95 |
|
| 96 |
|
| 97 |
def validate_commandlines(cmdline): |
| 98 |
'''Validate commandlines.''' |
| 99 |
definitions = _commandlines_to_definition_list(cmdline) |
| 100 |
scheme_validator.validate(definitions) |