Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 7, 2024
1 parent ff08d41 commit b04c27c
Show file tree
Hide file tree
Showing 37 changed files with 331 additions and 4,792 deletions.
5 changes: 3 additions & 2 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class cli(c.Module):
def __init__(self,
args = None,
module = 'module',
new_event_loop: bool = True,
verbose = True,
save: bool = True):
self.base_module = c.module(module)
Expand All @@ -28,18 +27,20 @@ def __init__(self,
output = output.toDict()
c.print(output, verbose=verbose)

if save:
if save and c.jsonable(output):
self.history_module().add({'input': 'c ' + ' '.join(args), 'output': output})

def get_output(self, args):

args, kwargs = self.parse_args(args)


base_module_attributes = list(set(self.base_module.functions() + self.base_module.get_attributes()))
# is it a fucntion, assume it is for the module
# handle module/function
is_fn = args[0] in base_module_attributes


if '/' in args[0]:
args = args[0].split('/') + args[1:]
is_fn = False
Expand Down
11 changes: 11 additions & 0 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def prepare_request(self, args: list = None, kwargs: dict = None, params=None, m

# serialize this into a json string
if message_type == "v0":
"""
{
'data' : {
'args': args,
'kwargs': kwargs,
'timestamp': timestamp,
}
'signature': signature
}
"""
request = self.serializer.serialize(input)
request = self.key.sign(request, return_json=True)
# key emoji
Expand Down
7 changes: 4 additions & 3 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def get_keys(cls, search=None, clean_failed_keys=False):


@classmethod
def key2address(cls, search=None, max_age=100000, update=False, **kwargs):
def key2address(cls, search=None, max_age=None, update=False, **kwargs):
path = 'key2address'
key2address = []
key2address = cls.get(path, key2address,max_age=max_age, update=update)
Expand Down Expand Up @@ -963,7 +963,8 @@ def signature2address(self, sig, **kwargs):
return self.verify(sig, return_address=True, **kwargs)
sig2addy = signature2address

def verify(self, data: Union[ScaleBytes, bytes, str, dict],
def verify(self,
data: Union[ScaleBytes, bytes, str, dict],
signature: Union[bytes, str] = None,
public_key:Optional[str]= None,
return_address = False,
Expand Down Expand Up @@ -995,7 +996,7 @@ def verify(self, data: Union[ScaleBytes, bytes, str, dict],
if isinstance(data, dict):

signature = data.pop('signature')
public_key = c.ss58_decode(data.pop('address'))
public_key = c.ss58_decode(data['address'])
if 'data' in data:
data = data.pop('data')

Expand Down
36 changes: 29 additions & 7 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class c:
'blacklist',
'fns'] # whitelist of helper functions to load
cost = 1

description = """This is a module"""
base_module = 'module' # the base module
encrypted_prefix = 'ENCRYPTED' # the prefix for encrypted values
Expand Down Expand Up @@ -1632,16 +1633,18 @@ def repo2module(self, repo:str, name=None, template_module='demo', **kwargs):


@classmethod
def simple2path(cls, path:str, **kwargs) -> str:
def simple2path(cls, path:str, trials=3, **kwargs) -> str:
tree = c.tree(**kwargs)
if path not in tree:
shortcuts = c.shortcuts()
if path in shortcuts:
path = shortcuts[path]
else:
modules = c.modules(path)
raise Exception(f'Could not find {path} in {modules} modules')

if trials > 0:
c.tree(update=True)
return c.simple2path(path, trials=trials-1, **kwargs)

raise Exception(f'Could not find {path} in {c.modules(path)} modules')
return tree[path]


Expand Down Expand Up @@ -2095,6 +2098,7 @@ def get_file_size(cls, path:str):
@classmethod
def ls(cls, path:str = '',
recursive:bool = False,
search = None,
return_full_path:bool = True):
"""
provides a list of files in the path
Expand All @@ -2111,6 +2115,8 @@ def ls(cls, path:str = '',
ls_files = [os.path.abspath(os.path.join(path,f)) for f in ls_files]

ls_files = sorted(ls_files)
if search != None:
ls_files = list(filter(lambda x: search in x, ls_files))
return ls_files

@classmethod
Expand Down Expand Up @@ -2447,7 +2453,6 @@ def wait_for_server(cls,
def attributes(self):
return list(self.__dict__.keys())



@classmethod
def get_attributes(cls, search = None, obj=None):
Expand Down Expand Up @@ -2537,8 +2542,9 @@ def namespace(cls,
update: bool = False,
**kwargs):
namespace = c.module("namespace").namespace(search=search, network=network, update=update, **kwargs)
namespace = dict(sorted(namespace.items(), key=lambda x: x[0]))
return namespace


get_namespace = namespace
@classmethod
def rm_namespace(cls, *args, **kwargs):
Expand Down Expand Up @@ -6415,14 +6421,28 @@ def has_function_arg(cls, fn, arg:str):

@classmethod
def classify_fn(cls, fn):
fn = cls.get_fn(fn)

if not callable(fn):
fn = cls.get_fn(fn)
if not callable(fn):
return None
args = cls.get_function_args(fn)
if len(args) == 0:
return 'static'
elif args[0] == 'self':
return 'self'
else:
return 'class'

def fn2type(self):
fn2type = {}
fns = self.fns()
for f in fns:
if callable(getattr(self, f)):
fn2type[f] = self.classify_fn(getattr(self, f))
return fn2type



@classmethod
def build(cls, *args, **kwargs):
Expand Down Expand Up @@ -6524,7 +6544,9 @@ def get_functions(cls, obj: Any = None,
for fn_name in dir_list:
if search != None and search not in fn_name:
continue

fn_obj = getattr(obj, fn_name)

if not callable(fn_obj):
continue
# skip hidden functions if include_hidden is False
Expand Down
4 changes: 4 additions & 0 deletions commune/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def namespace(cls, search=None,
max_age:int = None, **kwargs) -> dict:

network = network or 'local'

path = network

namespace = cls.get(path, None, max_age=max_age)
Expand Down Expand Up @@ -54,6 +55,9 @@ def namespace(cls, search=None,
namespace = {k:v.replace(c.default_ip, c.ip()) for k,v in namespace.items()}

namespace = {k:v for k,v in sorted(namespace.items(), key=lambda x: x[0])}

namespace = dict(sorted(namespace.items(), key=lambda x: x[0]))

return namespace

namespace = namespace
Expand Down
Loading

0 comments on commit b04c27c

Please sign in to comment.