filtration 2.2.0__tar.gz → 2.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- filtration-2.3.2/PKG-INFO +193 -0
- {filtration-2.2.0 → filtration-2.3.2}/filtration/__init__.py +52 -43
- filtration-2.3.2/filtration.egg-info/PKG-INFO +193 -0
- {filtration-2.2.0 → filtration-2.3.2}/filtration.egg-info/SOURCES.txt +3 -1
- filtration-2.3.2/filtration.egg-info/requires.txt +2 -0
- {filtration-2.2.0 → filtration-2.3.2}/setup.py +5 -3
- filtration-2.3.2/tests/test_parser.py +162 -0
- filtration-2.3.2/tests/test_tokens.py +106 -0
- filtration-2.2.0/PKG-INFO +0 -186
- filtration-2.2.0/filtration.egg-info/PKG-INFO +0 -186
- filtration-2.2.0/filtration.egg-info/requires.txt +0 -2
- {filtration-2.2.0 → filtration-2.3.2}/README.rst +0 -0
- {filtration-2.2.0 → filtration-2.3.2}/filtration.egg-info/dependency_links.txt +0 -0
- {filtration-2.2.0 → filtration-2.3.2}/filtration.egg-info/top_level.txt +0 -0
- {filtration-2.2.0 → filtration-2.3.2}/setup.cfg +0 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: filtration
|
|
3
|
+
Version: 2.3.2
|
|
4
|
+
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
+
Author: Steve McMaster
|
|
6
|
+
Author-email: mcmaster@hurricanelabs.com
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Requires-Dist: pyparsing>=3.0.0
|
|
12
|
+
Requires-Dist: ipcalc
|
|
13
|
+
Dynamic: author
|
|
14
|
+
Dynamic: author-email
|
|
15
|
+
Dynamic: classifier
|
|
16
|
+
Dynamic: description
|
|
17
|
+
Dynamic: project-url
|
|
18
|
+
Dynamic: requires-dist
|
|
19
|
+
Dynamic: summary
|
|
20
|
+
|
|
21
|
+
##########
|
|
22
|
+
Filtration
|
|
23
|
+
##########
|
|
24
|
+
|
|
25
|
+
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
26
|
+
|
|
27
|
+
Features
|
|
28
|
+
========
|
|
29
|
+
- Compatible with Python 3
|
|
30
|
+
- Parses human readable filters
|
|
31
|
+
|
|
32
|
+
Get Filtration
|
|
33
|
+
==============
|
|
34
|
+
|
|
35
|
+
.. code-block:: shell
|
|
36
|
+
|
|
37
|
+
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
38
|
+
|
|
39
|
+
Run the tests
|
|
40
|
+
=============
|
|
41
|
+
You must install nose2, then run:
|
|
42
|
+
|
|
43
|
+
.. code-block:: shell
|
|
44
|
+
|
|
45
|
+
git clone https://github.com/HurricaneLabs/filtration.git
|
|
46
|
+
cd filtration
|
|
47
|
+
nose2
|
|
48
|
+
|
|
49
|
+
########
|
|
50
|
+
Usage
|
|
51
|
+
########
|
|
52
|
+
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
53
|
+
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
54
|
+
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
55
|
+
"Expression Evaluation" for more details.
|
|
56
|
+
|
|
57
|
+
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
58
|
+
when joining statements together to enforce precedence. For example:
|
|
59
|
+
|
|
60
|
+
.. code-block:: python
|
|
61
|
+
|
|
62
|
+
>>> from filtration import Expression
|
|
63
|
+
>>> Expression.parseString("a and b or c")
|
|
64
|
+
((a and b) or c)
|
|
65
|
+
>>> Expression.parseString("a and (b or c)")
|
|
66
|
+
(a and (b or c))
|
|
67
|
+
>>>
|
|
68
|
+
|
|
69
|
+
Filtration can also be used to parse query string syntax. See below for more information.
|
|
70
|
+
|
|
71
|
+
Expression Format
|
|
72
|
+
=================
|
|
73
|
+
|
|
74
|
+
Operators
|
|
75
|
+
~~~~~~~~~
|
|
76
|
+
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
77
|
+
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
78
|
+
|
|
79
|
+
* Equal ("==")
|
|
80
|
+
* Not equal ("!=")
|
|
81
|
+
* Less than ("<")
|
|
82
|
+
* Less than or equal ("<=")
|
|
83
|
+
* Greater than (">")
|
|
84
|
+
* Greater than or equal (">=")
|
|
85
|
+
* Contains ("in")
|
|
86
|
+
* RHS must be a list or a Subnet
|
|
87
|
+
* Regular expression ("=~")
|
|
88
|
+
* RHS must be a regex token
|
|
89
|
+
|
|
90
|
+
Tokens
|
|
91
|
+
~~~~~~~~~~~~~~~
|
|
92
|
+
These tokens are used in the LHS or RHS of a statement.
|
|
93
|
+
|
|
94
|
+
Regex
|
|
95
|
+
-----
|
|
96
|
+
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
97
|
+
the closing "/" to represent the corresponding regex flags. Examples::
|
|
98
|
+
|
|
99
|
+
/abc/
|
|
100
|
+
/^abc/i
|
|
101
|
+
/^abc$/ms
|
|
102
|
+
|
|
103
|
+
Subnet
|
|
104
|
+
------
|
|
105
|
+
A subnet is an IPv4 subnet in CIDR notation::
|
|
106
|
+
|
|
107
|
+
127.0.0.0/8
|
|
108
|
+
192.168.0.0/24
|
|
109
|
+
|
|
110
|
+
Symbol
|
|
111
|
+
------
|
|
112
|
+
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
113
|
+
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
114
|
+
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
115
|
+
|
|
116
|
+
Value
|
|
117
|
+
-----
|
|
118
|
+
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
119
|
+
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
120
|
+
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
121
|
+
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
122
|
+
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
123
|
+
is lacking microseconds.
|
|
124
|
+
|
|
125
|
+
List
|
|
126
|
+
----
|
|
127
|
+
A list is two-or-more Value tokens separated by commas.
|
|
128
|
+
|
|
129
|
+
Expression Evaluation
|
|
130
|
+
=====================
|
|
131
|
+
|
|
132
|
+
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
133
|
+
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
134
|
+
this context:
|
|
135
|
+
|
|
136
|
+
.. code-block:: python
|
|
137
|
+
|
|
138
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
139
|
+
|
|
140
|
+
and this expression:
|
|
141
|
+
|
|
142
|
+
.. code-block:: python
|
|
143
|
+
|
|
144
|
+
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
145
|
+
|
|
146
|
+
calling the expression would return True:
|
|
147
|
+
|
|
148
|
+
.. code-block:: python
|
|
149
|
+
|
|
150
|
+
>>> expr(c)
|
|
151
|
+
True
|
|
152
|
+
>>>
|
|
153
|
+
|
|
154
|
+
whereas this expression would return False:
|
|
155
|
+
|
|
156
|
+
.. code-block:: python
|
|
157
|
+
|
|
158
|
+
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
159
|
+
>>> expr(c)
|
|
160
|
+
False
|
|
161
|
+
>>>
|
|
162
|
+
|
|
163
|
+
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
164
|
+
context. For example:
|
|
165
|
+
|
|
166
|
+
.. code-block:: python
|
|
167
|
+
|
|
168
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
169
|
+
>>> expr = Expression.parseString("a")
|
|
170
|
+
>>> expr(c)
|
|
171
|
+
True
|
|
172
|
+
>>> expr = Expression.parseString("d")
|
|
173
|
+
>>> expr(c)
|
|
174
|
+
False
|
|
175
|
+
>>>
|
|
176
|
+
|
|
177
|
+
Dictionary Traversal
|
|
178
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
179
|
+
|
|
180
|
+
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
181
|
+
|
|
182
|
+
.. code-block:: python
|
|
183
|
+
|
|
184
|
+
>>> c = {"a": {"b": {"c": 1}}}
|
|
185
|
+
|
|
186
|
+
This expression will return true:
|
|
187
|
+
|
|
188
|
+
.. code-block:: python
|
|
189
|
+
|
|
190
|
+
>>> expr = Expression.parseString("a.b.c == 1")
|
|
191
|
+
>>> expr(c)
|
|
192
|
+
True
|
|
193
|
+
>>>
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# pylint: disable=invalid-name
|
|
2
|
-
|
|
3
1
|
import datetime
|
|
4
2
|
import operator
|
|
5
3
|
import re
|
|
@@ -8,8 +6,8 @@ from functools import reduce
|
|
|
8
6
|
import ipcalc
|
|
9
7
|
from pyparsing import alphas, nums
|
|
10
8
|
from pyparsing import CaselessLiteral, Combine, Group, Keyword, Literal, MatchFirst, OneOrMore
|
|
11
|
-
from pyparsing import
|
|
12
|
-
from pyparsing import QuotedString,
|
|
9
|
+
from pyparsing import infix_notation, OpAssoc, Optional, ParseException, quoted_string
|
|
10
|
+
from pyparsing import QuotedString, remove_quotes, Suppress, Word, ZeroOrMore
|
|
13
11
|
|
|
14
12
|
|
|
15
13
|
class Token:
|
|
@@ -28,21 +26,21 @@ class Token:
|
|
|
28
26
|
return self.value
|
|
29
27
|
|
|
30
28
|
|
|
31
|
-
Year = Word(nums, max=4).
|
|
32
|
-
Month = Word(nums, max=2).
|
|
33
|
-
Day = Word(nums, max=2).
|
|
29
|
+
Year = Word(nums, max=4).set_parse_action(lambda toks: int(toks[0]))
|
|
30
|
+
Month = Word(nums, max=2).set_parse_action(lambda toks: int(toks[0]))
|
|
31
|
+
Day = Word(nums, max=2).set_parse_action(lambda toks: int(toks[0]))
|
|
34
32
|
|
|
35
|
-
Hour = Word(nums, max=2).
|
|
36
|
-
Minute = Word(nums, max=2).
|
|
37
|
-
Second = Word(nums, max=2).
|
|
33
|
+
Hour = Word(nums, max=2).set_parse_action(lambda toks: int(toks[0]))
|
|
34
|
+
Minute = Word(nums, max=2).set_parse_action(lambda toks: int(toks[0]))
|
|
35
|
+
Second = Word(nums, max=2).set_parse_action(lambda toks: int(toks[0]))
|
|
38
36
|
|
|
39
|
-
Date = (Year + Suppress("-") + Month + Suppress("-") + Day).
|
|
37
|
+
Date = (Year + Suppress("-") + Month + Suppress("-") + Day).set_parse_action(
|
|
40
38
|
lambda toks: datetime.datetime.combine(datetime.date(*toks), datetime.time.min)
|
|
41
39
|
)
|
|
42
|
-
Time = (Hour + Suppress(":") + Minute + Suppress(":") + Second).
|
|
40
|
+
Time = (Hour + Suppress(":") + Minute + Suppress(":") + Second).set_parse_action(
|
|
43
41
|
lambda toks: datetime.datetime.combine(datetime.date.today(), datetime.time(*toks))
|
|
44
42
|
)
|
|
45
|
-
DateTime = (Date + Suppress(Optional("T")) + Time).
|
|
43
|
+
DateTime = (Date + Suppress(Optional("T")) + Time).set_parse_action(
|
|
46
44
|
lambda toks: datetime.datetime.combine(toks[0].date(), toks[1].time())
|
|
47
45
|
)
|
|
48
46
|
|
|
@@ -52,17 +50,17 @@ class _Regex(Token):
|
|
|
52
50
|
# pylint: disable=no-self-use
|
|
53
51
|
try:
|
|
54
52
|
return re.compile(pattern, flags)
|
|
55
|
-
except re.error as
|
|
56
|
-
raise ParseException(str(
|
|
53
|
+
except re.error as exc:
|
|
54
|
+
raise ParseException(str(exc)) from exc
|
|
57
55
|
|
|
58
56
|
|
|
59
|
-
RegexFlag = Word("ims", exact=1).
|
|
57
|
+
RegexFlag = Word("ims", exact=1).set_parse_action(
|
|
60
58
|
lambda toks: getattr(re, toks[0].upper())
|
|
61
59
|
)
|
|
62
|
-
RegexFlags = ZeroOrMore(RegexFlag).
|
|
60
|
+
RegexFlags = ZeroOrMore(RegexFlag).set_parse_action(
|
|
63
61
|
lambda toks: reduce(lambda a, b: a | b, toks, 0)
|
|
64
62
|
)
|
|
65
|
-
Regex = (QuotedString("/") + RegexFlags).
|
|
63
|
+
Regex = (QuotedString("/") + RegexFlags).set_parse_action(_Regex)
|
|
66
64
|
|
|
67
65
|
Octet = Word(nums, max=3).addCondition(lambda toks: int(toks[0]) <= 255)
|
|
68
66
|
IpAddr = Combine(Octet + "." + Octet + "." + Octet + "." + Octet)
|
|
@@ -75,11 +73,16 @@ class _Subnet(Token):
|
|
|
75
73
|
return ipcalc.Network(subnet)
|
|
76
74
|
|
|
77
75
|
|
|
78
|
-
Subnet = Combine(IpAddr + "/" + Cidr).
|
|
76
|
+
Subnet = Combine(IpAddr + "/" + Cidr).set_parse_action(_Subnet)
|
|
79
77
|
|
|
80
78
|
|
|
81
79
|
class _Symbol(Token):
|
|
82
80
|
def __call__(self, ctx):
|
|
81
|
+
# If symbol is a literal 'a.b' and exists in ctx, return it
|
|
82
|
+
# Imperfect bugfix to allow for fields with literal dots in the root of ctx
|
|
83
|
+
if self.value in ctx:
|
|
84
|
+
return ctx[self.value]
|
|
85
|
+
|
|
83
86
|
parts = self.value.split(".")
|
|
84
87
|
value = ctx
|
|
85
88
|
for part in parts:
|
|
@@ -91,16 +94,16 @@ class _Symbol(Token):
|
|
|
91
94
|
return value
|
|
92
95
|
|
|
93
96
|
|
|
94
|
-
Symbol = Word((alphas + "_"), bodyChars=(alphas + nums + "." + "_")).
|
|
97
|
+
Symbol = Word((alphas + "_"), bodyChars=(alphas + nums + "." + "_")).set_parse_action(_Symbol)
|
|
95
98
|
|
|
96
99
|
|
|
97
100
|
Value = MatchFirst([
|
|
98
101
|
DateTime,
|
|
99
102
|
Date,
|
|
100
103
|
Time,
|
|
101
|
-
|
|
102
|
-
Word(nums).
|
|
103
|
-
]).
|
|
104
|
+
quoted_string.set_parse_action(remove_quotes),
|
|
105
|
+
Word(nums).set_parse_action(lambda toks: int(toks[0])),
|
|
106
|
+
]).set_parse_action(Token)
|
|
104
107
|
|
|
105
108
|
|
|
106
109
|
class _List(Token):
|
|
@@ -108,7 +111,7 @@ class _List(Token):
|
|
|
108
111
|
return [item(ctx) for item in self.value]
|
|
109
112
|
|
|
110
113
|
|
|
111
|
-
List = Group(Value + OneOrMore(Suppress(Literal(",")) + Value)).
|
|
114
|
+
List = Group(Value + OneOrMore(Suppress(Literal(",")) + Value)).set_parse_action(_List)
|
|
112
115
|
|
|
113
116
|
|
|
114
117
|
def in_op(lhs, rhs):
|
|
@@ -116,6 +119,10 @@ def in_op(lhs, rhs):
|
|
|
116
119
|
return False
|
|
117
120
|
|
|
118
121
|
try:
|
|
122
|
+
# If lhs is a list, we need to call ipcalc's contains() function for each item
|
|
123
|
+
if isinstance(lhs, list) and isinstance(rhs, ipcalc.Network):
|
|
124
|
+
return any([operator.contains(rhs, list_item) for list_item in lhs])
|
|
125
|
+
|
|
119
126
|
return operator.contains(rhs, lhs)
|
|
120
127
|
except ValueError:
|
|
121
128
|
return False
|
|
@@ -126,21 +133,21 @@ def re_op(lhs, rhs):
|
|
|
126
133
|
return False
|
|
127
134
|
|
|
128
135
|
if isinstance(lhs, list):
|
|
129
|
-
return any(
|
|
136
|
+
return any(rhs.search(item) for item in lhs)
|
|
130
137
|
|
|
131
138
|
return bool(rhs.search(lhs))
|
|
132
139
|
|
|
133
140
|
|
|
134
141
|
# Operators
|
|
135
142
|
ComparisonOp = MatchFirst([
|
|
136
|
-
Literal("==").
|
|
137
|
-
Literal("!=").
|
|
138
|
-
Literal("<=").
|
|
139
|
-
Literal("<").
|
|
140
|
-
Literal(">=").
|
|
141
|
-
Literal(">").
|
|
142
|
-
Keyword("in").
|
|
143
|
-
Literal("=~").
|
|
143
|
+
Literal("==").set_parse_action(lambda toks: operator.eq),
|
|
144
|
+
Literal("!=").set_parse_action(lambda toks: operator.ne),
|
|
145
|
+
Literal("<=").set_parse_action(lambda toks: operator.le),
|
|
146
|
+
Literal("<").set_parse_action(lambda toks: operator.lt),
|
|
147
|
+
Literal(">=").set_parse_action(lambda toks: operator.ge),
|
|
148
|
+
Literal(">").set_parse_action(lambda toks: operator.gt),
|
|
149
|
+
Keyword("in").set_parse_action(lambda toks: in_op),
|
|
150
|
+
Literal("=~").set_parse_action(lambda toks: re_op),
|
|
144
151
|
])
|
|
145
152
|
|
|
146
153
|
|
|
@@ -159,7 +166,7 @@ class _And(Token):
|
|
|
159
166
|
return toks[0][0::2]
|
|
160
167
|
|
|
161
168
|
def __call__(self, ctx):
|
|
162
|
-
return all(
|
|
169
|
+
return all(item(ctx) for item in self.value)
|
|
163
170
|
|
|
164
171
|
|
|
165
172
|
class _Or(Token):
|
|
@@ -168,13 +175,13 @@ class _Or(Token):
|
|
|
168
175
|
return toks[0][0::2]
|
|
169
176
|
|
|
170
177
|
def __call__(self, ctx):
|
|
171
|
-
return any(
|
|
178
|
+
return any(item(ctx) for item in self.value)
|
|
172
179
|
|
|
173
180
|
|
|
174
181
|
BooleanOps = [
|
|
175
|
-
(CaselessLiteral("not"), 1,
|
|
176
|
-
(CaselessLiteral("and"), 2,
|
|
177
|
-
(CaselessLiteral("or"), 2,
|
|
182
|
+
(CaselessLiteral("not"), 1, OpAssoc.RIGHT, _Not),
|
|
183
|
+
(CaselessLiteral("and"), 2, OpAssoc.LEFT, _And),
|
|
184
|
+
(CaselessLiteral("or"), 2, OpAssoc.LEFT, _Or),
|
|
178
185
|
]
|
|
179
186
|
|
|
180
187
|
LHS = Symbol | Value | Regex
|
|
@@ -184,7 +191,7 @@ RHS = Symbol | Subnet | List | Value | Regex
|
|
|
184
191
|
class _Statement:
|
|
185
192
|
def __init__(self, lhs, op=None, rhs=None):
|
|
186
193
|
self.lhs = lhs
|
|
187
|
-
self.op = op
|
|
194
|
+
self.op = op # pylint: disable=invalid-name
|
|
188
195
|
self.rhs = rhs
|
|
189
196
|
|
|
190
197
|
def __call__(self, ctx):
|
|
@@ -194,16 +201,18 @@ class _Statement:
|
|
|
194
201
|
return self.op(self.lhs(ctx), self.rhs(ctx))
|
|
195
202
|
|
|
196
203
|
|
|
197
|
-
Statement = (LHS + Optional(ComparisonOp + RHS)).
|
|
204
|
+
Statement = (LHS + Optional(ComparisonOp + RHS)).set_parse_action(
|
|
198
205
|
lambda toks: _Statement(*toks)
|
|
199
206
|
)
|
|
200
207
|
|
|
201
208
|
|
|
202
209
|
class Expression:
|
|
203
210
|
@classmethod
|
|
204
|
-
def
|
|
205
|
-
expr =
|
|
211
|
+
def parse_string(cls, *args, **kwargs):
|
|
212
|
+
expr = infix_notation(Statement, BooleanOps).set_parse_action(
|
|
206
213
|
lambda toks: _Statement(*toks)
|
|
207
214
|
)
|
|
208
215
|
|
|
209
|
-
return expr.
|
|
216
|
+
return expr.parse_string(*args, **kwargs)[0]
|
|
217
|
+
|
|
218
|
+
parseString = parse_string
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: filtration
|
|
3
|
+
Version: 2.3.2
|
|
4
|
+
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
+
Author: Steve McMaster
|
|
6
|
+
Author-email: mcmaster@hurricanelabs.com
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Requires-Dist: pyparsing>=3.0.0
|
|
12
|
+
Requires-Dist: ipcalc
|
|
13
|
+
Dynamic: author
|
|
14
|
+
Dynamic: author-email
|
|
15
|
+
Dynamic: classifier
|
|
16
|
+
Dynamic: description
|
|
17
|
+
Dynamic: project-url
|
|
18
|
+
Dynamic: requires-dist
|
|
19
|
+
Dynamic: summary
|
|
20
|
+
|
|
21
|
+
##########
|
|
22
|
+
Filtration
|
|
23
|
+
##########
|
|
24
|
+
|
|
25
|
+
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
26
|
+
|
|
27
|
+
Features
|
|
28
|
+
========
|
|
29
|
+
- Compatible with Python 3
|
|
30
|
+
- Parses human readable filters
|
|
31
|
+
|
|
32
|
+
Get Filtration
|
|
33
|
+
==============
|
|
34
|
+
|
|
35
|
+
.. code-block:: shell
|
|
36
|
+
|
|
37
|
+
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
38
|
+
|
|
39
|
+
Run the tests
|
|
40
|
+
=============
|
|
41
|
+
You must install nose2, then run:
|
|
42
|
+
|
|
43
|
+
.. code-block:: shell
|
|
44
|
+
|
|
45
|
+
git clone https://github.com/HurricaneLabs/filtration.git
|
|
46
|
+
cd filtration
|
|
47
|
+
nose2
|
|
48
|
+
|
|
49
|
+
########
|
|
50
|
+
Usage
|
|
51
|
+
########
|
|
52
|
+
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
53
|
+
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
54
|
+
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
55
|
+
"Expression Evaluation" for more details.
|
|
56
|
+
|
|
57
|
+
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
58
|
+
when joining statements together to enforce precedence. For example:
|
|
59
|
+
|
|
60
|
+
.. code-block:: python
|
|
61
|
+
|
|
62
|
+
>>> from filtration import Expression
|
|
63
|
+
>>> Expression.parseString("a and b or c")
|
|
64
|
+
((a and b) or c)
|
|
65
|
+
>>> Expression.parseString("a and (b or c)")
|
|
66
|
+
(a and (b or c))
|
|
67
|
+
>>>
|
|
68
|
+
|
|
69
|
+
Filtration can also be used to parse query string syntax. See below for more information.
|
|
70
|
+
|
|
71
|
+
Expression Format
|
|
72
|
+
=================
|
|
73
|
+
|
|
74
|
+
Operators
|
|
75
|
+
~~~~~~~~~
|
|
76
|
+
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
77
|
+
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
78
|
+
|
|
79
|
+
* Equal ("==")
|
|
80
|
+
* Not equal ("!=")
|
|
81
|
+
* Less than ("<")
|
|
82
|
+
* Less than or equal ("<=")
|
|
83
|
+
* Greater than (">")
|
|
84
|
+
* Greater than or equal (">=")
|
|
85
|
+
* Contains ("in")
|
|
86
|
+
* RHS must be a list or a Subnet
|
|
87
|
+
* Regular expression ("=~")
|
|
88
|
+
* RHS must be a regex token
|
|
89
|
+
|
|
90
|
+
Tokens
|
|
91
|
+
~~~~~~~~~~~~~~~
|
|
92
|
+
These tokens are used in the LHS or RHS of a statement.
|
|
93
|
+
|
|
94
|
+
Regex
|
|
95
|
+
-----
|
|
96
|
+
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
97
|
+
the closing "/" to represent the corresponding regex flags. Examples::
|
|
98
|
+
|
|
99
|
+
/abc/
|
|
100
|
+
/^abc/i
|
|
101
|
+
/^abc$/ms
|
|
102
|
+
|
|
103
|
+
Subnet
|
|
104
|
+
------
|
|
105
|
+
A subnet is an IPv4 subnet in CIDR notation::
|
|
106
|
+
|
|
107
|
+
127.0.0.0/8
|
|
108
|
+
192.168.0.0/24
|
|
109
|
+
|
|
110
|
+
Symbol
|
|
111
|
+
------
|
|
112
|
+
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
113
|
+
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
114
|
+
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
115
|
+
|
|
116
|
+
Value
|
|
117
|
+
-----
|
|
118
|
+
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
119
|
+
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
120
|
+
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
121
|
+
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
122
|
+
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
123
|
+
is lacking microseconds.
|
|
124
|
+
|
|
125
|
+
List
|
|
126
|
+
----
|
|
127
|
+
A list is two-or-more Value tokens separated by commas.
|
|
128
|
+
|
|
129
|
+
Expression Evaluation
|
|
130
|
+
=====================
|
|
131
|
+
|
|
132
|
+
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
133
|
+
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
134
|
+
this context:
|
|
135
|
+
|
|
136
|
+
.. code-block:: python
|
|
137
|
+
|
|
138
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
139
|
+
|
|
140
|
+
and this expression:
|
|
141
|
+
|
|
142
|
+
.. code-block:: python
|
|
143
|
+
|
|
144
|
+
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
145
|
+
|
|
146
|
+
calling the expression would return True:
|
|
147
|
+
|
|
148
|
+
.. code-block:: python
|
|
149
|
+
|
|
150
|
+
>>> expr(c)
|
|
151
|
+
True
|
|
152
|
+
>>>
|
|
153
|
+
|
|
154
|
+
whereas this expression would return False:
|
|
155
|
+
|
|
156
|
+
.. code-block:: python
|
|
157
|
+
|
|
158
|
+
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
159
|
+
>>> expr(c)
|
|
160
|
+
False
|
|
161
|
+
>>>
|
|
162
|
+
|
|
163
|
+
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
164
|
+
context. For example:
|
|
165
|
+
|
|
166
|
+
.. code-block:: python
|
|
167
|
+
|
|
168
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
169
|
+
>>> expr = Expression.parseString("a")
|
|
170
|
+
>>> expr(c)
|
|
171
|
+
True
|
|
172
|
+
>>> expr = Expression.parseString("d")
|
|
173
|
+
>>> expr(c)
|
|
174
|
+
False
|
|
175
|
+
>>>
|
|
176
|
+
|
|
177
|
+
Dictionary Traversal
|
|
178
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
179
|
+
|
|
180
|
+
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
181
|
+
|
|
182
|
+
.. code-block:: python
|
|
183
|
+
|
|
184
|
+
>>> c = {"a": {"b": {"c": 1}}}
|
|
185
|
+
|
|
186
|
+
This expression will return true:
|
|
187
|
+
|
|
188
|
+
.. code-block:: python
|
|
189
|
+
|
|
190
|
+
>>> expr = Expression.parseString("a.b.c == 1")
|
|
191
|
+
>>> expr(c)
|
|
192
|
+
True
|
|
193
|
+
>>>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from setuptools import setup
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
VERSION = "2.2
|
|
4
|
+
VERSION = "2.3.2"
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
with open("README.rst", "r") as f:
|
|
@@ -17,7 +17,7 @@ setup(
|
|
|
17
17
|
description="filtration - A library for parsing arbitrary filters",
|
|
18
18
|
long_description=long_description,
|
|
19
19
|
install_requires=[
|
|
20
|
-
"pyparsing",
|
|
20
|
+
"pyparsing>=3.0.0",
|
|
21
21
|
"ipcalc"
|
|
22
22
|
],
|
|
23
23
|
classifiers=[
|
|
@@ -25,5 +25,7 @@ setup(
|
|
|
25
25
|
"Programming Language :: Python :: 3",
|
|
26
26
|
"Development Status :: 5 - Production/Stable",
|
|
27
27
|
],
|
|
28
|
-
|
|
28
|
+
project_urls={
|
|
29
|
+
"Bug Tracker": "https://github.com/HurricaneLabs/filtration/issues"
|
|
30
|
+
}
|
|
29
31
|
)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from nose2.tools import params
|
|
4
|
+
from pyparsing import ParseException
|
|
5
|
+
import fauxfactory
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
symbol_tests = [
|
|
9
|
+
fauxfactory.gen_alpha(),
|
|
10
|
+
"_{0}".format(fauxfactory.gen_alpha()),
|
|
11
|
+
"{0}.{1}".format(fauxfactory.gen_alpha(), fauxfactory.gen_alpha()),
|
|
12
|
+
"_{0}.{1}".format(fauxfactory.gen_alpha(), fauxfactory.gen_alpha()),
|
|
13
|
+
("'{0}'".format(fauxfactory.gen_alpha()), ParseException),
|
|
14
|
+
('"{0}"'.format(fauxfactory.gen_alpha()), ParseException),
|
|
15
|
+
(fauxfactory.gen_numeric_string(), ParseException),
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
value_tests = [
|
|
19
|
+
"'{0}'".format(fauxfactory.gen_alpha()),
|
|
20
|
+
'"{0}"'.format(fauxfactory.gen_alpha()),
|
|
21
|
+
fauxfactory.gen_numeric_string(),
|
|
22
|
+
fauxfactory.gen_date().isoformat(),
|
|
23
|
+
fauxfactory.gen_time().isoformat(),
|
|
24
|
+
fauxfactory.gen_datetime().isoformat(),
|
|
25
|
+
(fauxfactory.gen_alpha(), ParseException),
|
|
26
|
+
("_{0}".format(fauxfactory.gen_alpha()), ParseException),
|
|
27
|
+
("{0}.{1}".format(fauxfactory.gen_alpha(), fauxfactory.gen_alpha()), ParseException),
|
|
28
|
+
("_{0}.{1}".format(fauxfactory.gen_alpha(), fauxfactory.gen_alpha()), ParseException),
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
regex_tests = [
|
|
32
|
+
"/regex/",
|
|
33
|
+
"/regex/i",
|
|
34
|
+
"/regex/im",
|
|
35
|
+
("/not[a-valid/", ParseException),
|
|
36
|
+
("/not(a-valid/", ParseException),
|
|
37
|
+
("/not-a-valid)", ParseException),
|
|
38
|
+
("'regex'", ParseException),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
subnet_tests = [
|
|
42
|
+
"0.0.0.0/0",
|
|
43
|
+
"0.0.0.0/8",
|
|
44
|
+
"172.16.0.0/8",
|
|
45
|
+
"192.168.0.0/16",
|
|
46
|
+
"192.168.100.0/24",
|
|
47
|
+
"255.255.255.255/32",
|
|
48
|
+
("0.0.0.0", ParseException),
|
|
49
|
+
("0.0.0.0/33", ParseException),
|
|
50
|
+
("256.256.256.256/0", ParseException),
|
|
51
|
+
("256.256.256.256/32", ParseException),
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
term_parse_tests = [
|
|
55
|
+
"symbol1",
|
|
56
|
+
"symbol1 == symbol2",
|
|
57
|
+
"symbol1 == 'value1'",
|
|
58
|
+
"symbol1 =~ /regex1/",
|
|
59
|
+
"symbol1 in 10.0.0.0/8",
|
|
60
|
+
"symbol1 in 'list','list','list'",
|
|
61
|
+
"symbol1 in symbol1",
|
|
62
|
+
"'value1' in symbol1",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
expression_parse_tests = [
|
|
66
|
+
"{0}",
|
|
67
|
+
"not {0}",
|
|
68
|
+
"({0})",
|
|
69
|
+
"not ({0})",
|
|
70
|
+
"{0} and {1}",
|
|
71
|
+
"{0} or {1}",
|
|
72
|
+
"{0} and {1} or {2}",
|
|
73
|
+
"{0} and ({1} or {2})",
|
|
74
|
+
"{0} or {1} and {2}",
|
|
75
|
+
"({0} or {1}) and {2}",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def generate_expression_tests():
|
|
80
|
+
for test_expr_fmt in expression_parse_tests:
|
|
81
|
+
for (expr_a, expr_b, expr_c) in zip(term_parse_tests, term_parse_tests, term_parse_tests):
|
|
82
|
+
yield test_expr_fmt.format(expr_a, expr_b, expr_c)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class TestGrammar(unittest.TestCase):
|
|
86
|
+
@params(*symbol_tests)
|
|
87
|
+
def test_parse_symbol(self, test_expr, expect_failure=None):
|
|
88
|
+
"""
|
|
89
|
+
A symbol begins with a letter or an underscore, and may contain any combination of
|
|
90
|
+
letters, numbers, dot (".") and underscore ("_")
|
|
91
|
+
"""
|
|
92
|
+
from filtration import Symbol
|
|
93
|
+
|
|
94
|
+
if expect_failure is None:
|
|
95
|
+
Symbol.parseString(test_expr)
|
|
96
|
+
else:
|
|
97
|
+
self.assertRaises(expect_failure, Symbol.parseString, test_expr)
|
|
98
|
+
|
|
99
|
+
@params(*value_tests)
|
|
100
|
+
def test_parse_value(self, test_expr, expect_failure=None):
|
|
101
|
+
"""
|
|
102
|
+
A value is a quoted string, with single or double quotes, or a number.
|
|
103
|
+
"""
|
|
104
|
+
from filtration import Value
|
|
105
|
+
|
|
106
|
+
if expect_failure is None:
|
|
107
|
+
Value.parseString(test_expr)
|
|
108
|
+
else:
|
|
109
|
+
self.assertRaises(expect_failure, Value.parseString, test_expr)
|
|
110
|
+
|
|
111
|
+
@params(*regex_tests)
|
|
112
|
+
def test_parse_regex(self, test_expr, expect_failure=None):
|
|
113
|
+
"""
|
|
114
|
+
A regex is a string surrounded by /
|
|
115
|
+
"""
|
|
116
|
+
from filtration import Regex
|
|
117
|
+
|
|
118
|
+
if expect_failure is None:
|
|
119
|
+
Regex.parseString(test_expr)
|
|
120
|
+
else:
|
|
121
|
+
self.assertRaises(expect_failure, Regex.parseString, test_expr)
|
|
122
|
+
|
|
123
|
+
@params(*subnet_tests)
|
|
124
|
+
def test_parse_subnet(self, test_expr, expect_failure=None):
|
|
125
|
+
"""
|
|
126
|
+
Subnets are defined in CIDR notation
|
|
127
|
+
"""
|
|
128
|
+
from filtration import Subnet
|
|
129
|
+
|
|
130
|
+
if expect_failure is None:
|
|
131
|
+
Subnet.parseString(test_expr)
|
|
132
|
+
else:
|
|
133
|
+
self.assertRaises(expect_failure, Subnet.parseString, test_expr)
|
|
134
|
+
|
|
135
|
+
@params(*range(1, 10))
|
|
136
|
+
def test_parse_valuelist(self, test_expr_len, expect_failure=None):
|
|
137
|
+
"""
|
|
138
|
+
A list is two or more values, separated by commas
|
|
139
|
+
"""
|
|
140
|
+
from filtration import List
|
|
141
|
+
|
|
142
|
+
test_expr = ",".join(["'value'" for _ in range(0, test_expr_len)])
|
|
143
|
+
if test_expr_len > 1:
|
|
144
|
+
List.parseString(test_expr)
|
|
145
|
+
else:
|
|
146
|
+
self.assertRaises(ParseException, List.parseString, test_expr)
|
|
147
|
+
|
|
148
|
+
@params(*term_parse_tests)
|
|
149
|
+
def test_parse_statement(self, test_expr, expect_failure=None):
|
|
150
|
+
"""
|
|
151
|
+
"""
|
|
152
|
+
from filtration import Statement
|
|
153
|
+
|
|
154
|
+
Statement.parseString(test_expr)
|
|
155
|
+
|
|
156
|
+
@params(*generate_expression_tests())
|
|
157
|
+
def test_parse_expression(self, test_expr):
|
|
158
|
+
"""
|
|
159
|
+
"""
|
|
160
|
+
from filtration import Expression
|
|
161
|
+
|
|
162
|
+
Expression.parseString(test_expr)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from nose2.tools import params
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
regex_evaluate_tests = [
|
|
8
|
+
("/abc/", re.compile("abc")),
|
|
9
|
+
("/abc/i", re.compile("abc", flags=re.I)),
|
|
10
|
+
("/abc/m", re.compile("abc", flags=re.M)),
|
|
11
|
+
("/abc/s", re.compile("abc", flags=re.S)),
|
|
12
|
+
("/abc/ims", re.compile("abc", flags=re.I | re.M | re.S)),
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
expression_evaluate_tests = [
|
|
16
|
+
("symbol_true", "symbol_false"),
|
|
17
|
+
("symbol_true and symbol_true", "symbol_true and symbol_false"),
|
|
18
|
+
("symbol_true or symbol_false", "symbol_false or symbol_false"),
|
|
19
|
+
("not symbol_false", "not symbol_true"),
|
|
20
|
+
("dict.symbol_true", "dict.doesnt_exist"),
|
|
21
|
+
("symbol_int == 1", "symbol_int == 2"),
|
|
22
|
+
("symbol_int != 2", "symbol_int != 1"),
|
|
23
|
+
("symbol_int < 2", "symbol_int < 1"),
|
|
24
|
+
("symbol_int <= 1", "symbol_int <= 0"),
|
|
25
|
+
("symbol_int > 0", "symbol_int > 1"),
|
|
26
|
+
("symbol_int >= 1", "symbol_int >= 2"),
|
|
27
|
+
("symbol_str in 'a','b'", "symbol_str in 'c','d'"),
|
|
28
|
+
(None, "'a' in nonexistant"),
|
|
29
|
+
("symbol_str =~ /^a/", "symbol_str =~ /c$/"),
|
|
30
|
+
("symbol_list =~ /cde/", "symbol_list =~ /xyz/"),
|
|
31
|
+
(None, "nonexistant =~ /a/"),
|
|
32
|
+
("symbol_dt > 2015-03-01", "symbol_dt < 2000-01-01"),
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class TestTokens(unittest.TestCase):
|
|
37
|
+
def test_evaluate_symbol(self):
|
|
38
|
+
"""
|
|
39
|
+
"""
|
|
40
|
+
from filtration import Symbol
|
|
41
|
+
|
|
42
|
+
sym = Symbol.parseString("symbol")[0]
|
|
43
|
+
self.assertEqual(sym({"symbol": "value"}), "value")
|
|
44
|
+
|
|
45
|
+
def test_evaluate_value(self):
|
|
46
|
+
"""
|
|
47
|
+
"""
|
|
48
|
+
from filtration import Value
|
|
49
|
+
|
|
50
|
+
val = Value.parseString("'value'")[0]
|
|
51
|
+
self.assertEqual(val({}), "value")
|
|
52
|
+
|
|
53
|
+
@params(*regex_evaluate_tests)
|
|
54
|
+
def test_evaluate_regex(self, test_expr, expected):
|
|
55
|
+
"""
|
|
56
|
+
"""
|
|
57
|
+
from filtration import Regex
|
|
58
|
+
|
|
59
|
+
rex = Regex.parseString(test_expr)[0]
|
|
60
|
+
result = rex({})
|
|
61
|
+
|
|
62
|
+
self.assertEqual(result.pattern, expected.pattern)
|
|
63
|
+
self.assertEqual(result.flags, expected.flags)
|
|
64
|
+
|
|
65
|
+
def test_evaluate_subnet(self):
|
|
66
|
+
"""
|
|
67
|
+
"""
|
|
68
|
+
from filtration import Subnet
|
|
69
|
+
|
|
70
|
+
subnet = Subnet.parseString("192.168.0.0/16")[0]
|
|
71
|
+
self.assertEqual(subnet({}).network(), "192.168.0.0")
|
|
72
|
+
self.assertEqual(subnet({}).size(), 2 ** 16)
|
|
73
|
+
|
|
74
|
+
@params(*expression_evaluate_tests)
|
|
75
|
+
def test_evaluate_expression(self, test_expr_true, test_expr_false=None):
|
|
76
|
+
"""
|
|
77
|
+
"""
|
|
78
|
+
import datetime
|
|
79
|
+
from filtration import Expression
|
|
80
|
+
|
|
81
|
+
context = {
|
|
82
|
+
"symbol_true": True,
|
|
83
|
+
"symbol_False": False,
|
|
84
|
+
"symbol_int": 1,
|
|
85
|
+
"symbol_str": "a",
|
|
86
|
+
"symbol_dt": datetime.datetime.now(),
|
|
87
|
+
"symbol_list": ["abcde", "cdefg"]
|
|
88
|
+
}
|
|
89
|
+
context["dict"] = context
|
|
90
|
+
|
|
91
|
+
if test_expr_true:
|
|
92
|
+
expr = Expression.parseString(test_expr_true)
|
|
93
|
+
self.assertTrue(expr(context))
|
|
94
|
+
|
|
95
|
+
if test_expr_false:
|
|
96
|
+
expr = Expression.parseString(test_expr_false)
|
|
97
|
+
self.assertFalse(expr(context))
|
|
98
|
+
|
|
99
|
+
def test_subnet_contains(self):
|
|
100
|
+
"""
|
|
101
|
+
"""
|
|
102
|
+
from filtration import Expression
|
|
103
|
+
|
|
104
|
+
expr = Expression.parseString("src in 127.0.0.0/8")
|
|
105
|
+
self.assertTrue(expr({"src": "127.0.0.1"}))
|
|
106
|
+
self.assertFalse(expr({"src": "localhost"}))
|
filtration-2.2.0/PKG-INFO
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.1
|
|
2
|
-
Name: filtration
|
|
3
|
-
Version: 2.2.0
|
|
4
|
-
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
-
Home-page: UNKNOWN
|
|
6
|
-
Author: Steve McMaster
|
|
7
|
-
Author-email: mcmaster@hurricanelabs.com
|
|
8
|
-
License: UNKNOWN
|
|
9
|
-
Description: ##########
|
|
10
|
-
Filtration
|
|
11
|
-
##########
|
|
12
|
-
|
|
13
|
-
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
14
|
-
|
|
15
|
-
Features
|
|
16
|
-
========
|
|
17
|
-
- Compatible with Python 3
|
|
18
|
-
- Parses human readable filters
|
|
19
|
-
|
|
20
|
-
Get Filtration
|
|
21
|
-
==============
|
|
22
|
-
|
|
23
|
-
.. code-block:: shell
|
|
24
|
-
|
|
25
|
-
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
26
|
-
|
|
27
|
-
Run the tests
|
|
28
|
-
=============
|
|
29
|
-
You must install nose2, then run:
|
|
30
|
-
|
|
31
|
-
.. code-block:: shell
|
|
32
|
-
|
|
33
|
-
git clone https://github.com/HurricaneLabs/filtration.git
|
|
34
|
-
cd filtration
|
|
35
|
-
nose2
|
|
36
|
-
|
|
37
|
-
########
|
|
38
|
-
Usage
|
|
39
|
-
########
|
|
40
|
-
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
41
|
-
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
42
|
-
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
43
|
-
"Expression Evaluation" for more details.
|
|
44
|
-
|
|
45
|
-
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
46
|
-
when joining statements together to enforce precedence. For example:
|
|
47
|
-
|
|
48
|
-
.. code-block:: python
|
|
49
|
-
|
|
50
|
-
>>> from filtration import Expression
|
|
51
|
-
>>> Expression.parseString("a and b or c")
|
|
52
|
-
((a and b) or c)
|
|
53
|
-
>>> Expression.parseString("a and (b or c)")
|
|
54
|
-
(a and (b or c))
|
|
55
|
-
>>>
|
|
56
|
-
|
|
57
|
-
Filtration can also be used to parse query string syntax. See below for more information.
|
|
58
|
-
|
|
59
|
-
Expression Format
|
|
60
|
-
=================
|
|
61
|
-
|
|
62
|
-
Operators
|
|
63
|
-
~~~~~~~~~
|
|
64
|
-
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
65
|
-
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
66
|
-
|
|
67
|
-
* Equal ("==")
|
|
68
|
-
* Not equal ("!=")
|
|
69
|
-
* Less than ("<")
|
|
70
|
-
* Less than or equal ("<=")
|
|
71
|
-
* Greater than (">")
|
|
72
|
-
* Greater than or equal (">=")
|
|
73
|
-
* Contains ("in")
|
|
74
|
-
* RHS must be a list or a Subnet
|
|
75
|
-
* Regular expression ("=~")
|
|
76
|
-
* RHS must be a regex token
|
|
77
|
-
|
|
78
|
-
Tokens
|
|
79
|
-
~~~~~~~~~~~~~~~
|
|
80
|
-
These tokens are used in the LHS or RHS of a statement.
|
|
81
|
-
|
|
82
|
-
Regex
|
|
83
|
-
-----
|
|
84
|
-
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
85
|
-
the closing "/" to represent the corresponding regex flags. Examples::
|
|
86
|
-
|
|
87
|
-
/abc/
|
|
88
|
-
/^abc/i
|
|
89
|
-
/^abc$/ms
|
|
90
|
-
|
|
91
|
-
Subnet
|
|
92
|
-
------
|
|
93
|
-
A subnet is an IPv4 subnet in CIDR notation::
|
|
94
|
-
|
|
95
|
-
127.0.0.0/8
|
|
96
|
-
192.168.0.0/24
|
|
97
|
-
|
|
98
|
-
Symbol
|
|
99
|
-
------
|
|
100
|
-
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
101
|
-
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
102
|
-
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
103
|
-
|
|
104
|
-
Value
|
|
105
|
-
-----
|
|
106
|
-
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
107
|
-
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
108
|
-
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
109
|
-
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
110
|
-
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
111
|
-
is lacking microseconds.
|
|
112
|
-
|
|
113
|
-
List
|
|
114
|
-
----
|
|
115
|
-
A list is two-or-more Value tokens separated by commas.
|
|
116
|
-
|
|
117
|
-
Expression Evaluation
|
|
118
|
-
=====================
|
|
119
|
-
|
|
120
|
-
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
121
|
-
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
122
|
-
this context:
|
|
123
|
-
|
|
124
|
-
.. code-block:: python
|
|
125
|
-
|
|
126
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
127
|
-
|
|
128
|
-
and this expression:
|
|
129
|
-
|
|
130
|
-
.. code-block:: python
|
|
131
|
-
|
|
132
|
-
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
133
|
-
|
|
134
|
-
calling the expression would return True:
|
|
135
|
-
|
|
136
|
-
.. code-block:: python
|
|
137
|
-
|
|
138
|
-
>>> expr(c)
|
|
139
|
-
True
|
|
140
|
-
>>>
|
|
141
|
-
|
|
142
|
-
whereas this expression would return False:
|
|
143
|
-
|
|
144
|
-
.. code-block:: python
|
|
145
|
-
|
|
146
|
-
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
147
|
-
>>> expr(c)
|
|
148
|
-
False
|
|
149
|
-
>>>
|
|
150
|
-
|
|
151
|
-
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
152
|
-
context. For example:
|
|
153
|
-
|
|
154
|
-
.. code-block:: python
|
|
155
|
-
|
|
156
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
157
|
-
>>> expr = Expression.parseString("a")
|
|
158
|
-
>>> expr(c)
|
|
159
|
-
True
|
|
160
|
-
>>> expr = Expression.parseString("d")
|
|
161
|
-
>>> expr(c)
|
|
162
|
-
False
|
|
163
|
-
>>>
|
|
164
|
-
|
|
165
|
-
Dictionary Traversal
|
|
166
|
-
~~~~~~~~~~~~~~~~~~~~
|
|
167
|
-
|
|
168
|
-
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
169
|
-
|
|
170
|
-
.. code-block:: python
|
|
171
|
-
|
|
172
|
-
>>> c = {"a": {"b": {"c": 1}}}
|
|
173
|
-
|
|
174
|
-
This expression will return true:
|
|
175
|
-
|
|
176
|
-
.. code-block:: python
|
|
177
|
-
|
|
178
|
-
>>> expr = Expression.parseString("a.b.c == 1")
|
|
179
|
-
>>> expr(c)
|
|
180
|
-
True
|
|
181
|
-
>>>
|
|
182
|
-
|
|
183
|
-
Platform: UNKNOWN
|
|
184
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
185
|
-
Classifier: Programming Language :: Python :: 3
|
|
186
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.1
|
|
2
|
-
Name: filtration
|
|
3
|
-
Version: 2.2.0
|
|
4
|
-
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
-
Home-page: UNKNOWN
|
|
6
|
-
Author: Steve McMaster
|
|
7
|
-
Author-email: mcmaster@hurricanelabs.com
|
|
8
|
-
License: UNKNOWN
|
|
9
|
-
Description: ##########
|
|
10
|
-
Filtration
|
|
11
|
-
##########
|
|
12
|
-
|
|
13
|
-
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
14
|
-
|
|
15
|
-
Features
|
|
16
|
-
========
|
|
17
|
-
- Compatible with Python 3
|
|
18
|
-
- Parses human readable filters
|
|
19
|
-
|
|
20
|
-
Get Filtration
|
|
21
|
-
==============
|
|
22
|
-
|
|
23
|
-
.. code-block:: shell
|
|
24
|
-
|
|
25
|
-
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
26
|
-
|
|
27
|
-
Run the tests
|
|
28
|
-
=============
|
|
29
|
-
You must install nose2, then run:
|
|
30
|
-
|
|
31
|
-
.. code-block:: shell
|
|
32
|
-
|
|
33
|
-
git clone https://github.com/HurricaneLabs/filtration.git
|
|
34
|
-
cd filtration
|
|
35
|
-
nose2
|
|
36
|
-
|
|
37
|
-
########
|
|
38
|
-
Usage
|
|
39
|
-
########
|
|
40
|
-
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
41
|
-
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
42
|
-
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
43
|
-
"Expression Evaluation" for more details.
|
|
44
|
-
|
|
45
|
-
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
46
|
-
when joining statements together to enforce precedence. For example:
|
|
47
|
-
|
|
48
|
-
.. code-block:: python
|
|
49
|
-
|
|
50
|
-
>>> from filtration import Expression
|
|
51
|
-
>>> Expression.parseString("a and b or c")
|
|
52
|
-
((a and b) or c)
|
|
53
|
-
>>> Expression.parseString("a and (b or c)")
|
|
54
|
-
(a and (b or c))
|
|
55
|
-
>>>
|
|
56
|
-
|
|
57
|
-
Filtration can also be used to parse query string syntax. See below for more information.
|
|
58
|
-
|
|
59
|
-
Expression Format
|
|
60
|
-
=================
|
|
61
|
-
|
|
62
|
-
Operators
|
|
63
|
-
~~~~~~~~~
|
|
64
|
-
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
65
|
-
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
66
|
-
|
|
67
|
-
* Equal ("==")
|
|
68
|
-
* Not equal ("!=")
|
|
69
|
-
* Less than ("<")
|
|
70
|
-
* Less than or equal ("<=")
|
|
71
|
-
* Greater than (">")
|
|
72
|
-
* Greater than or equal (">=")
|
|
73
|
-
* Contains ("in")
|
|
74
|
-
* RHS must be a list or a Subnet
|
|
75
|
-
* Regular expression ("=~")
|
|
76
|
-
* RHS must be a regex token
|
|
77
|
-
|
|
78
|
-
Tokens
|
|
79
|
-
~~~~~~~~~~~~~~~
|
|
80
|
-
These tokens are used in the LHS or RHS of a statement.
|
|
81
|
-
|
|
82
|
-
Regex
|
|
83
|
-
-----
|
|
84
|
-
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
85
|
-
the closing "/" to represent the corresponding regex flags. Examples::
|
|
86
|
-
|
|
87
|
-
/abc/
|
|
88
|
-
/^abc/i
|
|
89
|
-
/^abc$/ms
|
|
90
|
-
|
|
91
|
-
Subnet
|
|
92
|
-
------
|
|
93
|
-
A subnet is an IPv4 subnet in CIDR notation::
|
|
94
|
-
|
|
95
|
-
127.0.0.0/8
|
|
96
|
-
192.168.0.0/24
|
|
97
|
-
|
|
98
|
-
Symbol
|
|
99
|
-
------
|
|
100
|
-
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
101
|
-
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
102
|
-
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
103
|
-
|
|
104
|
-
Value
|
|
105
|
-
-----
|
|
106
|
-
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
107
|
-
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
108
|
-
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
109
|
-
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
110
|
-
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
111
|
-
is lacking microseconds.
|
|
112
|
-
|
|
113
|
-
List
|
|
114
|
-
----
|
|
115
|
-
A list is two-or-more Value tokens separated by commas.
|
|
116
|
-
|
|
117
|
-
Expression Evaluation
|
|
118
|
-
=====================
|
|
119
|
-
|
|
120
|
-
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
121
|
-
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
122
|
-
this context:
|
|
123
|
-
|
|
124
|
-
.. code-block:: python
|
|
125
|
-
|
|
126
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
127
|
-
|
|
128
|
-
and this expression:
|
|
129
|
-
|
|
130
|
-
.. code-block:: python
|
|
131
|
-
|
|
132
|
-
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
133
|
-
|
|
134
|
-
calling the expression would return True:
|
|
135
|
-
|
|
136
|
-
.. code-block:: python
|
|
137
|
-
|
|
138
|
-
>>> expr(c)
|
|
139
|
-
True
|
|
140
|
-
>>>
|
|
141
|
-
|
|
142
|
-
whereas this expression would return False:
|
|
143
|
-
|
|
144
|
-
.. code-block:: python
|
|
145
|
-
|
|
146
|
-
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
147
|
-
>>> expr(c)
|
|
148
|
-
False
|
|
149
|
-
>>>
|
|
150
|
-
|
|
151
|
-
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
152
|
-
context. For example:
|
|
153
|
-
|
|
154
|
-
.. code-block:: python
|
|
155
|
-
|
|
156
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
157
|
-
>>> expr = Expression.parseString("a")
|
|
158
|
-
>>> expr(c)
|
|
159
|
-
True
|
|
160
|
-
>>> expr = Expression.parseString("d")
|
|
161
|
-
>>> expr(c)
|
|
162
|
-
False
|
|
163
|
-
>>>
|
|
164
|
-
|
|
165
|
-
Dictionary Traversal
|
|
166
|
-
~~~~~~~~~~~~~~~~~~~~
|
|
167
|
-
|
|
168
|
-
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
169
|
-
|
|
170
|
-
.. code-block:: python
|
|
171
|
-
|
|
172
|
-
>>> c = {"a": {"b": {"c": 1}}}
|
|
173
|
-
|
|
174
|
-
This expression will return true:
|
|
175
|
-
|
|
176
|
-
.. code-block:: python
|
|
177
|
-
|
|
178
|
-
>>> expr = Expression.parseString("a.b.c == 1")
|
|
179
|
-
>>> expr(c)
|
|
180
|
-
True
|
|
181
|
-
>>>
|
|
182
|
-
|
|
183
|
-
Platform: UNKNOWN
|
|
184
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
185
|
-
Classifier: Programming Language :: Python :: 3
|
|
186
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|