from urllib.parse import quote, unquote, parse_qs from jinja2 import Environment, FileSystemLoader from network import Network filename = 'network0.pimesh' with open(filename) as net_file: net = Network.from_file(net_file) jinja_env = Environment(loader=FileSystemLoader('./html'), extensions=['jinja2.ext.autoescape'], autoescape=True) tpl_list = jinja_env.get_template('list.html') tpl_view = jinja_env.get_template('view.html') def application(env, start_response): path = env.get('PATH_INFO', "").lstrip('/') cmd, *rest = path.split("/", 1) arg = unquote(rest[0]).strip() if rest else "" try: request_body_size = int(env.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 request_body = env['wsgi.input'].read(request_body_size).decode() status = "200 OK"
#!/usr/bin/env python3 #Top-level wrapper script for a general-purpose information-management tool import sys from network import Network from cliui import UI try: filename = sys.argv[1] try: networkfile = open(filename, 'r+') network = Network.from_file(networkfile) # Note that this does not (and should not) create a new file print("Loaded PIMesh network from file") except FileNotFoundError: network = Network() print("Created empty network (file not found)") ui = UI(network) try: ui.run() except KeyboardInterrupt: print("Caught leopard interrupt") try: network.to_file(networkfile) print("Saved session changes to file.") except: print("Saving to file failed!") except IndexError:
#!/usr/bin/env python3 #Top-level wrapper script for a general-purpose information-management tool import sys from network import Network from cliui import UI try: filename = sys.argv[1] try: networkfile = open(filename, 'r+') # Note that this does not (and should not) create a new file network = Network.from_file(networkfile) print("Loaded PIMesh network from file") except FileNotFoundError: networkfile = open(filename, 'w') network = Network() print("Created empty network (file not found)") ui = UI(network) try: ui.run() except KeyboardInterrupt: print("Caught leopard interrupt") networkfile.seek(0) try: network.to_file(networkfile) networkfile.truncate() print("Saved session changes to file.")
class UI: def __init__(self, lines, cols): self.mode = Mode.list self.status = "Started PIMesh" self.lines = lines self.cols = cols UI = UI(shutil.get_terminal_size()[0], shutil.get_terminal_size()[1]) print("\x1B]0;%s\x07" % "PIMesh") # Set window title filename = "network0.pimesh" # Currently fixed filename - should it really be an argument when starting the script? try: network = Network.from_file(open(filename)) UI.status = "Loaded PIMesh network from file" except FileNotFoundError: network = Network() UI.status = "Created empty network (file not found)" def print_entity_list(): """Print a list of entitites which have one of more links to/from them""" #global UI.status if len(network) == 0: print("(No entities in network)") # Could be slightly confusing, but saves a big empty space return 1 for n, name in enumerate(network.origins): print(str(n) + " | " + name) return len(network) # number of lines printed by this function, needed to pad vertically by the right amount