Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a basic config loader
  • Loading branch information
tdemin committed Jun 2, 2019
1 parent 694b6cd commit be69464
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions project_amber/app.py
@@ -1,6 +1,11 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from project_amber.config import config

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = config["database"]
db = SQLAlchemy(app)

@app.route("/")
def hello():
Expand Down
36 changes: 36 additions & 0 deletions project_amber/config.py
@@ -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]

0 comments on commit be69464

Please sign in to comment.