Debian Codegraph @

crazy_complete/bash_when.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 '''Code for creating when conditions in Bash.'''
5
6 from . import when
7 from . import shell
8
9
10 class ConditionGenerator:
11 '''Class for generating conditions.'''
12
13 def __init__(self, ctxt, commandline, variable_manager):
14 self.ctxt = ctxt
15 self.commandline = commandline
16 self.variable_manager = variable_manager
17
18 def generate(self, tokens):
19 '''Turn tokens/objects into condition code.'''
20
21 r = []
22
23 for obj in tokens:
24 if isinstance(obj, when.OptionIs):
25 r.append(self._gen_option_is(obj))
26
27 elif isinstance(obj, when.OptionMatch):
28 r.append(self._gen_option_match(obj))
29
30 elif isinstance(obj, when.HasOption):
31 r.append(self._gen_has_option(obj))
32
33 elif isinstance(obj, when.PositionalCount):
34 r.append(self._gen_positional_count(obj))
35
36 elif isinstance(obj, when.PositionalContains):
37 r.append(self._gen_positional_contains(obj))
38
39 elif isinstance(obj, when.PositionalMatch):
40 r.append(self._gen_positional_match(obj))
41
42 elif obj in ('&&', '||', '!'):
43 r.append(obj)
44
45 elif obj == '(':
46 r.append('{')
47
48 elif obj == ')':
49 r.append(';}')
50
51 else:
52 raise AssertionError("Not reached")
53
54 if when.needs_braces(tokens):
55 return '{ %s; }' % ' '.join(r)
56
57 return ' '.join(r)
58
59 def _gen_option_is(self, obj):
60 variables = self._gen_variables(obj.options, obj.any)
61 conditions = []
62
63 if obj.ignore_case:
64 func = self.ctxt.helpers.use_function('array_contains_nocase')
65 else:
66 func = self.ctxt.helpers.use_function('array_contains')
67
68 for value in obj.values:
69 value = shell.quote(value)
70 conditions.append(f'{func} {value} {" ".join(variables)}')
71
72 if len(conditions) == 1:
73 return conditions[0]
74
75 return '{ %s; }' % ' || '.join(conditions)
76
77 def _gen_option_match(self, obj):
78 regex = shell.quote(obj.regex)
79 func = self.ctxt.helpers.use_function('array_match')
80 variables = self._gen_variables(obj.options, obj.any)
81
82 if obj.ignore_case:
83 return f'{func} -i -- {regex} {" ".join(variables)}'
84 else:
85 return f'{func} -- {regex} {" ".join(variables)}'
86
87 def _gen_has_option(self, obj):
88 conditions = []
89
90 for o in self.commandline.get_options_by_option_strings(obj.options):
91 cond = '${#%s[@]}' % self.variable_manager.capture_variable(o)
92 conditions.append(cond)
93
94 return f'(( {" + ".join(conditions)} ))'
95
96 def _gen_positional_count(self, obj):
97 return f'(( ${{#POSITIONALS[@]}} {obj.operator} {obj.number} ))'
98
99 def _gen_positional_contains(self, obj):
100 func = self.ctxt.helpers.use_function('array_contains')
101 index = obj.number - 1
102 values = shell.join_quoted(obj.values)
103
104 if obj.ignore_case:
105 return f'{func} "${{POSITIONALS[{index}],,}}" {values.lower()}'
106 else:
107 return f'{func} "${{POSITIONALS[{index}]}}" {values}'
108
109 def _gen_positional_match(self, obj):
110 func = self.ctxt.helpers.use_function('regex_match')
111 regex = shell.quote(obj.regex)
112 index = obj.number - 1
113
114 if obj.ignore_case:
115 return f'{func} -i -- "${{POSITIONALS[{index}]}}" {regex}'
116 else:
117 return f'{func} -- "${{POSITIONALS[{index}]}}" {regex}'
118
119 def _gen_variables(self, options, any_options):
120 variables = []
121
122 for o in self.commandline.get_options_by_option_strings(options):
123 variables.append(self.variable_manager.capture_variable(o))
124
125 if any_options:
126 return ['"${%s[@]}"' % v for v in variables]
127 else:
128 return ['"${%s[@]: -1:1}"' % v for v in variables]
129
130
131 def generate_when_conditions(ctxt, commandline, variable_manager, when_):
132 '''Generate when condition code.'''
133
134 tokens = when.parse_when(when_)
135 generator = ConditionGenerator(ctxt, commandline, variable_manager)
136 return generator.generate(tokens)