-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the persistent config more modular and simpler
- Loading branch information
Showing
19 changed files
with
128 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .persistent_config import PersistentConfig as PersistentConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import json | ||
|
||
|
||
def exists(filename: str) -> bool: | ||
try: | ||
os.stat(filename) | ||
return True | ||
except OSError: | ||
return False | ||
|
||
|
||
class PersistentConfig: | ||
def __init__(self, initial_config={}, file="config.json"): | ||
self.__file = file | ||
|
||
self.update(initial_config) | ||
self.read() | ||
|
||
def get_dict(self): | ||
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")} | ||
|
||
def read(self): | ||
data = None | ||
if exists(self.__file): | ||
with open(self.__file, 'r', encoding="UTF-8") as file: | ||
data = file.read() | ||
if data: | ||
new_config = json.loads(data) | ||
self.update(new_config) | ||
|
||
def set_value(self, key: str, value): | ||
if hasattr(self, key): | ||
if type(getattr(self, key)) == type(value) or type(value) == type(None): | ||
setattr(self, key, value) | ||
else: | ||
print( | ||
f"Config types did not match: {key} ({type(getattr(self, key))}) ({type(value)})") | ||
else: | ||
print("Attribute does not exist") | ||
|
||
def update(self, new_config: dict) -> None: | ||
for (key, value) in new_config.items(): | ||
self.set_value(key, value) | ||
|
||
def save(self): | ||
with open(self.__file, 'w', encoding="utf-8") as file: | ||
json.dump(self.get_dict(), file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.