forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
127 lines (87 loc) · 3.06 KB
/
Copy pathoperators.py
File metadata and controls
127 lines (87 loc) · 3.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import logging
import re
import sys
from collections import defaultdict
from numbers import Number
import six
import strict_rfc3339
log = logging.getLogger(sys.modules[__name__].__name__)
def _string_operator(u, c, fn):
if isinstance(u, six.string_types):
if isinstance(c, six.string_types):
return fn(u, c)
return False
def _numeric_operator(u, c, fn):
# bool is a subtype of int, and we don't want to try and compare it as a number.
if isinstance(input, bool):
log.warn("Got unexpected bool type when attempting to parse time")
return None
if isinstance(u, Number):
if isinstance(c, Number):
return fn(u, c)
return False
def _parse_time(input):
"""
:param input: Either a number as milliseconds since Unix Epoch, or a string as a valid RFC3339 timestamp
:return: milliseconds since Unix epoch, or None if input was invalid.
"""
# bool is a subtype of int, and we don't want to try and compare it as a time.
if isinstance(input, bool):
log.warn("Got unexpected bool type when attempting to parse time")
return None
if isinstance(input, Number):
return float(input)
if isinstance(input, six.string_types):
try:
timestamp = strict_rfc3339.rfc3339_to_timestamp(input)
return timestamp * 1000.0
except Exception as e:
log.warn("Couldn't parse timestamp:" + str(input) + " with error: " + str(e))
return None
log.warn("Got unexpected type: " + type(input) + " with value: " + str(input) + " when attempting to parse time")
return None
def _time_operator(u, c, fn):
u_time = _parse_time(u)
if u_time is not None:
c_time = _parse_time(c)
if c_time is not None:
return fn(u_time, c_time)
return False
def _in(u, c):
if u == c:
return True
return False
def _starts_with(u, c):
return _string_operator(u, c, lambda u, c: u.startswith(c))
def _ends_with(u, c):
return _string_operator(u, c, lambda u, c: u.endswith(c))
def _contains(u, c):
return _string_operator(u, c, lambda u, c: c in u)
def _matches(u, c):
return _string_operator(u, c, lambda u, c: re.match(c, u))
def _less_than(u, c):
return _numeric_operator(u, c, lambda u, c: u < c)
def _less_than_or_equal(u, c):
return _numeric_operator(u, c, lambda u, c: u <= c)
def _greater_than(u, c):
return _numeric_operator(u, c, lambda u, c: u > c)
def _greater_than_or_equal(u, c):
return _numeric_operator(u, c, lambda u, c: u >= c)
def _before(u, c):
return _time_operator(u, c, lambda u, c: u < c)
def _after(u, c):
return _time_operator(u, c, lambda u, c: u > c)
ops = {
"in": _in,
"endsWith": _ends_with,
"startsWith": _starts_with,
"matches": _matches,
"contains": _contains,
"lessThan": _less_than,
"lessThanOrEqual": _less_than_or_equal,
"greaterThan": _greater_than,
"greaterThanOrEqual": _greater_than_or_equal,
"before": _before,
"after": _after
}
ops = defaultdict(lambda: False, ops)