Skip to content

Parsing

sereto.parsing

parse_query(query, keys)

Parse a search query string into a structured dictionary.

Source code in sereto/parsing.py
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
def parse_query(query: str, keys: dict[str, str]) -> dict[str, list[str]]:
    """Parse a search query string into a structured dictionary."""
    parser = search_parser(keys)
    reverse_keys = {v: k for k, v in keys.items()}

    result: dict[str, list[str]] = {key: [] for key in keys}

    try:
        for token in parser.parseString(query):
            if len(token) == 2:
                key, value = token

                if isinstance(value, str) and not value.strip():
                    continue

                full_key = reverse_keys.get(key, key)
                result[full_key].append(value)
            else:
                value = token[0]

                if isinstance(value, str) and not value.strip():
                    continue

                first_key = next(iter(keys))
                result[first_key].append(value)

    except pp.ParseException:
        pass

    return result

search_parser(keys)

Builds and returns a search query parser for simple key-value syntax.

Source code in sereto/parsing.py
 4
 5
 6
 7
 8
 9
10
11
12
13
def search_parser(keys: dict[str, str]) -> pp.ParserElement:
    """Builds and returns a search query parser for simple key-value syntax."""
    value = pp.Word(pp.alphanums + "-_.") | pp.QuotedString('"') | pp.QuotedString("'")
    key = pp.oneOf(list(keys.keys()) + list(keys.values()))

    complete = pp.Group(key + pp.Suppress(":") + value)
    partial = pp.Group(key + pp.Suppress(":"))
    free_value = pp.Group(value)

    return pp.ZeroOrMore(complete | partial | free_value)