File: str_utils.py

package info (click to toggle)
crazy-complete 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,404 kB
  • sloc: python: 7,949; sh: 4,636; makefile: 74
file content (92 lines) | stat: -rw-r--r-- 2,515 bytes parent folder | download | duplicates (2)
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
'''String utility functions.'''

import re


_VALID_OPTION_STRING_RE = re.compile('-[^\\s,]+')
_VALID_VARIABLE_RE = re.compile('[a-zA-Z_][a-zA-Z0-9_]*')


def contains_space(string):
    '''Check if string contains space type characters.'''

    return (' ' in string or '\t' in string or '\n' in string)


def is_empty_or_whitespace(string):
    '''Check if string is empty or whitespace.'''

    return not string.strip()


def indent(string, num_spaces):
    '''Indents each line in a string by a specified number of spaces,
    preserving empty lines.

    Args:
        string (str): The input string to be indented.
        num_spaces (int): The number of spaces to indent each line.

    Returns:
        str: The indented string.
    '''

    assert isinstance(string, str), \
        f"indent: string: expected str, got {string!r}"

    assert isinstance(num_spaces, int), \
        f"indent: num_spaces: expected int, got {num_spaces!r}"

    lines = string.split('\n')
    indented_lines = [((' ' * num_spaces) + line) if line.strip() else line for line in lines]
    return '\n'.join(indented_lines)


def join_with_wrap(primary_sep, secondary_sep, max_line_length, items):
    '''
    Join a list of strings with a primary separator, automatically wrapping
    to a new line (using the secondary separator) when the current line
    would exceed max_line_length.

    Args:
        primary_sep: String used to separate items on the same line.
        secondary_sep: String used to separate wrapped lines.
        max_line_length: Maximum number of characters per line.
        items: List of strings to join.

    Returns:
        str: Joined string with line wrapping
    '''
    lines = []
    current_line = ""

    for item in items:
        candidate = (current_line + primary_sep + item) if current_line else item
        if len(candidate) > max_line_length and current_line:
            lines.append(current_line)
            current_line = item
        else:
            current_line = candidate

    if current_line:
        lines.append(current_line)

    return secondary_sep.join(lines)


def is_valid_option_string(option_string):
    '''Check if `option_string` is a valid option string.'''

    if not _VALID_OPTION_STRING_RE.fullmatch(option_string):
        return False

    if option_string == '--':
        return False

    return True


def is_valid_variable_name(string):
    '''Check if string is a valid shell variable name.'''

    return _VALID_VARIABLE_RE.fullmatch(string)