2
0
mirror of https://github.com/xcat2/confluent.git synced 2024-11-25 19:10:10 +00:00

Add [] slicing/indexing to confluent attribute expression syntax

This permits expressions like:
node[:-3]
To say nodename, but leave out 3 chars.
Or:
node[3:]
To skip the first three characters.
This commit is contained in:
Jarrod Johnson 2022-03-11 11:23:43 -05:00
parent f168c4be2b
commit dc0183fdf4

View File

@ -1061,6 +1061,34 @@ class _ExpressionFormat(string.Formatter):
op = self._supported_ops[optype]
return op(int(self._handle_ast_node(node.left)),
int(self._handle_ast_node(node.right)))
elif isinstance(node, ast.Constant):
return node.value
elif isinstance(node, ast.Index):
return self._handle_ast_node(node.value)
elif isinstance(node, ast.UnaryOp):
if isinstance(node.op, ast.USub):
return 0 - self._handle_ast_node(node.operand)
else:
raise ValueError("Invalid operation in expression")
elif isinstance(node, ast.Subscript):
strval = self._handle_ast_node(node.value)
if isinstance(node.slice, ast.Slice):
lower = node.slice.lower
if lower is None:
lower = 0
else:
lower = self._handle_ast_node(lower)
upper = node.slice.upper
if upper is None:
return strval[lower:]
else:
upper = self._handle_ast_node(upper)
return strval[lower:upper]
else:
index = self._handle_ast_node(node.slice)
return strval[index]
else:
raise ValueError("Unrecognized expression syntax")
def _expand_attribute(self, key):
if '_expressionkeys' not in self._nodeobj: