def test_overlay(self, dir): pytest.importorskip("yaml") path1 = dir.write( 'etc/myapp.yml', ''' base: 1 user: bad file: bad ''') path2 = dir.write( 'home/.myapp.yml', ''' user: 2 file: bad-user ''') path3 = dir.write('app.yml', ''' file: 3 ''') config = Config.from_path(path1) config.merge(Config.from_path(path2)) config.merge(Config.from_path(path3)) compare(config.base, expected=1) compare(config.user, expected=2) compare(config.file, expected=3)
def test_layered(self, dir): # defaults config = Config({'database': {'user': '******'}, 'special': False}) # from system file: path = dir.write('etc/myapp.json', '{"special": true}') config.merge(Config.from_path(path)) # from user file: path = dir.write('home/user/myapp.json', '{"database": {"password": "******"}}') config.merge(Config.from_path(path)) # end result: compare(config.database.user, expected='foo') compare(config.database.password, expected='123') compare(config.special, expected=True)
def test_layered(self, dir): # defaults config = Config({ 'database': {'user': '******'}, 'special': False }) # from system file: path = dir.write('etc/myapp.json', '{"special": true}') config.merge(Config.from_path(path)) # from user file: path = dir.write('home/user/myapp.json', '{"database": {"password": "******"}}') config.merge(Config.from_path(path)) # end result: compare(config.database.user, expected='foo') compare(config.database.password, expected='123') compare(config.special, expected=True)
def test_overlay(self, dir): pytest.importorskip("yaml") path1 = dir.write('etc/myapp.yml', ''' base: 1 user: bad file: bad ''') path2 = dir.write('home/.myapp.yml', ''' user: 2 file: bad-user ''') path3 = dir.write('app.yml', ''' file: 3 ''') config = Config.from_path(path1) config.merge(Config.from_path(path2)) config.merge(Config.from_path(path3)) compare(config.base, expected=1) compare(config.user, expected=2) compare(config.file, expected=3)
def load_config(): config = Config() cwd = Path.cwd() dirs = [cwd, *cwd.parents] for dir in reversed(dirs): paths = [ dir / '.config' / 'freezerbox' / 'conf.toml', dir / '.freezerboxrc', ] for path in paths: if path.exists(): subconf = Config.from_path(path, parser='toml') config.merge(subconf) dir = dir.parent return config.data
target_path = Path(target).expanduser() / f'octopus-{date}{suffix}.csv' with target_path.open('w') as target_file: headers = ['interval_start', 'interval_end', 'consumption'] writer = csv.DictWriter(target_file, headers) writer.writerow({h: h for h in headers}) for reading in readings: writer.writerow(reading) print(f'Downloaded {target_path}') def date(text): return pendulum.parse(text, tz='Europe/London') def parse_args(): parser = ArgumentParser() parser.add_argument('--start', type=date) parser.add_argument('--end', type=date) return parser.parse_args() if __name__ == '__main__': config = Config.from_path('config.yaml') args = parse_args() download(start=args.start.start_of('day') if args.start else None, end=args.end.end_of('day') if args.end else None, target=config.directories.storage, **config.octopus.api.data)
"~/.config/reportinator/layouts", "~/AppData/Local/Programs/reportinator/layouts", }, }, "locations": { "/etc/reportinator.yaml", "~/.reportinator/config.yaml", "~/.config/reportinator/config.yaml", "~/AppData/Local/Programs/reportinator/config.yaml", }, "location": "", } ) for dirs in config.locations: config = config + Config.from_path(dirs, optional=True) for dirs in config.script.locations: dirs = os.path.expanduser(dirs) if os.path.isdir(dirs): config.script.location = dirs break for dirs in config.locations: dirs = os.path.expanduser(dirs) if os.path.exists(dirs): config.location = dirs break for dirs in config.layout.locations: dirs = os.path.expanduser(dirs)
def test_path_with_encoding(self): with NamedTemporaryFile() as source: source.write(b'{"x": "\xa3"}') source.flush() config = Config.from_path(source.name, 'json', encoding='latin-1') compare(config.x, expected=u'\xa3')
"""Try Gammapy config with configurator.""" from astropy.coordinates import Angle from configurator import Config from voluptuous import Schema, All, Required, PathExists if __name__ == '__main__': # Support to and from dict / JSON / YAML config = Config.from_path('template-basic.yaml') # Support hierarchical config (general.logging.level) # Support lists for composed observation filtering # Property dot access and tab completion for getting & setting # Nice representation of overall config and specific settings # Support for config update with a file or dict as in dict.update() # Validation for python and custom types at init and setting: # regexp (short syntax for complex params) # astropy quantities and angles # dependencies and required values # Support for type casting? # Support "frozen" config to guard against mistyping, i.e. config.something = "spam" will raise an error if config.something isn't defined in the config # Good error messages schema = Schema({ 'cache': { 'location': All(str, PathExists()), 'max_files': int }, 'banner': Required(str), 'threads': Required(int), })
def test_path_explicit_callable_parser(self): with NamedTemporaryFile() as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name, python_literal) compare(config.x, expected=1)
def test_path_guess_parser_bad_extension(self): with NamedTemporaryFile(suffix='.nope') as source: with ShouldRaise(ParseError("No parser found for 'nope'")): Config.from_path(source.name)
def test_path_guess_parser(self): with NamedTemporaryFile(suffix='.json') as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name) compare(config.x, expected=1)
def test_fake_fs(fs): fs.create_file('/foo/bar.yml', contents='foo: 1\n') config = Config.from_path('/foo/bar.yml') compare(config.foo, expected=1)
def test_fake_fs(fs): fs.create_file('/foo/bar.yml', contents='foo: 1\n') config = Config.from_path('/foo/bar.yml') compare(config.foo, expected=1)
def test_path_guess_parser(self): with NamedTemporaryFile(suffix='.json') as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name) compare(config.x, expected=1)
def test_path_guess_parser_no_extension(self): with TempDirectory() as dir: path = dir.write('nope', b'{"x": 1}') with ShouldRaise(ParseError("No parser found for None")): Config.from_path(path)
def test_path_guess_parser_no_extension(self): with TempDirectory() as dir: path = dir.write('nope', b'{"x": 1}') with ShouldRaise(ParseError("No parser found for None")): Config.from_path(path)
def test_path_explicit_string_parser(self): with NamedTemporaryFile() as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name, 'json') compare(config.x, expected=1)
def test_path_guess_parser_bad_extension(self): with NamedTemporaryFile(suffix='.nope') as source: with ShouldRaise(ParseError("No parser found for 'nope'")): Config.from_path(source.name)
def test_path_with_encoding(self): with NamedTemporaryFile() as source: source.write(b'{"x": "\xa3"}') source.flush() config = Config.from_path(source.name, 'json', encoding='latin-1') compare(config.x, expected=u'\xa3')
def test_path_explicit_string_parser(self): with NamedTemporaryFile() as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name, 'json') compare(config.x, expected=1)
from pathlib import Path from configurator import Config config = Config.from_path(Path(__file__).parent / 'config.yaml')
def test_path_explicit_callable_parser(self): with NamedTemporaryFile() as source: source.write(b'{"x": 1}') source.flush() config = Config.from_path(source.name, python_literal) compare(config.x, expected=1)