Je kunt niet meer dan 25 onderwerpen selecteren
Onderwerpen moeten beginnen met een letter of nummer, kunnen streepjes bevatten ('-') en kunnen maximaal 35 tekens lang zijn.
203 regels
4.8 KiB
Python
203 regels
4.8 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
import sys
|
|
import os
|
|
try:
|
|
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
|
except:
|
|
pass
|
|
|
|
import tempfile
|
|
import i18n
|
|
import validators
|
|
import requests
|
|
import api
|
|
from config import *
|
|
from termcolors import *
|
|
from getpass import getpass
|
|
|
|
|
|
editor = "/usr/bin/editor"
|
|
|
|
notes = {}
|
|
|
|
|
|
def firstsetup(url="", username=""):
|
|
# Get URL
|
|
while True:
|
|
if url != "":
|
|
url = input(i18n.t("Server URL") + " (" + url + "): ") or url
|
|
else:
|
|
url = input(i18n.t("Server URL") + ": ")
|
|
if validators.url(url):
|
|
break
|
|
else:
|
|
print(i18n.t("That doesn't look right, try again."))
|
|
# Get username
|
|
while True:
|
|
if username != "":
|
|
username = input(i18n.t("Username") + " (" + username + "): ") or username
|
|
else:
|
|
username = input(i18n.t("Username") + ": ")
|
|
if username != "":
|
|
break
|
|
# Get password
|
|
while True:
|
|
password = getpass(i18n.t("Password") + ": ")
|
|
if password != "":
|
|
break
|
|
|
|
try:
|
|
r = requests.post(url + "/api/ping", auth=(username, password))
|
|
except:
|
|
print(i18n.t("Could not connect to the server. Try again."))
|
|
firstsetup()
|
|
return
|
|
|
|
if r.status_code == 401:
|
|
print(i18n.t("Login incorrect, try again."))
|
|
firstsetup(url)
|
|
return
|
|
|
|
try:
|
|
resp = r.json()
|
|
if resp["status"] == "ERROR":
|
|
print(resp["msg"])
|
|
firstsetup(url, username)
|
|
return
|
|
config["url"] = url
|
|
config["username"] = username
|
|
config["password"] = password
|
|
saveconfig()
|
|
return
|
|
except ValueError:
|
|
print(i18n.t("Login incorrect, try again."))
|
|
firstsetup(url, username)
|
|
return
|
|
|
|
|
|
def loadnotes():
|
|
global notes
|
|
notes = api.do("getnotes")["notes"]
|
|
|
|
|
|
def savenote(note):
|
|
api.do("savenote", {
|
|
"id": note["noteid"],
|
|
"text": note["content"]
|
|
})
|
|
|
|
|
|
def editnote(note):
|
|
global editor
|
|
content = note["content"]
|
|
f = tempfile.NamedTemporaryFile(mode='w+')
|
|
f.write(content)
|
|
f.flush()
|
|
command = editor + " " + f.name
|
|
status = os.system(command)
|
|
f.seek(0, 0)
|
|
text = f.read()
|
|
f.close()
|
|
assert not os.path.exists(f.name)
|
|
note["content"] = text
|
|
clearscreen()
|
|
print(i18n.t("Saving..."))
|
|
savenote(note)
|
|
print(i18n.t("Note saved!"))
|
|
|
|
|
|
def editmenu():
|
|
global notes
|
|
clearscreen()
|
|
loadnotes()
|
|
i = 1
|
|
notelist = {}
|
|
for note in notes:
|
|
notelist[str(i)] = note
|
|
print(chr(27) + "[0m", end='')
|
|
print("\n\nNote #" + str(i) + ":")
|
|
setbghexcolor(note["color"])
|
|
setfgbybghex(note["color"])
|
|
linecount = 0
|
|
for line in note["content"].splitlines():
|
|
if linecount > 5:
|
|
print("\n(" + str(len(note["content"].splitlines())) + " more lines" + ")", end='')
|
|
break
|
|
print("\n " + line[0:70].ljust(70), end='')
|
|
if len(line) > 70:
|
|
print("...", end='')
|
|
linecount += 1
|
|
i += 1
|
|
resetcolor()
|
|
print("\n======================")
|
|
noteid = input(i18n.t("Note to edit (M for main menu): "))
|
|
if noteid.upper() == "M":
|
|
clearscreen()
|
|
return
|
|
try:
|
|
editnote(notelist[noteid])
|
|
except KeyError:
|
|
print(i18n.t("Invalid selection."))
|
|
|
|
|
|
def newnote():
|
|
global editor
|
|
print()
|
|
f = tempfile.NamedTemporaryFile(mode='w+')
|
|
f.write("")
|
|
f.flush()
|
|
command = editor + " " + f.name
|
|
status = os.system(command)
|
|
f.seek(0, 0)
|
|
text = f.read()
|
|
f.close()
|
|
assert not os.path.exists(f.name)
|
|
if text == "":
|
|
clearscreen()
|
|
print(i18n.t("Nothing to do."))
|
|
return
|
|
print(i18n.t("Saving..."))
|
|
api.do("savenote", {
|
|
"text": text
|
|
})
|
|
print(i18n.t("Note created!"))
|
|
|
|
|
|
def mainmenu():
|
|
print("\n======================")
|
|
print(i18n.t("Main Menu"))
|
|
print("======================")
|
|
print("E. " + i18n.t("View and edit notes"))
|
|
print("C. " + i18n.t("Create a new note"))
|
|
print("Q. " + i18n.t("Exit"))
|
|
option = input(i18n.t("Choose an option: ")).upper()
|
|
clearscreen()
|
|
if option == "E":
|
|
editmenu()
|
|
elif option == "C":
|
|
newnote()
|
|
elif option == "Q":
|
|
exit(0)
|
|
else:
|
|
print(i18n.t("Unrecognized selection, try again."))
|
|
|
|
|
|
def main():
|
|
print("NotePostCLI v0.1")
|
|
if not checkconfig():
|
|
print(i18n.t("No valid settings file found, running setup wizard."))
|
|
firstsetup()
|
|
else:
|
|
loadconfig()
|
|
while True:
|
|
mainmenu()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|