Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 17, 2024
1 parent fe8c1e6 commit b3ee853
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 89 deletions.
31 changes: 15 additions & 16 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,15 +1361,14 @@ def get_module(cls,
trials = 3,
tree = 'commune',
verbose = 0,
path2objectpath = {'tree': 'commune.tree.tree.Tree'}) -> str:
) -> str:
"""
params:
path: the path to the module
cache: whether to cache the module
tree: the tree to search for the module
"""
path = path or 'module'
tree = tree or 'commune'
module = None
cache_key = f'{tree}_{path}'
t0 = c.time()
Expand All @@ -1380,19 +1379,13 @@ def get_module(cls,
return module

try:
if path in path2objectpath:
object_path = path2objectpath[path]
else:
object_path = c.simple2objectpath(path)

module = c.import_object(object_path)
module = c.simple2object(path)
except Exception as e:
c.print(e)
raise e
if trials == 0:
raise Exception(f'Could not find {path} in {c.modules(path)} modules')
c.print(f'Could not find {path} in {c.modules(path)} modules, so we are updating the tree', color='red')
c.tree(update=True)
module = c.get_module(path, cache=cache, tree=tree, verbose=verbose, trials=trials-1)
module = c.get_module(path, cache=cache , verbose=verbose, trials=trials-1)
if cache:
c.module_cache[cache_key] = module

Expand Down Expand Up @@ -1451,8 +1444,7 @@ def search_dict(self, d:dict = 'k,d', search:str = {'k.d': 1}) -> dict:


@classmethod
def tree(cls, *args,
**kwargs) -> List[str]:
def tree(cls, *args, **kwargs) -> List[str]:
return c.module('tree').tree(*args, **kwargs)

@classmethod
Expand Down Expand Up @@ -1509,8 +1501,16 @@ def simple2path(cls, path:str, **kwargs) -> str:
return c.module('tree').simple2path(path, **kwargs)

@classmethod
def simple2objectpath(cls, path:str, **kwargs) -> str:
return c.module('tree').simple2objectpath(path, **kwargs)
def simple2objectpath(cls, path:str,path2objectpath = {'tree': 'commune.tree.tree.Tree'}, **kwargs) -> str:
if path in path2objectpath:
object_path = path2objectpath[path]
else:
object_path = c.module('tree').simple2objectpath(path, **kwargs)
return object_path
@classmethod
def simple2object(cls, path:str, **kwargs) -> str:
return c.import_object(c.simple2objectpath(path, **kwargs))


@classmethod
def python_paths(cls, path:str = None, recursive=True, **kwargs) -> List[str]:
Expand Down Expand Up @@ -3881,7 +3881,6 @@ def status(cls, *args, **kwargs):
return cls.console.status(*args, **kwargs)
@classmethod
def log(cls, *args, **kwargs):
console = cls.resolve_console()
return cls.console.log(*args, **kwargs)

@classmethod
Expand Down
16 changes: 15 additions & 1 deletion commune/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,17 @@ def format_module(self, module: 'ModuleInfo', fmt:str='j') -> 'ModuleInfo':

module['stake_from'] = {k: self.format_amount(v, fmt=fmt) for k, v in module['stake_from']}
return module


def df(self,
netuid=0,
features=['name', 'address', 'incentive', 'dividends', 'emission', 'last_update', 'delegation_fee', 'stake'],
**kwargs) -> 'pd.DataFrame':
df = c.df(self.modules(netuid=netuid, **kwargs))
if len(df) > 0:
df = df[features]
return df


def modules(self,
search:str= None,
network = 'main',
Expand All @@ -1716,6 +1726,7 @@ def modules(self,
timeout = 100,
max_age=1000,
subnet = None,
df = False,
vector_features =['dividends', 'incentive', 'trust', 'last_update', 'emission'],
**kwargs
) -> Dict[str, 'ModuleInfo']:
Expand Down Expand Up @@ -1798,6 +1809,9 @@ def query(name, **kwargs):
if len(modules) > 0:
for i in range(len(modules)):
modules[i] = self.format_module(modules[i], fmt=fmt)
for m in modules:
m['stake'] = sum([v
for k,v in m['stake_from'].items()])

if search != None:
modules = [m for m in modules if search in m['name']]
Expand Down
143 changes: 71 additions & 72 deletions commune/tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def __init__(self, **kwargs):
def simple2path(cls, path:str, tree = None, **kwargs) -> bool:
pwd = c.pwd()
simple_path = path

if path == c.homepath:
path = cls.default_tree_path
path = pwd + '/' + path.replace('.', '/')

if os.path.isdir(path):
paths_in_dir = os.listdir(path)
for p in paths_in_dir:
Expand All @@ -32,11 +32,11 @@ def simple2path(cls, path:str, tree = None, **kwargs) -> bool:
if os.path.exists(path + '.py'):
path = path + '.py'
else:
tree = cls.tree()
tree = cls.tree(update=True, include_default=True)
is_module_in_tree = simple_path in tree
tree = {k:v for k,v in tree.items() if simple_path in k}
if not is_module_in_tree:
tree = cls.tree(update=True)
tree = cls.tree(update=True, include_default=True)
assert len(tree) > 0, f'No module found for {simple_path}'
path = tree[simple_path]

Expand All @@ -54,53 +54,63 @@ def path2tree(self, **kwargs) -> str:


@classmethod
def tree(cls, tree = None,
def default_tree(cls, **kwargs):
return cls.tree(path = c.libpath, include_default=False, **kwargs)

_tree_cache = {}

@classmethod
def is_repo(cls, libpath:str ):
# has the .git folder
return bool([f for f in cls.ls(libpath) if '.git' in f and os.path.isdir(f)])

@classmethod
def tree(cls,
path = None,
search=None,
update = False,
max_age = None, **kwargs
max_age = None,
include_default = False,
**kwargs
) -> List[str]:

tree_path = cls.resolve_path(tree)
module_tree = {}
tree = cls.resolve_tree(tree)
path = cls.resolve_path(f'{tree}/tree')
max_age = 0 if update else max_age
module_tree = c.get(path, {}, max_age=max_age)

if len(module_tree) == 0:
tree2path = cls.tree2path()
if tree in tree2path:
tree_path = tree2path[tree]
else:
tree_path = cls.default_tree_path
# get modules from each tree
python_paths = c.get_module_python_paths(path=tree_path)
# add the modules to the module tree
new_tree = {c.path2simple(f): f for f in python_paths}
for k,v in new_tree.items():
if k not in module_tree:
module_tree[k] = v
# to use functions like c. we need to replace it with module lol

if cls.root_module_class in module_tree:
module_tree[cls.root_module_class] = module_tree.pop(cls.root_module_class)

c.put(path, module_tree)

# cache the module tree
path = cls.resolve_path(path or c.pwd())
is_repo = cls.is_repo(path)
if not is_repo:
path = cls.default_tree_path
cache_path = path.split('/')[-1]
if cache_path in cls._tree_cache:
tree = cls._tree_cache[cache_path]
else:
tree = c.get(cache_path, {}, max_age=max_age, update=update)
if len(tree) == 0:
tree = cls.build_tree(path)
cls._tree_cache[cache_path] = tree
cls.put(cache_path, tree)
if search != None:
module_tree = {k:v for k,v in module_tree.items() if search in k}

return module_tree
tree = {k:v for k,v in tree.items() if search in k}
if include_default:
default_tree = cls.default_tree()
tree = {**default_tree, **tree}
return tree




@classmethod
def tree_paths(cls, update=False, **kwargs) -> List[str]:
path = cls.tree_folders_path
trees = [] if update else c.get(path, [], **kwargs)
if len(trees) == 0:
def build_tree(cls, tree_path:str = './', **kwargs):
tree_path = cls.resolve_path(tree_path)
module_tree = {}
for root, dirs, files in os.walk(tree_path):
for file in files:
if file.endswith('.py') and '__init__' not in file and not file.split('/')[-1].startswith('_'):
path = os.path.join(root, file)
simple_path = cls.path2simple(path)
module_tree[simple_path] = path

trees = cls.default_trees
return trees
return module_tree
@classmethod
def tree_paths(cls, update=False, **kwargs) -> List[str]:
return cls.ls()

@classmethod
def tree_hash(cls, *args, **kwargs):
Expand Down Expand Up @@ -128,25 +138,16 @@ def run_loop(self, *args, sleep_time=10, **kwargs):

@classmethod
def add_tree(cls, tree_path:str = './', **kwargs):

tree_path = os.path.abspath(tree_path)

path = cls.tree_folders_path
tree_folder = c.get(path, [])
tree_folder = list(set(tree_folder + cls.default_trees + [tree_path]))
assert os.path.isdir(tree_path)
assert isinstance(tree_folder, list)
c.put(path, tree_folder, **kwargs)
return {'module_tree_folders': tree_folder}
return cls.tree(tree_path, update=True)

@classmethod
def rm_tree(cls, tree_path:str, **kwargs):
path = cls.tree_folders_path
tree_folder = c.get(path, [])
tree_folder = [f for f in tree_folder if f != tree_path ]
c.put(path, tree_folder)
return {'module_tree_folders': tree_folder}

return cls.rm(tree_path)

@classmethod
def rm_trees(cls, **kwargs):
for tree_path in cls.tree_paths():
cls.rm(tree_path)



Expand Down Expand Up @@ -186,21 +187,20 @@ def resolve_tree(cls, tree:str=None):
@classmethod
def path2simple(cls, path:str, ignore_prefixes = ['commune', 'modules', 'commune.modules']) -> str:

tree = cls.resolve_tree(None)

path = os.path.abspath(path)

pwd = c.pwd()

if path.startswith(pwd):
path = path.replace(pwd, '')
elif path.startswith(c.libpath):
if path.startswith(c.libpath):
path = path.replace(c.libpath, '')

simple_path = path.split(deepcopy(tree))[-1]
elif path.startswith(pwd):
path = path.replace(pwd, '')

if cls.path_config_exists(path):
simple_path = os.path.dirname(simple_path)
else:
simple_path = path
simple_path = simple_path.replace('.py', '')
simple_path = simple_path.replace('/', '.')
if simple_path.startswith('.'):
Expand Down Expand Up @@ -271,13 +271,12 @@ def find_classes(cls, path):
@classmethod
def simple2objectpath(cls, simple_path:str, **kwargs) -> str:
path = cls.simple2path(simple_path, **kwargs)
libpath = c.libpath
if path.startswith(libpath):
object_path = path.replace(libpath, '')
else:
pwd = c.pwd()
object_path = path.replace(pwd, '')
classes = cls.find_classes(path)
if path.startswith(c.libpath):
object_path = path.replace(c.libpath, '')
else:
object_path = path.replace(c.pwd(), '')

object_path = object_path.replace('.py', '')
object_path = object_path.replace('/', '.')
if object_path.startswith('.'):
Expand Down
5 changes: 5 additions & 0 deletions modules/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@


class Git(c.Module):


def is_repo(self, libpath:str ):
# has the .git folder
return c.cmd(f'ls -a {libpath}').count('.git') > 0


@staticmethod
Expand Down

0 comments on commit b3ee853

Please sign in to comment.