Skip to content

Commit

Permalink
Update snet_gate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWesker authored Oct 25, 2024
1 parent 18875b1 commit 67ded73
Showing 1 changed file with 6 additions and 56 deletions.
62 changes: 6 additions & 56 deletions python/hyperon/exts/snet_io/snet_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
from snet import sdk
from hyperon import *


class SNetSDKWrapper:

def __init__(self):
self.snet_sdk = None

def init_sdk(self,
private_key=os.getenv("SNET_PRIVATE_KEY", '0' * 32),
private_key=os.getenv("SNET_PRIVATE_KEY", '0'*32),
eth_rpc_endpoint=os.getenv("ETH_RPC_ENDPOINT"),
email=os.getenv("SNET_EMAIL"),
identity_name="hyperon",
Expand All @@ -33,7 +32,7 @@ def init_sdk(self,

def organization_list(self):
return self.snet_sdk.get_organization_list()

def service_list(self, org_id):
return self.snet_sdk.get_services_list(org_id)

Expand All @@ -44,7 +43,7 @@ def create_service_client(self, org_id, service_id,
free_call_token_expiry_block = int(free_call_token_expiry_block)
service_client = self.snet_sdk.create_service_client(
org_id=org_id, service_id=service_id,
# group_name="default_group",
#group_name="default_group",
free_call_auth_token_bin=free_call_auth_token_bin,
free_call_token_expiry_block=free_call_token_expiry_block)
return ServiceCall(service_client)
Expand All @@ -58,7 +57,7 @@ def create_service_space(self, org_id, service_id, **kwargs):
space = GroundingSpaceRef()
service_client = self.create_service_client(org_id, service_id, **kwargs)
space.add_atom(E(S('='), E(S(org_id), S(service_id)),
service_client.get_operation_atom()))
service_client.get_operation_atom()))
atoms = service_client.generate_callers()
for atom in atoms:
space.add_atom(atom)
Expand Down Expand Up @@ -101,7 +100,7 @@ def __call__(self, command_a, *args_a):
service_client = self.create_service_client(*args, **kwargs)
return [service_client.get_operation_atom()]
return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a),
ValueAtom(f'unknown command {repr(command_a)}'))]
ValueAtom(f'unknown command {repr(command_a)}'))]


class ServiceCall:
Expand All @@ -115,9 +114,6 @@ def __init__(self, service_client):
self.outputs = []
self.io_types = []
self.func_names = []
self.len_threshold = 50
self.current_len = 0
self.len_to_last_eol = 0
for method in methods:
self.func_names += [method[0]]
types = method[1:]
Expand All @@ -144,51 +140,6 @@ def get_service_messages(self):
def get_operation_atom(self):
return OperationAtom(self.service_details[1], self)

def __pretty_print_atoms__(self, input_atoms):

def process_svg_atom(atom):
repr_atom = repr(atom)
self.current_len += len(repr_atom)
return repr_atom

def check_len(depth):
if self.current_len > self.len_threshold:
self.current_len = 0
return "\n" + "\t" * (depth - 1)
else:
return ""

def process_atom(atom, depth):
process_res = ""
metatype = atom.get_metatype()
if metatype == AtomKind.EXPR:
len_to_last_eol_flag = self.current_len > 5
self.current_len *= (depth <= 1) * (not len_to_last_eol_flag)
process_res += ("\n" + "\t" * depth) * (
depth > 0) * len_to_last_eol_flag + f"({process_expr_atom(atom, depth + 1)})"
elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE) or (metatype == AtomKind.GROUNDED):
process_res += process_svg_atom(atom) + check_len(depth)
else:
raise Exception(f"Unexpected type of the Atom: {str(metatype)}")
return process_res

def process_expr_atom(expr_atom, depth):
sub_atoms = expr_atom.get_children()
process_res = ""
for sub_atom in sub_atoms:
process_atom_res = process_atom(sub_atom, depth)
process_res += process_atom_res + check_len(depth)
process_res += " "
return process_res[:-1]

res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR))
self.current_len = 0
for atom in input_atoms:
res_string += process_atom(atom, 0)
res_string += "\n\n"
self.current_len = 0
return res_string

def generate_callers_text(self):
return self.__pretty_print_atoms__(self.generate_callers())

Expand All @@ -200,7 +151,7 @@ def _map_type(self, t):
type_map = {'bool': 'Bool',
'string': 'String',
'int32': 'Number',
'float ': 'Number'}
'float': 'Number'}
return type_map[t] if t in type_map else t

def generate_callers(self):
Expand Down Expand Up @@ -232,7 +183,6 @@ def generate_callers(self):
atoms += [metta_fun_type, function_expr]
return atoms


@register_atoms()
def snet_atoms():
defaultSDKAtom = OperationAtom("snet-sdk", SNetSDKWrapper(), unwrap=False)
Expand Down

0 comments on commit 67ded73

Please sign in to comment.