forked from AirtestProject/Poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_util.py
More file actions
77 lines (62 loc) · 2.25 KB
/
Copy pathquery_util.py
File metadata and controls
77 lines (62 loc) · 2.25 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
66
67
68
69
70
71
72
73
74
75
76
77
# coding=utf-8
from __future__ import unicode_literals
import poco.utils.six as six
__all__ = ['query_expr']
TranslatePred = {
'attr=': '=',
'attr.*=': ' matches ',
}
TranslateOp = {
'and': '&',
'or': '|',
'/': '/',
'>': '>',
'-': '-',
'^': '\'s parent',
}
ComparableTypes = six.integer_types + six.string_types + (six.binary_type, bool, float)
def query_expr(query):
op = query[0]
if op in ('/', '>', '-', '^'):
return TranslateOp[op].join([query_expr(q) for q in query[1]])
elif op == 'index':
return '{}[{}]'.format(query_expr(query[1][0]), query[1][1])
elif op in ('and', 'or'):
exprs = []
for subquery in query[1]:
pred, (k, v) = subquery
if k == 'name':
exprs.append(v)
else:
exprs.append('{}{}{}'.format(k, TranslatePred[pred], v))
return TranslateOp[op].join(exprs)
else:
raise RuntimeError('Bad query format. "{}"'.format(repr(query)))
def ensure_text(value):
if not isinstance(value, six.text_type):
return value.decode("utf-8")
else:
return value
def build_query(name, **attrs):
query = []
if name is not None:
if not isinstance(name, six.string_types):
raise ValueError("Name selector should only be string types. Got {}".format(repr(name)))
name = ensure_text(name)
attrs['name'] = name
for attr_name, attr_val in attrs.items():
if not isinstance(attr_val, ComparableTypes):
raise ValueError('Selector value should be one of the following types "{}". Got {}'
.format(ComparableTypes, type(attr_val)))
if isinstance(attr_val, six.string_types):
attr_val = ensure_text(attr_val)
if attr_name.startswith('_'):
raise NameError("Cannot use private attribute '{}' in your Query Expression as private attributes do not "
"have stable values.".format(attr_name))
elif attr_name.endswith('Matches'):
attr_name = attr_name[:-7] # textMatches -> (attr.*=, text)
op = 'attr.*='
else:
op = 'attr='
query.append((op, (attr_name, attr_val)))
return 'and', tuple(query)