Skip to content

Commit

Permalink
vali updates
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Aug 9, 2024
1 parent bd00c07 commit 1fe81c4
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 485 deletions.
2 changes: 1 addition & 1 deletion commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_output(self, argv):
if len(init_kwargs) > 0 or fn_class == 'self':
print('init_kwargs', init_kwargs)
module = module(**init_kwargs)
print(module)
module_name = module.module_name()
fn_path = f'{module_name}/{fn}'
try:
Expand Down
21 changes: 12 additions & 9 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ def call(cls,
*args,
kwargs = None,
params = None,
module : str = None,
module : str = 'module',
network:str = 'local',
key:str = None,
timeout=40,
**extra_kwargs) -> None:
if '/' in str(fn):
module = '.'.join(fn.split('/')[:-1])
fn = fn.split('/')[-1]
else:
module = fn
fn = 'info'
client = cls.connect(module, virtual=False, network=network)
return client.forward(fn=fn,
args=args,
Expand Down Expand Up @@ -114,22 +117,21 @@ def get_params(self, args=None, kwargs=None, params=None, version=1):
return input


def get_url(self, fn, module, mode='http', network=None):
def get_url(self, fn, mode='http', network=None):
network = network or self.network
module = module or self.module
if '/' in str(module):
if '://' in str(fn):
mode ,fn = fn.split('://')
if '/' in str(fn):
module, fn = module.split('/')
else:
module = self.module
if '/' in str(fn):
module, fn = fn.split('/')
if '/' in module.split('://')[-1]:
module = module.split('://')[-1]

namespace = self.resolve_namespace(network)
if module in namespace:
module = namespace[module]
url = f"{module}/{fn}/"
url = f'{mode}://' + url if not url.startswith(f'{mode}://') else url

return url


Expand All @@ -151,7 +153,8 @@ async def async_forward(self,
mode = 'http',
**extra_kwargs):
key = self.resolve_key(key)
url = self.get_url(fn=fn, module=module,mode=mode, network=network)
network = network or self.network
url = self.get_url(fn=fn,mode=mode, network=network)
kwargs = {**(kwargs or {}), **extra_kwargs}
input = self.get_params( args=args,
kwargs=kwargs,
Expand Down
13 changes: 3 additions & 10 deletions commune/module/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ def determine_type(cls, x):
except ValueError:
return x


@classmethod
def resolve_object(cls, obj) -> Any:
return obj or cls


@classmethod
def fn2code(cls, search=None, module=None)-> Dict[str, str]:
Expand Down Expand Up @@ -400,9 +395,9 @@ def lines_of_code(cls, code:str=None):
def code(cls, module = None, search=None, *args, **kwargs):
if '/' in str(module) or module in cls.fns():
return cls.fn_code(module)

module = cls.resolve_object(module)
text = cls.get_text( module.pypath(), *args, **kwargs)
print(module)
text = cls.get_text( module.filepath(), *args, **kwargs)
if search != None:
find_lines = cls.find_lines(text=text, search=search)
return find_lines
Expand Down Expand Up @@ -494,7 +489,6 @@ def set_line(cls, idx:int, text:str):
front_lines = lines[:idx]
back_lines = lines[idx:]
new_lines = text.split('\n')
c.print(new_lines)
lines = front_lines + new_lines + back_lines
else:
lines[idx-1] = text
Expand Down Expand Up @@ -1101,9 +1095,8 @@ def add_line(cls, path:str, text:str, line=None) -> None:
# Write the text to the file
if line != None:
line=int(line)
lines = c.get_text(path).split('\n')
lines = cls.get_text(path).split('\n')
lines = lines[:line] + [text] + lines[line:]
c.print(lines)

text = '\n'.join(lines)
with open(path, 'w') as file:
Expand Down
60 changes: 60 additions & 0 deletions commune/module/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import threading
from typing import *

from typing import Union
import threading



class Task:

@classmethod
Expand Down Expand Up @@ -228,3 +233,58 @@ async def _asubmit():
kwargs.update(kwargs.pop('kwargs',{}))
return fn(*args, **kwargs)
return _asubmit()



thread_map = {}

@classmethod
def thread(cls,fn: Union['callable', str],
args:list = None,
kwargs:dict = None,
daemon:bool = True,
name = None,
tag = None,
start:bool = True,
tag_seperator:str='::',
**extra_kwargs):

if isinstance(fn, str):
fn = cls.get_fn(fn)
if args == None:
args = []
if kwargs == None:
kwargs = {}

assert callable(fn), f'target must be callable, got {fn}'
assert isinstance(args, list), f'args must be a list, got {args}'
assert isinstance(kwargs, dict), f'kwargs must be a dict, got {kwargs}'

# unique thread name
if name == None:
name = fn.__name__
cnt = 0
while name in cls.thread_map:
cnt += 1
if tag == None:
tag = ''
name = name + tag_seperator + tag + str(cnt)

if name in cls.thread_map:
cls.thread_map[name].join()

t = threading.Thread(target=fn, args=args, kwargs=kwargs, **extra_kwargs)
# set the time it starts
setattr(t, 'start_time', cls.time())
t.daemon = daemon
if start:
t.start()
cls.thread_map[name] = t
return t

@classmethod
def threads(cls, search:str = None):
threads = list(cls.thread_map.keys())
if search != None:
threads = [t for t in threads if search in t]
return threads
21 changes: 10 additions & 11 deletions commune/module/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ def resolve_extension(cls, filename:str, extension = '.py') -> str:
@classmethod
def simple2path(cls,
simple:str,
tree = None,
extension = '.py',

verbose = 1,
avoid_dirnames = ['', '/src', '/commune', '/commune/module', '/commune/modules', '/modules', '/blocks', '/agents', 'commune/agents'],
**kwargs) -> bool:
"""
converts the module path to a file path
Expand All @@ -33,6 +31,8 @@ def simple2path(cls,
"""
# if cls.libname in simple and '/' not in simple and cls.can_import_module(simple):
# return simple
shortcuts = cls.shortcuts()
simple = shortcuts.get(simple, simple)


if simple.endswith(extension):
Expand All @@ -43,10 +43,9 @@ def simple2path(cls,
path_options = []
simple = simple.replace('/', '.')

avoid_dirnames = ['', '/src', '/modules', '/commune']
local_prefix_paths = list([pwd+x for x in avoid_dirnames])
root_prefix_paths = list([cls.root_path + x for x in avoid_dirnames])
dir_paths = local_prefix_paths+root_prefix_paths # pwd prefixes
dir_paths = list([pwd+x for x in avoid_dirnames]) # local first
dir_paths += list([cls.libpath + x for x in avoid_dirnames]) # add libpath stuff

for dir_path in dir_paths:
# '/' count how many times the path has been split
module_dirpath = dir_path + '/' + simple.replace('.', '/')
Expand All @@ -58,16 +57,15 @@ def simple2path(cls,
module_filepath = dir_path + '/' + cls.resolve_extension(simple.replace('.', '/'), extension=extension)
path_options += [module_filepath]


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

if path != None:
break
Expand Down Expand Up @@ -439,12 +437,13 @@ def simple2objectpath(cls,
object_path = object_path[len(cls.libpath)+1:]

object_path = object_path.replace('.py', '')

object_path = object_path.replace('/', '.')
if object_path.startswith('.'):
object_path = object_path[1:]
if '.__init__' in object_path:
object_path = object_path.replace('__init__', '')
object_path = object_path.replace('.__init__', '')

object_path = object_path + '.' + classes[-1]
return object_path

Expand Down
70 changes: 36 additions & 34 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class c(*CORE_MODULES):
'is_admin',
'namespace',
'whitelist',
'forward',
'fns'] # whitelist of helper functions to load
cost = 1
description = """This is a module"""
Expand Down Expand Up @@ -126,7 +127,6 @@ def dirpath(cls) -> str:
def module_name(cls, obj=None):
if hasattr(cls, 'name') and isinstance(cls.name, str):
return cls.name

obj = cls.resolve_object(obj)
module_file = inspect.getfile(obj)
return cls.path2simple(module_file)
Expand Down Expand Up @@ -154,6 +154,7 @@ def sandbox(cls):
sand = sandbox

module_cache = {}
_obj = None
@classmethod
def get_module(cls,
path:str = 'module',
Expand Down Expand Up @@ -191,6 +192,8 @@ def get_module(cls,
if cache and cache_key in c.module_cache:
module = c.module_cache[cache_key]
return module


module = c.simple2object(path)

# ensure module
Expand All @@ -202,23 +205,34 @@ def get_module(cls,
if init_kwargs != None:
module = module(**init_kwargs)

module = c.resolve_module(module)

is_module = c.is_module(module)
if not is_module:
module = cls.obj2module(module)
if cache:
c.module_cache[cache_key] = module



c.module_cache[cache_key] = module
return module

@classmethod
def resolve_module(self,module):
if not hasattr(module, 'module_name'):
setattr(module, 'module_name', lambda: module.__name__.lower())

return module

def obj2module(cls,obj):
import commune as c
class WrapperModule(c.Module):
_obj = obj
def __name__(self):
return self._obj.__name__
def __class__(self):
return self._obj.__class__

m = WrapperModule

for fn in dir(obj):
try:
setattr(m, fn, getattr(obj, fn))
except:
pass

return m()




@classmethod
Expand Down Expand Up @@ -336,12 +350,11 @@ def resolve_server_name(cls,
resolve_name = resolve_server_name

@classmethod
def resolve_object(cls, module:str = None, **kwargs):
if module == None:
module = cls.module_name()
if isinstance(module, str):
module = c.module(module)
return module
def resolve_object(cls, obj:str = None, **kwargs):
if cls._obj != None:
return cls._obj
else:
return obj or cls

def self_destruct(self):
c.kill(self.server_name)
Expand Down Expand Up @@ -815,21 +828,6 @@ def new_module( cls,
return {'success': True, 'msg': f'Created module {module}', 'path': path}

add_module = new_module

@classmethod
def resolve_object(cls, module=None):
"""
Resolves the moduls from the class
Case type(module):
if None -> cls, the class method of the object
if str -> c.module({module})
if
"""
if module == None:
module = cls
if isinstance(module, str):
module = c.module(module)
return module


thread_map = {}
Expand Down Expand Up @@ -1047,6 +1045,10 @@ def find_word(self, word:str, path='./')-> str:
progress.update(1)
return found_files






# def update(self):
# c.ip(update=1)
Expand Down
1 change: 1 addition & 0 deletions commune/module/shortcuts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ w: wombo
router: model.openrouter
ticket: key.ticket
namespace: server.namespace
serializer: server.serializer

4 changes: 4 additions & 0 deletions commune/modules/fam/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import commune as c

class Fam(c.Module):
fam = 1
File renamed without changes.
Loading

0 comments on commit 1fe81c4

Please sign in to comment.