def _combine(self, env=os.environ, keep_string=None): """ Perform a prioritized recursive merge of config files. Returns a new dict. Prior to merging the config files are interpolated with environment variables. 1. Loads Molecule defaults. 2. Loads a base config (if provided) and merges ontop of defaults. 3. Loads the scenario's ``molecule file`` and merges ontop of previous merge. :return: dict """ defaults = self._get_defaults() base_config = self.args.get("base_config") if base_config and os.path.exists(base_config): with util.open_file(base_config) as stream: s = stream.read() self._preflight(s) interpolated_config = self._interpolate(s, env, keep_string) defaults = util.merge_dicts( defaults, util.safe_load(interpolated_config)) if self.molecule_file: with util.open_file(self.molecule_file) as stream: s = stream.read() self._preflight(s) interpolated_config = self._interpolate(s, env, keep_string) defaults = util.merge_dicts( defaults, util.safe_load(interpolated_config)) return defaults
def _combine(self): """ Perform a prioritized recursive merge of config files, and returns a new dict. Prior to merging the config files are interpolated with environment variables. 1. Loads Molecule defaults. 2. Loads a base config (if provided) and merges ontop of defaults. 3. Loads the scenario's `molecule file` and merges ontop of previous merge. :return: dict """ defaults = self._get_defaults() base_config = self.args.get('base_config') if base_config: if os.path.exists(base_config): with util.open_file(base_config) as stream: interpolated_config = self._interpolate(stream.read()) defaults = util.merge_dicts( defaults, util.safe_load(interpolated_config)) with util.open_file(self.molecule_file) as stream: interpolated_config = self._interpolate(stream.read()) defaults = util.merge_dicts(defaults, util.safe_load(interpolated_config)) return defaults
def test_safe_load_exits_when_cannot_parse(): data = """ --- %foo: """.strip() with pytest.raises(SystemExit) as e: util.safe_load(data) assert 1 == e.value.code
def _config(_molecule_file, request): with open(_molecule_file) as f: d = util.safe_load(f) if hasattr(request, "param"): if isinstance(request.getfixturevalue(request.param), str): d2 = util.safe_load(request.getfixturevalue(request.param)) else: d2 = request.getfixturevalue(request.param) # print(100, d) # print(200, d2) d = util.merge_dicts(d, d2) # print(300, d) return d
def _combine(self): """ Perform a prioritized recursive merge of the `molecule_file` with defaults, interpolate the result with environment variables, and returns a new dict. :return: dict """ i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) base = self._get_defaults() with util.open_file(self.molecule_file) as stream: try: interpolated_config = i.interpolate(stream.read()) base = self.merge_dicts(base, util.safe_load(interpolated_config)) except interpolation.InvalidInterpolation as e: msg = ("parsing config file '{}'.\n\n" '{}\n{}'.format(self.molecule_file, e.place, e.string)) util.sysexit_with_message(msg) schema.validate(base) return base
def pre_validate(stream, env, keep_string): data = util.safe_load(stream) v = Validator(allow_unknown=True) v.validate(data, pre_validate_base_schema(env, keep_string)) return v.errors
def pre_validate(stream, env: MutableMapping, keep_string: str): """Pre-validate stream.""" data = util.safe_load(stream) v = Validator(allow_unknown=True) v.validate(data, pre_validate_base_schema(env, keep_string)) return v.errors, data
def from_yaml(data): """ Interpolate the provided data and return a dict. Currently, this is used to reinterpolate the `molecule.yml` inside an Ansible playbook. If there were any interpolation errors, they would have been found and raised earlier. :return: dict """ i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) interpolated_data = i.interpolate(data) return util.safe_load(interpolated_data)
def _combine(self): """ Perform a prioritized recursive merge of the `molecule_file` with defaults, interpolate the result with environment variables, and returns a new dict. :return: dict """ i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) base = self._get_defaults() with open(self.molecule_file, 'r') as stream: interpolated_config = i.interpolate(stream.read()) base = self.merge_dicts(base, util.safe_load(interpolated_config)) return base
def _combine(self): """ Perform a prioritized recursive merge of the `molecule_file` with defaults, interpolate the result with environment variables, and returns a new dict. :return: dict """ i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) base = self._get_defaults() with util.open_file(self.molecule_file) as stream: interpolated_config = i.interpolate(stream.read()) base = self.merge_dicts(base, util.safe_load(interpolated_config)) schema.validate(base) return base
def from_yaml(data): """ Interpolate the provided data and return a dict. Currently, this is used to reinterpolate the `molecule.yml` inside an Ansible playbook. If there were any interpolation errors, they would have been found and raised earlier. :return: dict """ molecule_env_file = os.environ['MOLECULE_ENV_FILE'] env = os.environ.copy() env = config.set_env_from_file(env, molecule_env_file) i = interpolation.Interpolator(interpolation.TemplateWithDefaults, env) interpolated_data = i.interpolate(data) return util.safe_load(interpolated_data)
<<<<<<< HEAD:Rake/molecule/__GEMS_.py/__GEMS_.py/apt-py.git/commandinit.yaml/init.yml/config.py i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) ======= ======= <<<<<<< HEAD:molecule/config.py >>>>>>> e91355cf081d9dcd78efe38cdcc6f0353a1aa3ac defaults = self._get_defaults() base_config = self.args.get('base_config') if base_config and os.path.exists(base_config): with util.open_file(base_config) as stream: s = stream.read() self._preflight(s) interpolated_config = self._interpolate(s, env, keep_string) defaults = util.merge_dicts( defaults, util.safe_load(interpolated_config)) <<<<<<< HEAD >>>>>>> 0fa82e7a3daa84ebd03d8af67403c6551113d3e4:molecule/config.py base = self._get_defaults() with util.open_file(self.molecule_file) as stream: <<<<<<< HEAD:Rake/molecule/__GEMS_.py/__GEMS_.py/apt-py.git/commandinit.yaml/init.yml/config.py try: interpolated_config = i.interpolate(stream.read()) base = self.merge_dicts(base, ======= ======= ======= i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) >>>>>>> b1eb06d375fd544a849fcf5c39f51dc334b87338:Rake/molecule/__GEMS_.py/__GEMS_.py/apt-py.git/commandinit.yaml/init.yml/config.py
def _config(_molecule_file, request): d = util.safe_load(open(_molecule_file)) if hasattr(request, 'param'): d = util.merge_dicts(d, request.getfixturevalue(request.param)) return d
def test_safe_load(): assert {"foo": "bar"} == util.safe_load("foo: bar")
def test_safe_load_returns_empty_dict_on_empty_string(): assert {} == util.safe_load("")
def _get_v1_config(self): d = util.safe_load(open(self._molecule_file)) schema_v1.validate(d) return d
def test_safe_load(): assert {'foo': 'bar'} == util.safe_load('foo: bar')
def pre_validate(stream, env, keep_string): data = util.safe_load(stream) v = Validator(allow_unknown=True)
if molecule_env_var: if re.match(pattern, value): msg = ('cannot reference $MOLECULE special variables ' 'in this section') self._error(field, msg) <<<<<<< HEAD <<<<<<< HEAD:Rake/Gemfile/molecule/model/schema_v2.py def pre_validate(stream, env, keep_string): ======= <<<<<<< HEAD:molecule/model/schema_v2.py def pre_validate(stream): >>>>>>> e91355cf081d9dcd78efe38cdcc6f0353a1aa3ac data = util.safe_load(stream) v = Validator(allow_unknown=True) v.validate(data, pre_validate_base_schema) ======= def pre_validate(stream, env, keep_string): data = util.safe_load(stream) v = Validator(allow_unknown=True) <<<<<<< HEAD v.validate(data, pre_validate_base_schema) >>>>>>> 0fa82e7a3daa84ebd03d8af67403c6551113d3e4:molecule/model/schema_v2.py ======= v.validate(data, pre_validate_base_schema(env, keep_string)) >>>>>>> b1eb06d375fd544a849fcf5c39f51dc334b87338:Rake/Gemfile/molecule/model/schema_v2.py >>>>>>> e91355cf081d9dcd78efe38cdcc6f0353a1aa3ac
def from_yaml(data): i = interpolation.Interpolator(interpolation.TemplateWithDefaults, os.environ) interpolated_data = i.interpolate(data) return util.safe_load(interpolated_data)
def test_safe_load_returns_empty_dict_on_empty_string(): assert {} == util.safe_load('')
def config(molecule_v1_file): return util.safe_load(open(molecule_v1_file))
def _get_v1_config(self): d = util.safe_load(open(self._molecule_file)) errors = schema_v1.validate(d) self._check_errors(errors) return d