def __init__(self, *args, **kwargs):
     name                = kwargs.pop('namespace', config.DEFAULT)
     flatten             = kwargs.pop('flatten', True)
     config_data         = dict(*args, **kwargs)
     self.namespace      = config.get_namespace(name)
     self.config_data    = dict(loader.flatten_dict(config_data)) if flatten else config_data
     self.old_values     = None
 def setup_configuration_values(self):
     validator = mock.Mock(return_value=2)
     self.name = 'test_namespace'
     self.namespace = config.get_namespace(self.name)
     self.config_key = 'something'
     self.value_proxy = proxy.ValueProxy(
         validator, self.namespace, self.config_key)
 def setup_configuration_values(self):
     validator = mock.Mock(return_value=2)
     self.name = 'test_namespace'
     self.namespace = config.get_namespace(self.name)
     self.config_key = 'something'
     self.value_proxy = proxy.ValueProxy(validator, self.namespace,
                                         self.config_key)
 def __init__(self, *args, **kwargs):
     name                = kwargs.pop('namespace', config.DEFAULT)
     flatten             = kwargs.pop('flatten', True)
     config_data         = dict(*args, **kwargs)
     self.namespace      = config.get_namespace(name)
     self.config_data    = (dict(loader.flatten_dict(config_data)) if flatten
                           else config_data)
     self.old_values     = None
 def proxy_register(key_name,
                    default=UndefToken,
                    help=None,
                    namespace=None):
     name = namespace or getter_namespace or config.DEFAULT
     namespace = config.get_namespace(name)
     return proxy_factory.build(validator, namespace, key_name, default,
                                help)
 def proxy_register(key_name, default=UndefToken, help=None, namespace=None):
     name        = namespace or getter_namespace or config.DEFAULT
     namespace   = config.get_namespace(name)
     args        = validator, namespace.get_config_values(), key_name, default
     value_proxy = proxy.ValueProxy(*args)
     namespace.register_proxy(value_proxy)
     config.add_config_key_description(key_name, validator, default, name, help)
     return value_proxy
 def test_build_getter(self):
     validator = mock.Mock()
     getter = getters.build_getter(validator)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(config.DEFAULT)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
 def proxy_register(key_name,
                    default=UndefToken,
                    help=None,
                    namespace=None):
     name = namespace or getter_namespace or config.DEFAULT
     namespace = config.get_namespace(name)
     value_proxy = proxy.ValueProxy(validator, namespace, key_name, default)
     register_value_proxy(namespace, value_proxy, help)
     return value_proxy
 def test_build_getter(self):
     validator = mock.Mock()
     getter = getters.build_getter(validator)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(config.DEFAULT)
     assert value_proxy is namespace.get_value_proxies()[-1]
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.value_cache, namespace.configuration_values)
Beispiel #10
0
 def test_build_getter(self):
     validator = mock.Mock()
     getter = getters.build_getter(validator)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(config.DEFAULT)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
 def test_build_getter_with_getter_namespace(self):
     validator = mock.Mock()
     name = 'the stars'
     getter = getters.build_getter(validator, getter_namespace=name)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(name)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
Beispiel #12
0
 def test_build_getter_with_getter_namespace(self):
     validator = mock.Mock()
     name = 'the stars'
     getter = getters.build_getter(validator, getter_namespace=name)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(name)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
    def test_getters(self):
        get_conf = getters.NamespaceGetters('the_space')
        proxies = [
            get_conf.get_bool('is_it'),
            get_conf.get_time('when')
        ]

        namespace = config.get_namespace(get_conf.namespace)
        for i in xrange(len(proxies)):
            assert proxies[i] is namespace.value_proxies[i]
    def loader(*args, **kwargs):
        err_on_unknown      = kwargs.pop('error_on_unknown', False)
        err_on_dupe         = kwargs.pop('error_on_duplicate', False)
        name                = kwargs.pop('namespace', config.DEFAULT)

        config_data = load_config_data(loader_func, *args, **kwargs)
        config_data = dict(flatten_dict(config_data))
        namespace   = config.get_namespace(name)
        namespace.apply_config_data(config_data, err_on_unknown, err_on_dupe)
        return config_data
    def loader(*args, **kwargs):
        err_on_unknown = kwargs.pop('error_on_unknown', False)
        err_on_dupe = kwargs.pop('error_on_duplicate', False)
        name = kwargs.pop('namespace', config.DEFAULT)

        config_data = load_config_data(loader_func, *args, **kwargs)
        config_data = dict(flatten_dict(config_data))
        namespace = config.get_namespace(name)
        namespace.apply_config_data(config_data, err_on_unknown, err_on_dupe)
        return config_data
