1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>
'''Module for generating conditional code in Zsh.'''
from . import when
from . import shell
def _generate(query, tokens):
'''Turn tokens/objects into condition code.'''
r = []
for obj in tokens:
if isinstance(obj, when.OptionIs):
r.append(_generate_option_is(query, obj))
elif isinstance(obj, when.HasOption):
r.append(_generate_has_option(query, obj))
elif obj in ('&&', '||', '!'):
r.append(obj)
elif obj == '(':
r.append('{')
elif obj == ')':
r.append(';}')
else:
raise AssertionError("Not reached")
if when.needs_braces(tokens):
return '{ %s; }' % ' '.join(r)
return ' '.join(r)
def _generate_option_is(query, obj):
func = query.use('option_is')
args = [func, 'option_is', *obj.options, '--', *obj.values]
return shell.join_quoted(args)
def _generate_has_option(query, obj):
func = query.use('has_option')
args = [func, 'has_option', *obj.options]
return shell.join_quoted(args)
def generate_when_conditions(query, when_):
'''Generate when condition code.'''
tokens = when.parse_when(when_)
return _generate(query, tokens)
|