def test_xdg_cache_dpath(): cfg = Birch(NSPACE4) returned_dpath = cfg.xdg_cache_dpath() try: xdg_cache_home = os.environ['XDG_CACHE_HOME'] expected_path = os.path.join(xdg_cache_home, NSPACE4) except KeyError: homedir = os.path.expanduser('~') expected_path = os.path.join(homedir, '.cache', NSPACE4) assert expected_path in returned_dpath assert expected_path in Birch.xdg_cache_dpath_by_namespace(NSPACE4)
def test_json(): cfg = Birch(NSPACE, load_all=True) print(cfg._val_dict) assert cfg['basekey'] == 'base_val' assert cfg['BASEKEY'] == 'base_val' res = cfg['server'] assert isinstance(res, dict) assert len(res) == 2 assert res['PORT'] == 1293 assert res['port'] == 1293 assert cfg['SERVER__PORT'] == 1293 assert cfg['server__port'] == 1293 assert cfg['SERVER']['PORT'] == 1293 assert cfg['server']['PORT'] == 1293 assert cfg['SERVER']['port'] == 1293 assert cfg['server']['port'] == 1293 with pytest.raises(KeyError): cfg['SERVER'][4] assert cfg['{}_SERVER__PORT'.format(NSPACE)] == 1293 assert cfg['{}__SERVER__PORT'.format(NSPACE)] == 1293 assert cfg['nega'] == 'Uvavo' assert cfg['NEGA'] == 'Uvavo' assert cfg['mike'] == '88' assert cfg['MAN']['HEIGHT'] == '175' assert cfg['MAN__WEIGHT'] == '73' with pytest.raises(ValueError): cfg[54] assert cfg['MOCK__LVL'] == 'A' assert cfg['mock__lvl'] == 'A' assert cfg['MOCK']['LVL'] == 'A' assert cfg['mock']['LVL'] == 'A' assert cfg['MOCK']['lvl'] == 'A' assert cfg['mock']['lvl'] == 'A' assert cfg['MOCK__LVL2'] == 'B' assert cfg['mock__lvl2'] == 'B' assert cfg['NOT_LVL2'] == 'C' assert cfg.get('NOT_LVL2') == 'C' assert cfg.get('NOT_LVL2', '32') == 'C' assert cfg.get('doesnt exists', '3321') == '3321' assert cfg.get('doesnt exists', None) is None with pytest.warns(UserWarning): assert cfg.get('doesnt exists', None, warn=True) is None with pytest.raises(KeyError): assert cfg['JON'] == 'Hello' assert cfg.get('JON') is None assert cfg.get('JON', default='Hello') == 'Hello' with pytest.raises(KeyError): assert cfg.get('JON', throw=True) is None assert len(cfg) == 17 for name, value in cfg: assert isinstance(name, str)
def test_yaml(): dpath = os.path.join( os.path.expanduser('~'), '.{}'.format(NSPACE2), ) cfg = Birch( NSPACE2, directories=[dpath], supported_formats=['yaml'], ) print(cfg._val_dict) assert cfg['lone'] == 'puf' assert cfg['LONE'] == 'puf' assert cfg['WRITE']['A'] == 'koko' assert cfg['write']['A'] == 'koko' assert cfg['WRITE']['a'] == 'koko' assert cfg['write']['a'] == 'koko' assert cfg['WRITE__A'] == 'koko' assert cfg['MOLE'] == 'geers' assert cfg['SHAKE']['BAKE'] == 'bob' assert cfg['SHAKE__BAKE'] == 'bob' assert cfg['PING_PONG'] == 'lola' with pytest.raises(KeyError): assert cfg['PING']['PONG'] == 'lola' with pytest.raises(KeyError): assert cfg['JON'] == 'Hello' for name, value in cfg: assert isinstance(name, str)
def test_default_casters(): cfg = Birch( namespace=NSPACE4, default_casters={ 'biil': true_false_caster, 'bool': true_false_caster, 'baal': true_false_caster, 'shik': { 'shuk': int, } }, ) val = cfg['pik'] assert isinstance(val, str) val = cfg['biil'] assert isinstance(val, bool) assert val is True val = cfg['bool'] assert isinstance(val, bool) assert val is False with pytest.raises(ValueError): val = cfg['baal'] val = cfg['shik__shuk'] assert isinstance(val, int) assert val == 8 val = cfg['shik']['shuk'] assert isinstance(val, str) assert val == '8'
def test_envvar_with_reload(): cfg = Birch( NSPACE2, directories=[os.path.expanduser('~/.{}'.format(NSPACE2))], supported_formats=['yaml'], ) print(cfg._val_dict) assert cfg['mole'] == 'geers' assert cfg['MOLE'] == 'geers' mole_envar = '{}__MOLE'.format(NSPACE2.upper()) mole_val = 'kirgizi' os.environ[mole_envar] = mole_val cfg.reload() print(cfg._val_dict) assert cfg[mole_envar] == mole_val assert cfg['mole'] == mole_val assert cfg['MOLE'] == mole_val
def test_yaml_with_reload(): cfg = Birch( NSPACE2, directories=[os.path.expanduser('~/.{}'.format(NSPACE2))], supported_formats=['yaml'], ) print(cfg._val_dict) assert cfg['lone'] == 'puf' assert cfg['LONE'] == 'puf' lone_val = 'kakara' updated_valdict = copy.deepcopy(VAL_DICT2) updated_valdict['lone'] = lone_val setup_cfg_file(namespace=NSPACE2, val_dict=updated_valdict, ext='yml') cfg.reload() print(cfg._val_dict) assert cfg['lone'] == lone_val assert cfg['LONE'] == lone_val
def test_xdg_cfg_dir_with_load_all(): cfg = Birch(NSPACE4, load_all=True) print(cfg._val_dict) assert cfg['pik'] == 'puk' assert cfg['shik']['shuk'] == str(8) assert cfg['shik__shuk'] == str(8) with pytest.raises(KeyError): assert cfg['JON'] == 'Hello' for name, value in cfg: assert isinstance(name, str)
def test_directories_str_param(): cfg = Birch( NSPACE2, directories=os.path.expanduser('~/{}'.format(NSPACE2)), supported_formats='yaml', ) print(cfg._val_dict) assert cfg['lone'] == 'puf' assert cfg['WRITE']['A'] == 'koko' assert cfg['WRITE__A'] == 'koko' assert cfg['MOLE'] == 'geers' assert cfg['SHAKE']['BAKE'] == 'bob' assert cfg['shake']['BAKE'] == 'bob' assert cfg['SHAKE']['bake'] == 'bob' assert cfg['shake']['bake'] == 'bob' assert cfg['SHAKE__BAKE'] == 'bob' with pytest.raises(KeyError): assert cfg['JON'] == 'Hello' for name, value in cfg: assert isinstance(name, str)
def test_yaml_with_auto_reload(): prepare_namespace_2() dpath = os.path.join( os.path.expanduser('~'), '.{}'.format(NSPACE2), ) cfg = Birch( NSPACE2, directories=[dpath], supported_formats=['yaml'], auto_reload=True, ) print(cfg._val_dict) assert cfg['lone'] == 'puf' assert cfg['LONE'] == 'puf' lone_val = 'kakara' updated_valdict = copy.deepcopy(VAL_DICT2) updated_valdict['lone'] = lone_val setup_cfg_file(namespace=NSPACE2, val_dict=updated_valdict, ext='yml') print(cfg._val_dict) assert cfg['lone'] == lone_val assert cfg['LONE'] == lone_val
def test_envvar_with_auto_reload(): prepare_namespace_2() dpath = os.path.join( os.path.expanduser('~'), '.{}'.format(NSPACE2), ) cfg = Birch( NSPACE2, directories=[dpath], supported_formats=['yaml'], auto_reload=True, ) print(cfg._val_dict) assert cfg['mole'] == 'geers' assert cfg['MOLE'] == 'geers' mole_envar = '{}__MOLE'.format(NSPACE2.upper()) mole_val = 'kirgizi' os.environ[mole_envar] = mole_val print(cfg._val_dict) assert cfg[mole_envar] == mole_val assert cfg['mole'] == mole_val assert cfg['MOLE'] == mole_val
def test_envvars_with_defaults(): prepare_namespace_2() k1 = 'NAKOKO' full_k1 = '{}_{}'.format(NSPACE2, k1) v1 = 45 k2_1 = 'NANA' k2_2 = 'BOKO' full_k2 = '{}__{}__{}'.format(NSPACE2, k2_1, k2_2) v2 = 'rar' k3 = 'lemer' v3 = 'yever' full_k4 = '{}_magi'.format(NSPACE2) v4 = 39 k5_1 = 'anil' k5_2 = 'shanil' v5 = 'baril' defaults = { k1: v1, full_k2: v2, k3: v3, full_k4: v4, k5_1: { k5_2: v5 }, } cfg = Birch( NSPACE2, defaults=defaults, ) assert cfg[k1] == v1 assert cfg[full_k1] == v1 assert cfg.mget(full_k1) == v1 assert cfg.get(k1) == v1 assert cfg[k2_1][k2_2] == v2 assert cfg[full_k2] == v2 assert cfg[k3] == v3 assert cfg[full_k4] == v4 assert cfg[k5_1][k5_2] == v5 with pytest.raises(ValueError): cfg = Birch( NSPACE2, defaults={2: 'kpkp'}, )
"""Barn configuration.""" import os from birch import Birch SHED_CFG = Birch('mlshed') def _base_dir(): dpath = SHED_CFG['base_dir'] if '~' in dpath: dpath = os.path.expanduser(dpath) return dpath def _snail_case(s): s = s.lower() return s.replace(' ', '_') def resource_dirpath(task=None, **kwargs): """Get the path of the corresponding resource directory. Parameters ---------- task : str, optional The task for which resources in the desired directory are used for. If not given, a path for the corresponding task-agnostic directory is returned.
"""Test commands from mongozen.util.""" import os import shutil import pytest import pandas as pd from birch import Birch from pymongo.mongo_client import MongoClient from pymongo.database import Database from pymongo.collection import Collection from mongozen.util import export_collection TEST_CFG = Birch('mztest', supported_formats='json') TEMP_DIR = os.path.expanduser('~/.tempmongozentest') @pytest.fixture(scope="session", autouse=True) def do_something(request): # Will be executed before the first test os.makedirs(TEMP_DIR, exist_ok=True) yield # Will be executed after the last test shutil.rmtree(TEMP_DIR) def test_export(): client = MongoClient(TEST_CFG['TESTDBURI'])
def test_unsupported_format(): with pytest.raises(UnsupporedFormatException): Birch(NSPACE2, supported_formats=['yaml', 'lie'])
"""Configuration for khronus.""" from birch import Birch CFG = Birch('khronus') LOG_DPATH_KEY = 'LOG_DPATH' def log_dpath(): try: return CFG[LOG_DPATH_KEY] except KeyError: return CFG.xdg_cache_dpath()
import queue from urllib.parse import quote_plus from birch import Birch import pytest import pymongo import hashlib import pandas as pd from pymongo.mongo_client import MongoClient from pymongo.errors import OperationFailure from cachier import cachier from cachier.mongo_core import _MongoCore, RecalculationNeeded CFG = Birch("cachier") class CfgKey(): HOST = "TEST_HOST" UNAME = "TEST_USERNAME" PWD = "TEST_PASSWORD" DB = "TEST_DB" URI_TEMPLATE = ( "mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority") def _get_cachier_db_mongo_client(): host = quote_plus(CFG[CfgKey.HOST])
"""Barn configuration.""" import os from birch import Birch BARN_CFG = Birch('barn') def _base_dir(): dpath = BARN_CFG['base_dir'] if '~' in dpath: dpath = os.path.expanduser(dpath) return dpath def _snail_case(s): s = s.lower() return s.replace(' ', '_') def data_dirpath(task=None, **kwargs): """Get the path of the corresponding data directory. Parameters ---------- task : str, optional The task for which datasets in the desired directory are used for. If not given, a path for the corresponding task-agnostic directory is returned. **kwargs : extra keyword arguments