diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 400512ba5..6bce0ae24 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -4,13 +4,14 @@ 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", @@ -32,7 +33,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) @@ -43,7 +44,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) @@ -57,7 +58,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) @@ -100,7 +101,58 @@ 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)}'))] + +def pretty_print_atoms(input_atoms): + len_threshold = 50 + current_len = 0 + def process_svg_atom(atom): + nonlocal len_threshold + nonlocal current_len + repr_atom = repr(atom) + current_len += len(repr_atom) + return repr_atom + + def check_len(depth): + nonlocal len_threshold + nonlocal current_len + if current_len > len_threshold: + current_len = 0 + return "\n" + "\t" * (depth - 1) + else: + return "" + + def process_atom(atom, depth): + nonlocal len_threshold + nonlocal current_len + process_res = "" + metatype = atom.get_metatype() + if metatype == AtomKind.EXPR: + len_to_last_eol_flag = current_len > 5 + 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)) + for atom in input_atoms: + res_string += process_atom(atom, 0) + res_string += "\n\n" + current_len = 0 + return res_string class ServiceCall: @@ -139,14 +191,13 @@ def get_service_messages(self): def get_operation_atom(self): return OperationAtom(self.service_details[1], self) + def generate_callers_text(self): + return pretty_print_atoms(self.generate_callers()) + def open_channel_and_deposit(self, amount, expiration): self.service_client.deposit_and_open_channel(amount, expiration) return [E()] - def generate_callers_text(self): - # TODO: pretty print - return "\n".join([repr(e) for e in self.generate_callers()]) - def _map_type(self, t): type_map = {'bool': 'Bool', 'string': 'String', @@ -183,6 +234,7 @@ def generate_callers(self): atoms += [metta_fun_type, function_expr] return atoms + @register_atoms() def snet_atoms(): defaultSDKAtom = OperationAtom("snet-sdk", SNetSDKWrapper(), unwrap=False) diff --git a/python/hyperon/exts/snet_io/test_snet_call.metta b/python/hyperon/exts/snet_io/test_snet_call.metta index 2db17ac9f..7ecabda78 100644 --- a/python/hyperon/exts/snet_io/test_snet_call.metta +++ b/python/hyperon/exts/snet_io/test_snet_call.metta @@ -46,3 +46,6 @@ ; possible usage of open_channel_and_deposit function. 100 is the amount of AGIX and 10000 is a payment channel's TTL in blocks (snet-sdk open_channel_and_deposit &generative-lms 100 10000) + +; usage of generate_callers_text method +((py-dot &generative-lms generate_callers_text)) \ No newline at end of file