def initialize_db_process(config_values): from aleph.web import app from configmanager import Config from aleph.config import get_defaults config = Config(schema=get_defaults()) app['config'] = config config.load_values(config_values) filestore.init_store(config)
def test_read_dict_as_defaults_loads_default_values_from_a_dictionary(): config = Config() # both will be ignored config.load_values({ 'a': 5, 'b': True, }) assert 'a' not in config assert 'b' not in config # both will be added config.load_values({ 'a': 5, 'b': True, }, as_defaults=True) assert config.a.value == 5 assert config.b.value is True
def test_json_read_and_write(defaults_json_path, user_json_path): c1 = Config() c1.json.load(defaults_json_path, as_defaults=True) c2 = Config() c2.json.load([defaults_json_path], as_defaults=True) c3 = Config() with open(defaults_json_path) as f: c3.json.load(f, as_defaults=True) assert c1.dump_values(with_defaults=False) == {} assert c1.dump_values(with_defaults=True) == { 'uploads': { 'threads': 1, 'enabled': False, 'tmp_dir': '/tmp', } } assert c1.dump_values() == c2.dump_values() == c3.dump_values() c1.json.load(user_json_path) c2.json.load([user_json_path]) with open(user_json_path) as f: c3.json.load(f) assert c1.dump_values(with_defaults=False) == { 'uploads': { 'threads': 5, 'enabled': True, } } assert c1.dump_values() == c2.dump_values() == c3.dump_values() updates = { 'uploads': { 'threads': 10, 'enabled': False, } } c1.load_values(updates) c2.load_values(updates) c3.load_values(updates) assert c1.dump_values() == c2.dump_values() == c3.dump_values() c1.json.dump(user_json_path) c2.json.load(user_json_path) assert c1.dump_values() == c2.dump_values() == c3.dump_values() assert c1.dump_values() == c2.dump_values() == c3.dump_values() with open(user_json_path, 'w') as f: c2.json.dump(f) c1.json.load(user_json_path) assert c1.dump_values() == c2.dump_values() == c3.dump_values()
def prepare_loop(config_values, manager=None, idx=1): from aleph.model import init_db from aleph.web import app from configmanager import Config from aleph.config import get_defaults from aleph.services.ipfs.common import get_ipfs_api from aleph.services.p2p import init_p2p, http from aleph.services import filestore # uvloop.install() # manager = NodeManager() # manager.start() if isinstance(manager, tuple): manager_info = manager DBManager.register("_set_value") DBManager.register("_get_value") manager = DBManager(address=manager_info[0], authkey=manager_info[1]) manager.connect() filestore._set_value = function_proxy(manager, "_set_value") filestore._get_value = function_proxy(manager, "_get_value") http.SESSION = None # loop = asyncio.new_event_loop() # asyncio.set_event_loop(loop) loop = asyncio.get_event_loop() config = Config(schema=get_defaults()) app["config"] = config config.load_values(config_values) init_db(config, ensure_indexes=False) loop.run_until_complete(get_ipfs_api(timeout=2, reset=True)) tasks = loop.run_until_complete(init_p2p(config, listen=False, port_id=idx)) return loop, tasks
def test_read_dict_recursively_loads_values_from_a_dictionary(): config = Config({ 'a': { 'x': 0, 'y': True, 'z': 0.0, }, 'b': { 'c': { 'd': { 'x': 'xxx', 'y': 'yyy', }, }, }, }) assert config.a.x.value == 0 assert config.a.y.value is True config.load_values({ 'a': { 'x': '5', 'y': 'no' }, }) assert config.a.x.value == 5 assert config.a.y.value is False config.b.c.load_values({ 'e': 'haha', # will be ignored 'd': { 'x': 'XXX' }, }) assert config.b.c.d.x.value == 'XXX' assert 'e' not in config.b.c
def test_item_value_changed_hook(): config = Config({'uploads': { 'db': { 'user': '******', } }}) calls = [] @config.hooks.item_value_changed def item_value_changed(old_value=None, new_value=None, item=None, **kwargs): calls.append((item, old_value, new_value)) assert calls == [] config.reset() assert calls == [] config.uploads.db.user.set('admin') assert len(calls) == 1 assert calls[-1] == (config.uploads.db.user, not_set, 'admin') config.uploads.db.user.value = 'Administrator' assert len(calls) == 2 assert calls[-1] == (config.uploads.db.user, 'admin', 'Administrator') config.load_values({'uploads': {'something_nonexistent': True}}) assert len(calls) == 2 config.load_values({'uploads': { 'db': { 'user': '******' } }}, as_defaults=True) assert len(calls) == 2 config.load_values({'uploads': {'db': {'user': '******'}}}) assert len(calls) == 3 assert calls[-1] == (config.uploads.db.user, 'Administrator', 'NEW VALUE')
def test_nested_config(): """ This demonstrates how an application config can be created from multiple sections (which in turn can be created from others). """ # Declaration of a config section may be a plain dictionary db_config = { 'host': 'localhost', 'user': '******', 'password': '******', } # Or, it may be an already functional instance of Config server_config = Config({ 'port': 8080, }) # # All these sections can be combined into one config: # config = Config({ 'db': db_config, 'server': server_config, 'greeting': 'Hello', # and you can have plain config items next to sections }) # You can load values assert config.greeting.value == 'Hello' # Your original schemas are safe -- db_config dictionary won't be changed config.db.user.value = 'root' assert config.db.user.value == 'root' assert db_config['user'] == 'admin' # You can also change values by reading them from a dictionary. # Unknown names will be ignored unless you pass as_defaults=True # but in that case you will overwrite any previously existing items. config.load_values({ 'greeting': 'Good morning!', 'comments': { 'enabled': False } }) assert config.greeting.value == 'Good morning!' assert 'comments' not in config # You can check if config value is the default value assert not config.db.user.is_default assert config.server.port.is_default # Or if it has any value at all assert config.server.port.has_value # Iterate over all items (recursively) all = dict(config.iter_items(recursive=True)) assert all[('db', 'host')] is config.db.host assert all[('server', 'port')] is config.server.port # Export all values config_dict = config.dump_values() assert config_dict['db'] == { 'host': 'localhost', 'user': '******', 'password': '******' } # Each section is a Config instance too, so you can export those separately too: assert config.server.dump_values() == config_dict['server'] # You can reset individual items to their default values assert config.db.user.value == 'root' config.db.user.reset() assert config.db.user.value == 'admin' # Or sections config.db.user.value = 'root_again' assert config.db.user.value == 'root_again' config.db.reset() assert config.db.user.value == 'admin' # Or you can reset all configuration and you can make sure all values match defaults assert not config.is_default config.reset() assert config.is_default