python tool: refactor
This commit is contained in:
parent
cb99880407
commit
30f9f786c2
|
@ -52,6 +52,14 @@ def savedata():
|
||||||
else:
|
else:
|
||||||
writetextfile("data/local.json", json.dumps(data))
|
writetextfile("data/local.json", json.dumps(data))
|
||||||
|
|
||||||
|
def loaddata():
|
||||||
|
if settings["mode"] == "remote":
|
||||||
|
subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"])
|
||||||
|
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"])
|
||||||
|
return json.loads(readtextfile("data/backupdata.json"))
|
||||||
|
else:
|
||||||
|
return json.loads(readtextfile("data/local.json"))
|
||||||
|
|
||||||
def ask(question):
|
def ask(question):
|
||||||
answer = input(question)
|
answer = input(question)
|
||||||
return answer == "y" or answer == "yes"
|
return answer == "y" or answer == "yes"
|
||||||
|
@ -64,12 +72,7 @@ settings = json.loads(readtextfile("settings.json"))
|
||||||
if os.path.isfile("data/backupdata.acs"):
|
if os.path.isfile("data/backupdata.acs"):
|
||||||
os.remove("data/backupdata.acs")
|
os.remove("data/backupdata.acs")
|
||||||
|
|
||||||
if settings["mode"] == "remote":
|
data = loaddata()
|
||||||
subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"])
|
|
||||||
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"])
|
|
||||||
data = json.loads(readtextfile("data/backupdata.json"))
|
|
||||||
else:
|
|
||||||
data = json.loads(readtextfile("data/local.json"))
|
|
||||||
|
|
||||||
command = ""
|
command = ""
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
@ -79,12 +82,10 @@ 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"):
|
||||||
|
|
||||||
|
delete = False
|
||||||
if command[0:3] == "rm ":
|
if command[0:3] == "rm ":
|
||||||
index = int(command[3:])
|
delete = True
|
||||||
if ask("delete '" + data[index]["title"] + "'? "):
|
command = command[3:]
|
||||||
data.remove(data[index])
|
|
||||||
savedata()
|
|
||||||
else:
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
index = int(command)
|
index = int(command)
|
||||||
|
@ -92,7 +93,11 @@ while not (command == "quit" or command == "exit" or command == "q"):
|
||||||
except:
|
except:
|
||||||
note = next((note for note in data if note["title"] == command), None)
|
note = next((note for note in data if note["title"] == command), None)
|
||||||
|
|
||||||
if note:
|
if delete:
|
||||||
|
if note and ask("delete '" + note["title"] + "'? "):
|
||||||
|
data.remove(note)
|
||||||
|
savedata()
|
||||||
|
elif note:
|
||||||
editnote(note)
|
editnote(note)
|
||||||
else:
|
else:
|
||||||
if listnotes(command) == 0:
|
if listnotes(command) == 0:
|
||||||
|
|
Loading…
Reference in New Issue