diff --git a/commune/module.py b/commune/module.py index 43476ea5..e86d09c2 100755 --- a/commune/module.py +++ b/commune/module.py @@ -665,48 +665,13 @@ def routes(cls, cache=True): #### THE FINAL TOUCH , ROUTE ALL OF THE MODULES TO THE CURRENT MODULE BASED ON THE routes CONFIG @classmethod - def route_fns(cls): - routes = cls.routes() - route_fns = [] - for module, fns in routes.items(): - for fn in fns: - if isinstance(fn, dict): - fn = fn['to'] - elif isinstance(fn, list): - fn = fn[1] - elif isinstance(fn, str): - fn - else: - raise ValueError(f'Invalid route {fn}') - route_fns.append(fn) - return route_fns - - @staticmethod - def resolve_to_from_fn_routes(fn): - ''' - resolve the from and to function names from the routes - option 1: - {fn: 'fn_name', name: 'name_in_current_module'} - option 2: - {from: 'fn_name', to: 'name_in_current_module'} - ''' - - if type(fn) in [list, set, tuple] and len(fn) == 2: - # option 1: ['fn_name', 'name_in_current_module'] - from_fn = fn[0] - to_fn = fn[1] - elif isinstance(fn, dict) and all([k in fn for k in ['fn', 'name']]): - if 'fn' in fn and 'name' in fn: - to_fn = fn['name'] - from_fn = fn['fn'] - elif 'from' in fn and 'to' in fn: - from_fn = fn['from'] - to_fn = fn['to'] + def resolve_routes(cls, routes:dict=None): + if hasattr(cls, 'routes'): + routes = cls.routes() if callable(cls.routes) else cls.routes else: - from_fn = fn - to_fn = fn - - return from_fn, to_fn + routes = {} + assert isinstance(routes, dict), f'routes must be a dictionary, not {type(routes)}' + return routes @classmethod def add_routes(cls, routes:dict=None, verbose=False): @@ -746,8 +711,14 @@ def fn_generator( *args, module_ph, fn_ph, **kwargs): if fns in ['all', '*']: fns = c.functions(m) for fn in fns: - # resolve the from and to function names - from_fn, to_fn = cls.resolve_to_from_fn_routes(fn) + + if type(fn) in [list, set, tuple] and len(fn) == 2: + # option 1: ['fn_name', 'name_in_current_module'] + from_fn = fn[0] + to_fn = fn[1] + else: + from_fn = fn + to_fn = fn # create a partial function that is bound to the module fn_obj = partial(fn_generator, fn_ph=from_fn, module_ph=m ) # make sure the funciton is as close to the original function as possible @@ -4187,8 +4158,7 @@ def vscode(self, path = None): def path(self): return self.filepath() - - + c.add_routes() Module = c # Module is alias of c Module.run(__name__) diff --git a/commune/routes.yaml b/commune/routes.yaml index 84e2caaa..9e399d93 100644 --- a/commune/routes.yaml +++ b/commune/routes.yaml @@ -1,5 +1,6 @@ vali: - run_epoch + - leaderboard cli: - parse_args streamlit: diff --git a/scripts/install_commune.sh b/scripts/install_commune.sh index e69de29b..195a4845 100644 --- a/scripts/install_commune.sh +++ b/scripts/install_commune.sh @@ -0,0 +1,2 @@ +# install commune +