Esempio n. 1
0
    def _values_from_config_file(clazz, config_filename):
        error_class = clazz.error_class()
        if not path.exists(config_filename):
            raise error_class(
                'Config file not found: "{}"'.format(config_filename))

        if not path.isfile(config_filename):
            raise error_class(
                'Config file is not a file: "{}"'.format(config_filename))

        try:
            config = simple_config.from_file(config_filename)
        except Exception as ex:
            raise error_class(str(ex))

        config_file_section = clazz.config_file_section()
        if not config.has_section(config_file_section):
            raise error_class(
                'No section "{}" found in config file: "{}"'.format(
                    config_file_section, config_filename))
        section = config.section(config_file_section)
        values = section.to_dict(
            resolve_env_vars=not clazz.ignore_config_file_variables())

        valid_keys = clazz.default_values().keys()
        filtered_values = dict_util.filter_with_keys(values, valid_keys)
        return filtered_values
Esempio n. 2
0
  def test_variables_global(self):
    text = '''\
kiwi
  color: ${COLOR}
  flavor: ${FLAVOR}
  size: small

apple
  color: ${COLOR}
  flavor: ${FLAVOR}
  size: small

lemon
  color: ${COLOR}
  flavor: ${FLAVOR}
  size: small
'''
    tmp = self.make_temp_file(content = text, suffix = '.config')
    s = simple_config.from_file(tmp)
    s.set_variables({
      'COLOR': 'green',
      'FLAVOR': 'tart',
    })
    self.assertEqual( 'small', s.kiwi.size )
    self.assertEqual( 'green', s.kiwi.color )
    self.assertEqual( 'tart', s.kiwi.flavor )

    self.assertEqual( 'small', s.apple.size )
    self.assertEqual( 'green', s.apple.color )
    self.assertEqual( 'tart', s.apple.flavor )

    self.assertEqual( 'small', s.lemon.size )
    self.assertEqual( 'green', s.lemon.color )
    self.assertEqual( 'tart', s.lemon.flavor )
Esempio n. 3
0
    def add_tasks_from_config(self, config_filename):
        check.check_string(config_filename)

        config = simple_config.from_file(config_filename)
        for section in config:
            task, values = self._parse_task(section)
            if task:
                self.add_task(task, values)
Esempio n. 4
0
 def _config(self):
     if not path.exists(self._filename):
         file_util.save(self._filename, content='', codec='utf-8')
     return simple_config.from_file(
         self._filename,
         check_env_vars=False,
         entry_parser=self._parse_ssh_config_entry,
         entry_formatter=self._ssh_config_entry_formatter)
Esempio n. 5
0
def _read_config_file():
    DEFAULT_CONFIG = '''\
# config file for bes_test.py.  Goes in ~/.bes_test/bes_test.config
environment
  keep_keys: DEBUG VERBOSE
  keep_patterns: BES.* BES_.*

python
  keep_keys: DEBUG VERBOSE
  keep_patterns: BES.* BES_.*

'''
    from bes.config.simple_config import simple_config
    config_filename = path.expanduser('~/.bes_test/bes_test.config')
    if path.exists(config_filename):
        return simple_config.from_file(config_filename)
    else:
        return simple_config.from_text(DEFAULT_CONFIG)
Esempio n. 6
0
 def load(clazz, config_filename):
     if not path.isfile(config_filename):
         raise vfs_error(
             'config_filename not found: {}'.format(config_filename))
     config = simple_config.from_file(config_filename)
     sections = config.find_all_sections('fsconfig')
     if not sections:
         raise vfs_error(
             'no fsconfig section found: {}'.format(config_filename))
     if len(sections) != 1:
         raise vfs_error(
             'only one fsconfig section should be given: {}'.format(
                 config_filename))
     values = sections[0].to_key_value_list(resolve_env_vars=True).to_dict()
     fs_type = values.get('vfs_type', None)
     if fs_type is None:
         raise vfs_error(
             'no fs_type found in fsconfig: {}'.format(config_filename))
     del values['vfs_type']
     return vfs_config(fs_type, values)