From c6793902a1a73b8dd35748c7f6d183b7fbf2cb39 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 28 Dec 2018 01:56:10 -0700 Subject: [PATCH] Working barebones app --- .idea/workspace.xml | 116 +++++++++++++++++++++++++++++++++--------- api.py | 23 +++++++++ config.py | 2 - notepost.py | 120 ++++++++++++++++++++++++++++++++++++++++++-- termcolors.py | 28 +++++++++++ 5 files changed, 261 insertions(+), 28 deletions(-) create mode 100644 api.py create mode 100644 termcolors.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d09f5cb..04cb864 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,11 @@ - + + + + - + - - + + - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -53,16 +90,18 @@ - - - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/api.py b/api.py new file mode 100644 index 0000000..48e0b61 --- /dev/null +++ b/api.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import i18n +import requests + +from config import getconfig + + +def do(action, data={}): + conf = getconfig() + try: + r = requests.post(conf["url"] + "/api/" + action, data=data, auth=(conf["username"], conf["password"])) + except: + print(i18n.t("Could not connect to the server. Try again.")) + return + + try: + resp = r.json() + if resp["status"] == "ERROR": + raise Exception(resp["msg"]) + return resp + except ValueError: + raise Exception(i18n.t("Invalid response from server.")) diff --git a/config.py b/config.py index 5f420c9..9ddbc11 100644 --- a/config.py +++ b/config.py @@ -1,10 +1,8 @@ #!/usr/bin/env python3 - import json import appdirs import os -import validators from pathlib import Path appname = "NotePostCLI" diff --git a/notepost.py b/notepost.py index f933b5c..20da006 100755 --- a/notepost.py +++ b/notepost.py @@ -1,14 +1,22 @@ #!/usr/bin/env python3 +import math +import tempfile import i18n import validators import requests +import api from config import * +from termcolors import * from getpass import getpass -def firstsetup(url = "", username = ""): - config = getconfig() +editor = "/usr/bin/editor" + +notes = {} + + +def firstsetup(url="", username=""): # Get URL while True: if url != "": @@ -62,13 +70,119 @@ def 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 + editnote(notelist[noteid]) + + +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 v1.0") if not checkconfig(): print(i18n.t("No valid settings file found, running setup wizard.")) firstsetup() else: loadconfig() - print(getconfig()) + while True: + mainmenu() if __name__ == "__main__": diff --git a/termcolors.py b/termcolors.py new file mode 100644 index 0000000..5694b3c --- /dev/null +++ b/termcolors.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +from math import sqrt + + +def clearscreen(): + print(chr(27) + "[2J", end='') + + +def resetcolor(): + print(chr(27) + "[0m", end='') + + +def setbghexcolor(hex): + r = int(hex[0:2], 16) + g = int(hex[2:4], 16) + b = int(hex[4:6], 16) + print(chr(27) + "[48;2;" + str(r) + ";" + str(g) + ";" + str(b) + "m", end='') + + +def setfgbybghex(hex): + r = int(hex[0:2], 16) + g = int(hex[2:4], 16) + b = int(hex[4:6], 16) + contrast = sqrt(r * r * .241 + g * g * .691 + b * b * .068) + if contrast > 130: + print(chr(27) + "[38;30m", end='') + else: + print(chr(27) + "[38;97m", end='')