Skip to content

Commit

Permalink
refsctoring core to less files
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 3, 2024
1 parent 04d357b commit b9ffee8
Show file tree
Hide file tree
Showing 55 changed files with 6,870 additions and 12,455 deletions.
File renamed without changes.
18 changes: 2 additions & 16 deletions commune/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@



from .module.module import Module
from .module import Module
from functools import partial
# set the module functions as globals
for k,v in Module.__dict__.items():
globals()[k] = v

# for f in :
# globals()[f] = getattr(Module, f)

for f in Module.class_functions() + Module.static_functions():
globals()[f] = getattr(Module, f)

for f in Module.self_functions():
def wrapper_fn(f, *args, **kwargs):
try:
fn = getattr(Module(), f)
except:
fn = getattr(Module, f)
return fn(*args, **kwargs)
globals()[f] = partial(wrapper_fn, f)



c = Block = Lego = M = Module # alias c.Module as c.Block, c.Lego, c.M
c.add_to_globals(globals())
22 changes: 0 additions & 22 deletions commune/app/login/login.py

This file was deleted.

15 changes: 13 additions & 2 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def __init__(self,
self.forget_fns = forget_fns
self.base_module = c.module(module) if isinstance(module, str) else module
self.base_module_attributes = list(set(self.base_module.functions() + self.base_module.attributes()))

self.forward(args)

def forward(self, argv=None):
Expand All @@ -47,6 +46,18 @@ def forward(self, argv=None):
for arg in c.copy(argv):
if arg.startswith('--'):
key = arg[2:].split('=')[0]
# if key == 'cwd':
# new_argv = []
# for item in c.copy(argv):
# if '--cwd' in item:
# continue
# new_argv.append(item)
# new_cmd = 'c ' + ' '.join(c.copy(new_argv))

# cwd = c.resolve_path(arg.split('=')[1])
# v = c.cmd(f'{new_cmd}', cwd=cwd)
# c.print(v)
# return new_cmd
if key in self.helper_fns:
new_argvs = self.argv()
new_argvs.remove(arg)
Expand Down Expand Up @@ -214,6 +225,6 @@ def determine_type(cls, x):

def argv(self):
return sys.argv[1:]

def main():
cli()
156 changes: 152 additions & 4 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,38 @@
import commune as c
import json
import requests
from .tools import ClientTools
from .virtual import ClientVirtual

# from .pool import ClientPool

class Client(c.Module, ClientTools):

class ClientVirtual:
protected_attributes = [ 'client', 'remote_call']

def __init__(self, client: str ='ReactAgentModule'):
if isinstance(client, str):
client = c.connect(client)
self.client = client

def remote_call(self, remote_fn, *args, return_future= False, timeout:int=10, key=None, **kwargs):
result = self.client.forward(fn=remote_fn, args=args, kwargs=kwargs, timeout=timeout, key=key, return_future=return_future)
return result

def __str__(self):
return str(self.client)

def __repr__(self):
return self.__str__()

def __getattr__(self, key):

if key in self.protected_attributes :
return getattr(self, key)
else:
return lambda *args, **kwargs : self.remote_call( remote_fn=key, *args, **kwargs)



class Client(c.Module):
network2namespace = {}
def __init__(
self,
Expand Down Expand Up @@ -236,4 +262,126 @@ def process_stream_line(self, line , stream_prefix=None):
if isinstance(event_data, str):
if event_data.startswith('{') and event_data.endswith('}') and 'data' in event_data:
event_data = json.loads(event_data)['data']
return event_data
return event_data



@classmethod
def call_search(cls,
search : str,
*args,
timeout : int = 10,
network:str = 'local',
key:str = None,
kwargs = None,
**extra_kwargs) -> None:
if '/' in search:
search, fn = search.split('/')
namespace = c.namespace(search=search, network=network)
future2module = {}
for module, address in namespace.items():
c.print(f"Calling {module}/{fn}", color='green')
future = c.submit(cls.call,
args = [module, fn] + list(args),
kwargs = {'timeout': timeout,
'network': network, 'key': key,
'kwargs': kwargs,
**extra_kwargs} , timeout=timeout)
future2module[future] = module
futures = list(future2module.keys())
result = {}
progress_bar = c.tqdm(len(futures))
for future in c.as_completed(futures, timeout=timeout):
module = future2module.pop(future)
futures.remove(future)
progress_bar.update(1)
result[module] = future.result()

return result


@classmethod
def call_pool(cls,
modules,
fn = 'info',
*args,
network = 'local',
timeout = 10,
n=None,
**kwargs):

args = args or []
kwargs = kwargs or {}

if isinstance(modules, str) or modules == None:
modules = c.servers(modules, network=network)
if n == None:
n = len(modules)
modules = cls.shuffle(modules)[:n]
assert isinstance(modules, list), 'modules must be a list'
futures = []
for m in modules:
job_kwargs = {'module': m, 'fn': fn, 'network': network, **kwargs}
future = c.submit(c.call, kwargs=job_kwargs, args=[*args] , timeout=timeout)
futures.append(future)
responses = c.wait(futures, timeout=timeout)
return responses


@classmethod
def connect_pool(cls, modules=None, *args, return_dict:bool=False, **kwargs):
if modules == None:
modules = c.servers(modules)

module_clients = cls.gather([cls.async_connect(m, ignore_error=True,**kwargs) for m in modules])
if return_dict:
return dict(zip(modules, module_clients))
return module_clients


@staticmethod
def check_response(x) -> bool:
if isinstance(x, dict) and 'error' in x:
return False
else:
return True


def get_curl(self,
fn='info',
params=None,
args=None,
kwargs=None,
timeout=10,
module=None,
key=None,
headers={'Content-Type': 'application/json'},
network=None,
version=1,
mode='http',
**extra_kwargs):
key = self.resolve_key(key)
network = network or self.network
url = self.get_url(fn=fn, mode=mode, network=network)
kwargs = {**(kwargs or {}), **extra_kwargs}
input_data = self.get_params(args=args, kwargs=kwargs, params=params, version=version)

# Convert the headers to curl format
headers_str = ' '.join([f'-H "{k}: {v}"' for k, v in headers.items()])

# Convert the input data to JSON string
data_str = json.dumps(input_data).replace('"', '\\"')

# Construct the curl command
curl_command = f'curl -X POST {headers_str} -d "{data_str}" "{url}"'

return curl_command


def run_curl(self, *args, **kwargs):
curl_command = self.get_curl(*args, **kwargs)
# get the output of the curl command
import subprocess
output = subprocess.check_output(curl_command, shell=True)
return output.decode('utf-8')

132 changes: 0 additions & 132 deletions commune/client/tools.py

This file was deleted.

Loading

0 comments on commit b9ffee8

Please sign in to comment.