def test_interpolation(self): parser = InterpolationNamespace() parser.default_section = 'main' parser.read('tests/vars.ini') self.assertEqual(parser['main']['var_test'], 'Hello world!') self.assertEqual(dict(parser['main'].items(raw=True))['var_test'], 'Hello { test}!') parser['main']['foo'] = 'bar {var_test}' self.assertEqual(parser['main']['foo'], 'bar Hello world!') parser['main']['test'] = '{foo}' if version_info >= (2, 7): with self.assertRaises(ValueError): self.assertEqual(parser['main']['foo'], 'bar Hello world!') parser['main']['test'] = 'parse {unknown}done' self.assertEqual(parser['main']['test'], 'parse done') self.assertEqual(parser['other']['b'], 'Hello parse done! start') test = dict(parser.default.items()) self.assertTrue('parse' in test['foo'])
def test_interpolation(self): parser = InterpolationNamespace() parser.default_section = 'main' parser.read('tests/vars.ini') self.assertEqual(parser['main']['var_test'], 'Hello world!') self.assertEqual( dict(parser['main'].items(raw=True))['var_test'], 'Hello { test}!') parser['main']['foo'] = 'bar {var_test}' self.assertEqual(parser['main']['foo'], 'bar Hello world!') parser['main']['test'] = '{foo}' if version_info >= (2, 7): with self.assertRaises(ValueError): self.assertEqual(parser['main']['foo'], 'bar Hello world!') parser['main']['test'] = 'parse {unknown}done' self.assertEqual(parser['main']['test'], 'parse done') self.assertEqual(parser['other']['b'], 'Hello parse done! start') test = dict(parser.default.items()) self.assertTrue('parse' in test['foo'])
def __init__(self, params, *tpldirs): """ Save params and create INI parser. """ self.params = params self.tpldirs = list(tpldirs) + self.default_tmpldirs # Initialize parser self.parser = InterpolationNamespace( current_dir=CURDIR, deploy_dir=self.params.TARGET, datetime=datetime.now(), USER=environ.get("USER"), **dict(params.context) ) self.parser.read(*self.default_configs) self.parser.read(self.params.config)
class Starter(FS): """ Clone templates to file system. """ # Seek user configs default_configs = map(lambda d: op.abspath(op.join(d, CFGFILE)), [ # nolint environ.get('HOME', '~'), CURDIR ]) # Seek templates default_tmpldirs = [ op.join(environ.get('HOME', '~'), HOME_TMPLDIR_NAME), BUILTIN_TMPLDIR] include_key = '__include__' def __init__(self, params, *tpldirs): """ Save params and create INI parser. """ self.params = params self.tpldirs = list(tpldirs) + self.default_tmpldirs # Initialize parser self.parser = InterpolationNamespace( current_dir=CURDIR, deploy_dir=self.params.TARGET, datetime=datetime.now(), USER=environ.get("USER"), **dict(params.context) ) self.parser.read(*self.default_configs) self.parser.read(self.params.config) def copy(self): """ Prepare and paste self templates. """ templates = self.prepare_templates() self.parser.default['templates'] = tt = ','.join( t.name for t in templates) logging.warning("Paste templates: {0}".format(tt)) self.make_directory(self.params.TARGET) return [t.paste(**self.parser.default) for t in templates] def prepare_templates(self): to_template = partial(map, lambda t: Template(t, tpldirs=self.tpldirs)) templates = list(to_template(self.params.TEMPLATES)) cache = set(templates) def open_templates(*templates): for t in templates: self.parser.read(t.configuration, update=False) cache.add(t) try: include = self.parser['templates'].pop(self.include_key) include = filter(None, oset( include.replace(' ', '').split(','))) requirements = filter( # nolint lambda t: not t in cache, to_template(include)) for tt in open_templates(*requirements): yield tt except KeyError: pass yield t return list(open_templates(*templates)) def list_templates(self): for dd in self.tpldirs: if not op.exists(dd): continue for pp in listdir(dd): pp = op.join(dd, pp) if op.isdir(pp): yield op.basename(pp) def __repr__(self): return "<Starter '{0}'>".format(CURDIR)