from __future__ import print_function from __future__ import unicode_literals from builtins import str, bytes, dict, int import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.server import App, template, threadsafe from io import open # This example demonstrates a simple wiki served by pattern.server. # A wiki is a web app where each page can be edited (e.g, Wikipedia). # We will store the contents of each page as a file in /data. app = App(name="wiki") # Our wiki app has a single URL handler listening at the root ("/"). # It takes any combination of positional and keyword arguments. # This means that any URL will be routed to the index() function. # For example, http://127.0.0.1:8080/pages/bio.html?edit calls index() # with path=("pages", "bio.html") and data={"edit": ""}. @app.route("/") def index(*path, **data): #print("path:", path) #print("data:", data) # Construct a file name in /data from the URL path. # For example, path=("pages", "bio.html") # is mapped to "/data/pages/bio.html.txt". page = "/".join(path) page = page if page else "index.html" page = page.replace(" ", "-") page = page + ".txt" page = os.path.join(app.path, "data", page) # Absolute paths are safer. #print("page:", page) # If the URL ends in "?save", update the page content. if "save" in data and "content" in data: return save(page, src=data["content"]) # If the URL ends in "?edit", show the page editor. if "edit" in data: return edit(page) # If the page does not exist, show the page editor. if not os.path.exists(page): return edit(page) # Show the page. else: return view(page) # The pattern.server module has a simple template() function # that takes a file path or a string and optional parameters. # Placeholders in the template source (e.g., "$name") # are replaced with the parameter values. # Below is a template with placeholders for page name and content. # The page content is loaded from a file stored in /data. # The page name is parsed from the filename, # e.g., "/data/index.html.txt" => "index.html". wiki = """