def test_load_with_real_file(self): import tempfile with tempfile.NamedTemporaryFile() as f: f.write(b'{"key": "value"}') f.flush() config = FileConfig(f.name, reader=JsonReader()) config.load() self.assertEqual('value', config['key'])
def test_init_reader_with_reader_value(self): reader = JsonReader() config = FileConfig('config.json', reader=reader) self.assertEqual(reader, config.reader)
def test_load_with_file_not_found(self): config = FileConfig('not_found') with self.assertRaises(FileNotFoundError): config.load()
def test_get_reader_with_default_value(self): config = FileConfig('config.json') self.assertIsNone(config.reader)
def test_init_reader_with_str_value(self): with self.assertRaises(TypeError): FileConfig('config.json', reader='non reader')
def test_init_filename_with_str_value(self): config = FileConfig('config.json') self.assertEqual('config.json', config.filename)
def test_init_filename_with_int_value(self): with self.assertRaises(TypeError): FileConfig(filename=123)
import requests from central.config.file import FileConfig from central.property import PropertyManager from flask import Flask config = FileConfig('config.json').reload_every(1000) config.load() properties = PropertyManager(config) timeout = properties.get_property('timeout').as_int(10) app = Flask(__name__) app.config.from_mapping(config) @app.route('/time') def get_current_time(): response = requests.get('http://date.jsontest.com', timeout=timeout.get()) data = response.json() return data.get('time') if __name__ == '__main__': app.run()
from central.config.file import FileConfig config = FileConfig('config.json') config.load() print(config.get('timeout')) print(config.get('database')) # you can set APP_ENV by using environment variables # export APP_ENV=prod # python main.py
from central.config import MergeConfig from central.config.file import FileConfig config = MergeConfig( FileConfig('config.json'), FileConfig('config.local.json') ) config.load() print(config.get('timeout')) print(config.get('database')) # you can set APP_ENV by using environment variables # export APP_ENV=prod # python main.py