okta-expression-parser 0.2.1__tar.gz → 0.3.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: okta-expression-parser
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: Returns a boolean by passing Okta profile data to an Okta Expression using Okta Expression Language
5
5
  Author: Mathew Moon
6
6
  Author-email: me@mathewmoon.net
@@ -24,6 +24,8 @@ class ExpressionLexer(Lexer):
24
24
  NOT,
25
25
  NULL,
26
26
  BOOL,
27
+ COLON,
28
+ QUESTION_MARK,
27
29
  MEMBEROF,
28
30
  MEMBEROFANY,
29
31
  USER,
@@ -49,6 +51,8 @@ class ExpressionLexer(Lexer):
49
51
  MEMBEROFANY = "isMemberOfAnyGroup"
50
52
  MEMBEROF = "isMemberOfGroup"
51
53
  USER = r"user\b"
54
+ COLON = r"\:"
55
+ QUESTION_MARK = r"\?"
52
56
 
53
57
  NULL = r"null|NULL|Null"
54
58
  CLASS = r"String|Arrays|Convert|Iso3166Convert|Groups"
@@ -14,9 +14,8 @@ from .lexer import ExpressionLexer
14
14
 
15
15
 
16
16
  class ExpressionParser(sly.Parser):
17
- if debug_file := environ.get("OKTA_EXPRESSION_PARSER_LOG_FILE"):
18
- #: Writes grammar rules to a file for debugging
19
- debugfile: str = environ.get(debug_file)
17
+ #: Writes grammar rules to a file for debugging
18
+ debugfile = environ.get("OKTA_EXPRESSION_PARSER_LOG_FILE")
20
19
 
21
20
  #: Gets the token list from the lexer (required)
22
21
  tokens: Sequence = ExpressionLexer.tokens
@@ -64,6 +63,12 @@ class ExpressionParser(sly.Parser):
64
63
  def parse(self, expression: str):
65
64
  return super().parse(ExpressionLexer().tokenize(expression))
66
65
 
66
+ @_("operand") # noqa: 821
67
+ @_("condition") # noqa: 821
68
+ def result(self, p: YaccProduction) -> bool: # noqa: 811
69
+ res = p.condition if hasattr(p, "condition") else p.operand
70
+ return res
71
+
67
72
  @_("operand EQ operand") # noqa: 821
68
73
  def condition(self, p: YaccProduction) -> bool:
69
74
  operand0 = p.operand0
@@ -107,17 +112,39 @@ class ExpressionParser(sly.Parser):
107
112
  operand1 = p.operand1
108
113
  return type(operand0) == type(operand1) and operand0 > operand1
109
114
 
115
+ @_('"(" condition ")"') # noqa: 821
116
+ def condition(self, p: YaccProduction) -> bool: # noqa: 811
117
+ return p.condition
118
+
119
+ @_('"(" operand ")"') # noqa: 821
120
+ def operand(self, p: YaccProduction) -> bool: # noqa: 811
121
+ return p.operand
122
+
110
123
  @_("condition AND condition") # noqa: 821
111
124
  def condition(self, p: YaccProduction) -> bool: # noqa: 811
112
- condition0 = p.condition0
113
- condition1 = p.condition1
114
- return condition0 and condition1
125
+ return p.condition0 and p.condition1
126
+
127
+ @_("operand AND operand") # noqa: 821
128
+ def condition(self, p: YaccProduction) -> bool: # noqa: 811
129
+ return p.operand0 and p.operand1
130
+
131
+ @_("operand AND condition") # noqa: 821
132
+ @_("condition AND operand") # noqa: 821
133
+ def condition(self, p: YaccProduction) -> bool: # noqa: 811
134
+ return p.condition and p.operand
115
135
 
116
136
  @_("condition OR condition") # noqa: 821
117
137
  def condition(self, p: YaccProduction) -> bool: # noqa: 811
118
- condition0 = p.condition0
119
- condition1 = p.condition1
120
- return condition0 or condition1
138
+ return p.condition0 or p.condition1
139
+
140
+ @_("operand OR operand") # noqa: 821
141
+ def condition(self, p: YaccProduction) -> bool: # noqa: 811
142
+ return p.operand0 or p.operand1
143
+
144
+ @_("operand OR condition") # noqa: 821
145
+ @_("condition OR operand") # noqa: 821
146
+ def condition(self, p: YaccProduction) -> bool: # noqa: 811
147
+ return p.condition or p.operand
121
148
 
122
149
  @_("NOT condition") # noqa: 821
123
150
  def condition(self, p: YaccProduction) -> bool: # noqa: 811
@@ -128,22 +155,11 @@ class ExpressionParser(sly.Parser):
128
155
  def condition(self, p: YaccProduction) -> bool: # noqa: 811
129
156
  return not p.operand
130
157
 
131
- @_('"(" condition ")"') # noqa: 821
132
- def condition(self, p: YaccProduction) -> bool: # noqa: 811
133
- condition = p.condition
134
- return condition
135
-
136
158
  @_('operand "," operand') # noqa: 821
137
159
  def operand(self, p: YaccProduction) -> List[Any]: # noqa: 811
138
160
  operand0 = p.operand0
139
161
  operand1 = p.operand1
140
162
 
141
- # if not isinstance(operand0, tuple):
142
- # return (operand0, operand1)
143
- #
144
- # else:
145
- # return operand0 + (operand1,)
146
-
147
163
  if isinstance(operand0, tuple) and isinstance(operand1, tuple):
148
164
  res = operand0 + operand1
149
165
  elif isinstance(operand0, tuple) and not isinstance(operand1, tuple):
@@ -292,7 +308,8 @@ class ExpressionParser(sly.Parser):
292
308
  return False
293
309
 
294
310
  val = p.operand
295
- if not isinstance(val, list):
311
+
312
+ if not isinstance(val, tuple):
296
313
  val = [val]
297
314
 
298
315
  res = list(set(self.__group_ids) & set(val))
@@ -306,9 +323,28 @@ class ExpressionParser(sly.Parser):
306
323
  if not self.__group_ids:
307
324
  return False
308
325
 
309
- # return p.operand.lower() in self.__group_ids
310
326
  return p.operand in self.__group_ids
311
327
 
328
+ #@_("turnary") # noqa: 821
329
+ #def operand(self, p: YaccProduction) -> Any:
330
+ # condition = p.turnary[0]
331
+ # if_true = p.turnary[1]
332
+ # if_false = p.turnary[2]
333
+ # if condition:
334
+ # return if_true
335
+ # else:
336
+ # return if_false
337
+
338
+ @_("operand QUESTION_MARK operand COLON operand") # noqa: 821
339
+ @_("condition QUESTION_MARK operand COLON operand") # noqa: 821
340
+ def operand(self, p: YaccProduction) -> Any: # noqa: 811
341
+ condition = p.condition if hasattr(p, "condition") else p.operand0
342
+ if_true = p.operand0 if hasattr(p, "condition") else p.operand1
343
+ if_false = p.operand1 if hasattr(p, "condition") else p.operand2
344
+
345
+ return if_true if condition else if_false
346
+ #return (condition, p.operand0, p.operand1)
347
+
312
348
  @_("condition error") # noqa: 821
313
349
  def operand(self, x): # noqa: 811
314
350
  raise SyntaxError(
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "okta-expression-parser"
3
- version = "0.2.1"
3
+ version = "0.3.0"
4
4
  description = "Returns a boolean by passing Okta profile data to an Okta Expression using Okta Expression Language"
5
5
  authors = ["Mathew Moon <me@mathewmoon.net>"]
6
6
  readme = "README.md"