Skip to content

Commit

Permalink
Merge pull request #16 from filippofinke/traveltime
Browse files Browse the repository at this point in the history
Fifth week and Sixth week
  • Loading branch information
danistrigaro authored Jul 10, 2023
2 parents d8d920c + 4226cc8 commit a78556e
Show file tree
Hide file tree
Showing 10 changed files with 678 additions and 325 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""
Module: STA2REST filter visitor
Author: Filippo Finke
This module provides a visitor for the filter AST.
"""
from odata_query import ast, visitor

class FilterVisitor(visitor.NodeVisitor):

def visit_Add(self, node: ast.Add) -> str:
print("Add")
print(node)
return node
"""
Visitor for the filter AST.
"""

def visit_All(self, node: ast.All) -> str:
return "all"
Expand All @@ -17,91 +22,63 @@ def visit_Any(self, node: ast.Any) -> str:
return "any"

def visit_Attribute(self, node: ast.Attribute) -> str:
print("Attribute")
print(node)
return node

def visit_BinOp(self, node: ast.BinOp) -> str:
print("BinOp")
print(node)
return node

def visit_BoolOp(self, node: ast.BoolOp) -> str:
operator = self.visit(node.op)
left = self.visit(node.left)
right = self.visit(node.right)

# Check if the is AND, because it is the default operator
if(isinstance(node.op, ast.And)):
return f"{left}&{right}"
# Otherwise it will be OR
else:
left = left.replace("=", ".")
# check if another or is present by checking the first or=
if "or=" in left:
# remove the parenthesis
left = left[4:-1]
else:
left = left.replace("=", ".")

right = right.replace("=", ".")
# Or syntax is different from the other operators
return f"{operator}=({left},{right})"

def visit_Boolean(self, node: ast.Boolean) -> str:
print("Boolean")
print(node)
return node

def visit_Call(self, node: ast.Call) -> str:
print("Call")
print(node)
return node

def visit_CollectionLambda(self, node: ast.CollectionLambda) -> str:
print("CollectionLambda")
print(node)
return node

def visit_Compare(self, node: ast.Compare) -> str:

print(node.left, node.comparator, node.right)

left = super().visit(node.left)
comparator = super().visit(node.comparator)
right = super().visit(node.right)

# Check if the left is an attribute
if isinstance(left, (ast.Attribute)):
owner = left.owner.name
attr = left.attr
left = f"{owner}->>{attr}"
# Otherwise it is an identifier
elif isinstance(left, (ast.Identifier)):
left = left.name

# Check if the right is an attribute
if isinstance(right, (ast.Identifier)):
right = right.name

return f"{left}={comparator}.{right}"

def visit_Date(self, node: ast.Date) -> str:
print("Date")
print(node)
return node


def visit_DateTime(self, node: ast.DateTime) -> str:
return node.val

def visit_Div(self, node: ast.Div) -> str:
print("Div")
print(node)
return node

def visit_Duration(self, node: ast.Duration) -> str:
print("Duration")
print(node)
return node


def visit_Eq(self, node: ast.Eq) -> str:
return "eq"

def visit_Float(self, node: ast.Float) -> str:
return node.val

def visit_GUID(self, node: ast.GUID) -> str:
print("GUID")
print(node)
return node

def visit_Gt(self, node: ast.Gt) -> str:
return "gt"

Expand All @@ -114,16 +91,6 @@ def visit_In(self, node: ast.In) -> str:
def visit_Integer(self, node: ast.Integer) -> str:
return node.val

def visit_Lambda(self, node: ast.Lambda) -> str:
print("Lambda")
print(node)
return node

def visit_List(self, node: ast.List) -> str:
print("List")
print(node)
return node

def visit_Lt(self, node: ast.Lt) -> str:
return "lt"

Expand All @@ -136,36 +103,11 @@ def visit_Not(self, node: ast.Not) -> str:
def visit_NotEq(self, node: ast.NotEq) -> str:
return "neq"

def visit_Null(self, node: ast.Null) -> str:
print("Null")
print(node)
return node

def visit_Or(self, node: ast.Or) -> str:
return "or"

def visit_String(self, node: ast.String) -> str:
return node.val

def visit_Sub(self, node: ast.Sub) -> str:
print("Sub")
print(node)
return node

def visit_Time(self, node: ast.Time) -> str:
print("Time")
print(node)
return node

def visit_USub(self, node: ast.USub) -> str:
print("USub")
print(node)
return node

def visit_UnaryOp(self, node: ast.UnaryOp) -> str:
print("UnaryOp")
print(node)
return node

def visit_Identifier(self, node: ast.Identifier) -> str:
return node
Loading

0 comments on commit a78556e

Please sign in to comment.