python cli tool: handle script arguments
This commit is contained in:
parent
a47a5364c4
commit
a4e015af10
|
@ -3,6 +3,7 @@ import json
|
||||||
import subprocess
|
import subprocess
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
def listnotes(filter = ""):
|
def listnotes(filter = ""):
|
||||||
for i in reversed(range(len(data))):
|
for i in reversed(range(len(data))):
|
||||||
|
@ -17,6 +18,37 @@ def writetextfile(path, content):
|
||||||
with io.open(path, mode = "w", encoding = "utf-8") as f:
|
with io.open(path, mode = "w", encoding = "utf-8") as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
|
def editnote(note):
|
||||||
|
content = note["content"]
|
||||||
|
|
||||||
|
writetextfile("data/backupnote.md", content)
|
||||||
|
writetextfile("data/note.md", content)
|
||||||
|
|
||||||
|
subprocess.call(settings["commands"]["editor"] + ["data/note.md"])
|
||||||
|
newcontent = readtextfile("data/note.md")
|
||||||
|
|
||||||
|
if newcontent != content:
|
||||||
|
|
||||||
|
listnotes()
|
||||||
|
subprocess.call(["diff", "--color", "data/backupnote.md", "data/note.md"])
|
||||||
|
|
||||||
|
note["content"] = newcontent
|
||||||
|
data.remove(note)
|
||||||
|
data.insert(0, note)
|
||||||
|
|
||||||
|
writetextfile("data/data.json", json.dumps(data))
|
||||||
|
|
||||||
|
subprocess.call([settings["commands"]["gpg"], "-q", "--encrypt", "--yes", "--trust-model", "always", "--output", "data/data.acs", "--armor", "-r", settings["gpguser"], "data/data.json"]);
|
||||||
|
newdata = readtextfile("data/data.acs")
|
||||||
|
|
||||||
|
postdata = "action=push&password=" + settings["password"] + "&data=" + urllib.parse.quote_plus(newdata)
|
||||||
|
|
||||||
|
writetextfile("data/postdata", postdata)
|
||||||
|
|
||||||
|
subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"])
|
||||||
|
else:
|
||||||
|
print("no change")
|
||||||
|
|
||||||
settings = json.loads(readtextfile("settings.json"))
|
settings = json.loads(readtextfile("settings.json"))
|
||||||
|
|
||||||
if os.path.isfile("data/backupdata.acs"):
|
if os.path.isfile("data/backupdata.acs"):
|
||||||
|
@ -28,6 +60,9 @@ subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/b
|
||||||
data = json.loads(readtextfile("data/backupdata.json"))
|
data = json.loads(readtextfile("data/backupdata.json"))
|
||||||
|
|
||||||
command = "list"
|
command = "list"
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
command = sys.argv[1]
|
||||||
|
|
||||||
while not (command == "quit" or command == "exit" or command == "q"):
|
while not (command == "quit" or command == "exit" or command == "q"):
|
||||||
|
|
||||||
if command == "list":
|
if command == "list":
|
||||||
|
@ -36,40 +71,17 @@ while not (command == "quit" or command == "exit" or command == "q"):
|
||||||
searchstring = command.replace("find ", "")
|
searchstring = command.replace("find ", "")
|
||||||
listnotes(searchstring)
|
listnotes(searchstring)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
index = int(command)
|
index = int(command)
|
||||||
note = data[index]
|
note = data[index]
|
||||||
content = note["content"]
|
|
||||||
|
|
||||||
writetextfile("data/backupnote.md", content)
|
|
||||||
writetextfile("data/note.md", content)
|
|
||||||
|
|
||||||
subprocess.call(settings["commands"]["editor"] + ["data/note.md"])
|
|
||||||
newcontent = readtextfile("data/note.md")
|
|
||||||
|
|
||||||
if newcontent != content:
|
|
||||||
|
|
||||||
listnotes()
|
|
||||||
subprocess.call(["diff", "--color", "data/backupnote.md", "data/note.md"])
|
|
||||||
|
|
||||||
note["content"] = newcontent
|
|
||||||
data.remove(note)
|
|
||||||
data.insert(0, note)
|
|
||||||
|
|
||||||
writetextfile("data/data.json", json.dumps(data))
|
|
||||||
|
|
||||||
subprocess.call([settings["commands"]["gpg"], "-q", "--encrypt", "--yes", "--trust-model", "always", "--output", "data/data.acs", "--armor", "-r", settings["gpguser"], "data/data.json"]);
|
|
||||||
newdata = readtextfile("data/data.acs")
|
|
||||||
|
|
||||||
postdata = "action=push&password=" + settings["password"] + "&data=" + urllib.parse.quote_plus(newdata)
|
|
||||||
|
|
||||||
writetextfile("data/postdata", postdata)
|
|
||||||
|
|
||||||
subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"])
|
|
||||||
else:
|
|
||||||
print("no change")
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
print("error with command " + command)
|
note = next((x for x in data if x["title"] == command), None)
|
||||||
|
|
||||||
|
if note:
|
||||||
|
editnote(note)
|
||||||
|
else:
|
||||||
|
print("unknown command")
|
||||||
|
|
||||||
command = input("> ")
|
command = input("> ")
|
Loading…
Reference in New Issue