| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Functions that are used in the generation process.''' |
| 5 |
|
| 6 |
from .errors import CrazyError |
| 7 |
from . import completion_validator |
| 8 |
from . import cli |
| 9 |
from . import config as _config |
| 10 |
from . import when |
| 11 |
|
| 12 |
|
| 13 |
class GenerationContext: |
| 14 |
'''Holds global configuration and helpers used during code generation.''' |
| 15 |
|
| 16 |
# pylint: disable=too-few-public-methods |
| 17 |
|
| 18 |
def __init__(self, config, helpers): |
| 19 |
self.config = config |
| 20 |
self.helpers = helpers |
| 21 |
|
| 22 |
def get_option_context(self, commandline, option): |
| 23 |
'''Return OptionGenerationContext based on current context.''' |
| 24 |
|
| 25 |
return OptionGenerationContext( |
| 26 |
self.config, |
| 27 |
self.helpers, |
| 28 |
commandline, |
| 29 |
option |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
class OptionGenerationContext(GenerationContext): |
| 34 |
'''Extends GenerationContext; holds commandline and option objects.''' |
| 35 |
|
| 36 |
# pylint: disable=too-few-public-methods |
| 37 |
|
| 38 |
def __init__(self, config, helpers, commandline, option): |
| 39 |
super().__init__(config, helpers) |
| 40 |
self.commandline = commandline |
| 41 |
self.option = option |
| 42 |
|
| 43 |
|
| 44 |
def _apply_config(commandline, config): |
| 45 |
'''Applies configuration settings to a command line object. |
| 46 |
|
| 47 |
If a setting in the CommandLine or Option object is set to cli.INHERIT, |
| 48 |
it will be overridden by the corresponding setting from the config object. |
| 49 |
|
| 50 |
Args: |
| 51 |
commandline (CommandLine): |
| 52 |
The command line object to apply the configuration to. |
| 53 |
|
| 54 |
config (Config): |
| 55 |
The configuration object containing the settings to apply. |
| 56 |
|
| 57 |
Returns: |
| 58 |
None |
| 59 |
''' |
| 60 |
assert isinstance(commandline, cli.CommandLine) |
| 61 |
assert isinstance(config, _config.Config) |
| 62 |
|
| 63 |
if commandline.abbreviate_commands == cli.INHERIT: |
| 64 |
commandline.abbreviate_commands = config.abbreviate_commands |
| 65 |
|
| 66 |
if commandline.abbreviate_options == cli.INHERIT: |
| 67 |
commandline.abbreviate_options = config.abbreviate_options |
| 68 |
|
| 69 |
if commandline.inherit_options == cli.INHERIT: |
| 70 |
commandline.inherit_options = config.inherit_options |
| 71 |
|
| 72 |
for option in commandline.options: |
| 73 |
if option.repeatable == cli.INHERIT: |
| 74 |
option.repeatable = config.repeatable_options |
| 75 |
|
| 76 |
if option.long_opt_arg_sep == cli.INHERIT: |
| 77 |
option.long_opt_arg_sep = config.long_opt_arg_sep |
| 78 |
|
| 79 |
if config.disabled_hidden: |
| 80 |
option.hidden = False |
| 81 |
|
| 82 |
if config.disabled_final: |
| 83 |
option.final = False |
| 84 |
|
| 85 |
if config.disabled_groups: |
| 86 |
option.groups = [] |
| 87 |
|
| 88 |
if config.disabled_repeatable: |
| 89 |
option.repeatable = True |
| 90 |
|
| 91 |
if config.disabled_when: |
| 92 |
option.when = None |
| 93 |
|
| 94 |
for positional in commandline.positionals: |
| 95 |
if config.disabled_when: |
| 96 |
positional.when = None |
| 97 |
|
| 98 |
|
| 99 |
def _add_parsed_when(commandline): |
| 100 |
for option in commandline.options: |
| 101 |
if option.when: |
| 102 |
try: |
| 103 |
option.when_parsed = when.parse_when(option.when) |
| 104 |
except CrazyError as e: |
| 105 |
raise CrazyError('%s: %s: when: %s' % ( |
| 106 |
commandline.get_command_path(), |
| 107 |
option.get_option_strings_key('|'), |
| 108 |
e)) from e |
| 109 |
else: |
| 110 |
option.when_parsed = None |
| 111 |
|
| 112 |
for positional in commandline.positionals: |
| 113 |
if positional.when: |
| 114 |
try: |
| 115 |
positional.when_parsed = when.parse_when(positional.when) |
| 116 |
except CrazyError as e: |
| 117 |
raise CrazyError('%s: %s: %s: when: %s' % ( |
| 118 |
commandline.get_command_path(), |
| 119 |
positional.number, |
| 120 |
positional.metavar, |
| 121 |
e)) from e |
| 122 |
else: |
| 123 |
positional.when_parsed = None |
| 124 |
|
| 125 |
|
| 126 |
def enhance_commandline(commandline, config): |
| 127 |
'''Enhance commandline. |
| 128 |
|
| 129 |
- Make a copy of commandline |
| 130 |
- Apply configuration to it |
| 131 |
- Add `when_parsed` attribute |
| 132 |
''' |
| 133 |
|
| 134 |
commandline = commandline.copy() |
| 135 |
commandline.visit_commandlines(lambda c: _apply_config(c, config)) |
| 136 |
commandline.visit_commandlines(_add_parsed_when) |
| 137 |
completion_validator.validate_commandlines(commandline) |
| 138 |
return commandline |
| 139 |
|
| 140 |
|
| 141 |
def visit_commandlines(completion_class, ctxt, commandline): |
| 142 |
'''Visit commandlines with a completer class.''' |
| 143 |
|
| 144 |
result = [] |
| 145 |
|
| 146 |
def _call_generator(commandline): |
| 147 |
result.append(completion_class(ctxt, commandline)) |
| 148 |
|
| 149 |
commandline.visit_commandlines(_call_generator) |
| 150 |
|
| 151 |
return result |