Skip to content

Instantly share code, notes, and snippets.

@roganjoshp
Created June 24, 2020 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roganjoshp/aa7b8ccf1aebe860d40d71554189411c to your computer and use it in GitHub Desktop.
Save roganjoshp/aa7b8ccf1aebe860d40d71554189411c to your computer and use it in GitHub Desktop.
relative dump of config files
# The contents of beatroute/src/beatroute/config.py
import __main__
import json
import os
import sqlite3
import uuid
from pathlib import Path
class Config:
def __init__(self, filename=None):
self.filename = filename or 'beatroute_config.json'
if os.path.exists(self.filename):
with open(self.filename) as infile:
data = json.load(infile)
for feature, config in data.items():
for attribute, value in config.items():
setattr(self, attribute, value)
else:
self._build_defaults()
def _build_defaults(self):
# First find where script is being executed
self.cur_dir = Path(__main__.__file__).parent # WAS: '\\'.join(stack_trace[1][1].split('\\')[:-1])
# Flask app config
self.SECRET_KEY = uuid.uuid4().hex
self.SQLALCHEMY_DATABASE_URI = 'sqlite:///beatroute.db'
self.SQLALCHEMY_TRACK_MODIFICATIONS = False
self.SESSION_TYPE = 'sqlalchemy'
config = {
'app': {
'SECRET_KEY': self.SECRET_KEY,
'SQLALCHEMY_DATABASE_URI': self.SQLALCHEMY_DATABASE_URI,
'SQLALCHEMY_TRACK_MODIFICATIONS': self.SQLALCHEMY_TRACK_MODIFICATIONS,
'SESSION_TYPE': self.SESSION_TYPE,
},
'algorithm': {}
}
with open(self.filename, 'w') as outfile:
json.dump(config, outfile, indent=4)
# The user creates this file in any location that they want.
# The config file is dumped into the same directory as their script
from beatroute.config import Config
config = Config()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment