def __init__(self, ast: lark.tree.Tree) -> None: self.number = int(ast.children[0].value) format = next(ast.find_data("partition_format"), None) if format is not None: self.format = format.children[0].value.lower() if self.format not in self.__supported_formats(): raise RuntimeError( f"Unsupported partition format '{self.format}'") else: self.format = None mount = next(ast.find_data("partition_mount"), None) if mount is not None: self.mount = mount.children[0].value else: self.mount = None size = next(ast.find_data("partition_size"), None) if size is not None: self.units = size.children[1].value.upper() self.size = int(size.children[0].value) else: self.units = None self.size = None flags = next(ast.find_data("partition_flags"), None) if flags is not None: self.flags = [child.value.lower() for child in flags.children] else: self.flags = None
def _ast_to_where_clause(self, tree: lark.tree.Tree, table: sa.Table) -> ClauseElement: values_stack = [] op_stack = [] cond_stack = [] for index, subtree in enumerate(tree.iter_subtrees_topdown()): data = subtree.data children = subtree.children if children and isinstance(children[0], lark.lexer.Token): token = str(children[0]) if token in self._FILTERS: op_stack.append(token) else: if data == "word": if token not in table.c: raise ValueError( "Unknown column name in the table", token) values_stack.append(table.c[token]) elif data == "string": values_stack.append(token[1:-1]) else: values_stack.append(token) while op_stack: op = op_stack.pop() if op == "not": cond = not_(values_stack.pop()) cond_stack.append(cond) continue if op in ["and", "or"]: value1 = cond_stack.pop() value2 = cond_stack.pop() else: value2 = values_stack.pop() value1 = values_stack.pop() if op == "gt": cond = value1 > value2 elif op == "gte": cond = value1 >= value2 elif op == "lt": cond = value1 < value2 elif op == "lte": cond = value1 <= value2 elif op == "eq": cond = value1 == value2 elif op == "ne": cond = value1 != value2 elif op == "and": cond = and_(value1, value2) elif op == "or": cond = or_(value1, value2) cond_stack.append(cond) return cond_stack.pop()
def __init__(self, ast: lark.tree.Tree) -> None: self.source = [ node.children[0].value for node in ast.find_data("add_source") ] self.destination = next( ast.find_data("add_destination")).children[0].value