示例#1
0
 def test_filename_with_file_source(self):
     source = confuse.ConfigSource({'foo': 'foo/bar'},
                                   filename='/baz/config.yaml')
     config = _root(source)
     config.config_dir = lambda: '/config/path'
     valid = config['foo'].get(confuse.Filename())
     self.assertEqual(valid, os.path.realpath('/config/path/foo/bar'))
示例#2
0
    def load(filename=None):
        """
        Loads a file and imports the settings
        :param filename: the file to import
        """
        config = confuse.LazyConfig('Yatcobot', __name__)

        # Add default config when in egg (using this way because egg is breaking the default way)
        if len(config.sources) == 0:
            default_config_text = pkg_resources.resource_string("yatcobot.config", "config_default.yaml")
            default_config = confuse.ConfigSource(yaml.load(default_config_text, Loader=confuse.Loader),
                                                  'pkg/config/config_default.yaml',
                                                  True)
            config.add(default_config)

        # Add user specified config
        if filename is not None and os.path.isfile(filename):
            config.set_file(filename)

        logger.info('Loading config files (From highest priority to lowest):')
        config.resolve()
        for i, config_source in enumerate(config.sources):
            logger.info(f'{i}: Path: {config_source.filename}')

        # Update template from plugins
        template = Config.get_template()
        Config._valid = config.get(template)
示例#3
0
 def test_filename_in_app_dir_overrides_config_source_dir(self):
     source = confuse.ConfigSource({'foo': 'foo/bar'},
                                   filename='/baz/config.yaml',
                                   base_for_paths=True)
     config = _root(source)
     config.config_dir = lambda: '/config/path'
     valid = config['foo'].get(confuse.Filename(in_app_dir=True))
     self.assertEqual(valid, os.path.realpath('/config/path/foo/bar'))
示例#4
0
 def test_as_filename_with_default_source(self):
     source = confuse.ConfigSource({'foo': 'foo/bar'},
                                   filename='/baz/config.yaml',
                                   default=True)
     config = _root(source)
     config.config_dir = lambda: '/config/path'
     value = config['foo'].as_filename()
     self.assertEqual(value, os.path.realpath('/config/path/foo/bar'))
示例#5
0
def config_stack(*sources):
    """Uses Confuse config flattener to merge together nested config sources.
    The main goal here is to allow multiple sources to merge smoothly rather
    than completely overwrite contents for nested properties.

    In this example, a is the project config and b is the config from command
    arguments. We want the command arguments to overwrite the project config,
    but a standard a.update(b) would not give the desired results

        a = {'foo': {'bar': 42, 'baz': 24}}
        b = {'foo': {'bar': 123}}

        >>> a.update(b)
        >>> a
        {'foo': {'bar': 123}}

        Instead config stack gives us a union:

        >>> config_stack(a, b)
        {'foo': {'bar': 123, 'baz': 24}}


    Warning: this is accomplished with a couple hacks -

    1) To prevent Confuse from searching the filesystem for existing config
    files, an unlikely name is added to the Configuration object when it's
    created.

    2) The resulting config object is json dumped then loaded to get convert
    the ordered dictionaries to standard dicts. So.. high performance is not a
    focus here.

    3) Appending/extending a nested array is still not possible:

        >>> b = {'foo': {'bar': [42, 24,]}}
        >>> a = {'foo': {'bar': [123,]}}
        >>> jetstream.utils.config_stack(a, b)
        {'foo': {'bar': [42, 24]}}

    """
    # Hack to prevent Confuse from accidentally finding a config file on the
    # system when were just trying to create an in-memory config object
    n = 'UNLIKELYAPPNAME-' + str(uuid4())
    conf = confuse.Configuration(n, read=False)
    for s in sources:
        if s:
            if isinstance(s, Mapping):
                conf.set(confuse.ConfigSource(s))
            else:
                err = 'Config sources should return Mapping-type objects'
                raise ValueError(err)

    # Hack to remove the ordereddicts, they're just ugly to look at and when
    # working with primitive data types coming from json/yaml files they're
    # generally useless.
    return json.loads(json.dumps(conf.flatten()))
示例#6
0
 def add_config(self, configfn):
     self.__config.add(
         confuse.ConfigSource(confuse.load_yaml(configfn), configfn))
     self.update_conf()
示例#7
0
 def _add_default_source(self):
     filename = self.default_config
     self.add(
         confuse.ConfigSource(confuse.load_yaml(filename), filename, True))
示例#8
0
 def _add_user_source(self):
     if not Path(self._config).is_file():
         Path(self._config).write_bytes(Path(self._default).read_bytes())
     data = confuse.load_yaml(self._config, loader=self.loader)
     self.add(
         confuse.ConfigSource(data, filename=self._config, default=True))
示例#9
0
 def _add_default_source(self):
     assert Path(self._default).is_file()
     data = confuse.load_yaml(self._default, loader=self.loader)
     self.add(
         confuse.ConfigSource(data, filename=self._default, default=True))
示例#10
0
 def test_filename_in_app_dir_non_file_source(self):
     source = confuse.ConfigSource({'foo': 'foo/bar'})
     config = _root(source)
     config.config_dir = lambda: '/config/path'
     valid = config['foo'].get(confuse.Filename(in_app_dir=True))
     self.assertEqual(valid, os.path.realpath('/config/path/foo/bar'))
示例#11
0
    'device': str,
    'username': str,
    'password': str,
    'domain': str,
    'ac_policy': str,
    'intrusion_policy': str,
    'file_policy': str,
    'variable_set': str,
    'syslog_to_server': str,
    'log_to_fmc': bool,
    'log_at_begin': bool,
    'log_at_end': bool
}
config = confuse.Configuration('fmc-tools', __name__)
try:
    config.add(confuse.ConfigSource(confuse.load_yaml('config.yaml'), 'config.yaml', True))
except confuse.ConfigReadError:
    pass
config.add(confuse.ConfigSource(confuse.load_yaml('config_default.yaml'), 'config_default.yaml', True))

valid = config.get(template)

# Set variables for execution.
# Make sure your credentials are correct.
# Make sure ACP and all logging and inspection objects already exist.

loglevel = valid.loglevel
device = valid.device
username = valid.username
password = valid.password
domain = valid.domain