filtration 2.3.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.
@@ -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
+ >>>
@@ -78,6 +78,11 @@ Subnet = Combine(IpAddr + "/" + Cidr).set_parse_action(_Subnet)
78
78
 
79
79
  class _Symbol(Token):
80
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
+
81
86
  parts = self.value.split(".")
82
87
  value = ctx
83
88
  for part in parts:
@@ -114,6 +119,10 @@ def in_op(lhs, rhs):
114
119
  return False
115
120
 
116
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
+
117
126
  return operator.contains(rhs, lhs)
118
127
  except ValueError:
119
128
  return False
@@ -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
+ >>>
@@ -5,4 +5,6 @@ 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
@@ -1,7 +1,7 @@
1
1
  from setuptools import setup
2
2
 
3
3
 
4
- VERSION = "2.3.0"
4
+ VERSION = "2.3.2"
5
5
 
6
6
 
7
7
  with open("README.rst", "r") as f:
@@ -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.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
File without changes
File without changes