| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Bash utility functions.''' |
| 5 |
|
| 6 |
from . import shell |
| 7 |
|
| 8 |
|
| 9 |
def make_option_variable_name(option, prefix=''): |
| 10 |
'''Make a variable for an option.''' |
| 11 |
long_options = option.get_long_option_strings() |
| 12 |
if long_options: |
| 13 |
return prefix + shell.make_identifier(long_options[0].lstrip('-')) |
| 14 |
|
| 15 |
old_options = option.get_old_option_strings() |
| 16 |
if old_options: |
| 17 |
return prefix + shell.make_identifier(old_options[0].lstrip('-')) |
| 18 |
|
| 19 |
short_options = option.get_short_option_strings() |
| 20 |
if short_options: |
| 21 |
return prefix + short_options[0][1] |
| 22 |
|
| 23 |
raise AssertionError("make_option_variable_name: Should not be reached") |
| 24 |
|
| 25 |
|
| 26 |
class VariableManager: |
| 27 |
'''Variable manager.''' |
| 28 |
|
| 29 |
def __init__(self, prefix): |
| 30 |
self.prefix = prefix |
| 31 |
self.variables = set() |
| 32 |
|
| 33 |
def make_variable(self, option): |
| 34 |
'''Make a variable for an option.''' |
| 35 |
|
| 36 |
var = make_option_variable_name(option, self.prefix) |
| 37 |
self.variables.add(var) |
| 38 |
return var |
| 39 |
|
| 40 |
def capture_variable(self, option): |
| 41 |
'''Return the capture variable of an option, creating it if necessary.''' |
| 42 |
|
| 43 |
if option.capture is None: |
| 44 |
option.capture = self.make_variable(option) |
| 45 |
return option.capture |
| 46 |
|
| 47 |
def get_variables(self): |
| 48 |
'''Get a list of all defined variables.''' |
| 49 |
|
| 50 |
return list(sorted(self.variables)) |
| 51 |
|
| 52 |
|
| 53 |
class CasePatterns: |
| 54 |
'''Functions for creating case patterns.''' |
| 55 |
|
| 56 |
@staticmethod |
| 57 |
def for_long_without_arg(option_strings): |
| 58 |
'''Return a case pattern for long options without arguments.''' |
| 59 |
|
| 60 |
return '|'.join(option_strings) |
| 61 |
|
| 62 |
@staticmethod |
| 63 |
def for_long_with_arg(option_strings): |
| 64 |
'''Return a case pattern for long options with arguments.''' |
| 65 |
|
| 66 |
return '|'.join(f'{o}=*' for o in option_strings) |
| 67 |
|
| 68 |
@staticmethod |
| 69 |
def for_short(option_strings): |
| 70 |
'''Return a case pattern for short options.''' |
| 71 |
|
| 72 |
return '|'.join(o[1] for o in option_strings) |
| 73 |
|
| 74 |
@staticmethod |
| 75 |
def for_old_without_arg(option_strings): |
| 76 |
'''Return a case pattern for old options without arguments.''' |
| 77 |
|
| 78 |
if len(option_strings) <= 2: |
| 79 |
return '|'.join(option_strings) |
| 80 |
|
| 81 |
return '-@(%s)' % '|'.join(o.lstrip('-') for o in option_strings) |
| 82 |
|
| 83 |
|
| 84 |
def make_file_extension_pattern(extensions, fuzzy): |
| 85 |
'''Make a case-insensitive glob pattern matching `extensions`. |
| 86 |
|
| 87 |
Takes a list of extensions (e.g. ['txt', 'jpg']) and returns a Bash-style |
| 88 |
pattern that matches all of them, ignoring case. |
| 89 |
|
| 90 |
Example output: |
| 91 |
'@([tT][xX][tT]|[jJ][pP][gG])' |
| 92 |
''' |
| 93 |
|
| 94 |
patterns = [] |
| 95 |
|
| 96 |
for extension in extensions: |
| 97 |
pattern = '' |
| 98 |
for c in extension: |
| 99 |
if c.isalpha(): |
| 100 |
pattern += '[%s%s]' % (c.lower(), c.upper()) |
| 101 |
else: |
| 102 |
pattern += c |
| 103 |
|
| 104 |
patterns.append(pattern) |
| 105 |
|
| 106 |
if not fuzzy: |
| 107 |
return '@(%s)' % '|'.join(patterns) |
| 108 |
|
| 109 |
return '@(%s)*' % '|'.join(patterns) |