2019-03-23 22:02:55 +00:00
|
|
|
class ConvertTable():
|
|
|
|
'''The class is used to store and find the right function to convert value from
|
|
|
|
one key to another'''
|
|
|
|
def __init__(self):
|
|
|
|
self.convert_functions = []
|
|
|
|
|
|
|
|
def add_function(self, function):
|
|
|
|
'''Here you can add function to ConvertTable.'''
|
|
|
|
#TODO: make this method produce new functions, that will be able to
|
|
|
|
#create converter chains
|
|
|
|
self.convert_functions.append(function)
|
|
|
|
|
2019-07-05 14:26:25 +00:00
|
|
|
def all_possible_conversions(self, from_keys):
|
|
|
|
result = set()
|
|
|
|
from_keys = set(from_keys)
|
|
|
|
for function in self.convert_functions:
|
|
|
|
input_args = set(value for key, value in
|
|
|
|
function.__annotations__.items() if
|
|
|
|
key!='return')
|
|
|
|
if input_args.issubset(from_keys):
|
|
|
|
result = result.union(set(function.__annotations__['return']))
|
|
|
|
return result
|
|
|
|
|
2019-03-23 22:02:55 +00:00
|
|
|
def get_converter(self, from_keys, to_key):
|
|
|
|
'''This function returns converter function, that can convert one key
|
|
|
|
to another'''
|
2019-07-05 14:26:25 +00:00
|
|
|
to_key = {to_key}
|
2019-03-23 22:02:55 +00:00
|
|
|
for function in self.convert_functions:
|
2019-07-05 14:26:25 +00:00
|
|
|
input_args = set(value for key, value in
|
|
|
|
function.__annotations__.items() if
|
|
|
|
key!='return')
|
|
|
|
if input_args.issubset(from_keys) and to_key.issubset(function.__annotations__['return']):
|
|
|
|
return input_args, function
|
|
|
|
raise Exception("There is no converter for %s to %s" % (from_keys,
|
|
|
|
to_key))
|
2019-03-23 22:02:55 +00:00
|
|
|
return None, None
|