Skip to content

Commit

Permalink
fam
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Aug 28, 2024
1 parent 58bcd2f commit 58ebbe2
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 729 deletions.
9 changes: 6 additions & 3 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def get_url(self, fn, mode='http', network=None):
url = f"{module_address}/{fn}/"
return url
def request(self, url: str, data: dict, headers: dict, timeout: int = 10, stream: bool = True):
response = self.session.post(url, json=data, headers=headers, timeout=timeout, stream=stream)
try:
response = self.session.post(url, json=data, headers=headers, timeout=timeout, stream=stream)

if 'text/event-stream' in response.headers.get('Content-Type', ''):
return self.stream(response)
if 'application/json' in response.headers.get('Content-Type', ''):
Expand Down Expand Up @@ -190,8 +191,10 @@ def forward(self,
url = self.get_url(fn=fn, mode=mode, network=network)
data = data or self.get_data(args=args, kwargs=kwargs, params=params, **extra_kwargs)
headers = headers or self.get_headers(data=data, key=key)
kwargs = {**(kwargs or {}), **extra_kwargs}
result = self.request( url=url,data=data,headers= headers,timeout= timeout)
result = self.request(url=url,
data=data,
headers=headers,
timeout=timeout)
return result

def __del__(self):
Expand Down
1 change: 0 additions & 1 deletion commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class MnemonicLanguageCode:


class Keypair(c.Module):
keys_path = c.data_path + '/keys.json'
def __init__(self,
ss58_address: str = None,
public_key: Union[bytes, str] = None,
Expand Down
10 changes: 9 additions & 1 deletion commune/module/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,12 @@ def config_path(cls) -> str:

def update_config(self, config):
self.config.update(config)
return self.config
return self.config


@classmethod
def base_config(cls, cache=True):
if cache and hasattr(cls, '_base_config'):
return cls._base_config
cls._base_config = cls.get_yaml(cls.config_path())
return cls._base_config
189 changes: 168 additions & 21 deletions commune/module/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ def resolve_extension(cls, filename:str, extension = '.py') -> str:
def simple2path(cls,
simple:str,
extension = '.py',
avoid_dirnames = ['', 'src', 'commune', 'commune/module', 'commune/modules', 'modules', 'blocks', 'agents', 'commune/agents'],
avoid_dirnames = ['', 'src',
'commune',
'commune/module',
'commune/modules',
'modules',
'blocks',
'agents',
'commune/agents'],
**kwargs) -> bool:
"""
converts the module path to a file path
Expand Down Expand Up @@ -52,24 +59,19 @@ def simple2path(cls,
if os.path.isdir(module_dirpath):
simple_filename = simple.replace('.', '_')
filename_options = [simple_filename, simple_filename + '_module', 'module_'+ simple_filename] + ['module'] + simple.split('.') + ['__init__']
path_options += [module_dirpath + '/' + cls.resolve_extension(f) for f in filename_options]
path_options += [module_dirpath + '/' + f for f in filename_options]
else:
module_filepath = dir_path + '/' + cls.resolve_extension(simple.replace('.', '/'), extension=extension)
module_filepath = dir_path + '/' + simple.replace('.', '/')
path_options += [module_filepath]

for p in path_options:
p = cls.resolve_extension(p)
if os.path.exists(p):
p_text = cls.get_text(p)
# gas class in text
is_class_text = 'commune' in p_text and 'class ' in p_text or ' def ' in p_text
if is_class_text:
path = p
break
path = p

if 'commune' in p_text and 'class ' in p_text or ' def ' in p_text:
return p
if path != None:
break
assert path != None, f'MODULE {simple} DOES NOT EXIST'
return path


Expand Down Expand Up @@ -119,7 +121,6 @@ def path2simple(cls,
module_extension = '.'+module_extension
if path.endswith(module_extension):
path = path[:-len(module_extension)]

if compress_path:
# we want to remove redundant chunks
# for example if the path is 'module/module' we want to remove the redundant module
Expand Down Expand Up @@ -173,7 +174,6 @@ def resolve_cache_path(self, path):
if path.startswith('_'):
path = path[1:]
path = f'cached_path/{path}'
print(path)
return path

@classmethod
Expand All @@ -187,7 +187,8 @@ def find_classes(cls, path='./', working=False):
path = os.path.abspath(path)
if os.path.isdir(path):
classes = []
for p in cls.glob(path+'/**/**.py', recursive=True):
generator = cls.glob(path+'/**/**.py', recursive=True)
for p in generator:
if p.endswith('.py'):
p_classes = cls.find_classes(p )
if working:
Expand Down Expand Up @@ -223,14 +224,60 @@ def find_classes(cls, path='./', working=False):
classes = [c.replace(libpath_objpath_prefix, '') for c in classes]
return classes




@classmethod
def find_class2functions(cls, path, working=False):

path = os.path.abspath(path)
if os.path.isdir(path):
class2functions = {}
for p in cls.glob(path+'/**/**.py', recursive=True):
if p.endswith('.py'):
object_path = cls.path2objectpath(p)
response = cls.find_class2functions(p )
for k,v in response.items():
class2functions[object_path+ '.' +k] = v
return class2functions

code = cls.get_text(path)
classes = []
class2functions = {}
class_functions = []
new_class = None
for line in code.split('\n'):
if all([s in line for s in ['class ', ':']]):
new_class = line.split('class ')[-1].split('(')[0].strip()
if new_class.endswith(':'):
new_class = new_class[:-1]
if ' ' in new_class:
continue
classes += [new_class]
if len(class_functions) > 0:
class2functions[new_class] = cls.copy(class_functions)
class_functions = []
if all([s in line for s in [' def', '(']]):
fn = line.split(' def')[-1].split('(')[0].strip()
class_functions += [fn]
if new_class != None:
class2functions[new_class] = class_functions

return class2functions

@classmethod
def path2objectpath(cls, path:str, **kwargs) -> str:
libpath = cls.libpath + '/' + cls.libname
libpath = cls.libpath
path.replace
if path.startswith(libpath):
return cls.libname + '.' + path.replace(libpath , '')[1:].replace('/', '.').replace('.py', '')
pwd = cls.pwd()
if path.startswith(pwd):
return path.replace(pwd, '')[1:].replace('/', '.').replace('.py', '')
path = path.replace(libpath , '')[1:].replace('/', '.').replace('.py', '')
else:
pwd = cls.pwd()
if path.startswith(pwd):
path = path.replace(pwd, '')[1:].replace('/', '.').replace('.py', '')

return path.replace('__init__.', '.')


@classmethod
def find_functions(cls, path = './', working=False):
Expand Down Expand Up @@ -429,7 +476,7 @@ def simplify_paths(cls, paths):
return paths

@classmethod
def simplify_path(cls, p, avoid_terms=['modules']):
def simplify_path(cls, p, avoid_terms=['modules', 'agents']):
chunks = p.split('.')
if len(chunks) < 2:
return None
Expand Down Expand Up @@ -479,6 +526,52 @@ def get_tree(cls, path):
simple_paths = cls.simplify_paths(class_paths)
return dict(zip(simple_paths, class_paths))

@classmethod
def get_module(cls,
path:str = 'module',
cache=True,
verbose = False,
update_tree_if_fail = True,
init_kwargs = None,
catch_error = False,
) -> str:
import commune as c
path = path or 'module'
if catch_error:
try:
return cls.get_module(path=path, cache=cache,
verbose=verbose,
update_tree_if_fail=update_tree_if_fail,
init_kwargs=init_kwargs,
catch_error=False)
except Exception as e:
return c.detailed_error(e)
if path in ['module', 'c']:
return c
# if the module is a valid import path
shortcuts = c.shortcuts()
if path in shortcuts:
path = shortcuts[path]
module = None
cache_key = path
t0 = c.time()
if cache and cache_key in c.module_cache:
module = c.module_cache[cache_key]
return module
module = c.simple2object(path)
# ensure module
if verbose:
c.print(f'Loaded {path} in {c.time() - t0} seconds', color='green')

if init_kwargs != None:
module = module(**init_kwargs)
is_module = c.is_module(module)
if not is_module:
module = cls.obj2module(module)
if cache:
c.module_cache[cache_key] = module
return module


_tree = None
@classmethod
Expand Down Expand Up @@ -530,4 +623,58 @@ def modules(cls, search=None, cache=True, **kwargs)-> List[str]:

@classmethod
def has_module(cls, module):
return module in cls.modules()
return module in cls.modules()





def new_modules(self, *modules, **kwargs):
for module in modules:
self.new_module(module=module, **kwargs)



@classmethod
def new_module( cls,
module : str ,
base_module : str = 'demo',
folder_module : bool = False,
update=1
):

import commune as c
base_module = c.module(base_module)
module_class_name = ''.join([m[0].capitalize() + m[1:] for m in module.split('.')])
base_module_class_name = base_module.class_name()
base_module_code = base_module.code().replace(base_module_class_name, module_class_name)
pwd = c.pwd()
path = os.path.join(pwd, module.replace('.', '/'))
if folder_module:
dirpath = path
filename = module.replace('.', '_')
path = os.path.join(path, filename)

path = path + '.py'
dirpath = os.path.dirname(path)
if os.path.exists(path) and not update:
return {'success': True, 'msg': f'Module {module} already exists', 'path': path}
if not os.path.exists(dirpath):
os.makedirs(dirpath, exist_ok=True)

c.put_text(path, base_module_code)

return {'success': True, 'msg': f'Created module {module}', 'path': path}

add_module = new_module


@classmethod
def has_local_module(cls, path=None):
import commune as c
path = '.' if path == None else path
if os.path.exists(f'{path}/module.py'):
text = c.get_text(f'{path}/module.py')
if 'class ' in text:
return True
return False
Loading

0 comments on commit 58ebbe2

Please sign in to comment.