python tool: added command to delete note

This commit is contained in:
quenousimporte 2023-09-14 11:31:21 +02:00
parent 230582920e
commit cb99880407
1 changed files with 40 additions and 32 deletions

View File

@ -6,10 +6,6 @@ import os
import sys import sys
import time import time
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
def listnotes(filter = ""): def listnotes(filter = ""):
cnt = 0 cnt = 0
for i in reversed(range(len(data))): for i in reversed(range(len(data))):
@ -36,25 +32,33 @@ def editnote(note):
newcontent = readtextfile("data/note.md") newcontent = readtextfile("data/note.md")
if newcontent != content: if newcontent != content:
subprocess.call(settings["commands"]["diff"] + ["data/backupnote.md", "data/note.md"]) subprocess.call(settings["commands"]["diff"] + ["data/backupnote.md", "data/note.md"])
note["content"] = newcontent note["content"] = newcontent
data.remove(note) data.remove(note)
data.insert(0, note) data.insert(0, note)
savedata()
if settings["mode"] == "remote":
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:
writetextfile("data/local.json", json.dumps(data))
else: else:
print("no change") print("no change")
def savedata():
if settings["mode"] == "remote":
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:
writetextfile("data/local.json", json.dumps(data))
def ask(question):
answer = input(question)
return answer == "y" or answer == "yes"
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
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"):
@ -75,25 +79,29 @@ if len(sys.argv) > 1:
while not (command == "quit" or command == "exit" or command == "q"): while not (command == "quit" or command == "exit" or command == "q"):
try: if command[0:3] == "rm ":
index = int(command) index = int(command[3:])
note = data[index] if ask("delete '" + data[index]["title"] + "'? "):
except: data.remove(data[index])
note = next((note for note in data if note["title"] == command), None) savedata()
if note:
editnote(note)
else: else:
cnt = listnotes(command)
if cnt == 0: try:
answer = input("create '" + command + "'? ") index = int(command)
if answer == "y" or answer == "yes": note = data[index]
note = { except:
"title": command, note = next((note for note in data if note["title"] == command), None)
"content": "---\ntitle: " + command + "\ndate: " + time.strftime("%Y-%m-%d") + "\ntags: \n---\n\n"
} if note:
data.insert(0, note)
editnote(note) editnote(note)
else:
if listnotes(command) == 0:
if ask("create '" + command + "'? "):
note = {
"title": command,
"content": "---\ntitle: " + command + "\ndate: " + time.strftime("%Y-%m-%d") + "\ntags: \n---\n\n"
}
data.insert(0, note)
editnote(note)
command = input("> ") command = input("> ")