| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Classes for including functions in the generation process.''' |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from . import cli |
| 9 |
from . import shell |
| 10 |
from . import preprocessor |
| 11 |
from .str_utils import (indent, strip_comments, strip_double_empty_lines) |
| 12 |
|
| 13 |
|
| 14 |
# pylint: disable=too-few-public-methods |
| 15 |
|
| 16 |
|
| 17 |
def replace_function_name(search, replace, s): |
| 18 |
'''Safely replace a function name in code''' |
| 19 |
|
| 20 |
pattern = f'\\b{search}\\b' |
| 21 |
return re.sub(pattern, replace, s) |
| 22 |
|
| 23 |
|
| 24 |
class FunctionBase: |
| 25 |
'''Base class for functions.''' |
| 26 |
|
| 27 |
def __init__(self, funcname, code, dependencies=None): |
| 28 |
self.funcname = funcname |
| 29 |
self.code = code.strip() |
| 30 |
self.dependencies = dependencies if dependencies else [] |
| 31 |
|
| 32 |
def get_code(self): |
| 33 |
'''Return the whole function.''' |
| 34 |
raise NotImplementedError |
| 35 |
|
| 36 |
|
| 37 |
class ShellFunction(FunctionBase): |
| 38 |
'''Class for generating Bash and Zsh functions.''' |
| 39 |
|
| 40 |
def get_code(self, funcname=None, defines=frozenset()): |
| 41 |
if funcname is None: |
| 42 |
funcname = self.funcname |
| 43 |
|
| 44 |
code = self.code.replace('%FUNCNAME%', funcname) |
| 45 |
code = preprocessor.preprocess(code, defines) |
| 46 |
code = strip_double_empty_lines(code) |
| 47 |
|
| 48 |
r = '%s() {\n' % funcname |
| 49 |
r += '%s\n' % indent(code, 2).rstrip() |
| 50 |
r += '}' |
| 51 |
return r |
| 52 |
|
| 53 |
|
| 54 |
class FishFunction(FunctionBase): |
| 55 |
'''Class for generating Fish functions.''' |
| 56 |
|
| 57 |
def get_code(self, funcname=None, defines=frozenset()): |
| 58 |
if funcname is None: |
| 59 |
funcname = self.funcname |
| 60 |
|
| 61 |
code = self.code.replace('%FUNCNAME%', funcname) |
| 62 |
code = preprocessor.preprocess(code, defines) |
| 63 |
code = strip_double_empty_lines(code) |
| 64 |
|
| 65 |
r = 'function %s\n' % funcname |
| 66 |
r += '%s\n' % indent(code, 2).rstrip() |
| 67 |
r += 'end' |
| 68 |
return r |
| 69 |
|
| 70 |
|
| 71 |
def make_completion_funcname_for_context(ctxt, prefix): |
| 72 |
'''TODO.''' |
| 73 |
|
| 74 |
commandlines = ctxt.commandline.get_parents(include_self=True) |
| 75 |
prognames = [p.prog for p in commandlines] |
| 76 |
prognames[0] = prefix |
| 77 |
|
| 78 |
funcname = shell.make_identifier('_'.join(prognames)) |
| 79 |
|
| 80 |
if isinstance(ctxt.option, cli.Option): |
| 81 |
return '%s__%s' % (funcname, ctxt.option.option_strings[0]) |
| 82 |
if isinstance(ctxt.option, cli.Positional): |
| 83 |
if ctxt.option.metavar: |
| 84 |
identifier = shell.make_identifier(ctxt.option.metavar) |
| 85 |
else: |
| 86 |
identifier = 'arg%d' % ctxt.option.get_positional_num() |
| 87 |
|
| 88 |
return '%s__%s' % (funcname, identifier) |
| 89 |
|
| 90 |
raise AssertionError('Should not be reached') |
| 91 |
|
| 92 |
|
| 93 |
class GeneralHelpers: |
| 94 |
'''Class for including functions in the generation process.''' |
| 95 |
|
| 96 |
def __init__(self, config, prog, function_class): |
| 97 |
self.config = config |
| 98 |
self.function_prefix = config.function_prefix.replace('$PROG', prog) |
| 99 |
self.function_class = function_class |
| 100 |
|
| 101 |
# Dynamic functions |
| 102 |
self.dynamic_functions_code_to_funcname = {} |
| 103 |
|
| 104 |
# Builtin functins |
| 105 |
self.functions = {} |
| 106 |
self.used_functions = {} # funcname:set(defines) |
| 107 |
self.global_defines = set() |
| 108 |
|
| 109 |
# Set debug mode |
| 110 |
if self.config.debug: |
| 111 |
self.define('DEBUG') |
| 112 |
|
| 113 |
# ========================================================================= |
| 114 |
# Dynamic functions |
| 115 |
# ========================================================================= |
| 116 |
|
| 117 |
def add_dynamic_func(self, ctxt, code): |
| 118 |
'''Add dynamically generated function.''' |
| 119 |
|
| 120 |
try: |
| 121 |
return self.dynamic_functions_code_to_funcname[code] |
| 122 |
except KeyError: |
| 123 |
funcname = self.get_dynamic_funcname(ctxt) |
| 124 |
self.dynamic_functions_code_to_funcname[code] = funcname |
| 125 |
return funcname |
| 126 |
|
| 127 |
def get_dynamic_funcname(self, ctxt): |
| 128 |
'''Return a unique function name for `ctxt`.''' |
| 129 |
|
| 130 |
funcname = make_completion_funcname_for_context(ctxt, self.function_prefix) |
| 131 |
num = 0 |
| 132 |
funcname_plus_num = funcname |
| 133 |
while funcname_plus_num in self.dynamic_functions_code_to_funcname.values(): |
| 134 |
funcname_plus_num = '%s%d' % (funcname, num) |
| 135 |
num += 1 |
| 136 |
return funcname_plus_num |
| 137 |
|
| 138 |
def get_all_dynamic_functions(self): |
| 139 |
'''Return a list of code for all dynamically defined functions.''' |
| 140 |
|
| 141 |
r = [] |
| 142 |
for code, funcname in self.dynamic_functions_code_to_funcname.items(): |
| 143 |
r.append(self.function_class(funcname, code).get_code()) |
| 144 |
return r |
| 145 |
|
| 146 |
# ========================================================================= |
| 147 |
# Builtin functions |
| 148 |
# ========================================================================= |
| 149 |
|
| 150 |
def add_function(self, function): |
| 151 |
'''Add a function to the function repository.''' |
| 152 |
|
| 153 |
assert isinstance(function, FunctionBase) |
| 154 |
|
| 155 |
self.functions[function.funcname] = function |
| 156 |
|
| 157 |
def define(self, name): |
| 158 |
'''Define a macro on global scope.''' |
| 159 |
|
| 160 |
self.global_defines.add(name) |
| 161 |
|
| 162 |
def use_function(self, function_name, *defines): |
| 163 |
'''Use a function from the function repository.''' |
| 164 |
|
| 165 |
if function_name not in self.functions: |
| 166 |
raise KeyError('No such function: %r' % function_name) |
| 167 |
|
| 168 |
if function_name not in self.used_functions: |
| 169 |
self.used_functions[function_name] = set(defines) |
| 170 |
else: |
| 171 |
self.used_functions[function_name].update(defines) |
| 172 |
|
| 173 |
# Add dependencies |
| 174 |
for function in self.functions[function_name].dependencies: |
| 175 |
self.use_function(function) |
| 176 |
|
| 177 |
return self.get_real_function_name(function_name) |
| 178 |
|
| 179 |
def get_real_function_name(self, function_name): |
| 180 |
'''Return the function name with its prefix.''' |
| 181 |
|
| 182 |
return '%s__%s' % (self.function_prefix, function_name) |
| 183 |
|
| 184 |
def is_used(self, function_name): |
| 185 |
'''Check if a function is used.''' |
| 186 |
|
| 187 |
return function_name in self.used_functions |
| 188 |
|
| 189 |
def get_used_functions_code(self): |
| 190 |
'''Return a list of code for all used functions.''' |
| 191 |
|
| 192 |
r = [] |
| 193 |
|
| 194 |
for funcname, defines in self.used_functions.items(): |
| 195 |
function = self.functions[funcname] |
| 196 |
realname = self.get_real_function_name(funcname) |
| 197 |
code = function.get_code(realname, self.global_defines | defines) |
| 198 |
|
| 199 |
for dep_func in function.dependencies: |
| 200 |
real_func = self.get_real_function_name(dep_func) |
| 201 |
code = replace_function_name(dep_func, real_func, code) |
| 202 |
|
| 203 |
code = code.replace('%PREFIX%', self.function_prefix) |
| 204 |
|
| 205 |
if not self.config.keep_comments: |
| 206 |
code = strip_comments(code) |
| 207 |
|
| 208 |
code = strip_double_empty_lines(code) |
| 209 |
|
| 210 |
r.append(code) |
| 211 |
|
| 212 |
r.extend(self.get_all_dynamic_functions()) |
| 213 |
|
| 214 |
return r |
| 215 |
|
| 216 |
# ========================================================================= |
| 217 |
# Other methods |
| 218 |
# ========================================================================= |
| 219 |
|
| 220 |
def make_completion_funcname(self, commandline, suffix=''): |
| 221 |
'''Generates a function name for auto-completing a program or subcommand. |
| 222 |
|
| 223 |
Args: |
| 224 |
cmdline (CommandLine): |
| 225 |
The CommandLine instance representing the program or subcommand. |
| 226 |
|
| 227 |
suffix (str): |
| 228 |
The suffix that shall be appended to the result. |
| 229 |
|
| 230 |
Returns: |
| 231 |
str: The generated function name for auto-completion. |
| 232 |
|
| 233 |
This function is used to generate a unique function name for |
| 234 |
auto-completing a program or subcommand in the specified shell. |
| 235 |
It concatenates the names of all parent commandlines, including the |
| 236 |
current commandline, and converts them into a valid function name |
| 237 |
format. |
| 238 |
|
| 239 |
Example: |
| 240 |
For a program with the name 'my_program' and a subcommand with the name 'subcommand', |
| 241 |
the generated function name is '_my_program_subcommand'. |
| 242 |
''' |
| 243 |
assert isinstance(commandline, cli.CommandLine) |
| 244 |
|
| 245 |
commandlines = commandline.get_parents(include_self=True) |
| 246 |
prognames = [cmdline.prog for cmdline in commandlines] |
| 247 |
prognames[0] = self.function_prefix |
| 248 |
identifier = shell.make_identifier('_'.join(prognames)) |
| 249 |
|
| 250 |
return f'{identifier}{suffix}' |