Beispiel #16
0
    def test_getters(self):
        get_conf = getters.NamespaceGetters(self.namespace)
        proxies = [
            get_conf.get_bool('is_it'),
            get_conf.get_time('when'),
            get_conf.get_list_of_bool('options')
        ]

        namespace = config.get_namespace(get_conf.namespace)
        for proxy in proxies:
            assert_in(id(proxy), namespace.value_proxies)
    def test_getters(self):
        get_conf = getters.NamespaceGetters(self.namespace)
        proxies = [
            get_conf.get_bool('is_it'),
            get_conf.get_time('when'),
            get_conf.get_list_of_bool('options')
        ]

        namespace = config.get_namespace(get_conf.namespace)
        for proxy in proxies:
            assert_in(id(proxy), namespace.value_proxies)
    def loader(*args, **kwargs):
        error_on_unknown    = kwargs.pop('error_on_unknown', False)
        error_on_duplicate  = kwargs.pop('error_on_duplicate', False)
        name                = kwargs.pop('namespace', config.DEFAULT)

        config_data = load_config_data(loader_func, *args, **kwargs)
        config_data = dict(flatten_dict(config_data))
        namespace   = config.get_namespace(name)
        namespace.validate_keys(config_data.keys(), error_on_unknown)
        namespace.has_duplicate_keys(config_data, error_on_duplicate)
        namespace.update_values(config_data)
        return config_data
Beispiel #19
0
    def loader(*args, **kwargs):
        err_on_unknown      = kwargs.pop('error_on_unknown', False)
        log_keys_only       = kwargs.pop('log_keys_only', False)
        err_on_dupe         = kwargs.pop('error_on_duplicate', False)
        flatten             = kwargs.pop('flatten', True)
        name                = kwargs.pop('namespace', config.DEFAULT)

        config_data = load_config_data(loader_func, *args, **kwargs)
        if flatten:
            config_data = dict(flatten_dict(config_data))
        namespace   = config.get_namespace(name)
        namespace.apply_config_data(
            config_data,
            err_on_unknown,
            err_on_dupe,
            log_keys_only=log_keys_only,
        )
        return config_data
Beispiel #20
0
    def loader(*args, **kwargs):
        err_on_unknown      = kwargs.pop('error_on_unknown', False)
        log_keys_only       = kwargs.pop('log_keys_only', False)
        err_on_dupe         = kwargs.pop('error_on_duplicate', False)
        flatten             = kwargs.pop('flatten', True)
        name                = kwargs.pop('namespace', config.DEFAULT)

        config_data = load_config_data(loader_func, *args, **kwargs)
        if flatten:
            config_data = dict(flatten_dict(config_data))
        namespace   = config.get_namespace(name)
        namespace.apply_config_data(
            config_data,
            err_on_unknown,
            err_on_dupe,
            log_keys_only=log_keys_only,
        )
        return config_data
