Added config backup and write operations

This commit is contained in:
Raelon Masters
2019-12-27 10:28:49 -05:00
parent ee96fbb85d
commit 39f59aa689
3 changed files with 47 additions and 45 deletions

13
config.json vendored
View File

@@ -1,12 +1 @@
{
"TITLE": "pyShelf E-Book Server",
"VERSION": "0.3.0",
"BOOKPATH": "books/",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DATABASE": "pyshelf",
"USER": "pyshelf",
"PASSWORD": "pyshelf",
"BOOKSHELF": "data/shelf.json",
"ALLOWED_HOSTS": "*"
}
{"TITLE": "pyShelf E-Book Server", "VERSION": "0.3.0", "BOOKPATH": "~/Books", "DB_HOST": "localhost", "DB_PORT": "5432", "DATABASE": "pyshelf", "USER": "pyshelf", "PASSWORD": "pyshelf", "BOOKSHELF": "data/shelf.json", "ALLOWED_HOSTS": "*"}

43
install vendored
View File

@@ -1,35 +1,44 @@
#!/usr/bin/python3.8
import json
import pathlib
from pprint import pprint
# from src.backend.lib.config import Config # Use ptShelfs configuration class or stay independant for portability?
import os
from src.backend.lib.display import TerminalDisplay
# PRG_PATH = pathlib.Path.cwd()
# LIB_PATH = pathlib.Path.joinpath(PRG_PATH, "src", "backend", "lib")
# sys.path.insert(0, PRG_PATH)
# Call for the ui and installer questions.
install_answers = TerminalDisplay().installer()
class Configuration:
def __init__(self):
self._cp = pathlib.Path("config.json")
self._data = self.open_file()
self.system = os.sys.environ
def open_file(self):
with open(str(self._cp), "r") as read_file:
data = json.load(read_file)
return data
"""
Try to open and then backup the configuration file.
Fail and return false if initial configuration is not found.
# TODO: More specific error handling
"""
try:
with open(str(self._cp), "r") as read_file:
data = json.load(read_file)
with open('config.backup.json', 'w') as backup_file:
json.dump(data, backup_file)
return data
except Exception as e:
print(e)
return False
def write_file(self, data):
with open(str(self._cp), "rw") as write_file:
json.dumps(write_file)
"""
Write the provided data to the new configuration file
"""
with open(str(self._cp), "w") as write_file:
json.dump(data, write_file)
return True
config = Configuration().open_file()
# Print a comparison between config.json and user inputs.
pprint(install_answers)
pprint(config)
install_answers = TerminalDisplay().installer()
for key in install_answers:
config[key["name"]] = key["answer"]
Configuration().write_file(config)

View File

@@ -1,9 +1,5 @@
from __future__ import unicode_literals
import os
import sys
from pprint import pprint
from prompt_toolkit import prompt as prm
@@ -11,6 +7,8 @@ class TerminalDisplay:
def __init__(self):
self.term = True
self.w, self.y = os.get_terminal_size()[0], os.get_terminal_size()[1]
self.home = os.environ["HOME"]
self.user = os.environ["USER"]
def screen(self):
return self.term
@@ -18,38 +16,42 @@ class TerminalDisplay:
def installer(self):
questions = [
{
"message": "Input the absolute path to your ebooks\nEg. /home/{user}/Books > ",
"message": "Input the absolute path to your ebooks\nEnter for default \"~/Books\" > ",
"options": "",
"name": "BOOKPATH",
"answer": "",
"answer": None,
"default": self.home+"/Books"
},
{
"message": "Input your PostgreSQL server ip\nEg. localhost > ",
"message": "Input your PostgreSQL server ip\nEnter for default \"localhost\" > ",
"options": "localhost",
"name": "DB_HOST",
"answer": "",
"answer": None,
"default": "localhost"
},
{
"message": "Input your PostgreSQL server port\nEg. 5432 > ",
"message": "Input your PostgreSQL server port\nEnter for default \"5432\" > ",
"options": "5432",
"name": "DB_PORT",
"answer": "",
"answer": None,
"default": "5432"
},
{
"message": "Input your PostgreSQL user name\nEg. pyshelf > ",
"message": "Input your PostgreSQL user name\nEnter for default \"pyshelf\" > ",
"options": "pyshelf",
"name": "USER",
"answer": "",
"answer": None,
"default": "pyshelf"
},
{
"message": "Input your PostgreSQL password\neg. pyshelf > ",
"message": "Input your PostgreSQL password\nEnter for default \"pyshelf\" > ",
"options": "pyshelf",
"name": "PASSWORD",
"answer": "",
"answer": None,
"default": "pyshelf"
},
]
answers = self.prompt(questions)
pprint(answers)
return self.prompt(questions)
@staticmethod
def clear():
@@ -61,6 +63,8 @@ class TerminalDisplay:
for answer in answers:
self.h_rule()
answer["answer"] = prm(answer["message"])
if answer["answer"] == "":
answer["answer"] = answer["default"]
self.clear()
return answers