refactor(py):

- use note title as note file name
- changed backup file names
- create data folder if needed
This commit is contained in:
quenousimporte 2023-09-18 09:01:53 +02:00
parent a23035ce18
commit 859ffecda4
1 changed files with 24 additions and 12 deletions

View File

@ -5,13 +5,19 @@ import urllib.parse
import os import os
import sys import sys
import time import time
import re
def filename(note):
return re.sub(r'[\?\"<>|\*:\/\\]', '_', note["title"]) + ".md"
def listnotes(filter = "", checkcontent = False): def listnotes(filter = "", checkcontent = False):
matching = [] matching = []
for i in reversed(range(len(data))): for i in reversed(range(len(data))):
if filter.lower() in data[i]["title"].lower(): if filter.lower() in data[i]["title"].lower():
print("[" + str(i) + "]", data[i]["title"]) print("[" + str(i) + "]", data[i]["title"])
matching.append(data[i]) matching.append(data[i])
elif checkcontent and filter.lower() in data[i]["content"].lower(): elif checkcontent and filter.lower() in data[i]["content"].lower():
print("[" + str(i) + "]", data[i]["title"]) print("[" + str(i) + "]", data[i]["title"])
lines = data[i]["content"].split("\n") lines = data[i]["content"].split("\n")
@ -21,6 +27,7 @@ def listnotes(filter = "", checkcontent = False):
index = line.lower().index(filter.lower()) index = line.lower().index(filter.lower())
print("\t" + str(j) + ":", line[:100]) print("\t" + str(j) + ":", line[:100])
matching.append(data[i]) matching.append(data[i])
return matching return matching
def readtextfile(path): def readtextfile(path):
@ -34,14 +41,14 @@ def writetextfile(path, content):
def editnote(note): def editnote(note):
content = note["content"] content = note["content"]
writetextfile("data/backupnote.md", content) writetextfile("data/" + filename(note) + ".bak", content)
writetextfile("data/note.md", content) writetextfile("data/" + filename(note), content)
subprocess.call(settings["commands"]["editor"] + ["data/note.md"]) subprocess.call(settings["commands"]["editor"] + ["data/" + filename(note)])
newcontent = readtextfile("data/note.md") newcontent = readtextfile("data/" + filename(note) )
if newcontent != content: if newcontent != content:
subprocess.call(settings["commands"]["diff"] + ["data/backupnote.md", "data/note.md"]) subprocess.call(settings["commands"]["diff"] + ["data/" + filename(note) + ".bak", "data/" + filename(note)])
note["content"] = newcontent note["content"] = newcontent
data.remove(note) data.remove(note)
data.insert(0, note) data.insert(0, note)
@ -63,9 +70,9 @@ def savedata():
def loaddata(): def loaddata():
if settings["mode"] == "remote": if settings["mode"] == "remote":
subprocess.call(["curl", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"]) subprocess.call(["curl", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/data.acs.bak", settings["url"] + "/handler.php"])
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"]) subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/data.json.bak", "--decrypt", "data/data.acs.bak"])
return json.loads(readtextfile("data/backupdata.json")) return json.loads(readtextfile("data/data.json.bak"))
else: else:
return json.loads(readtextfile("data/local.json")) return json.loads(readtextfile("data/local.json"))
@ -73,15 +80,20 @@ def ask(question):
answer = input(question + " [Y/n] ") answer = input(question + " [Y/n] ")
return answer == "y" or answer == "yes" or answer == "" return answer == "y" or answer == "yes" or answer == ""
def initdatapath():
if not os.path.exists("data"):
os.mkdir("data")
if os.path.isfile("data/data.acs.bak"):
os.remove("data/data.acs.bak")
abspath = os.path.abspath(__file__) abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath) dname = os.path.dirname(abspath)
os.chdir(dname) os.chdir(dname)
initdatapath()
settings = json.loads(readtextfile("settings.json")) settings = json.loads(readtextfile("settings.json"))
if os.path.isfile("data/backupdata.acs"):
os.remove("data/backupdata.acs")
data = loaddata() data = loaddata()
command = "" command = ""
if len(sys.argv) > 1: if len(sys.argv) > 1: