1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add another recursive monster

This commit is contained in:
Steffo 2019-01-09 15:04:21 +01:00
parent 262045108b
commit f952b3cc8b

22
db.py
View file

@ -40,7 +40,7 @@ os.environ["COLOREDLOGS_LOG_FORMAT"] = "%(asctime)s %(levelname)s %(name)s %(mes
coloredlogs.install(level="DEBUG", logger=logger)
def recursive_relationship_name_search(_class, keyword) -> typing.Optional[tuple]:
def relationship_name_search(_class, keyword) -> typing.Optional[tuple]:
"""Recursively find a relationship with a given name."""
inspected = set()
@ -61,6 +61,26 @@ def recursive_relationship_name_search(_class, keyword) -> typing.Optional[tuple
return search(inspect(_class), tuple())
def relationship_link_chain(starting_class, ending_class) -> typing.Optional[tuple]:
"""Find the path to follow to get from the starting table to the ending table."""
inspected = set()
def search(_mapper, chain):
inspected.add(_mapper)
if _mapper.class_ == ending_class:
return chain
relationships = _mapper.relationships
for _relationship in set(relationships):
if _relationship.mapper in inspected:
continue
result = search(_relationship.mapper, chain + (_relationship,))
if result is not None:
return result
return None
return search(inspect(starting_class), tuple())
class Mini(object):
@classmethod
def mini_get_all(cls, session: Session) -> list: