| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Functions for parsing the `when` attribute.''' |
| 5 |
|
| 6 |
from . import shell_parser |
| 7 |
from . import messages as m |
| 8 |
from .errors import CrazyError |
| 9 |
from .str_utils import is_valid_option_string, is_valid_extended_regex |
| 10 |
|
| 11 |
|
| 12 |
# pylint: disable=too-few-public-methods |
| 13 |
|
| 14 |
|
| 15 |
class OptionIs: |
| 16 |
'''Class for holding `option_is`.''' |
| 17 |
|
| 18 |
def __init__(self, args): |
| 19 |
self.options = [] |
| 20 |
self.values = [] |
| 21 |
self.ignore_case = False |
| 22 |
self.any = False |
| 23 |
has_end_of_options = False |
| 24 |
|
| 25 |
while args: |
| 26 |
lower = args[0].lower() |
| 27 |
|
| 28 |
if lower == 'nocase': |
| 29 |
self.ignore_case = True |
| 30 |
args.pop(0) |
| 31 |
elif lower == 'any': |
| 32 |
self.any = True |
| 33 |
args.pop(0) |
| 34 |
else: |
| 35 |
break |
| 36 |
|
| 37 |
for arg in args: |
| 38 |
if not has_end_of_options: |
| 39 |
if arg == '--': |
| 40 |
has_end_of_options = True |
| 41 |
else: |
| 42 |
self.options.append(arg) |
| 43 |
else: |
| 44 |
self.values.append(arg) |
| 45 |
|
| 46 |
for option in self.options: |
| 47 |
if not is_valid_option_string(option): |
| 48 |
msg = 'option_is: %s: %s' % (m.invalid_value(), option) |
| 49 |
raise CrazyError(msg) |
| 50 |
|
| 51 |
if not self.options: |
| 52 |
msg = 'option_is: %s: %s' % (m.missing_arg(), 'options') |
| 53 |
raise CrazyError(msg) |
| 54 |
|
| 55 |
if not self.values: |
| 56 |
msg = 'option_is: %s: %s' % (m.missing_arg(), 'values') |
| 57 |
raise CrazyError(msg) |
| 58 |
|
| 59 |
|
| 60 |
class OptionMatch: |
| 61 |
'''Class for holding `option_match`.''' |
| 62 |
|
| 63 |
def __init__(self, args): |
| 64 |
self.options = [] |
| 65 |
self.regex = None |
| 66 |
self.ignore_case = False |
| 67 |
self.any = False |
| 68 |
has_end_of_options = False |
| 69 |
|
| 70 |
while args: |
| 71 |
lower = args[0].lower() |
| 72 |
|
| 73 |
if lower == 'nocase': |
| 74 |
self.ignore_case = True |
| 75 |
args.pop(0) |
| 76 |
elif lower == 'any': |
| 77 |
self.any = True |
| 78 |
args.pop(0) |
| 79 |
else: |
| 80 |
break |
| 81 |
|
| 82 |
for arg in args: |
| 83 |
if not has_end_of_options: |
| 84 |
if arg == '--': |
| 85 |
has_end_of_options = True |
| 86 |
else: |
| 87 |
self.options.append(arg) |
| 88 |
else: |
| 89 |
if self.regex is not None: |
| 90 |
msg = 'option_match: %s: %s' % (m.too_many_arguments(), arg) |
| 91 |
raise CrazyError(msg) |
| 92 |
|
| 93 |
self.regex = arg |
| 94 |
|
| 95 |
for option in self.options: |
| 96 |
if not is_valid_option_string(option): |
| 97 |
msg = 'option_match: %s: %s' % (m.invalid_value(), option) |
| 98 |
raise CrazyError(msg) |
| 99 |
|
| 100 |
if not self.options: |
| 101 |
msg = 'option_match: %s: %s' % (m.missing_arg(), 'options') |
| 102 |
raise CrazyError(msg) |
| 103 |
|
| 104 |
if not self.regex: |
| 105 |
msg = 'option_match: %s: %s' % (m.missing_arg(), 'regex') |
| 106 |
raise CrazyError(msg) |
| 107 |
|
| 108 |
if not is_valid_extended_regex(self.regex): |
| 109 |
msg = 'option_match: %s: %s' % (m.not_an_extended_regex(), self.regex) |
| 110 |
raise CrazyError(msg) |
| 111 |
|
| 112 |
|
| 113 |
class HasOption: |
| 114 |
'''Class for holding `has_option`.''' |
| 115 |
|
| 116 |
def __init__(self, args): |
| 117 |
self.options = args |
| 118 |
|
| 119 |
if not self.options: |
| 120 |
msg = 'has_option: %s: %s' % (m.missing_arg(), 'options') |
| 121 |
raise CrazyError(msg) |
| 122 |
|
| 123 |
|
| 124 |
class PositionalCount: |
| 125 |
'''Class for holding `positional_count`.''' |
| 126 |
|
| 127 |
OPERATORS = ('==', '!=', '<=', '>=', '<', '>') |
| 128 |
|
| 129 |
def __init__(self, args): |
| 130 |
if len(args) > 2: |
| 131 |
msg = 'positional_count: %s' % (m.too_many_arguments()) |
| 132 |
raise CrazyError(msg) |
| 133 |
|
| 134 |
try: |
| 135 |
self.operator = args[0] |
| 136 |
except IndexError: |
| 137 |
msg = 'positional_count: %s: %s' % (m.missing_arg(), 'operator') |
| 138 |
raise CrazyError(msg) |
| 139 |
|
| 140 |
if self.operator not in self.OPERATORS: |
| 141 |
msg = 'positional_count: operator: %s' % ( |
| 142 |
m.invalid_value_expected_values(', '.join(self.OPERATORS))) |
| 143 |
raise CrazyError(msg) |
| 144 |
|
| 145 |
try: |
| 146 |
self.number = int(args[1]) |
| 147 |
except ValueError: |
| 148 |
msg = 'positional_count: number: %s' % (m.invalid_value()) |
| 149 |
raise CrazyError(msg) |
| 150 |
except IndexError: |
| 151 |
msg = 'positional_count: %s: %s' % (m.missing_arg(), 'number') |
| 152 |
raise CrazyError(msg) |
| 153 |
|
| 154 |
|
| 155 |
class PositionalContains: |
| 156 |
'''Class for holding `positional_contains`.''' |
| 157 |
|
| 158 |
def __init__(self, args): |
| 159 |
self.ignore_case = False |
| 160 |
|
| 161 |
if args and args[0].lower() == 'nocase': |
| 162 |
self.ignore_case = True |
| 163 |
args.pop(0) |
| 164 |
|
| 165 |
try: |
| 166 |
self.number = int(args.pop(0)) |
| 167 |
except ValueError: |
| 168 |
msg = 'positional_contains: number: %s' % (m.invalid_value()) |
| 169 |
raise CrazyError(msg) |
| 170 |
except IndexError: |
| 171 |
msg = 'positional_contains: %s: %s' % (m.missing_arg(), 'number') |
| 172 |
raise CrazyError(msg) |
| 173 |
|
| 174 |
if len(args) == 0: |
| 175 |
msg = 'positional_contains: %s: %s' % (m.missing_arg(), 'values') |
| 176 |
raise CrazyError(msg) |
| 177 |
|
| 178 |
self.values = args |
| 179 |
|
| 180 |
|
| 181 |
class PositionalMatch: |
| 182 |
'''Class for holding `positional_match`.''' |
| 183 |
|
| 184 |
def __init__(self, args): |
| 185 |
self.number = None |
| 186 |
self.regex = None |
| 187 |
self.ignore_case = False |
| 188 |
|
| 189 |
if args and args[0].lower() == 'nocase': |
| 190 |
self.ignore_case = True |
| 191 |
args.pop(0) |
| 192 |
|
| 193 |
try: |
| 194 |
self.number = int(args.pop(0)) |
| 195 |
except ValueError: |
| 196 |
msg = 'positional_match: number: %s' % (m.invalid_value()) |
| 197 |
raise CrazyError(msg) |
| 198 |
except IndexError: |
| 199 |
msg = 'positional_match: %s: %s' % (m.missing_arg(), 'number') |
| 200 |
raise CrazyError(msg) |
| 201 |
|
| 202 |
try: |
| 203 |
self.regex = args.pop(0) |
| 204 |
except IndexError: |
| 205 |
msg = 'positional_match: %s: %s' % (m.missing_arg(), 'regex') |
| 206 |
raise CrazyError(msg) |
| 207 |
|
| 208 |
if not is_valid_extended_regex(self.regex): |
| 209 |
msg = 'positional_match: %s: %s' % (m.not_an_extended_regex(), self.regex) |
| 210 |
raise CrazyError(msg) |
| 211 |
|
| 212 |
if args: |
| 213 |
msg = 'positional_match: %s' % m.too_many_arguments() |
| 214 |
raise CrazyError(msg) |
| 215 |
|
| 216 |
|
| 217 |
def replace_commands(tokens): |
| 218 |
'''Replaced `shell_parser.Command` objects by condition objects.''' |
| 219 |
|
| 220 |
r = [] |
| 221 |
|
| 222 |
for token in tokens: |
| 223 |
if isinstance(token, shell_parser.Command): |
| 224 |
cmd, *args = token.args |
| 225 |
|
| 226 |
if cmd == 'option_is': |
| 227 |
r.append(OptionIs(args)) |
| 228 |
elif cmd == 'option_match': |
| 229 |
r.append(OptionMatch(args)) |
| 230 |
elif cmd == 'has_option': |
| 231 |
r.append(HasOption(args)) |
| 232 |
elif cmd == 'positional_count': |
| 233 |
r.append(PositionalCount(args)) |
| 234 |
elif cmd == 'positional_contains': |
| 235 |
r.append(PositionalContains(args)) |
| 236 |
elif cmd == 'positional_match': |
| 237 |
r.append(PositionalMatch(args)) |
| 238 |
else: |
| 239 |
raise CrazyError(f"Invalid command: {cmd!r}") |
| 240 |
else: |
| 241 |
r.append(token) |
| 242 |
|
| 243 |
return r |
| 244 |
|
| 245 |
|
| 246 |
def needs_braces(tokens): |
| 247 |
'''Check if we need braces around our tokens. |
| 248 |
|
| 249 |
We don't need braces if: |
| 250 |
- We only have one token (single command) |
| 251 |
- We only have two tokens (negated single command) |
| 252 |
- We don't have an OR inside the tokens |
| 253 |
''' |
| 254 |
return len(tokens) > 2 and '||' in tokens |
| 255 |
|
| 256 |
|
| 257 |
def parse_when(s): |
| 258 |
'''Parse `when` string and return an object.''' |
| 259 |
|
| 260 |
try: |
| 261 |
tokens = shell_parser.parse(s) |
| 262 |
except ValueError as e: |
| 263 |
raise CrazyError(str(e)) |
| 264 |
|
| 265 |
return replace_commands(tokens) |