diff --git a/.gitignore b/.gitignore index f7275bb..93526df 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ venv/ +__pycache__/ diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3851d2c..d09f5cb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,11 @@ - + + + + + - + - - + + + + + + + + + + + + + + - + @@ -24,6 +40,13 @@ + + + @@ -31,6 +54,7 @@ @@ -121,12 +145,12 @@ - + - + @@ -134,19 +158,39 @@ - + + + + + + + + + + + - - + + + + + + + + + + + + - + diff --git a/config.py b/config.py new file mode 100644 index 0000000..5f420c9 --- /dev/null +++ b/config.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + + +import json +import appdirs +import os +import validators +from pathlib import Path + +appname = "NotePostCLI" +appauthor = "Netsyms Technologies" + +CONFIG_FOLDER = appdirs.user_config_dir(appname, appauthor) +CONFIG_FILE = CONFIG_FOLDER + "/settings.json" + +config = {} + + +def checkconfig(): + config_path = Path(CONFIG_FILE) + if config_path.is_file(): + try: + # Attempt to read and parse the config file + with open(CONFIG_FILE, 'r') as config_file: + confjson = json.load(config_file) + if "url" not in confjson: + return False + if "username" not in confjson: + return False + if "password" not in confjson: + return False + return True + except Exception: + return False + else: + return False + + +def loadconfig(): + global config + with open(CONFIG_FILE, 'r') as config_file: + config = json.load(config_file) + + +def saveconfig(): + global config + jsonstring = json.dumps(config, sort_keys=True, indent=4) + if not os.path.exists(CONFIG_FOLDER): + os.makedirs(CONFIG_FOLDER) + with open(CONFIG_FILE, 'w') as config_file: + config_file.write(jsonstring) + # Protect from other users who might want to peek at our credentials + os.chmod(CONFIG_FILE, 0o600) + + +def getconfig(): + global config + return config diff --git a/notepost.py b/notepost.py index 7b86064..f933b5c 100755 --- a/notepost.py +++ b/notepost.py @@ -1,55 +1,75 @@ #!/usr/bin/env python3 -from os import read import i18n import validators -import json import requests -from pathlib import Path +from config import * from getpass import getpass -CONFIG_FILE = "~/.config/notepostcli.json" - -def checkconfig(): - config_path = Path(CONFIG_FILE) - if config_path.is_file(): - try: - # Attempt to read and parse the config file - config_file = open(CONFIG_FILE, 'r') - json.load(config_file) - return True - except Exception: - return False - else: - return False - - -def firstsetup(): +def firstsetup(url = "", username = ""): + config = getconfig() + # Get URL while True: - url = input(i18n.t("Server URL: ")) + 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: - username = input(i18n.t("Username: ")) + 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: ")) + password = getpass(i18n.t("Password") + ": ") if password != "": break - r = requests.post(url + "/api/ping", data = {"username": username, "password": password}) - print(r.json()) + 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 main(): if not checkconfig(): print(i18n.t("No valid settings file found, running setup wizard.")) firstsetup() - + else: + loadconfig() + print(getconfig()) + if __name__ == "__main__": main()