Implement load/save/check config file and account setup
This commit is contained in:
parent
353dfb90e9
commit
0913f6d2ce
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
venv/
|
venv/
|
||||||
|
__pycache__/
|
||||||
|
66
.idea/workspace.xml
generated
66
.idea/workspace.xml
generated
@ -1,7 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="7e2ff7f2-75de-431f-9457-e0d648b65015" name="Default Changelist" comment="" />
|
<list default="true" id="7e2ff7f2-75de-431f-9457-e0d648b65015" name="Default Changelist" comment="">
|
||||||
|
<change afterPath="$PROJECT_DIR$/config.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/notepost.py" beforeDir="false" afterPath="$PROJECT_DIR$/notepost.py" afterDir="false" />
|
||||||
|
</list>
|
||||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@ -10,13 +14,25 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="FileEditorManager">
|
<component name="FileEditorManager">
|
||||||
<leaf>
|
<leaf>
|
||||||
<file pinned="false" current-in-tab="true">
|
<file pinned="false" current-in-tab="false">
|
||||||
<entry file="file://$PROJECT_DIR$/notepost.py">
|
<entry file="file://$PROJECT_DIR$/notepost.py">
|
||||||
<provider selected="true" editor-type-id="text-editor">
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
<state relative-caret-position="240">
|
<state relative-caret-position="270">
|
||||||
<caret line="16" column="12" lean-forward="true" selection-start-line="16" selection-start-column="12" selection-end-line="16" selection-end-column="12" />
|
<caret line="18" column="17" lean-forward="true" selection-start-line="17" selection-start-column="8" selection-end-line="18" selection-end-column="17" />
|
||||||
<folding>
|
<folding>
|
||||||
<element signature="e#23#42#0" expanded="true" />
|
<element signature="e#24#35#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="true">
|
||||||
|
<entry file="file://$PROJECT_DIR$/config.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="426">
|
||||||
|
<caret line="54" lean-forward="true" selection-start-line="54" selection-end-line="54" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#25#36#0" expanded="true" />
|
||||||
</folding>
|
</folding>
|
||||||
</state>
|
</state>
|
||||||
</provider>
|
</provider>
|
||||||
@ -24,6 +40,13 @@
|
|||||||
</file>
|
</file>
|
||||||
</leaf>
|
</leaf>
|
||||||
</component>
|
</component>
|
||||||
|
<component name="FileTemplateManagerImpl">
|
||||||
|
<option name="RECENT_TEMPLATES">
|
||||||
|
<list>
|
||||||
|
<option value="Python Script" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
<component name="Git.Settings">
|
<component name="Git.Settings">
|
||||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
@ -31,6 +54,7 @@
|
|||||||
<option name="CHANGED_PATHS">
|
<option name="CHANGED_PATHS">
|
||||||
<list>
|
<list>
|
||||||
<option value="$PROJECT_DIR$/notepost.py" />
|
<option value="$PROJECT_DIR$/notepost.py" />
|
||||||
|
<option value="$PROJECT_DIR$/config.py" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
@ -121,12 +145,12 @@
|
|||||||
<frame x="0" y="0" width="1920" height="1042" extended-state="6" />
|
<frame x="0" y="0" width="1920" height="1042" extended-state="6" />
|
||||||
<editor active="true" />
|
<editor active="true" />
|
||||||
<layout>
|
<layout>
|
||||||
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.18028466" />
|
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.18028466" />
|
||||||
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
|
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
|
||||||
<window_info id="Favorites" order="2" side_tool="true" />
|
<window_info id="Favorites" order="2" side_tool="true" />
|
||||||
<window_info anchor="bottom" id="Message" order="0" />
|
<window_info anchor="bottom" id="Message" order="0" />
|
||||||
<window_info anchor="bottom" id="Find" order="1" />
|
<window_info anchor="bottom" id="Find" order="1" />
|
||||||
<window_info anchor="bottom" id="Run" order="2" weight="0.32932165" />
|
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32932165" />
|
||||||
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
|
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
|
||||||
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
|
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
|
||||||
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
|
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
|
||||||
@ -134,19 +158,39 @@
|
|||||||
<window_info anchor="bottom" id="Version Control" order="7" />
|
<window_info anchor="bottom" id="Version Control" order="7" />
|
||||||
<window_info anchor="bottom" id="Terminal" order="8" />
|
<window_info anchor="bottom" id="Terminal" order="8" />
|
||||||
<window_info anchor="bottom" id="Event Log" order="9" side_tool="true" />
|
<window_info anchor="bottom" id="Event Log" order="9" side_tool="true" />
|
||||||
<window_info anchor="bottom" id="Python Console" order="10" />
|
<window_info anchor="bottom" id="Python Console" order="10" weight="0.32932165" />
|
||||||
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
|
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
|
||||||
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
|
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
|
||||||
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
|
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
|
||||||
</layout>
|
</layout>
|
||||||
</component>
|
</component>
|
||||||
<component name="editorHistoryManager">
|
<component name="editorHistoryManager">
|
||||||
|
<entry file="file://$PROJECT_DIR$/notepostcli/__init__.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor" />
|
||||||
|
</entry>
|
||||||
|
<entry file="file:///usr/lib/python3.6/os.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="180">
|
||||||
|
<caret line="12" column="61" lean-forward="true" selection-start-line="12" selection-start-column="61" selection-end-line="12" selection-end-column="61" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
<entry file="file://$PROJECT_DIR$/notepost.py">
|
<entry file="file://$PROJECT_DIR$/notepost.py">
|
||||||
<provider selected="true" editor-type-id="text-editor">
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
<state relative-caret-position="240">
|
<state relative-caret-position="270">
|
||||||
<caret line="16" column="12" lean-forward="true" selection-start-line="16" selection-start-column="12" selection-end-line="16" selection-end-column="12" />
|
<caret line="18" column="17" lean-forward="true" selection-start-line="17" selection-start-column="8" selection-end-line="18" selection-end-column="17" />
|
||||||
<folding>
|
<folding>
|
||||||
<element signature="e#23#42#0" expanded="true" />
|
<element signature="e#24#35#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/config.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="426">
|
||||||
|
<caret line="54" lean-forward="true" selection-start-line="54" selection-end-line="54" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#25#36#0" expanded="true" />
|
||||||
</folding>
|
</folding>
|
||||||
</state>
|
</state>
|
||||||
</provider>
|
</provider>
|
||||||
|
58
config.py
Normal file
58
config.py
Normal file
@ -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
|
72
notepost.py
72
notepost.py
@ -1,55 +1,75 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from os import read
|
|
||||||
|
|
||||||
import i18n
|
import i18n
|
||||||
import validators
|
import validators
|
||||||
import json
|
|
||||||
import requests
|
import requests
|
||||||
from pathlib import Path
|
from config import *
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
|
||||||
CONFIG_FILE = "~/.config/notepostcli.json"
|
|
||||||
|
|
||||||
|
def firstsetup(url = "", username = ""):
|
||||||
def checkconfig():
|
config = getconfig()
|
||||||
config_path = Path(CONFIG_FILE)
|
# Get URL
|
||||||
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():
|
|
||||||
while True:
|
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):
|
if validators.url(url):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print(i18n.t("That doesn't look right, try again."))
|
print(i18n.t("That doesn't look right, try again."))
|
||||||
|
# Get username
|
||||||
while True:
|
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 != "":
|
if username != "":
|
||||||
break
|
break
|
||||||
|
# Get password
|
||||||
while True:
|
while True:
|
||||||
password = getpass(i18n.t("Password: "))
|
password = getpass(i18n.t("Password") + ": ")
|
||||||
if password != "":
|
if password != "":
|
||||||
break
|
break
|
||||||
|
|
||||||
r = requests.post(url + "/api/ping", data = {"username": username, "password": password})
|
try:
|
||||||
print(r.json())
|
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():
|
def main():
|
||||||
if not checkconfig():
|
if not checkconfig():
|
||||||
print(i18n.t("No valid settings file found, running setup wizard."))
|
print(i18n.t("No valid settings file found, running setup wizard."))
|
||||||
firstsetup()
|
firstsetup()
|
||||||
|
else:
|
||||||
|
loadconfig()
|
||||||
|
print(getconfig())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user