This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add a basic config loader
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 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 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,36 @@ | ||
import os | ||
from json import load | ||
|
||
if os.name == "posix": | ||
configPaths = ["./config.json", "/etc/amber.json"] | ||
else: | ||
configPaths = ["config.json"] | ||
|
||
config = { | ||
"database": "" | ||
} | ||
|
||
# search for every file name and load the config from the first file | ||
# that exists | ||
for testedFileName in configPaths: | ||
if os.path.isfile(testedFileName): | ||
# holds the actual config file name | ||
configFileName = testedFileName | ||
break | ||
if not "configFileName" in globals(): | ||
print("No configuration file found, exiting") | ||
exit(1) | ||
|
||
try: | ||
with open(configFileName, encoding="utf8") as configFile: | ||
# holds the new configuration variables which replace the | ||
# default ones | ||
loadedConfig = load(configFile) | ||
except OSError as ioerr: | ||
print("Could not open config file", configFileName) | ||
print(ioerr.strerror) | ||
exit(1) | ||
|
||
for entry in config: | ||
if entry in loadedConfig: | ||
config[entry] = loadedConfig[entry] |