Beispiel #21
0
def parse_config(config_file: str, watch_config: bool = False):
    """ Parses yaml config file (if indicated)

    :param str config_file: config file path
    :param bool watch_config: perform necessary setup to enable configuration hot swaps
    """
    with open(config_file) as f:
        config_data = yaml.safe_load(f)
    config_probe_names = {
        key
        for key in config_data if not key.startswith('_')
    }
    current_probe_names = {ns.name for ns in _get_probe_namespaces()}
    if watch_config and current_probe_names and config_probe_names != current_probe_names:
        # probes added or removed, triggering restart
        _drop_namespaces(current_probe_names - config_probe_names)
        return _clear_and_restart()
    for key in config_probe_names:
        probe_config = config_data[key]
        config_namespace = get_namespace(key)
        current_values = config_namespace.get_config_values().copy()
        if key not in HOTSWAP_CALLBACK_NAMESPACE:
            # First time loading
            callback_method = partial(_forward_config_change,
                                      SimpleQueue()) if watch_config else None
            DictConfiguration({key: callback_method},
                              namespace=HOTSWAP_CALLBACK_NAMESPACE.name)
        elif watch_config:
            is_different = probe_config != current_values
            if is_different and _non_hotswap_settings(
                    probe_config) != _non_hotswap_settings(current_values):
                # Non hot-swappable setting changed -> restart
                return _clear_and_restart()
            elif is_different:
                # Only hot-swappable settings changed, trigger proble filters reload
                HOTSWAP_CALLBACK_NAMESPACE[key](probe_config)
        # staticconf does clear namespaces before reloads, so we do it ourselves
        config_namespace.clear()
        DictConfiguration(probe_config, namespace=key, flatten=False)
 def test_get_namespace_existing(self):
     name = 'the_common_name'
     namespace = config.get_namespace(name)
     assert_equal(namespace, config.get_namespace(name))
 def test_get_namespace_new(self):
     name = 'some_unlikely_name'
     assert name not in config.configuration_namespaces
     config.get_namespace(name)
     assert name in config.configuration_namespaces
 def __init__(self, *args, **kwargs):
     name                = kwargs.pop('namespace', config.DEFAULT)
     self.namespace      = config.get_namespace(name)
     self.config_data    = dict(loader.flatten_dict(dict(*args, **kwargs)))
     self.old_values     = None
Beispiel #25
0
 def get_namespace(cls, attributes):
     if 'namespace' not in attributes:
         raise errors.ConfigurationError("ConfigSchema requires a namespace.")
     return config.get_namespace(attributes['namespace'])
 def reader(config_key, default=UndefToken, namespace=None):
     config_namespace = config.get_namespace(namespace or reader_namespace)
     return validator(_read_config(config_key, config_namespace, default))
Beispiel #27
0
 def proxy_register(key_name, default=UndefToken, help=None, namespace=None):
     name        = namespace or getter_namespace or config.DEFAULT
     namespace   = config.get_namespace(name)
     return proxy_factory.build(validator, namespace, key_name, default, help)
Beispiel #28
0
 def get_namespace(cls, attributes):
     if 'namespace' not in attributes:
         raise errors.ConfigurationError(
             "ConfigSchema requires a namespace.")
     return config.get_namespace(attributes['namespace'])
Beispiel #29
0
 def reader(config_key, default=UndefToken, namespace=None):
     config_namespace = config.get_namespace(namespace or reader_namespace)
     return validator(_read_config(config_key, config_namespace, default))
 def proxy_register(key_name, default=UndefToken, help=None, namespace=None):
     name        = namespace or getter_namespace or config.DEFAULT
     namespace   = config.get_namespace(name)
     value_proxy = proxy.ValueProxy(validator, namespace, key_name, default)
     register_value_proxy(namespace, value_proxy, help)
     return value_proxy
 def test_get_namespace_new(self):
     name = 'some_unlikely_name'
     assert name not in config.configuration_namespaces
     config.get_namespace(name)
     assert name in config.configuration_namespaces
 def test_get_namespace_existing(self):
     name = 'the_common_name'
     namespace = config.get_namespace(name)
     assert_equal(namespace, config.get_namespace(name))
Beispiel #33
0
from typing import Optional
from typing import Tuple

import staticconf.config
import yaml
from staticconf.config import ConfigNamespace
from staticconf.config import ConfigurationWatcher
from staticconf.config import DEFAULT as DEFAULT_NAMESPACE
from staticconf.config import get_namespace
from staticconf.config import get_namespaces_from_names
from staticconf.loader import DictConfiguration

from pidtree_bcc.utils import never_crash
from pidtree_bcc.utils import self_restart

HOTSWAP_CALLBACK_NAMESPACE = get_namespace('__change_callbacks')
HOT_SWAPPABLE_SETTINGS = ('filters', 'excludeports', 'includeports')


@never_crash
def _forward_config_change(queue: SimpleQueue, config_data: dict):
    queue.put(config_data)


def _non_hotswap_settings(config_data: dict) -> dict:
    return {
        k: v
        for k, v in config_data.items() if k not in HOT_SWAPPABLE_SETTINGS
    }