~fkfd/sophon

cbc6dab781295ad7801ac46067d60ca6f39cbd2d — Frederick Yin 3 years ago 7405914
Demo client

gcat is no longer executable
2 files changed, 91 insertions(+), 10 deletions(-)

M test/demo_client.py -rw-r--r-- => -rwxr-xr-x
M test/gcat.py -rwxr-xr-x => -rw-r--r--
M test/demo_client.py => test/demo_client.py +91 -8
@@ 1,4 1,8 @@
#!/usr/bin/python3
import subprocess
import os
import urllib
from datetime import datetime
from diff_match_patch import diff_match_patch
from gcat import gemini_request



@@ 15,7 19,8 @@ def req(path: str):
    return gemini_request(path)


def print_page(path: str):
def preq(path: str):
    # print req
    resp = req(path)
    header = f"<<==== {resp.status} {resp.meta} ====>>"
    footer = "<<" + "=" * (len(header) - 4) + ">>"


@@ 25,19 30,97 @@ def print_page(path: str):


print("Welcome to the demo client for Sophon")
print_page("/")
print_page("/pages")
preq("/")
preq("/pages")
found_page = False
page = ""
while not found_page:
    page = input("Select page to read: ")
    resp = print_page(f"/wiki/{page}")
    page = input("Select page to read (default: sandbox): ") or "sandbox"
    resp = req(f"/wiki/{page}")
    if resp.status == 20:
        found_page = True

page_fp = f"/tmp/{page}.gmi_{datetime.now().isoformat()}"
page_file = open(page_fp, "x")
page_file.write(resp.body)
page_file.close()
# print page in pager
pager = os.environ.get("PAGER")
page_file = open(page_fp, "r")
subprocess.run(pager, stdin=page_file)

selected_part = False
session = ""
session = ""  # /wiki/<page>/edit/<id>/
while not selected_part:
    part = input("Select part to edit ('meta' or 'section=1.1.' etc): ")
    resp = print_page(f"/wiki/{page}/edit?{part}")
    part = input("Select section to edit ('meta' or '1.1.' etc): ")
    resp = req(f"/wiki/{page}/edit?{part}")
    if resp.status == 30:
        selected_part = True
        session = resp.meta

# m = more edits, a = abort, c = commit, p = preview page
action = "m"
while action == "m":
    # preview stage
    resp = req(session + "preview")
    if resp.status != 20:
        print("Fatal: Preview failed")
        raise SystemExit
    stage_old = resp.body
    stage_fp = f"/tmp/{page}_STAGE_{datetime.now().isoformat()}"
    stage_file = open(stage_fp, "x")
    stage_file.write(resp.body)
    stage_file.close()

    # ask user to edit page
    editor = os.environ.get("EDITOR")
    subprocess.run([editor, stage_fp])

    # generate unidiff
    stage_file = open(stage_fp, "r")
    stage_new = stage_file.read()
    unidiff = generate_unidiff(stage_old, stage_new)
    print("<<==== Generated Unidiff ====>>")
    print(unidiff)
    print("<<===========================>>")

    # send to server
    if len(unidiff) > 1000:
        print(
            "Sorry, your diff is too long. Extra-long diffs will be supported in the future."
        )
        raise SystemExit

    resp = req(session + "diff?" + urllib.parse.quote(unidiff))
    action = (
        input(
            "Action: m to make more edits, p to preview page, c to commit, a to abort (default m): "
        ).lower()
        or "m"
    )

    if action == "a":
        # abort
        resp = req(session + "abort")
        if resp.status == 30:
            print("Aborted")
    elif action == "c":
        # commit changes
        msg = ""
        while not msg:
            msg = input("Please enter a brief description of your edit: ")
        resp = req(session + "commit?" + urllib.parse.quote(msg))
        if resp.status == 30:
            print("Committed")
    elif action == "p":
        # preview page
        resp = preq(session + "preview?page")
        if resp.status != 20:
            print("Fatal: Preview failed")
            raise SystemExit
        action = (
            input(
                "Action: m to make more edits, c to commit, a to abort (default m): "
            ).lower()
            or "m"
        )

M test/gcat.py => test/gcat.py +0 -2
@@ 1,5 1,3 @@
#!/usr/bin/env python3

import cgi
import os
import socket