Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set(self, key, value, force_list=False):
key = self._safekey(key)
previous = self.get(key)
if previous is None:
if force_list:
super(AST, self).__setitem__(key, [value])
else:
super(AST, self).__setitem__(key, value)
self._order.append(key)
elif is_list(previous):
previous.append(value)
else:
super(AST, self).__setitem__(key, [previous, value])
return self
def _extend_cst(self, node):
if node is None:
return
previous = self.cst
if previous is None:
self.cst = self._copy_node(node)
elif is_list(node):
if is_list(previous):
previous.extend(node)
else:
self.cst = [previous] + node
elif is_list(previous):
previous.append(node)
else:
self.cst = [previous, node]
def walk(self, node, *args, **kwargs):
supers_walk = super().walk
if isinstance(node, Node):
children = [self.walk(c, *args, **kwargs) for c in node.children()]
return supers_walk(node, children, *args, **kwargs)
elif isinstance(node, Mapping):
return {n: self.walk(e, *args, **kwargs) for n, e in node.items()}
elif is_list(node):
return [self.walk(e, *args, **kwargs) for e in iter(node)]
else:
return supers_walk(node, [], *args, **kwargs)
def _save_result(self, key, result):
if is_list(result.node):
result = RuleResult(closure(result.node), result.newpos, result.newstate)
self._results[key] = result
def _add_cst_node(self, node):
if node is None:
return
previous = self.cst
if previous is None:
self.cst = self._copy_node(node)
elif is_list(previous):
previous.append(node)
else:
self.cst = [previous, node]
def _isolate(self, block, drop=False):
self._push_cst()
try:
block()
cst = self.cst
finally:
self._pop_cst()
if is_list(cst):
cst = closure(cst)
if not drop:
self._add_cst_node(cst)
return cst