File: bash_utils.py

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (109 lines) | stat: -rw-r--r-- 3,048 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>

'''Bash utility functions.'''

from . import shell


def make_option_variable_name(option, prefix=''):
    '''Make a variable for an option.'''
    long_options = option.get_long_option_strings()
    if long_options:
        return prefix + shell.make_identifier(long_options[0].lstrip('-'))

    old_options = option.get_old_option_strings()
    if old_options:
        return prefix + shell.make_identifier(old_options[0].lstrip('-'))

    short_options = option.get_short_option_strings()
    if short_options:
        return prefix + short_options[0][1]

    raise AssertionError("make_option_variable_name: Should not be reached")


class VariableManager:
    '''Variable manager.'''

    def __init__(self, prefix):
        self.prefix = prefix
        self.variables = set()

    def make_variable(self, option):
        '''Make a variable for an option.'''

        var = make_option_variable_name(option, self.prefix)
        self.variables.add(var)
        return var

    def capture_variable(self, option):
        '''Return the capture variable of an option, creating it if necessary.'''

        if option.capture is None:
            option.capture = self.make_variable(option)
        return option.capture

    def get_variables(self):
        '''Get a list of all defined variables.'''

        return list(sorted(self.variables))


class CasePatterns:
    '''Functions for creating case patterns.'''

    @staticmethod
    def for_long_without_arg(option_strings):
        '''Return a case pattern for long options without arguments.'''

        return '|'.join(option_strings)

    @staticmethod
    def for_long_with_arg(option_strings):
        '''Return a case pattern for long options with arguments.'''

        return '|'.join(f'{o}=*' for o in option_strings)

    @staticmethod
    def for_short(option_strings):
        '''Return a case pattern for short options.'''

        return '|'.join(o[1] for o in option_strings)

    @staticmethod
    def for_old_without_arg(option_strings):
        '''Return a case pattern for old options without arguments.'''

        if len(option_strings) <= 2:
            return '|'.join(option_strings)

        return '-@(%s)' % '|'.join(o.lstrip('-') for o in option_strings)


def make_file_extension_pattern(extensions, fuzzy):
    '''Make a case-insensitive glob pattern matching `extensions`.

    Takes a list of extensions (e.g. ['txt', 'jpg']) and returns a Bash-style
    pattern that matches all of them, ignoring case.

    Example output:
        '@([tT][xX][tT]|[jJ][pP][gG])'
    '''

    patterns = []

    for extension in extensions:
        pattern = ''
        for c in extension:
            if c.isalpha():
                pattern += '[%s%s]' % (c.lower(), c.upper())
            else:
                pattern += c

        patterns.append(pattern)

    if not fuzzy:
        return '@(%s)' % '|'.join(patterns)

    return '@(%s)*' % '|'.join(patterns)