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 time
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
def listnotes(filter = ""):
cnt = 0
for i in reversed(range(len(data))):
@ -36,13 +32,16 @@ def editnote(note):
newcontent = readtextfile("data/note.md")
if newcontent != content:
subprocess.call(settings["commands"]["diff"] + ["data/backupnote.md", "data/note.md"])
note["content"] = newcontent
data.remove(note)
data.insert(0, note)
savedata()
else:
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"]);
@ -52,9 +51,14 @@ def editnote(note):
subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"])
else:
writetextfile("data/local.json", json.dumps(data))
else:
print("no change")
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"))
if os.path.isfile("data/backupdata.acs"):
@ -75,6 +79,13 @@ if len(sys.argv) > 1:
while not (command == "quit" or command == "exit" or command == "q"):
if command[0:3] == "rm ":
index = int(command[3:])
if ask("delete '" + data[index]["title"] + "'? "):
data.remove(data[index])
savedata()
else:
try:
index = int(command)
note = data[index]
@ -84,11 +95,8 @@ while not (command == "quit" or command == "exit" or command == "q"):
if note:
editnote(note)
else:
cnt = listnotes(command)
if cnt == 0:
answer = input("create '" + command + "'? ")
if answer == "y" or answer == "yes":
if listnotes(command) == 0:
if ask("create '" + command + "'? "):
note = {
"title": command,
"content": "---\ntitle: " + command + "\ndate: " + time.strftime("%Y-%m-%d") + "\ntags: \n---\n\n"