mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
45 lines
1.2 KiB
Python
Executable File
Vendored
45 lines
1.2 KiB
Python
Executable File
Vendored
#!/usr/bin/python3.8
|
|
import json
|
|
import pathlib
|
|
import os
|
|
from src.backend.lib.display import TerminalDisplay
|
|
|
|
|
|
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):
|
|
"""
|
|
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):
|
|
"""
|
|
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()
|
|
install_answers = TerminalDisplay().installer()
|
|
for key in install_answers:
|
|
config[key["name"]] = key["answer"]
|
|
Configuration().write_file(config)
|
|
|