kubernator 1.0.12__py3-none-any.whl → 1.0.13__py3-none-any.whl

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.

Potentially problematic release.


This version of kubernator might be problematic. Click here for more details.

kubernator/__init__.py CHANGED
@@ -16,7 +16,7 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- __version__ = "1.0.12"
19
+ __version__ = "1.0.13"
20
20
 
21
21
 
22
22
  def _main():
@@ -0,0 +1,113 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2020 Express Systems USA, Inc
4
+ # Copyright 2024 Karellen, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ import re
20
+ from functools import cache
21
+
22
+ from jsonpath_ng import JSONPath, DatumInContext
23
+ from jsonpath_ng.ext import parse as jp_parse, parser
24
+ from jsonpath_ng.ext.string import DefintionInvalid
25
+
26
+ __all__ = ["jp", "JPath"]
27
+
28
+
29
+ class JPath:
30
+ def __init__(self, pattern):
31
+ self.pattern = jp_parse(pattern)
32
+
33
+ def find(self, val):
34
+ return self.pattern.find(val)
35
+
36
+ def all(self, val):
37
+ return list(map(lambda x: x.value, self.find(val)))
38
+
39
+ def first(self, val):
40
+ """Returns the first element or None if it doesn't exist"""
41
+ try:
42
+ return next(map(lambda x: x.value, self.find(val)))
43
+ except StopIteration:
44
+ return None
45
+
46
+ def only(self, val):
47
+ """Returns the first and only element.
48
+ Raises ValueError if more than one value found
49
+ Raises KeyError if no value found
50
+ """
51
+ m = map(lambda x: x.value, self.find(val))
52
+ try:
53
+ v = next(m)
54
+ except StopIteration:
55
+ raise KeyError("no value found")
56
+ try:
57
+ next(m)
58
+ raise ValueError("more than one value returned")
59
+ except StopIteration:
60
+ return v
61
+
62
+
63
+ @cache
64
+ def jp(pattern) -> JPath:
65
+ return JPath(pattern)
66
+
67
+
68
+ MATCH = re.compile(r"match\(/(.*)(?<!\\)/\)")
69
+
70
+
71
+ class Match(JSONPath):
72
+ """Direct node regex matcher
73
+
74
+ Concrete syntax is '`match(/regex/)`'
75
+ """
76
+
77
+ def __init__(self, method=None):
78
+ m = MATCH.match(method)
79
+ if m is None:
80
+ raise DefintionInvalid("%s is not valid" % method)
81
+ self.expr = m.group(1).strip()
82
+ self.regex = re.compile(self.expr)
83
+ self.method = method
84
+
85
+ def find(self, datum):
86
+ datum = DatumInContext.wrap(datum)
87
+
88
+ if hasattr(datum.path, "fields") and self.regex.match(datum.path.fields[0]):
89
+ return [datum]
90
+ return []
91
+
92
+ def __eq__(self, other):
93
+ return isinstance(other, Match) and self.method == other.method
94
+
95
+ def __repr__(self):
96
+ return '%s(%r)' % (self.__class__.__name__, self.method)
97
+
98
+ def __str__(self):
99
+ return '`match(/%s/)`' % (self.expr,)
100
+
101
+
102
+ old_p_jsonpath_named_operator = parser.ExtentedJsonPathParser.p_jsonpath_named_operator
103
+
104
+
105
+ def p_jsonpath_named_operator(self, p):
106
+ "jsonpath : NAMED_OPERATOR"
107
+ if p[1].startswith("match("):
108
+ p[0] = Match(p[1])
109
+ else:
110
+ old_p_jsonpath_named_operator(self, p)
111
+
112
+
113
+ parser.ExtentedJsonPathParser.p_jsonpath_named_operator = p_jsonpath_named_operator