Example #1
0
    def test_circular_self(self):
        f = tempfile.NamedTemporaryFile(suffix='.yaml')

        f.write('''
a: !include {}
        '''.format(f.name).encode())
        f.flush()
        f.seek(0)

        with self.assertRaises(ValueError):
            yamlenv.load(f)
Example #2
0
    def test_interpolate_invalid_yaml_value(self):
        self.assertEqual(
            yamlenv.load('''
{'a': {'b': '{foo} ${C:-foo}'}}
            '''), {'a': {
                'b': "{foo} foo"
            }})
Example #3
0
 def from_yaml_stream(cls, stream):
     """
     >>> os.environ['PORT_TEST'] = '5000'
     >>> ConfigDict.from_yaml_stream('port_test: ${PORT_TEST}')
     {'port_test': 5000}
     """
     return cls(yamlenv.load(stream))
Example #4
0
    def test_interpolate_nested(self):
        self.assertEqual(yamlenv.load('''
a: 1
b:
  b1: 21
  b2: ${B-22}
            '''), {'a': 1, 'b': {'b1': 21, 'b2': 22}})
def load_config_file(config_name):
    logger = logging.getLogger('utils.load_config_file')

    config_path = f'configs/{config_name}.yaml'

    logger.info(f"Using config {config_path}")
    with open(config_path, 'r') as stream:
        return yamlenv.load(stream)
Example #6
0
    def test_interpolate_default(self):
        self.assertEqual(yamlenv.load('''
a: ${A-1}
b: 2
            '''), {
            'a': '1',
            'b': 2
        })
Example #7
0
    def test_interpolate_default_alternative_separator(self):
        self.assertEqual(yamlenv.load('''
a: ${A:-1}
b: 2
            '''), {
            'a': '1',
            'b': 2
        })
Example #8
0
    def test_load(self):
        self.assertEqual(yamlenv.load('''
a: 1
b: 2
        '''), {
            'a': 1,
            'b': 2
        })
Example #9
0
    def test_interpolate_string_prefer_env(self):
        os.environ['A'] = 'password'
        self.assertEqual(yamlenv.load('''
a: ${A-1}
b: 2
        '''), {
            'a': 'password',
            'b': 2
        })
Example #10
0
    def test_interpolate_string_alternative_separator(self):
        os.environ['A'] = 'password'
        self.assertEqual(yamlenv.load('''
a: ${A:-1}
b: 2
        '''), {
            'a': 'password',
            'b': 2
        })
Example #11
0
    def test_interpolate_within_characters(self):
        os.environ['A'] = 'def'
        self.assertEqual(yamlenv.load('''
a: abc${A}ghi
b: 2
        '''), {
            'a': 'abcdefghi',
            'b': 2
        })
Example #12
0
    def test_interpolate_integer(self):
        os.environ['A'] = '1'
        self.assertEqual(yamlenv.load('''
a: ${A}
b: 2
            '''), {
            'a': '1',
            'b': 2
        })
Example #13
0
    def test_empty_file(self):
        f = tempfile.NamedTemporaryFile(suffix='.yaml')

        data = yamlenv.load('''
a: 1
b: !include {}
        '''.format(f.name))

        self.assertEqual(data, {'a': 1, 'b': None})
Example #14
0
    def test_include(self):
        f = tempfile.NamedTemporaryFile(suffix='.yaml')
        f.write(b'2')
        f.flush()

        data = yamlenv.load('''
a: 1
b: !include {}
        '''.format(f.name))

        self.assertEqual(data, {'a': 1, 'b': 2})
def inject(input_file: str = None,
           output_file: str = None,
           secrets: dict = None) -> None:
    """
    Function that takes an input template, injects secrets into it,
    and writes the interpolated result to an output file.
    """

    for k, v in secrets.items():
        os.environ[k] = v
    with open(input_file, 'r') as f_in:
        interpolated_template = yamlenv.load(f_in.read())
        with open(output_file, 'w') as f_out:
            f_out.write(yaml.dump(interpolated_template))
Example #16
0
    def test_fail_if_no_env(self):
        with self.assertRaises(ValueError):
            yamlenv.load('''
a: ${A}
b: 2
            ''')
Example #17
0
 def test_non_existing(self):
     with self.assertRaises(IOError):
         yamlenv.load('''
 a: 1
 b: !include non-existing.yaml
         ''')
Example #18
0
    def test_interpolate_two_values(self):
        os.environ['A'] = '1'
        os.environ['B'] = 'foo'
        self.assertEqual(yamlenv.load('''
a: ${A} ${B}
            '''), {'a': "1 foo"})
Example #19
0
    def test_interpolate_default_empty(self):
        self.assertEqual(yamlenv.load('''
a: bar ${FOO-} bar
'''), {'a': 'bar  bar'})
Example #20
0
    def test_interpolate_embedded_integer(self):
        self.assertEqual(yamlenv.load('''
a: ${FOO:-7} bar
'''), {'a': '7 bar'})
Example #21
0
    def test_interpolate_embedded(self):
        self.assertEqual(yamlenv.load('''
a: ${FOO:-foo} bar
'''), {'a': 'foo bar'})
Example #22
0
    def test_default_empty(self):
        self.assertEqual(yamlenv.load('''
a: ${FOO-}
'''), {'a': None})
Example #23
0
    def test_interpolate_two_values_with_defaults(self):
        self.assertEqual(
            yamlenv.load('''
a: ${C:-foo} ${D:-bar}
            '''), {'a': "foo bar"})
Example #24
0
def read_yaml(path):
    """Loads config from a YAML file with env interpolation"""
    with open(path, 'r') as yaml:
        contents = yaml.read()
        return yamlenv.load(contents)
Example #25
0
    def test_default_does_not_exist(self):
        with self.assertRaises(ValueError):
            yamlenv.load('''
a: ${FOO:-} bar
''')