utils.text module

Search and parse strings

utils.text.grep(regex: str, text: str, full_line: bool = True, case_sensitive: bool = True, invert_match: bool = False) str

Searches the given text with the regex and returns matching line(s)

Works one line at a time–multiline regexes won’t work, use the re library for more complex needs.

full_line causes the entire matching line to be returned, not just the matching portion.

utils.text.ics_split(raw, idx=None, *, ics='***XYLOK ICS***', splitlines: bool = False) str | List[str]

Split raw output based on ics

If given an index, returns only that instance of the split. If not, returns a list of the raw output split by the ics

Parameters:

splitlines – If True, each ICS group will have splitlines() called on it, resulting in a list of strings for each output section.

Returns:

raw output at the given ICS separator or a list of all raw outputs based on the separator

utils.text.levenshtein(string1: str, string2: str, collapse_whitespace: bool = True) int

Computes Levenshtein distance (the number of character changes) between two strings

Parameters:
  • string1 – First string

  • string2 – Second string

  • collapse_whitespace – Whether to compress all whitespace from the strings to single spaces before computing changes. Defaults to True

Returns:

Integer distance, the number of characters that changed between the strings

utils.text.parse_table(text: str, has_header: bool = True, columns: List[str] = None) List[Dict]

Processes a terminal “table” into a list of Python dicts

For example, the output of ps -A might be:

PID TTY          TIME CMD
1 ?        00:00:04 systemd
2 ?        00:00:00 kthreadd
4 ?        00:00:00 kworker/0:0H
7 ?        00:00:00 mm_percpu_wq

parse_table() will turn this into:

[
    {'CMD': 'systemd', 'PID': '1', 'TIME': '00:00:04', 'TTY': '?'},
    {'CMD': 'kthreadd', 'PID': '2', 'TIME': '00:00:00', 'TTY': '?'},
    {'CMD': 'kworker/0:0H', 'PID': '4', 'TIME': '00:00:00', 'TTY': '?'},
    {'CMD': 'mm_percpu_wq', 'PID': '7', 'TIME': '00:00:00', 'TTY': '?'},
]

The data must either have a header or have columns given–if neither is available, a ValueError will be thrown.

utils.text.remove_comments(text: str, comment_str: str = '#') str

Removes all lines that are commented out by the given character

Lines may begin with whitespace