-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (56 loc) · 1.93 KB
/
Copy pathutils.py
File metadata and controls
65 lines (56 loc) · 1.93 KB
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
#
# This file is part of the CloudBlue Connect Python OpenAPI Client.
#
# Copyright (c) 2025 CloudBlue. All Rights Reserved.
#
from datetime import date, datetime
from decimal import Decimal
COMP = ('eq', 'ne', 'lt', 'le', 'gt', 'ge')
SEARCH = ('like', 'ilike')
LIST = ('in', 'out')
NULL = 'null'
EMPTY = 'empty'
KEYWORDS = (*COMP, *SEARCH, *LIST, NULL, EMPTY)
def parse_kwargs(query_dict):
query = []
for lookup, value in query_dict.items():
tokens = lookup.split('__')
if len(tokens) == 1:
# field=value
field = tokens[0]
value = to_rql_value('eq', value)
query.append(f'eq({field},{value})')
continue
op = tokens[-1]
if op not in KEYWORDS:
# field__nested=value
field = '.'.join(tokens)
value = to_rql_value('eq', value)
query.append(f'eq({field},{value})')
continue
field = '.'.join(tokens[:-1])
if op in COMP or op in SEARCH:
value = to_rql_value(op, value)
query.append(f'{op}({field},{value})')
continue
if op in LIST:
value = to_rql_value(op, value)
query.append(f'{op}({field},({value}))')
continue
cmpop = 'eq' if value is True else 'ne'
expr = 'null()' if op == NULL else 'empty()'
query.append(f'{cmpop}({field},{expr})')
return query
def to_rql_value(op, value):
if op not in LIST:
if isinstance(value, str):
return value
if isinstance(value, bool):
return 'true' if value else 'false'
if isinstance(value, (int, float, Decimal)):
return str(value)
if isinstance(value, (date, datetime)):
return value.isoformat()
if op in LIST and isinstance(value, (list, tuple)):
return ','.join(value)
raise TypeError(f"the `{op}` operator doesn't support the {type(value)} type.")