Skip to content

Instantly share code, notes, and snippets.

@malinich
Last active July 27, 2016 08:18
Show Gist options
  • Save malinich/e2c809adac6d3a3e1db0dcbf66db7430 to your computer and use it in GitHub Desktop.
Save malinich/e2c809adac6d3a3e1db0dcbf66db7430 to your computer and use it in GitHub Desktop.
class Pipe(object):
def __init__(self, name, id=None):
self.name = name
self.id = id
self.children = []
def __rshift__(self, other):
if other.id:
self.children.append(other)
elif other.name in [x.id for x in self.children]:
i = self.children.index(other.name)
return self.children[i]
return self
def __eq__(self, other):
if self.id == other:
return True
return False
def __str__(self):
return self.name
def children(self):
for i in self.children:
yield i
def get(self, id_):
if self.id == id_:
return self
for i in self.children:
ch = i.get(id_)
if ch: return ch
def path(self, id_, path):
if self.id == id_:
path = path + self.id
return path
for i in self.children:
path_ = path + self.id + '/'
ch = i.path(id_, path_)
if ch: return ch
def first(self):
return self.children and self.children[0] or None
def __iter__(self):
return iter(self.children)
class Router(object):
def __init__(self):
#
# type: () -> object
self.routers = Pipe('host', id='host')
self.routers >> Pipe('type_objects', id='type_objects')
self.routers >> Pipe('type_objects') >> Pipe("databases", id="databases")
self.routers >> Pipe("type_objects") >> Pipe('databases') >> Pipe("tables", id='tables')
self.routers >> Pipe("type_objects") >> Pipe("databases") >> Pipe("sql_stored_procedure", id='sql_stored_procedure')
self.routers >> Pipe("type_objects") >> Pipe("databases") >> Pipe("views", id='views')
def get(self, id_):
ch = self.routers.get(id_)
return ch
def path(self, id_):
path = '/'
path_ = self.routers.path(id_, path)
path_ += '/'
return path_
def get_child(self, id_, path):
parent = self.get(id_)
guid = self.get_guid(path)
if guid in parent.children:
i = parent.children.index(guid)
ch = parent.children[i]
else:
ch = parent.first()
return ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment