From 1a84e0c019efda22c4da4cfeaf895e51192da451 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 15 Feb 2023 10:09:25 +0100 Subject: [PATCH] Added link variables --- nomadnet/Node.py | 2 +- nomadnet/ui/textui/Browser.py | 48 +++++++++++++++++++----------- nomadnet/ui/textui/MicronParser.py | 12 ++------ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/nomadnet/Node.py b/nomadnet/Node.py index 13e1f41..525cf4c 100644 --- a/nomadnet/Node.py +++ b/nomadnet/Node.py @@ -160,7 +160,7 @@ class Node: if data != None and isinstance(data, dict): for e in data: - if isinstance(e, str) and e.startswith("field_"): + if isinstance(e, str) and (e.startswith("field_") or e.startswith("var_")): env_map[e] = data[e] generated = subprocess.run([file_path], stdout=subprocess.PIPE, env=env_map) diff --git a/nomadnet/ui/textui/Browser.py b/nomadnet/ui/textui/Browser.py index 6c3aa51..372f190 100644 --- a/nomadnet/ui/textui/Browser.py +++ b/nomadnet/ui/textui/Browser.py @@ -81,7 +81,7 @@ class Browser: self.aspects = aspects self.destination_hash = destination_hash self.path = path - self.field_data = None + self.request_data = None self.timeout = Browser.DEFAULT_TIMEOUT self.last_keypress = None @@ -162,10 +162,21 @@ class Browser: else: return destination_type - def handle_link(self, link_target, link_fields = None): - field_data = None - if link_fields != None: - field_data = {} + def handle_link(self, link_target, link_data = None): + request_data = None + if link_data != None: + link_fields = [] + request_data = {} + all_fields = True if "*" in link_data else False + + for e in link_data: + if "=" in e: + c = e.split("=") + if len(c) == 2: + request_data["var_"+str(c[0])] = str(c[1]) + else: + link_fields.append(e) + def recurse_down(w): target = None if isinstance(w, list): @@ -181,11 +192,11 @@ class Browser: elif hasattr(w, "_original_widget"): recurse_down(w._original_widget) else: - if hasattr(w, "field_name") and (link_fields == "all" or w.field_name in link_fields): - field_data["field_"+w.field_name] = w.get_edit_text() + if hasattr(w, "field_name") and (all_fields or w.field_name in link_fields): + request_data["field_"+w.field_name] = w.get_edit_text() recurse_down(self.attr_maps) - RNS.log("Found field data: "+str(field_data)) + RNS.log("Including request data: "+str(request_data), RNS.LOG_DEBUG) components = link_target.split("@") destination_type = None @@ -202,7 +213,7 @@ class Browser: RNS.log("Browser handling link to: "+str(link_target), RNS.LOG_DEBUG) self.browser_footer = urwid.Text("Opening link to: "+str(link_target)) try: - self.retrieve_url(link_target, field_data) + self.retrieve_url(link_target, request_data) except Exception as e: self.browser_footer = urwid.Text("Could not open link: "+str(e)) self.frame.contents["footer"] = (self.browser_footer, self.frame.options()) @@ -376,6 +387,7 @@ class Browser: if self.link != None: self.link.teardown() + self.request_data = None self.attr_maps = [] self.status = Browser.DISCONECTED self.response_progress = 0 @@ -390,7 +402,7 @@ class Browser: self.update_display() - def retrieve_url(self, url, field_data = None): + def retrieve_url(self, url, request_data = None): self.previous_destination_hash = self.destination_hash self.previous_path = self.path @@ -443,7 +455,7 @@ class Browser: else: self.set_destination_hash(destination_hash) self.set_path(path) - self.set_field_data(field_data) + self.set_request_data(request_data) self.load_page() def set_destination_hash(self, destination_hash): @@ -457,8 +469,8 @@ class Browser: def set_path(self, path): self.path = path - def set_field_data(self, field_data): - self.field_data = field_data + def set_request_data(self, request_data): + self.request_data = request_data def set_timeout(self, timeout): self.timeout = timeout @@ -665,7 +677,7 @@ class Browser: def load_page(self): - if self.field_data == None: + if self.request_data == None: cached = self.get_cached(self.current_url()) else: cached = None @@ -703,10 +715,10 @@ class Browser: page_data = b"The requested local page did not exist in the file system" if os.path.isfile(page_path): if os.access(page_path, os.X_OK): - if self.field_data != None: - env_map = self.field_data + if self.request_data != None: + env_map = self.request_data else: - env_map = None + env_map = {} generated = subprocess.run([page_path], stdout=subprocess.PIPE, env=env_map) page_data = generated.stdout else: @@ -794,7 +806,7 @@ class Browser: self.update_display() receipt = self.link.request( self.path, - data = self.field_data, + data = self.request_data, response_callback = self.response_received, failed_callback = self.request_failed, progress_callback = self.response_progressed diff --git a/nomadnet/ui/textui/MicronParser.py b/nomadnet/ui/textui/MicronParser.py index 917808c..f6a82e8 100644 --- a/nomadnet/ui/textui/MicronParser.py +++ b/nomadnet/ui/textui/MicronParser.py @@ -6,9 +6,6 @@ from urwid.util import is_mouse_press from urwid.text_layout import calc_coords import re -# TODO: remove -import RNS - DEFAULT_FG_DARK = "ddd" DEFAULT_FG_LIGHT = "222" DEFAULT_BG = "default" @@ -562,12 +559,9 @@ def make_output(state, line, url_delegate): if url_delegate != None: linkspec = LinkSpec(link_url, orig_spec) if link_fields != "": - if link_fields == "*": - linkspec.link_fields = "all" - else: - lf = link_fields.split("|") - if len(lf) > 0: - linkspec.link_fields = lf + lf = link_fields.split("|") + if len(lf) > 0: + linkspec.link_fields = lf output.append((linkspec, link_label)) else: