aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/utils.py
blob: 61f0ae028fa19353b2e5c28531eb96cdecc1d3b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def split_on_dot(line: str, only_last_dot=False):
    """
    Split a string on the dot character (.).

    :param line: The line ot split on.
    :param only_last_dot: Only split on the last occurrence of the dot.

    :raises ValueError: if the line does not have a dot.
    """
    if "." not in line:
        # TODO: This is used in the parser for section.keys where a more specific
        # error message would be nice, but it is only used more generically to, say,
        # split a file path. We may just have to insert some try-catches to catch the
        # ValueError and insert a more specific error message or create a custom error
        # for this method?
        raise ValueError(
            "section_key must contain the section and key separated by a dot. "
            + "e.g. 'section.key'"
        )

    return line.rsplit(".", 1) if only_last_dot else line.split(".")