diff options
-rw-r--r-- | config.py | 2 | ||||
-rwxr-xr-x | main.py | 79 |
2 files changed, 46 insertions, 35 deletions
@@ -1,6 +1,6 @@ from configparser import ConfigParser -config_path = "/home/mark/projects/wikijscmd/config.ini" +config_path = "/home/mark/dev/wikijscmd/config.ini" config = ConfigParser() config.read(config_path) @@ -4,21 +4,22 @@ import os import subprocess from config import config import graphql_queries +import re def print_item(item): trimmmed_path = item["path"][:17]+"..." if len(item["path"]) > 20 else item["path"] print("| %6s | %20s | %44s |" % (item["id"], trimmmed_path, item["title"]) ) -def create(): - if len(sys.argv) < 4: +def create(argv): + if len(argv) != 2 or len(argv) != 3: print("Usage: ./main.py create <path> <title> <content?>") sys.exit(1) - path = sys.argv[2] - title = sys.argv[3] - if len(sys.argv) == 4: - content = open_editor("/tmp/wikijscmd-create", "") + path = argv[0] + title = argv[1] + if len(argv) == 2: + content = open_editor("create", path, "") else: - content = sys.argv[4] + content = argv[2] response = graphql_queries.create_page(content, title, path) result = response["data"]["pages"]["create"]["responseResult"] if not result["succeeded"]: @@ -26,44 +27,52 @@ def create(): sys.exit(1) print(result["message"]) -def tree(): +def tree(argv): response = graphql_queries.get_tree() for item in response["data"]["pages"]["list"]: - if len(sys.argv) == 3 and sys.argv[2] not in item["path"]: + if len(argv) == 1 and argv[0] not in item["path"]: continue print_item(item) -def get_single_page(): - if not sys.argv[2].isdigit(): - if sys.argv[2].startswith("/"): - sys.argv[2] = sys.argv[2][1:] +def get_single_page(argv): + argument = argv[0] + if not argument.isdigit(): + # Strip leading slash + if argument.startswith("/"): + argument = argument[1:] found = False for item in graphql_queries.get_tree()["data"]["pages"]["list"]: - if sys.argv[2] == item["path"]: - sys.argv[2] = item["id"] + if argument == item["path"]: + argument = item["id"] found = True if not found: - print("No page with path: %s" % sys.argv[2]) + print("No page with path: %s" % argument) sys.exit(0) - page_id = int(sys.argv[2]) + page_id = int(argument) response = graphql_queries.get_single_page(page_id) return response["data"]["pages"]["single"] -def single(): - if len(sys.argv) < 3: +def single(argv): + if len(argv) != 1: print("Usage: ./main.py single <id|path>") sys.exit(0) - page = get_single_page() + page = get_single_page(argv) print("-" * 80) print_item(page) print("-" * 80) print(page["content"]) -def open_editor(filename, initial_body): +def clean_filename(pathname): + pathname = str(pathname).strip().replace('/', '_') + pathname = re.sub(r'\W', '', pathname) + return pathname[:200] + +def open_editor(action, pathname, initial_body): if "VISUAL" in os.environ: editor = os.environ['VISUAL'] else: editor = os.environ['EDITOR'] + filename = "/tmp/wikijscmd-"+action+"-"+clean_filename(pathname) if len(initial_body) > 0: with open(filename, "w") as f: f.write(initial_body) @@ -73,16 +82,17 @@ def open_editor(filename, initial_body): os.remove(filename) return new_body -def edit(): +def edit(argv): # Load content to edit - if len(sys.argv) < 3: + if len(argv) != 1: print("Usage: ./main.py edit <id|path>") sys.exit(0) - page = get_single_page() + page = get_single_page(argv) body = page["content"] # Open it in editor - new_body = open_editor("/tmp/wikijscmd-edit", body) + print(argv) + new_body = open_editor("edit", argv[0], body) # Prompt user to save it to the wiki print_item(page) @@ -106,15 +116,16 @@ def main(): print("\tsingle <id|path>") print("\tedit <id|path>") sys.exit(0) - - if sys.argv[1] == "create": - create() - elif sys.argv[1] == "tree": - tree() - elif sys.argv[1] == "single": - single() - elif sys.argv[1] == "edit": - edit() + commands = { + "create": create, + "tree": tree, + "single": single, + "edit": edit + } + command = sys.argv[1] + if command in commands: + # Pass in arguments after the command + commands[command](sys.argv[2:]) else: print("Unknown command: %s" % sys.argv[1]) |