Added link variables

This commit is contained in:
Mark Qvist 2023-02-15 10:09:25 +01:00
parent c134ee180a
commit 1a84e0c019
3 changed files with 34 additions and 28 deletions

View File

@ -160,7 +160,7 @@ class Node:
if data != None and isinstance(data, dict): if data != None and isinstance(data, dict):
for e in data: 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] env_map[e] = data[e]
generated = subprocess.run([file_path], stdout=subprocess.PIPE, env=env_map) generated = subprocess.run([file_path], stdout=subprocess.PIPE, env=env_map)

View File

@ -81,7 +81,7 @@ class Browser:
self.aspects = aspects self.aspects = aspects
self.destination_hash = destination_hash self.destination_hash = destination_hash
self.path = path self.path = path
self.field_data = None self.request_data = None
self.timeout = Browser.DEFAULT_TIMEOUT self.timeout = Browser.DEFAULT_TIMEOUT
self.last_keypress = None self.last_keypress = None
@ -162,10 +162,21 @@ class Browser:
else: else:
return destination_type return destination_type
def handle_link(self, link_target, link_fields = None): def handle_link(self, link_target, link_data = None):
field_data = None request_data = None
if link_fields != None: if link_data != None:
field_data = {} 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): def recurse_down(w):
target = None target = None
if isinstance(w, list): if isinstance(w, list):
@ -181,11 +192,11 @@ class Browser:
elif hasattr(w, "_original_widget"): elif hasattr(w, "_original_widget"):
recurse_down(w._original_widget) recurse_down(w._original_widget)
else: else:
if hasattr(w, "field_name") and (link_fields == "all" or w.field_name in link_fields): if hasattr(w, "field_name") and (all_fields or w.field_name in link_fields):
field_data["field_"+w.field_name] = w.get_edit_text() request_data["field_"+w.field_name] = w.get_edit_text()
recurse_down(self.attr_maps) 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("@") components = link_target.split("@")
destination_type = None destination_type = None
@ -202,7 +213,7 @@ class Browser:
RNS.log("Browser handling link to: "+str(link_target), RNS.LOG_DEBUG) RNS.log("Browser handling link to: "+str(link_target), RNS.LOG_DEBUG)
self.browser_footer = urwid.Text("Opening link to: "+str(link_target)) self.browser_footer = urwid.Text("Opening link to: "+str(link_target))
try: try:
self.retrieve_url(link_target, field_data) self.retrieve_url(link_target, request_data)
except Exception as e: except Exception as e:
self.browser_footer = urwid.Text("Could not open link: "+str(e)) self.browser_footer = urwid.Text("Could not open link: "+str(e))
self.frame.contents["footer"] = (self.browser_footer, self.frame.options()) self.frame.contents["footer"] = (self.browser_footer, self.frame.options())
@ -376,6 +387,7 @@ class Browser:
if self.link != None: if self.link != None:
self.link.teardown() self.link.teardown()
self.request_data = None
self.attr_maps = [] self.attr_maps = []
self.status = Browser.DISCONECTED self.status = Browser.DISCONECTED
self.response_progress = 0 self.response_progress = 0
@ -390,7 +402,7 @@ class Browser:
self.update_display() 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_destination_hash = self.destination_hash
self.previous_path = self.path self.previous_path = self.path
@ -443,7 +455,7 @@ class Browser:
else: else:
self.set_destination_hash(destination_hash) self.set_destination_hash(destination_hash)
self.set_path(path) self.set_path(path)
self.set_field_data(field_data) self.set_request_data(request_data)
self.load_page() self.load_page()
def set_destination_hash(self, destination_hash): def set_destination_hash(self, destination_hash):
@ -457,8 +469,8 @@ class Browser:
def set_path(self, path): def set_path(self, path):
self.path = path self.path = path
def set_field_data(self, field_data): def set_request_data(self, request_data):
self.field_data = field_data self.request_data = request_data
def set_timeout(self, timeout): def set_timeout(self, timeout):
self.timeout = timeout self.timeout = timeout
@ -665,7 +677,7 @@ class Browser:
def load_page(self): def load_page(self):
if self.field_data == None: if self.request_data == None:
cached = self.get_cached(self.current_url()) cached = self.get_cached(self.current_url())
else: else:
cached = None cached = None
@ -703,10 +715,10 @@ class Browser:
page_data = b"The requested local page did not exist in the file system" page_data = b"The requested local page did not exist in the file system"
if os.path.isfile(page_path): if os.path.isfile(page_path):
if os.access(page_path, os.X_OK): if os.access(page_path, os.X_OK):
if self.field_data != None: if self.request_data != None:
env_map = self.field_data env_map = self.request_data
else: else:
env_map = None env_map = {}
generated = subprocess.run([page_path], stdout=subprocess.PIPE, env=env_map) generated = subprocess.run([page_path], stdout=subprocess.PIPE, env=env_map)
page_data = generated.stdout page_data = generated.stdout
else: else:
@ -794,7 +806,7 @@ class Browser:
self.update_display() self.update_display()
receipt = self.link.request( receipt = self.link.request(
self.path, self.path,
data = self.field_data, data = self.request_data,
response_callback = self.response_received, response_callback = self.response_received,
failed_callback = self.request_failed, failed_callback = self.request_failed,
progress_callback = self.response_progressed progress_callback = self.response_progressed

View File

@ -6,9 +6,6 @@ from urwid.util import is_mouse_press
from urwid.text_layout import calc_coords from urwid.text_layout import calc_coords
import re import re
# TODO: remove
import RNS
DEFAULT_FG_DARK = "ddd" DEFAULT_FG_DARK = "ddd"
DEFAULT_FG_LIGHT = "222" DEFAULT_FG_LIGHT = "222"
DEFAULT_BG = "default" DEFAULT_BG = "default"
@ -562,12 +559,9 @@ def make_output(state, line, url_delegate):
if url_delegate != None: if url_delegate != None:
linkspec = LinkSpec(link_url, orig_spec) linkspec = LinkSpec(link_url, orig_spec)
if link_fields != "": if link_fields != "":
if link_fields == "*": lf = link_fields.split("|")
linkspec.link_fields = "all" if len(lf) > 0:
else: linkspec.link_fields = lf
lf = link_fields.split("|")
if len(lf) > 0:
linkspec.link_fields = lf
output.append((linkspec, link_label)) output.append((linkspec, link_label))
else: else: