Skip to content

Commit

Permalink
sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Oct 1, 2024
1 parent 109e5f7 commit ae76041
Show file tree
Hide file tree
Showing 15 changed files with 2,674 additions and 6,342 deletions.
127 changes: 39 additions & 88 deletions commune/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

import commune as c
import json
import sys
import time
import os
import threading
import sys


Expand All @@ -15,121 +12,75 @@ class cli:
#

def __init__(self,
args = None,
module = 'module',
verbose = True,
forget_fns = ['module.key_info', 'module.save_keys'],
seperator = ' ',
buffer_size=4,
args = None,
base = 'module',
fn_splitters = [':', '/', '//', '::'],
helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info'],
save: bool = False):
sep = '--'):

self.helper_fns = helper_fns
self.seperator = seperator
self.buffer_size = buffer_size
self.verbose = verbose
self.save = save
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.fn_splitters = fn_splitters
self.sep = sep
self.base = c.module(base)()
self.forward(args)

def forward(self, argv=None):

t0 = time.time()
argv = argv or self.argv()
self.input_msg = 'c ' + ' '.join(argv)
output = None

init_kwargs = {}
if any([arg.startswith('--') for arg in argv]):
if any([arg.startswith(self.sep) for arg in argv]):
for arg in c.copy(argv):
if arg.startswith('--'):
key = arg[2:].split('=')[0]
if arg.startswith(self.sep):
key = arg[len(self.sep):]
if key in self.helper_fns:
new_argvs = self.argv()
new_argvs.remove(arg)
new_argvs = [key , new_argvs[0]]
return self.forward(new_argvs)
if '=' in arg:
value = arg.split('=')[1]
return self.forward([key , argv[0]])
else:
key = arg[2:]
value = True
argv.remove(arg)
init_kwargs[key] = self.determine_type(value)
if '=' in arg:
key, value = arg.split('=')
else:
value = True
argv.remove(arg)
init_kwargs[key] = self.determine_type(value)

# any of the --flags are init kwargs
if os.path.isdir(argv[0]):
argv[0] = c.path2simple(argv[0])

if ':' in argv[0]:
# {module}:{fn} arg1 arg2 arg3 ... argn
argv[0] = argv[0].replace(':', '/')

if '/' in argv[0]:
# prioritize the module over the function
module = '.'.join(argv[0].split('/')[:-1])
fn = argv[0].split('/')[-1]
argv = [module , fn , *argv[1:]]
is_fn = False
fn = argv.pop(0)
fn_obj = None
if fn in dir(self.base):
print(fn, self.base)
fn_obj = getattr(self.base, fn)
else:
is_fn = argv[0] in self.base_module_attributes

if is_fn:
module = self.base_module
fn = argv.pop(0)
else:
module = argv.pop(0)
fn = argv.pop(0)



if isinstance(module, str):
module = c.get_module(module)
# module = self.base_module.from_object(module)
module_name = module.module_name()
fn_path = f'{module_name}/{fn}'
fn_obj = getattr(module, fn)
fn_type = c.classify_fn(fn_obj)
is_property = c.is_property(fn_obj)
print(fn_type)
if fn_type == 'self' or len(init_kwargs) > 0 or is_property:
fn_obj = getattr(module(**init_kwargs), fn)
# calling function buffer
input_msg = f'[bold]fn[/bold]: {fn_path}'

for fs in self.fn_splitters:
if fs in fn:
module, fn = fn.split(fs)
module = c.get_module(module)
fn_obj = getattr(module, fn)
fn_type = c.classify_fn(fn_obj)
if fn_type == 'self':
fn_obj = getattr(module(**init_kwargs), fn)
print(fn_obj,fn_type)
break

if callable(fn_obj):
args, kwargs = self.parse_args(argv)
if len(args) > 0 or len(kwargs) > 0:
inputs = {"args":args, "kwargs":kwargs}
input_msg += ' ' + f'[purple][bold]params:[/bold] {json.dumps(inputs)}[/purple]'
output = lambda: fn_obj(*args, **kwargs)
else:
output = lambda: fn_obj
self.input_msg = input_msg
output = fn_obj(*args, **kwargs)
else:
output = fn_obj

buffer = '⚡️'*4
c.print(buffer+input_msg+buffer, color='yellow')
output = output()
c.print(buffer+fn+buffer, color='yellow')
latency = time.time() - t0
is_error = c.is_error(output)

if is_error:
buffer = '❌'
msg = f'Error(latency={latency:.3f})'
else:
buffer = '✅'
msg = f'Result(latency={latency:.3f})'

print(buffer + msg + buffer)

num_spacers = max(0, len(self.input_msg) - len(msg) )
left_spacers = num_spacers//2
right_spacers = num_spacers - left_spacers
msg = self.seperator*left_spacers + msg + self.seperator*right_spacers
buffer = self.buffer_size * buffer
is_generator = c.is_generator(output)

if is_generator:
# print the items side by side instead of vertically
for item in output:
Expand Down
7 changes: 0 additions & 7 deletions commune/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
import sys
import time
import queue
import random
import weakref
import itertools
import threading
from loguru import logger
from typing import Callable
import concurrent
from concurrent.futures._base import Future
import commune as c
import gc

import time
from concurrent.futures._base import Future
import commune as c

Expand Down
36 changes: 9 additions & 27 deletions commune/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MnemonicLanguageCode:
SPANISH = 'es'

class Key(c.Module):
crypto_type = 'sr25519'
def __init__(self,
private_key: Union[bytes, str] = None,
mnemonic: str = None,
Expand Down Expand Up @@ -335,19 +336,19 @@ def get_key_address(cls, key):

@classmethod
def rm_key(cls, key=None):

key2path = cls.key2path()
keys = list(key2path.keys())
if key not in keys:
raise Exception(f'key {key} not found, available keys: {keys}')
c.rm(key2path[key])
return {'deleted':[key]}


@property
def key_type(self):
return self.crypto_type2name(self.crypto_type).lower()


@classmethod
def rm_keys(cls, rm_keys, verbose:bool=False):

Expand All @@ -361,7 +362,6 @@ def rm_keys(cls, rm_keys, verbose:bool=False):
cls.rm_key(rm_key)

return {'removed_keys':rm_keys}


crypto_types = ['ED25519', 'SR25519', 'ECDSA']

Expand All @@ -384,7 +384,6 @@ def crypto_type2name(cls, crypto_type:str):
crypto_type_map ={v:k for k,v in cls.crypto_type_map().items()}
return crypto_type_map[crypto_type]


@classmethod
def resolve_crypto_type(cls, crypto_type):
if isinstance(crypto_type, str):
Expand Down Expand Up @@ -1122,34 +1121,21 @@ def duplicate_keys(cls) -> dict:
@classmethod
def from_private_key(cls, private_key:str):
return cls(private_key=private_key)

@classmethod
def valid_ss58_address(cls, address: str ) -> bool:
def valid_ss58_address(cls, address: str, __ss58_format__=c.__ss58_format__ ) -> bool:
"""
Checks if the given address is a valid ss58 address.
Args:
address(str): The address to check.
Returns:
True if the address is a valid ss58 address for Bittensor, False otherwise.
"""
try:
return ss58.is_valid_ss58_address( address, valid_ss58_format=c.__ss58_format__ )
except (IndexError):
return ss58.is_valid_ss58_address( address , valid_ss58_format =__ss58_format__ )
except Exception as e:
return False

@classmethod
def is_valid_ed25519_pubkey(cls, public_key: Union[str, bytes] ) -> bool:
def is_valid_ed25519_pubkey(cls, public_key: Union[str, bytes], ss58_format=c.__ss58_format__) -> bool:
"""
Checks if the given public_key is a valid ed25519 key.
Args:
public_key(Union[str, bytes]): The public_key to check.
Returns:
True if the public_key is a valid ed25519 key, False otherwise.
"""
try:
if isinstance( public_key, str ):
Expand All @@ -1161,8 +1147,7 @@ def is_valid_ed25519_pubkey(cls, public_key: Union[str, bytes] ) -> bool:
else:
raise ValueError( "public_key must be a string or bytes" )

keypair = Key(public_key=public_key,
ss58_format=c.__ss58_format__)
keypair = Key(public_key=public_key, ss58_format=ss58_format)

ss58_addr = keypair.ss58_address
return ss58_addr is not None
Expand Down Expand Up @@ -1197,7 +1182,6 @@ def is_valid_address_or_public_key(cls, address: Union[str, bytes] ) -> bool:

def id_card(self, return_json=True,**kwargs):
return self.sign(str(c.timestamp()), return_json=return_json, **kwargs)


@staticmethod
def is_ss58(address):
Expand Down Expand Up @@ -1280,8 +1264,6 @@ def resolve_key_address(cls, key):
else:
address = key
return address



# if __name__ == "__main__":
# Key.run()
Expand Down
Loading

0 comments on commit ae76041

Please sign in to comment.