filtration 2.3.0__tar.gz → 2.3.3__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.3/PKG-INFO +195 -0
- {filtration-2.3.0 → filtration-2.3.3}/README.rst +2 -2
- {filtration-2.3.0 → filtration-2.3.3}/filtration/__init__.py +58 -38
- filtration-2.3.3/filtration.egg-info/PKG-INFO +195 -0
- {filtration-2.3.0 → filtration-2.3.3}/filtration.egg-info/SOURCES.txt +4 -2
- filtration-2.3.3/filtration.egg-info/requires.txt +3 -0
- filtration-2.3.3/pyproject.toml +66 -0
- filtration-2.3.3/tests/test_parser.py +136 -0
- filtration-2.3.3/tests/test_tokens.py +98 -0
- filtration-2.3.0/PKG-INFO +0 -187
- filtration-2.3.0/filtration.egg-info/PKG-INFO +0 -187
- filtration-2.3.0/filtration.egg-info/requires.txt +0 -2
- filtration-2.3.0/setup.py +0 -31
- {filtration-2.3.0 → filtration-2.3.3}/filtration.egg-info/dependency_links.txt +0 -0
- {filtration-2.3.0 → filtration-2.3.3}/filtration.egg-info/top_level.txt +0 -0
- {filtration-2.3.0 → filtration-2.3.3}/setup.cfg +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: filtration
|
|
3
|
+
Version: 2.3.3
|
|
4
|
+
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
+
Author-email: Steve McMaster <mcmaster@hurricanelabs.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/x-rst
|
|
19
|
+
Requires-Dist: pyparsing>=3.0.1
|
|
20
|
+
Requires-Dist: ipcalc
|
|
21
|
+
Requires-Dist: six
|
|
22
|
+
|
|
23
|
+
##########
|
|
24
|
+
Filtration
|
|
25
|
+
##########
|
|
26
|
+
|
|
27
|
+
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
28
|
+
|
|
29
|
+
Features
|
|
30
|
+
========
|
|
31
|
+
- Compatible with Python 3
|
|
32
|
+
- Parses human readable filters
|
|
33
|
+
|
|
34
|
+
Get Filtration
|
|
35
|
+
==============
|
|
36
|
+
|
|
37
|
+
.. code-block:: shell
|
|
38
|
+
|
|
39
|
+
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
40
|
+
|
|
41
|
+
Run the tests
|
|
42
|
+
=============
|
|
43
|
+
With `uv <https://docs.astral.sh/uv/>`_ installed, run:
|
|
44
|
+
|
|
45
|
+
.. code-block:: shell
|
|
46
|
+
|
|
47
|
+
git clone https://github.com/HurricaneLabs/filtration.git
|
|
48
|
+
cd filtration
|
|
49
|
+
uv run pytest
|
|
50
|
+
|
|
51
|
+
########
|
|
52
|
+
Usage
|
|
53
|
+
########
|
|
54
|
+
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
55
|
+
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
56
|
+
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
57
|
+
"Expression Evaluation" for more details.
|
|
58
|
+
|
|
59
|
+
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
60
|
+
when joining statements together to enforce precedence. For example:
|
|
61
|
+
|
|
62
|
+
.. code-block:: python
|
|
63
|
+
|
|
64
|
+
>>> from filtration import Expression
|
|
65
|
+
>>> Expression.parseString("a and b or c")
|
|
66
|
+
((a and b) or c)
|
|
67
|
+
>>> Expression.parseString("a and (b or c)")
|
|
68
|
+
(a and (b or c))
|
|
69
|
+
>>>
|
|
70
|
+
|
|
71
|
+
Filtration can also be used to parse query string syntax. See below for more information.
|
|
72
|
+
|
|
73
|
+
Expression Format
|
|
74
|
+
=================
|
|
75
|
+
|
|
76
|
+
Operators
|
|
77
|
+
~~~~~~~~~
|
|
78
|
+
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
79
|
+
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
80
|
+
|
|
81
|
+
* Equal ("==")
|
|
82
|
+
* Not equal ("!=")
|
|
83
|
+
* Less than ("<")
|
|
84
|
+
* Less than or equal ("<=")
|
|
85
|
+
* Greater than (">")
|
|
86
|
+
* Greater than or equal (">=")
|
|
87
|
+
* Contains ("in")
|
|
88
|
+
* RHS must be a list or a Subnet
|
|
89
|
+
* Regular expression ("=~")
|
|
90
|
+
* RHS must be a regex token
|
|
91
|
+
|
|
92
|
+
Tokens
|
|
93
|
+
~~~~~~~~~~~~~~~
|
|
94
|
+
These tokens are used in the LHS or RHS of a statement.
|
|
95
|
+
|
|
96
|
+
Regex
|
|
97
|
+
-----
|
|
98
|
+
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
99
|
+
the closing "/" to represent the corresponding regex flags. Examples::
|
|
100
|
+
|
|
101
|
+
/abc/
|
|
102
|
+
/^abc/i
|
|
103
|
+
/^abc$/ms
|
|
104
|
+
|
|
105
|
+
Subnet
|
|
106
|
+
------
|
|
107
|
+
A subnet is an IPv4 subnet in CIDR notation::
|
|
108
|
+
|
|
109
|
+
127.0.0.0/8
|
|
110
|
+
192.168.0.0/24
|
|
111
|
+
|
|
112
|
+
Symbol
|
|
113
|
+
------
|
|
114
|
+
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
115
|
+
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
116
|
+
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
117
|
+
|
|
118
|
+
Value
|
|
119
|
+
-----
|
|
120
|
+
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
121
|
+
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
122
|
+
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
123
|
+
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
124
|
+
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
125
|
+
is lacking microseconds.
|
|
126
|
+
|
|
127
|
+
List
|
|
128
|
+
----
|
|
129
|
+
A list is two-or-more Value tokens separated by commas.
|
|
130
|
+
|
|
131
|
+
Expression Evaluation
|
|
132
|
+
=====================
|
|
133
|
+
|
|
134
|
+
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
135
|
+
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
136
|
+
this context:
|
|
137
|
+
|
|
138
|
+
.. code-block:: python
|
|
139
|
+
|
|
140
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
141
|
+
|
|
142
|
+
and this expression:
|
|
143
|
+
|
|
144
|
+
.. code-block:: python
|
|
145
|
+
|
|
146
|
+
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
147
|
+
|
|
148
|
+
calling the expression would return True:
|
|
149
|
+
|
|
150
|
+
.. code-block:: python
|
|
151
|
+
|
|
152
|
+
>>> expr(c)
|
|
153
|
+
True
|
|
154
|
+
>>>
|
|
155
|
+
|
|
156
|
+
whereas this expression would return False:
|
|
157
|
+
|
|
158
|
+
.. code-block:: python
|
|
159
|
+
|
|
160
|
+
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
161
|
+
>>> expr(c)
|
|
162
|
+
False
|
|
163
|
+
>>>
|
|
164
|
+
|
|
165
|
+
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
166
|
+
context. For example:
|
|
167
|
+
|
|
168
|
+
.. code-block:: python
|
|
169
|
+
|
|
170
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
171
|
+
>>> expr = Expression.parseString("a")
|
|
172
|
+
>>> expr(c)
|
|
173
|
+
True
|
|
174
|
+
>>> expr = Expression.parseString("d")
|
|
175
|
+
>>> expr(c)
|
|
176
|
+
False
|
|
177
|
+
>>>
|
|
178
|
+
|
|
179
|
+
Dictionary Traversal
|
|
180
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
181
|
+
|
|
182
|
+
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
183
|
+
|
|
184
|
+
.. code-block:: python
|
|
185
|
+
|
|
186
|
+
>>> c = {"a": {"b": {"c": 1}}}
|
|
187
|
+
|
|
188
|
+
This expression will return true:
|
|
189
|
+
|
|
190
|
+
.. code-block:: python
|
|
191
|
+
|
|
192
|
+
>>> expr = Expression.parseString("a.b.c == 1")
|
|
193
|
+
>>> expr(c)
|
|
194
|
+
True
|
|
195
|
+
>>>
|
|
@@ -18,13 +18,13 @@ Get Filtration
|
|
|
18
18
|
|
|
19
19
|
Run the tests
|
|
20
20
|
=============
|
|
21
|
-
|
|
21
|
+
With `uv <https://docs.astral.sh/uv/>`_ installed, run:
|
|
22
22
|
|
|
23
23
|
.. code-block:: shell
|
|
24
24
|
|
|
25
25
|
git clone https://github.com/HurricaneLabs/filtration.git
|
|
26
26
|
cd filtration
|
|
27
|
-
|
|
27
|
+
uv run pytest
|
|
28
28
|
|
|
29
29
|
########
|
|
30
30
|
Usage
|
|
@@ -4,17 +4,34 @@ import re
|
|
|
4
4
|
from functools import reduce
|
|
5
5
|
|
|
6
6
|
import ipcalc
|
|
7
|
-
from pyparsing import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
from pyparsing import (
|
|
8
|
+
CaselessLiteral,
|
|
9
|
+
Combine,
|
|
10
|
+
Group,
|
|
11
|
+
Keyword,
|
|
12
|
+
Literal,
|
|
13
|
+
MatchFirst,
|
|
14
|
+
OneOrMore,
|
|
15
|
+
OpAssoc,
|
|
16
|
+
Optional,
|
|
17
|
+
ParseException,
|
|
18
|
+
QuotedString,
|
|
19
|
+
Suppress,
|
|
20
|
+
Word,
|
|
21
|
+
ZeroOrMore,
|
|
22
|
+
alphas,
|
|
23
|
+
infix_notation,
|
|
24
|
+
nums,
|
|
25
|
+
quoted_string,
|
|
26
|
+
remove_quotes,
|
|
27
|
+
)
|
|
11
28
|
|
|
12
29
|
|
|
13
30
|
class Token:
|
|
14
31
|
def __init__(self, toks):
|
|
15
32
|
compiler = getattr(self, "compiler", None)
|
|
16
33
|
if callable(compiler):
|
|
17
|
-
self.value = compiler(*toks)
|
|
34
|
+
self.value = compiler(*toks)
|
|
18
35
|
else:
|
|
19
36
|
try:
|
|
20
37
|
self.value = toks[0]
|
|
@@ -22,7 +39,6 @@ class Token:
|
|
|
22
39
|
self.value = toks
|
|
23
40
|
|
|
24
41
|
def __call__(self, ctx):
|
|
25
|
-
# pylint: disable=unused-argument
|
|
26
42
|
return self.value
|
|
27
43
|
|
|
28
44
|
|
|
@@ -47,29 +63,25 @@ DateTime = (Date + Suppress(Optional("T")) + Time).set_parse_action(
|
|
|
47
63
|
|
|
48
64
|
class _Regex(Token):
|
|
49
65
|
def compiler(self, pattern, flags):
|
|
50
|
-
# pylint: disable=no-self-use
|
|
51
66
|
try:
|
|
52
67
|
return re.compile(pattern, flags)
|
|
53
68
|
except re.error as exc:
|
|
54
69
|
raise ParseException(str(exc)) from exc
|
|
55
70
|
|
|
56
71
|
|
|
57
|
-
RegexFlag = Word("ims", exact=1).set_parse_action(
|
|
58
|
-
lambda toks: getattr(re, toks[0].upper())
|
|
59
|
-
)
|
|
72
|
+
RegexFlag = Word("ims", exact=1).set_parse_action(lambda toks: getattr(re, toks[0].upper()))
|
|
60
73
|
RegexFlags = ZeroOrMore(RegexFlag).set_parse_action(
|
|
61
74
|
lambda toks: reduce(lambda a, b: a | b, toks, 0)
|
|
62
75
|
)
|
|
63
76
|
Regex = (QuotedString("/") + RegexFlags).set_parse_action(_Regex)
|
|
64
77
|
|
|
65
|
-
Octet = Word(nums, max=3).
|
|
78
|
+
Octet = Word(nums, max=3).add_condition(lambda toks: int(toks[0]) <= 255)
|
|
66
79
|
IpAddr = Combine(Octet + "." + Octet + "." + Octet + "." + Octet)
|
|
67
|
-
Cidr = Word(nums, max=2).
|
|
80
|
+
Cidr = Word(nums, max=2).add_condition(lambda toks: int(toks[0]) <= 32)
|
|
68
81
|
|
|
69
82
|
|
|
70
83
|
class _Subnet(Token):
|
|
71
84
|
def compiler(self, subnet):
|
|
72
|
-
# pylint: disable=no-self-use
|
|
73
85
|
return ipcalc.Network(subnet)
|
|
74
86
|
|
|
75
87
|
|
|
@@ -78,6 +90,11 @@ Subnet = Combine(IpAddr + "/" + Cidr).set_parse_action(_Subnet)
|
|
|
78
90
|
|
|
79
91
|
class _Symbol(Token):
|
|
80
92
|
def __call__(self, ctx):
|
|
93
|
+
# If symbol is a literal 'a.b' and exists in ctx, return it
|
|
94
|
+
# Imperfect bugfix to allow for fields with literal dots in the root of ctx
|
|
95
|
+
if self.value in ctx:
|
|
96
|
+
return ctx[self.value]
|
|
97
|
+
|
|
81
98
|
parts = self.value.split(".")
|
|
82
99
|
value = ctx
|
|
83
100
|
for part in parts:
|
|
@@ -89,16 +106,18 @@ class _Symbol(Token):
|
|
|
89
106
|
return value
|
|
90
107
|
|
|
91
108
|
|
|
92
|
-
Symbol = Word((alphas + "_"),
|
|
109
|
+
Symbol = Word((alphas + "_"), body_chars=(alphas + nums + "." + "_")).set_parse_action(_Symbol)
|
|
93
110
|
|
|
94
111
|
|
|
95
|
-
Value = MatchFirst(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
112
|
+
Value = MatchFirst(
|
|
113
|
+
[
|
|
114
|
+
DateTime,
|
|
115
|
+
Date,
|
|
116
|
+
Time,
|
|
117
|
+
quoted_string.set_parse_action(remove_quotes),
|
|
118
|
+
Word(nums).set_parse_action(lambda toks: int(toks[0])),
|
|
119
|
+
]
|
|
120
|
+
).set_parse_action(Token)
|
|
102
121
|
|
|
103
122
|
|
|
104
123
|
class _List(Token):
|
|
@@ -114,6 +133,10 @@ def in_op(lhs, rhs):
|
|
|
114
133
|
return False
|
|
115
134
|
|
|
116
135
|
try:
|
|
136
|
+
# If lhs is a list, we need to call ipcalc's contains() function for each item
|
|
137
|
+
if isinstance(lhs, list) and isinstance(rhs, ipcalc.Network):
|
|
138
|
+
return any([operator.contains(rhs, list_item) for list_item in lhs])
|
|
139
|
+
|
|
117
140
|
return operator.contains(rhs, lhs)
|
|
118
141
|
except ValueError:
|
|
119
142
|
return False
|
|
@@ -130,21 +153,22 @@ def re_op(lhs, rhs):
|
|
|
130
153
|
|
|
131
154
|
|
|
132
155
|
# Operators
|
|
133
|
-
ComparisonOp = MatchFirst(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
156
|
+
ComparisonOp = MatchFirst(
|
|
157
|
+
[
|
|
158
|
+
Literal("==").set_parse_action(lambda toks: operator.eq),
|
|
159
|
+
Literal("!=").set_parse_action(lambda toks: operator.ne),
|
|
160
|
+
Literal("<=").set_parse_action(lambda toks: operator.le),
|
|
161
|
+
Literal("<").set_parse_action(lambda toks: operator.lt),
|
|
162
|
+
Literal(">=").set_parse_action(lambda toks: operator.ge),
|
|
163
|
+
Literal(">").set_parse_action(lambda toks: operator.gt),
|
|
164
|
+
Keyword("in").set_parse_action(lambda toks: in_op),
|
|
165
|
+
Literal("=~").set_parse_action(lambda toks: re_op),
|
|
166
|
+
]
|
|
167
|
+
)
|
|
143
168
|
|
|
144
169
|
|
|
145
170
|
class _Not(Token):
|
|
146
171
|
def compiler(self, *toks):
|
|
147
|
-
# pylint: disable=no-self-use
|
|
148
172
|
return toks[0][1]
|
|
149
173
|
|
|
150
174
|
def __call__(self, ctx):
|
|
@@ -153,7 +177,6 @@ class _Not(Token):
|
|
|
153
177
|
|
|
154
178
|
class _And(Token):
|
|
155
179
|
def compiler(self, *toks):
|
|
156
|
-
# pylint: disable=no-self-use
|
|
157
180
|
return toks[0][0::2]
|
|
158
181
|
|
|
159
182
|
def __call__(self, ctx):
|
|
@@ -162,7 +185,6 @@ class _And(Token):
|
|
|
162
185
|
|
|
163
186
|
class _Or(Token):
|
|
164
187
|
def compiler(self, *toks):
|
|
165
|
-
# pylint: disable=no-self-use
|
|
166
188
|
return toks[0][0::2]
|
|
167
189
|
|
|
168
190
|
def __call__(self, ctx):
|
|
@@ -182,7 +204,7 @@ RHS = Symbol | Subnet | List | Value | Regex
|
|
|
182
204
|
class _Statement:
|
|
183
205
|
def __init__(self, lhs, op=None, rhs=None):
|
|
184
206
|
self.lhs = lhs
|
|
185
|
-
self.op = op
|
|
207
|
+
self.op = op
|
|
186
208
|
self.rhs = rhs
|
|
187
209
|
|
|
188
210
|
def __call__(self, ctx):
|
|
@@ -192,9 +214,7 @@ class _Statement:
|
|
|
192
214
|
return self.op(self.lhs(ctx), self.rhs(ctx))
|
|
193
215
|
|
|
194
216
|
|
|
195
|
-
Statement = (LHS + Optional(ComparisonOp + RHS)).set_parse_action(
|
|
196
|
-
lambda toks: _Statement(*toks)
|
|
197
|
-
)
|
|
217
|
+
Statement = (LHS + Optional(ComparisonOp + RHS)).set_parse_action(lambda toks: _Statement(*toks))
|
|
198
218
|
|
|
199
219
|
|
|
200
220
|
class Expression:
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: filtration
|
|
3
|
+
Version: 2.3.3
|
|
4
|
+
Summary: filtration - A library for parsing arbitrary filters
|
|
5
|
+
Author-email: Steve McMaster <mcmaster@hurricanelabs.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/x-rst
|
|
19
|
+
Requires-Dist: pyparsing>=3.0.1
|
|
20
|
+
Requires-Dist: ipcalc
|
|
21
|
+
Requires-Dist: six
|
|
22
|
+
|
|
23
|
+
##########
|
|
24
|
+
Filtration
|
|
25
|
+
##########
|
|
26
|
+
|
|
27
|
+
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
28
|
+
|
|
29
|
+
Features
|
|
30
|
+
========
|
|
31
|
+
- Compatible with Python 3
|
|
32
|
+
- Parses human readable filters
|
|
33
|
+
|
|
34
|
+
Get Filtration
|
|
35
|
+
==============
|
|
36
|
+
|
|
37
|
+
.. code-block:: shell
|
|
38
|
+
|
|
39
|
+
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
40
|
+
|
|
41
|
+
Run the tests
|
|
42
|
+
=============
|
|
43
|
+
With `uv <https://docs.astral.sh/uv/>`_ installed, run:
|
|
44
|
+
|
|
45
|
+
.. code-block:: shell
|
|
46
|
+
|
|
47
|
+
git clone https://github.com/HurricaneLabs/filtration.git
|
|
48
|
+
cd filtration
|
|
49
|
+
uv run pytest
|
|
50
|
+
|
|
51
|
+
########
|
|
52
|
+
Usage
|
|
53
|
+
########
|
|
54
|
+
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
55
|
+
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
56
|
+
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
57
|
+
"Expression Evaluation" for more details.
|
|
58
|
+
|
|
59
|
+
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
60
|
+
when joining statements together to enforce precedence. For example:
|
|
61
|
+
|
|
62
|
+
.. code-block:: python
|
|
63
|
+
|
|
64
|
+
>>> from filtration import Expression
|
|
65
|
+
>>> Expression.parseString("a and b or c")
|
|
66
|
+
((a and b) or c)
|
|
67
|
+
>>> Expression.parseString("a and (b or c)")
|
|
68
|
+
(a and (b or c))
|
|
69
|
+
>>>
|
|
70
|
+
|
|
71
|
+
Filtration can also be used to parse query string syntax. See below for more information.
|
|
72
|
+
|
|
73
|
+
Expression Format
|
|
74
|
+
=================
|
|
75
|
+
|
|
76
|
+
Operators
|
|
77
|
+
~~~~~~~~~
|
|
78
|
+
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
79
|
+
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
80
|
+
|
|
81
|
+
* Equal ("==")
|
|
82
|
+
* Not equal ("!=")
|
|
83
|
+
* Less than ("<")
|
|
84
|
+
* Less than or equal ("<=")
|
|
85
|
+
* Greater than (">")
|
|
86
|
+
* Greater than or equal (">=")
|
|
87
|
+
* Contains ("in")
|
|
88
|
+
* RHS must be a list or a Subnet
|
|
89
|
+
* Regular expression ("=~")
|
|
90
|
+
* RHS must be a regex token
|
|
91
|
+
|
|
92
|
+
Tokens
|
|
93
|
+
~~~~~~~~~~~~~~~
|
|
94
|
+
These tokens are used in the LHS or RHS of a statement.
|
|
95
|
+
|
|
96
|
+
Regex
|
|
97
|
+
-----
|
|
98
|
+
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
99
|
+
the closing "/" to represent the corresponding regex flags. Examples::
|
|
100
|
+
|
|
101
|
+
/abc/
|
|
102
|
+
/^abc/i
|
|
103
|
+
/^abc$/ms
|
|
104
|
+
|
|
105
|
+
Subnet
|
|
106
|
+
------
|
|
107
|
+
A subnet is an IPv4 subnet in CIDR notation::
|
|
108
|
+
|
|
109
|
+
127.0.0.0/8
|
|
110
|
+
192.168.0.0/24
|
|
111
|
+
|
|
112
|
+
Symbol
|
|
113
|
+
------
|
|
114
|
+
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
115
|
+
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
116
|
+
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
117
|
+
|
|
118
|
+
Value
|
|
119
|
+
-----
|
|
120
|
+
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
121
|
+
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
122
|
+
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
123
|
+
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
124
|
+
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
125
|
+
is lacking microseconds.
|
|
126
|
+
|
|
127
|
+
List
|
|
128
|
+
----
|
|
129
|
+
A list is two-or-more Value tokens separated by commas.
|
|
130
|
+
|
|
131
|
+
Expression Evaluation
|
|
132
|
+
=====================
|
|
133
|
+
|
|
134
|
+
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
135
|
+
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
136
|
+
this context:
|
|
137
|
+
|
|
138
|
+
.. code-block:: python
|
|
139
|
+
|
|
140
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
141
|
+
|
|
142
|
+
and this expression:
|
|
143
|
+
|
|
144
|
+
.. code-block:: python
|
|
145
|
+
|
|
146
|
+
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
147
|
+
|
|
148
|
+
calling the expression would return True:
|
|
149
|
+
|
|
150
|
+
.. code-block:: python
|
|
151
|
+
|
|
152
|
+
>>> expr(c)
|
|
153
|
+
True
|
|
154
|
+
>>>
|
|
155
|
+
|
|
156
|
+
whereas this expression would return False:
|
|
157
|
+
|
|
158
|
+
.. code-block:: python
|
|
159
|
+
|
|
160
|
+
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
161
|
+
>>> expr(c)
|
|
162
|
+
False
|
|
163
|
+
>>>
|
|
164
|
+
|
|
165
|
+
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
166
|
+
context. For example:
|
|
167
|
+
|
|
168
|
+
.. code-block:: python
|
|
169
|
+
|
|
170
|
+
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
171
|
+
>>> expr = Expression.parseString("a")
|
|
172
|
+
>>> expr(c)
|
|
173
|
+
True
|
|
174
|
+
>>> expr = Expression.parseString("d")
|
|
175
|
+
>>> expr(c)
|
|
176
|
+
False
|
|
177
|
+
>>>
|
|
178
|
+
|
|
179
|
+
Dictionary Traversal
|
|
180
|
+
~~~~~~~~~~~~~~~~~~~~
|
|
181
|
+
|
|
182
|
+
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
183
|
+
|
|
184
|
+
.. code-block:: python
|
|
185
|
+
|
|
186
|
+
>>> c = {"a": {"b": {"c": 1}}}
|
|
187
|
+
|
|
188
|
+
This expression will return true:
|
|
189
|
+
|
|
190
|
+
.. code-block:: python
|
|
191
|
+
|
|
192
|
+
>>> expr = Expression.parseString("a.b.c == 1")
|
|
193
|
+
>>> expr(c)
|
|
194
|
+
True
|
|
195
|
+
>>>
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
README.rst
|
|
2
|
-
|
|
2
|
+
pyproject.toml
|
|
3
3
|
filtration/__init__.py
|
|
4
4
|
filtration.egg-info/PKG-INFO
|
|
5
5
|
filtration.egg-info/SOURCES.txt
|
|
6
6
|
filtration.egg-info/dependency_links.txt
|
|
7
7
|
filtration.egg-info/requires.txt
|
|
8
|
-
filtration.egg-info/top_level.txt
|
|
8
|
+
filtration.egg-info/top_level.txt
|
|
9
|
+
tests/test_parser.py
|
|
10
|
+
tests/test_tokens.py
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "filtration"
|
|
7
|
+
version = "2.3.3"
|
|
8
|
+
description = "filtration - A library for parsing arbitrary filters"
|
|
9
|
+
readme = "README.rst"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
authors = [{ name = "Steve McMaster", email = "mcmaster@hurricanelabs.com" }]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
dependencies = [
|
|
14
|
+
# 3.0.0 can't parse subnets; 3.0.1 fixed it
|
|
15
|
+
"pyparsing>=3.0.1",
|
|
16
|
+
"ipcalc",
|
|
17
|
+
# ipcalc imports six without declaring it
|
|
18
|
+
"six",
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 5 - Production/Stable",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
24
|
+
"Programming Language :: Python :: 3.9",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
"Bug Tracker" = "https://github.com/HurricaneLabs/filtration/issues"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools]
|
|
36
|
+
packages = ["filtration"]
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
"pytest>=8",
|
|
41
|
+
"pytest-cov>=5",
|
|
42
|
+
# pinned so new ruff releases can't fail CI without a code change
|
|
43
|
+
"ruff==0.17.5",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
addopts = "--strict-markers -ra"
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
filterwarnings = ["error"]
|
|
50
|
+
|
|
51
|
+
[tool.coverage.run]
|
|
52
|
+
branch = true
|
|
53
|
+
source = ["filtration"]
|
|
54
|
+
|
|
55
|
+
[tool.coverage.report]
|
|
56
|
+
show_missing = true
|
|
57
|
+
|
|
58
|
+
[tool.ruff]
|
|
59
|
+
line-length = 99
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint]
|
|
62
|
+
select = ["E", "W", "F", "I", "B", "UP", "S"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff.lint.per-file-ignores]
|
|
65
|
+
# bandit only ever scanned filtration/; tests are full of asserts
|
|
66
|
+
"tests/*" = ["S"]
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from pyparsing import ParseException
|
|
3
|
+
|
|
4
|
+
from filtration import Expression, List, Regex, Statement, Subnet, Symbol, Value
|
|
5
|
+
|
|
6
|
+
SYMBOLS = ["abcXYZ", "_abcXYZ", "abc.XYZ", "_abc.XYZ"]
|
|
7
|
+
NOT_SYMBOLS = ["'abcXYZ'", '"abcXYZ"', "12345"]
|
|
8
|
+
|
|
9
|
+
VALUES = [
|
|
10
|
+
"'abcXYZ'",
|
|
11
|
+
'"abcXYZ"',
|
|
12
|
+
"12345",
|
|
13
|
+
"2015-03-01",
|
|
14
|
+
"12:34:56",
|
|
15
|
+
"2015-03-01T12:34:56",
|
|
16
|
+
]
|
|
17
|
+
NOT_VALUES = ["abcXYZ", "_abcXYZ", "abc.XYZ", "_abc.XYZ"]
|
|
18
|
+
|
|
19
|
+
REGEXES = ["/regex/", "/regex/i", "/regex/im"]
|
|
20
|
+
NOT_REGEXES = ["/not[a-valid/", "/not(a-valid/", "/not-a-valid)", "'regex'"]
|
|
21
|
+
|
|
22
|
+
SUBNETS = [
|
|
23
|
+
"0.0.0.0/0",
|
|
24
|
+
"0.0.0.0/8",
|
|
25
|
+
"172.16.0.0/8",
|
|
26
|
+
"192.168.0.0/16",
|
|
27
|
+
"192.168.100.0/24",
|
|
28
|
+
"255.255.255.255/32",
|
|
29
|
+
]
|
|
30
|
+
NOT_SUBNETS = ["0.0.0.0", "0.0.0.0/33", "256.256.256.256/0", "256.256.256.256/32"]
|
|
31
|
+
|
|
32
|
+
TERMS = [
|
|
33
|
+
"symbol1",
|
|
34
|
+
"symbol1 == symbol2",
|
|
35
|
+
"symbol1 == 'value1'",
|
|
36
|
+
"symbol1 =~ /regex1/",
|
|
37
|
+
"symbol1 in 10.0.0.0/8",
|
|
38
|
+
"symbol1 in 'list','list','list'",
|
|
39
|
+
"symbol1 in symbol1",
|
|
40
|
+
"'value1' in symbol1",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
EXPRESSION_FORMATS = [
|
|
44
|
+
"{0}",
|
|
45
|
+
"not {0}",
|
|
46
|
+
"({0})",
|
|
47
|
+
"not ({0})",
|
|
48
|
+
"{0} and {0}",
|
|
49
|
+
"{0} or {0}",
|
|
50
|
+
"{0} and {0} or {0}",
|
|
51
|
+
"{0} and ({0} or {0})",
|
|
52
|
+
"{0} or {0} and {0}",
|
|
53
|
+
"({0} or {0}) and {0}",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@pytest.mark.parametrize("expr", SYMBOLS)
|
|
58
|
+
def test_parse_symbol(expr: str) -> None:
|
|
59
|
+
"""A symbol starts with a letter or underscore, then letters, digits, dots or underscores."""
|
|
60
|
+
Symbol.parse_string(expr, parse_all=True)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@pytest.mark.parametrize("expr", NOT_SYMBOLS)
|
|
64
|
+
def test_parse_symbol_rejects(expr: str) -> None:
|
|
65
|
+
"""Quoted strings and bare numbers are not symbols."""
|
|
66
|
+
with pytest.raises(ParseException):
|
|
67
|
+
Symbol.parse_string(expr)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@pytest.mark.parametrize("expr", VALUES)
|
|
71
|
+
def test_parse_value(expr: str) -> None:
|
|
72
|
+
"""A value is a quoted string, a number, a date, a time or a date/time."""
|
|
73
|
+
Value.parse_string(expr, parse_all=True)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.parametrize("expr", NOT_VALUES)
|
|
77
|
+
def test_parse_value_rejects(expr: str) -> None:
|
|
78
|
+
"""Bare words are symbols, not values."""
|
|
79
|
+
with pytest.raises(ParseException):
|
|
80
|
+
Value.parse_string(expr)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@pytest.mark.parametrize("expr", REGEXES)
|
|
84
|
+
def test_parse_regex(expr: str) -> None:
|
|
85
|
+
"""A regex is wrapped in / with optional i, m or s flags after it."""
|
|
86
|
+
Regex.parse_string(expr, parse_all=True)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@pytest.mark.parametrize("expr", NOT_REGEXES)
|
|
90
|
+
def test_parse_regex_rejects(expr: str) -> None:
|
|
91
|
+
"""Invalid patterns and unslashed strings are not regexes."""
|
|
92
|
+
with pytest.raises(ParseException):
|
|
93
|
+
Regex.parse_string(expr)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@pytest.mark.parametrize("expr", SUBNETS)
|
|
97
|
+
def test_parse_subnet(expr: str) -> None:
|
|
98
|
+
"""Subnets are IPv4 CIDR notation."""
|
|
99
|
+
Subnet.parse_string(expr, parse_all=True)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@pytest.mark.parametrize("expr", NOT_SUBNETS)
|
|
103
|
+
def test_parse_subnet_rejects(expr: str) -> None:
|
|
104
|
+
"""A bare IP, a prefix over 32 or an octet over 255 is not a subnet."""
|
|
105
|
+
with pytest.raises(ParseException):
|
|
106
|
+
Subnet.parse_string(expr)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@pytest.mark.parametrize("length", range(2, 10))
|
|
110
|
+
def test_parse_valuelist(length: int) -> None:
|
|
111
|
+
"""A list is two or more values separated by commas."""
|
|
112
|
+
List.parse_string(",".join(["'value'"] * length), parse_all=True)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_parse_valuelist_rejects_single_value() -> None:
|
|
116
|
+
"""One value on its own is not a list."""
|
|
117
|
+
with pytest.raises(ParseException):
|
|
118
|
+
List.parse_string("'value'")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@pytest.mark.parametrize("expr", TERMS)
|
|
122
|
+
def test_parse_statement(expr: str) -> None:
|
|
123
|
+
"""Each statement form parses."""
|
|
124
|
+
Statement.parse_string(expr, parse_all=True)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.mark.parametrize("term", TERMS)
|
|
128
|
+
@pytest.mark.parametrize("fmt", EXPRESSION_FORMATS)
|
|
129
|
+
def test_parse_expression(fmt: str, term: str) -> None:
|
|
130
|
+
"""Statements combine with not, and, or and parentheses."""
|
|
131
|
+
Expression.parse_string(fmt.format(term), parse_all=True)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_parse_string_camelcase_alias() -> None:
|
|
135
|
+
"""Expression.parseString stays available; libhurricane and Tempest call it."""
|
|
136
|
+
assert Expression.parseString == Expression.parse_string
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
import re
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from filtration import Expression, Regex, Subnet, Symbol, Value
|
|
10
|
+
|
|
11
|
+
REGEX_EVALUATE = [
|
|
12
|
+
("/abc/", re.compile("abc")),
|
|
13
|
+
("/abc/i", re.compile("abc", flags=re.IGNORECASE)),
|
|
14
|
+
("/abc/m", re.compile("abc", flags=re.MULTILINE)),
|
|
15
|
+
("/abc/s", re.compile("abc", flags=re.DOTALL)),
|
|
16
|
+
("/abc/ims", re.compile("abc", flags=re.IGNORECASE | re.MULTILINE | re.DOTALL)),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
# (expression that should be true, expression that should be false)
|
|
20
|
+
EXPRESSION_EVALUATE = [
|
|
21
|
+
("symbol_true", "symbol_false"),
|
|
22
|
+
("symbol_true and symbol_true", "symbol_true and symbol_false"),
|
|
23
|
+
("symbol_true or symbol_false", "symbol_false or symbol_false"),
|
|
24
|
+
("not symbol_false", "not symbol_true"),
|
|
25
|
+
("dict.symbol_true", "dict.doesnt_exist"),
|
|
26
|
+
("symbol_int == 1", "symbol_int == 2"),
|
|
27
|
+
("symbol_int != 2", "symbol_int != 1"),
|
|
28
|
+
("symbol_int < 2", "symbol_int < 1"),
|
|
29
|
+
("symbol_int <= 1", "symbol_int <= 0"),
|
|
30
|
+
("symbol_int > 0", "symbol_int > 1"),
|
|
31
|
+
("symbol_int >= 1", "symbol_int >= 2"),
|
|
32
|
+
("symbol_str in 'a','b'", "symbol_str in 'c','d'"),
|
|
33
|
+
(None, "'a' in nonexistant"),
|
|
34
|
+
("symbol_str =~ /^a/", "symbol_str =~ /c$/"),
|
|
35
|
+
("symbol_list =~ /cde/", "symbol_list =~ /xyz/"),
|
|
36
|
+
(None, "nonexistant =~ /a/"),
|
|
37
|
+
("symbol_dt > 2015-03-01", "symbol_dt < 2000-01-01"),
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@pytest.fixture
|
|
42
|
+
def context() -> dict[str, Any]:
|
|
43
|
+
"""An evaluation context with one value of each type the grammar compares."""
|
|
44
|
+
ctx: dict[str, Any] = {
|
|
45
|
+
"symbol_true": True,
|
|
46
|
+
"symbol_false": False,
|
|
47
|
+
"symbol_int": 1,
|
|
48
|
+
"symbol_str": "a",
|
|
49
|
+
"symbol_dt": datetime.datetime.now(),
|
|
50
|
+
"symbol_list": ["abcde", "cdefg"],
|
|
51
|
+
}
|
|
52
|
+
ctx["dict"] = ctx
|
|
53
|
+
return ctx
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_evaluate_symbol() -> None:
|
|
57
|
+
"""A symbol evaluates to its value in the context."""
|
|
58
|
+
sym = Symbol.parse_string("symbol", parse_all=True)[0]
|
|
59
|
+
assert sym({"symbol": "value"}) == "value"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_evaluate_value() -> None:
|
|
63
|
+
"""A quoted value evaluates to the string without quotes."""
|
|
64
|
+
val = Value.parse_string("'value'", parse_all=True)[0]
|
|
65
|
+
assert val({}) == "value"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@pytest.mark.parametrize(("expr", "expected"), REGEX_EVALUATE)
|
|
69
|
+
def test_evaluate_regex(expr: str, expected: re.Pattern) -> None:
|
|
70
|
+
"""A regex evaluates to a compiled pattern with the flags given."""
|
|
71
|
+
rex = Regex.parse_string(expr, parse_all=True)[0]
|
|
72
|
+
result = rex({})
|
|
73
|
+
assert result.pattern == expected.pattern
|
|
74
|
+
assert result.flags == expected.flags
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_evaluate_subnet() -> None:
|
|
78
|
+
"""A subnet evaluates to a network object."""
|
|
79
|
+
subnet = Subnet.parse_string("192.168.0.0/16", parse_all=True)[0]
|
|
80
|
+
assert subnet({}).network() == "192.168.0.0"
|
|
81
|
+
assert subnet({}).size() == 2**16
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@pytest.mark.parametrize(("expr_true", "expr_false"), EXPRESSION_EVALUATE)
|
|
85
|
+
def test_evaluate_expression(
|
|
86
|
+
context: dict[str, Any], expr_true: str | None, expr_false: str
|
|
87
|
+
) -> None:
|
|
88
|
+
"""Each operator gives the expected result against the context."""
|
|
89
|
+
if expr_true:
|
|
90
|
+
assert Expression.parse_string(expr_true, parse_all=True)(context)
|
|
91
|
+
assert not Expression.parse_string(expr_false, parse_all=True)(context)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_subnet_contains() -> None:
|
|
95
|
+
"""`in` a subnet matches IPs inside it and rejects non-IPs."""
|
|
96
|
+
expr = Expression.parse_string("src in 127.0.0.0/8", parse_all=True)
|
|
97
|
+
assert expr({"src": "127.0.0.1"})
|
|
98
|
+
assert not expr({"src": "localhost"})
|
filtration-2.3.0/PKG-INFO
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.2
|
|
2
|
-
Name: filtration
|
|
3
|
-
Version: 2.3.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
|
-
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
10
|
-
Description: ##########
|
|
11
|
-
Filtration
|
|
12
|
-
##########
|
|
13
|
-
|
|
14
|
-
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
15
|
-
|
|
16
|
-
Features
|
|
17
|
-
========
|
|
18
|
-
- Compatible with Python 3
|
|
19
|
-
- Parses human readable filters
|
|
20
|
-
|
|
21
|
-
Get Filtration
|
|
22
|
-
==============
|
|
23
|
-
|
|
24
|
-
.. code-block:: shell
|
|
25
|
-
|
|
26
|
-
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
27
|
-
|
|
28
|
-
Run the tests
|
|
29
|
-
=============
|
|
30
|
-
You must install nose2, then run:
|
|
31
|
-
|
|
32
|
-
.. code-block:: shell
|
|
33
|
-
|
|
34
|
-
git clone https://github.com/HurricaneLabs/filtration.git
|
|
35
|
-
cd filtration
|
|
36
|
-
nose2
|
|
37
|
-
|
|
38
|
-
########
|
|
39
|
-
Usage
|
|
40
|
-
########
|
|
41
|
-
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
42
|
-
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
43
|
-
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
44
|
-
"Expression Evaluation" for more details.
|
|
45
|
-
|
|
46
|
-
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
47
|
-
when joining statements together to enforce precedence. For example:
|
|
48
|
-
|
|
49
|
-
.. code-block:: python
|
|
50
|
-
|
|
51
|
-
>>> from filtration import Expression
|
|
52
|
-
>>> Expression.parseString("a and b or c")
|
|
53
|
-
((a and b) or c)
|
|
54
|
-
>>> Expression.parseString("a and (b or c)")
|
|
55
|
-
(a and (b or c))
|
|
56
|
-
>>>
|
|
57
|
-
|
|
58
|
-
Filtration can also be used to parse query string syntax. See below for more information.
|
|
59
|
-
|
|
60
|
-
Expression Format
|
|
61
|
-
=================
|
|
62
|
-
|
|
63
|
-
Operators
|
|
64
|
-
~~~~~~~~~
|
|
65
|
-
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
66
|
-
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
67
|
-
|
|
68
|
-
* Equal ("==")
|
|
69
|
-
* Not equal ("!=")
|
|
70
|
-
* Less than ("<")
|
|
71
|
-
* Less than or equal ("<=")
|
|
72
|
-
* Greater than (">")
|
|
73
|
-
* Greater than or equal (">=")
|
|
74
|
-
* Contains ("in")
|
|
75
|
-
* RHS must be a list or a Subnet
|
|
76
|
-
* Regular expression ("=~")
|
|
77
|
-
* RHS must be a regex token
|
|
78
|
-
|
|
79
|
-
Tokens
|
|
80
|
-
~~~~~~~~~~~~~~~
|
|
81
|
-
These tokens are used in the LHS or RHS of a statement.
|
|
82
|
-
|
|
83
|
-
Regex
|
|
84
|
-
-----
|
|
85
|
-
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
86
|
-
the closing "/" to represent the corresponding regex flags. Examples::
|
|
87
|
-
|
|
88
|
-
/abc/
|
|
89
|
-
/^abc/i
|
|
90
|
-
/^abc$/ms
|
|
91
|
-
|
|
92
|
-
Subnet
|
|
93
|
-
------
|
|
94
|
-
A subnet is an IPv4 subnet in CIDR notation::
|
|
95
|
-
|
|
96
|
-
127.0.0.0/8
|
|
97
|
-
192.168.0.0/24
|
|
98
|
-
|
|
99
|
-
Symbol
|
|
100
|
-
------
|
|
101
|
-
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
102
|
-
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
103
|
-
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
104
|
-
|
|
105
|
-
Value
|
|
106
|
-
-----
|
|
107
|
-
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
108
|
-
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
109
|
-
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
110
|
-
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
111
|
-
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
112
|
-
is lacking microseconds.
|
|
113
|
-
|
|
114
|
-
List
|
|
115
|
-
----
|
|
116
|
-
A list is two-or-more Value tokens separated by commas.
|
|
117
|
-
|
|
118
|
-
Expression Evaluation
|
|
119
|
-
=====================
|
|
120
|
-
|
|
121
|
-
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
122
|
-
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
123
|
-
this context:
|
|
124
|
-
|
|
125
|
-
.. code-block:: python
|
|
126
|
-
|
|
127
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
128
|
-
|
|
129
|
-
and this expression:
|
|
130
|
-
|
|
131
|
-
.. code-block:: python
|
|
132
|
-
|
|
133
|
-
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
134
|
-
|
|
135
|
-
calling the expression would return True:
|
|
136
|
-
|
|
137
|
-
.. code-block:: python
|
|
138
|
-
|
|
139
|
-
>>> expr(c)
|
|
140
|
-
True
|
|
141
|
-
>>>
|
|
142
|
-
|
|
143
|
-
whereas this expression would return False:
|
|
144
|
-
|
|
145
|
-
.. code-block:: python
|
|
146
|
-
|
|
147
|
-
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
148
|
-
>>> expr(c)
|
|
149
|
-
False
|
|
150
|
-
>>>
|
|
151
|
-
|
|
152
|
-
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
153
|
-
context. For example:
|
|
154
|
-
|
|
155
|
-
.. code-block:: python
|
|
156
|
-
|
|
157
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
158
|
-
>>> expr = Expression.parseString("a")
|
|
159
|
-
>>> expr(c)
|
|
160
|
-
True
|
|
161
|
-
>>> expr = Expression.parseString("d")
|
|
162
|
-
>>> expr(c)
|
|
163
|
-
False
|
|
164
|
-
>>>
|
|
165
|
-
|
|
166
|
-
Dictionary Traversal
|
|
167
|
-
~~~~~~~~~~~~~~~~~~~~
|
|
168
|
-
|
|
169
|
-
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
170
|
-
|
|
171
|
-
.. code-block:: python
|
|
172
|
-
|
|
173
|
-
>>> c = {"a": {"b": {"c": 1}}}
|
|
174
|
-
|
|
175
|
-
This expression will return true:
|
|
176
|
-
|
|
177
|
-
.. code-block:: python
|
|
178
|
-
|
|
179
|
-
>>> expr = Expression.parseString("a.b.c == 1")
|
|
180
|
-
>>> expr(c)
|
|
181
|
-
True
|
|
182
|
-
>>>
|
|
183
|
-
|
|
184
|
-
Platform: UNKNOWN
|
|
185
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
186
|
-
Classifier: Programming Language :: Python :: 3
|
|
187
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.2
|
|
2
|
-
Name: filtration
|
|
3
|
-
Version: 2.3.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
|
-
Project-URL: Bug Tracker, https://github.com/HurricaneLabs/filtration/issues
|
|
10
|
-
Description: ##########
|
|
11
|
-
Filtration
|
|
12
|
-
##########
|
|
13
|
-
|
|
14
|
-
**A library for parsing arbitrary filters provided by a UI or as a Query String**
|
|
15
|
-
|
|
16
|
-
Features
|
|
17
|
-
========
|
|
18
|
-
- Compatible with Python 3
|
|
19
|
-
- Parses human readable filters
|
|
20
|
-
|
|
21
|
-
Get Filtration
|
|
22
|
-
==============
|
|
23
|
-
|
|
24
|
-
.. code-block:: shell
|
|
25
|
-
|
|
26
|
-
pip install git+https://github.com/HurricaneLabs/filtration.git
|
|
27
|
-
|
|
28
|
-
Run the tests
|
|
29
|
-
=============
|
|
30
|
-
You must install nose2, then run:
|
|
31
|
-
|
|
32
|
-
.. code-block:: shell
|
|
33
|
-
|
|
34
|
-
git clone https://github.com/HurricaneLabs/filtration.git
|
|
35
|
-
cd filtration
|
|
36
|
-
nose2
|
|
37
|
-
|
|
38
|
-
########
|
|
39
|
-
Usage
|
|
40
|
-
########
|
|
41
|
-
Filtration is used to parse "filter expressions" made up of one or more "statements". Each
|
|
42
|
-
statement is comprised of a "left hand side" (LHS), and optionally an operator and a "right hand
|
|
43
|
-
side" (RHS). When no operator and RHS are specified, a special "LHS exists" syntax is assumed. See
|
|
44
|
-
"Expression Evaluation" for more details.
|
|
45
|
-
|
|
46
|
-
Statements are joined together using "AND" or "OR" to form an expression. Parentheses may be used
|
|
47
|
-
when joining statements together to enforce precedence. For example:
|
|
48
|
-
|
|
49
|
-
.. code-block:: python
|
|
50
|
-
|
|
51
|
-
>>> from filtration import Expression
|
|
52
|
-
>>> Expression.parseString("a and b or c")
|
|
53
|
-
((a and b) or c)
|
|
54
|
-
>>> Expression.parseString("a and (b or c)")
|
|
55
|
-
(a and (b or c))
|
|
56
|
-
>>>
|
|
57
|
-
|
|
58
|
-
Filtration can also be used to parse query string syntax. See below for more information.
|
|
59
|
-
|
|
60
|
-
Expression Format
|
|
61
|
-
=================
|
|
62
|
-
|
|
63
|
-
Operators
|
|
64
|
-
~~~~~~~~~
|
|
65
|
-
These operators control comparison of the LHS to the RHS. If the operator is omitted (and thus
|
|
66
|
-
the RHS is also omitted), the statement returns True IF the symbol in the LHS exists at all.
|
|
67
|
-
|
|
68
|
-
* Equal ("==")
|
|
69
|
-
* Not equal ("!=")
|
|
70
|
-
* Less than ("<")
|
|
71
|
-
* Less than or equal ("<=")
|
|
72
|
-
* Greater than (">")
|
|
73
|
-
* Greater than or equal (">=")
|
|
74
|
-
* Contains ("in")
|
|
75
|
-
* RHS must be a list or a Subnet
|
|
76
|
-
* Regular expression ("=~")
|
|
77
|
-
* RHS must be a regex token
|
|
78
|
-
|
|
79
|
-
Tokens
|
|
80
|
-
~~~~~~~~~~~~~~~
|
|
81
|
-
These tokens are used in the LHS or RHS of a statement.
|
|
82
|
-
|
|
83
|
-
Regex
|
|
84
|
-
-----
|
|
85
|
-
A regular expression must be wrapped in "/" and may contain the characters "i", "m" or "s" after
|
|
86
|
-
the closing "/" to represent the corresponding regex flags. Examples::
|
|
87
|
-
|
|
88
|
-
/abc/
|
|
89
|
-
/^abc/i
|
|
90
|
-
/^abc$/ms
|
|
91
|
-
|
|
92
|
-
Subnet
|
|
93
|
-
------
|
|
94
|
-
A subnet is an IPv4 subnet in CIDR notation::
|
|
95
|
-
|
|
96
|
-
127.0.0.0/8
|
|
97
|
-
192.168.0.0/24
|
|
98
|
-
|
|
99
|
-
Symbol
|
|
100
|
-
------
|
|
101
|
-
A symbol is represented as a bare, unquoted string. It begins with a letter or underscore, and
|
|
102
|
-
can be followed by any number of letters, numbers, dots (".") or underscores. Dots have special
|
|
103
|
-
meaning and are used to indicate dictionary traversal (see Expression Evaluation below).
|
|
104
|
-
|
|
105
|
-
Value
|
|
106
|
-
-----
|
|
107
|
-
A value can be a date, time, date/time, quoted string, or an integer. A date is represented in
|
|
108
|
-
YYYY-MM-DD format (not quoted), and is interpreted to mean midnight on that date. A time is
|
|
109
|
-
represented in HH:MM:SS format (also not quoted), and is interpreted to mean that time on whatever
|
|
110
|
-
day the filter is evaluated. A date/time is represented in YYYY-MM-DDTHH:MM:SS format, where the
|
|
111
|
-
"T" is optional and could instead be represented by a space. This format is roughly ISO 8601, but
|
|
112
|
-
is lacking microseconds.
|
|
113
|
-
|
|
114
|
-
List
|
|
115
|
-
----
|
|
116
|
-
A list is two-or-more Value tokens separated by commas.
|
|
117
|
-
|
|
118
|
-
Expression Evaluation
|
|
119
|
-
=====================
|
|
120
|
-
|
|
121
|
-
Expression objects are callable, with a single "context" argument. When used in this way, either
|
|
122
|
-
true or false is returned, based on whether the "context" matches the filter. For example, given
|
|
123
|
-
this context:
|
|
124
|
-
|
|
125
|
-
.. code-block:: python
|
|
126
|
-
|
|
127
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
128
|
-
|
|
129
|
-
and this expression:
|
|
130
|
-
|
|
131
|
-
.. code-block:: python
|
|
132
|
-
|
|
133
|
-
>>> expr = Expression.parseString("a == 1 and b == 2")
|
|
134
|
-
|
|
135
|
-
calling the expression would return True:
|
|
136
|
-
|
|
137
|
-
.. code-block:: python
|
|
138
|
-
|
|
139
|
-
>>> expr(c)
|
|
140
|
-
True
|
|
141
|
-
>>>
|
|
142
|
-
|
|
143
|
-
whereas this expression would return False:
|
|
144
|
-
|
|
145
|
-
.. code-block:: python
|
|
146
|
-
|
|
147
|
-
>>> expr = Expression.parseString("a == 2 and b == 2")
|
|
148
|
-
>>> expr(c)
|
|
149
|
-
False
|
|
150
|
-
>>>
|
|
151
|
-
|
|
152
|
-
When no operator/RHS is provided, the statement resolves to true if the LHS key exists in the
|
|
153
|
-
context. For example:
|
|
154
|
-
|
|
155
|
-
.. code-block:: python
|
|
156
|
-
|
|
157
|
-
>>> c = {"a": 1, "b": 2, "c": 3}
|
|
158
|
-
>>> expr = Expression.parseString("a")
|
|
159
|
-
>>> expr(c)
|
|
160
|
-
True
|
|
161
|
-
>>> expr = Expression.parseString("d")
|
|
162
|
-
>>> expr(c)
|
|
163
|
-
False
|
|
164
|
-
>>>
|
|
165
|
-
|
|
166
|
-
Dictionary Traversal
|
|
167
|
-
~~~~~~~~~~~~~~~~~~~~
|
|
168
|
-
|
|
169
|
-
Symbols are able to traverse nested dictionaries in the context. Given this context:
|
|
170
|
-
|
|
171
|
-
.. code-block:: python
|
|
172
|
-
|
|
173
|
-
>>> c = {"a": {"b": {"c": 1}}}
|
|
174
|
-
|
|
175
|
-
This expression will return true:
|
|
176
|
-
|
|
177
|
-
.. code-block:: python
|
|
178
|
-
|
|
179
|
-
>>> expr = Expression.parseString("a.b.c == 1")
|
|
180
|
-
>>> expr(c)
|
|
181
|
-
True
|
|
182
|
-
>>>
|
|
183
|
-
|
|
184
|
-
Platform: UNKNOWN
|
|
185
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
186
|
-
Classifier: Programming Language :: Python :: 3
|
|
187
|
-
Classifier: Development Status :: 5 - Production/Stable
|
filtration-2.3.0/setup.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
from setuptools import setup
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
VERSION = "2.3.0"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
with open("README.rst", "r") as f:
|
|
8
|
-
long_description = f.read()
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
setup(
|
|
12
|
-
name="filtration",
|
|
13
|
-
version=VERSION,
|
|
14
|
-
author="Steve McMaster",
|
|
15
|
-
author_email="mcmaster@hurricanelabs.com",
|
|
16
|
-
packages=["filtration"],
|
|
17
|
-
description="filtration - A library for parsing arbitrary filters",
|
|
18
|
-
long_description=long_description,
|
|
19
|
-
install_requires=[
|
|
20
|
-
"pyparsing>=3.0.0",
|
|
21
|
-
"ipcalc"
|
|
22
|
-
],
|
|
23
|
-
classifiers=[
|
|
24
|
-
"License :: OSI Approved :: MIT License",
|
|
25
|
-
"Programming Language :: Python :: 3",
|
|
26
|
-
"Development Status :: 5 - Production/Stable",
|
|
27
|
-
],
|
|
28
|
-
project_urls={
|
|
29
|
-
"Bug Tracker": "https://github.com/HurricaneLabs/filtration/issues"
|
|
30
|
-
}
|
|
31
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|