Ejemplo n.º 1
0
    def set_config(self, config: ValueExtractor) -> None:
        cfg = config.new_parent(logger=__package__)
        super().set_config(cfg)

        # TODO: Remove deprecated code.
        if self.config.get("connection.init"):
            warnings.warn("Do not use connection.init config. Use pool.init",
                          DeprecationWarning)
            self._pool_init = self.context.get_object(
                self.config.get("connection.init"))

        pool_config = dict(self.config.get("pool", {}))
        if not pool_config:
            # Do not process pool config if there is no any parameter
            return

        pool_init: Optional[str] = pool_config.pop("init", None)
        if pool_init:
            self._pool_init = self.context.get_object(pool_init)

        pool_setup: Optional[str] = pool_config.pop("setup", None)
        if pool_setup:
            self._pool_setup = self.context.get_object(pool_setup)

        connection_class: Optional[str] = pool_config.pop(
            "connection_class", None)
        if connection_class:
            self._connection_class = self.context.get_object(connection_class)

        record_class: Optional[str] = pool_config.pop("record_class", None)
        if record_class:
            self._record_class = self.context.get_object(record_class)

        self._connect_kwargs = pool_config
 def set_config(self, config: ValueExtractor) -> None:
     cfg = config.connection
     dsn: URI = cfg.get_uri("dsn").with_auth(
         username=cfg.get("username"),
         password=cfg.get("password"),
     )
     super().set_config(config.new_child(dsn=dsn))
Ejemplo n.º 3
0
    def set_config(self, config: ValueExtractor) -> None:
        cfg = config.new_parent(logger=__package__)
        super().set_config(cfg)

        client_config = dict(self.config.get("client", {}))
        if client_config:
            self._client_config = client_config
            return

        # Support old uri only config.
        self._client_config = {
            "host": self.config.get("uri", self.default_mongo_uri),
        }
Ejemplo n.º 4
0
def test_value_extractor():
    e = ValueExtractor(os.environ)
    assert isinstance(e.get_path('HOME'), Path)
    v = ValueExtractor({'a': {'b': 1, 'c': None}, 'env': e})
    assert isinstance(v.get('a'), ValueExtractor)
    assert v.a.get_int('b') is v.a.b
    assert v.env is e

    with pytest.raises(RuntimeError):
        v.x = 1

    with pytest.raises(TypeError):
        v.a.get_int('c')
    assert v.a.get_int('c', null=True) is None

    with pytest.raises(AttributeError):
        getattr(v, '_a')

    assert repr(v)
Ejemplo n.º 5
0
 def set_config(self, config: ValueExtractor) -> None:
     cfg = config.new_parent(logger=__package__)
     super().set_config(cfg)
     registry: str = self.config.get('registry', REGISTRY)
     namespace: Optional[str] = self.config.get('namespace')
     cfg_metrics = self.config.get('metrics', {})
     for attr, params in cfg_metrics.items():
         kw = dict(params)
         m_name: str = kw.setdefault('name', attr)
         kw.setdefault('documentation', '')
         m_type: str = kw.pop('type', 'gauge')
         cls = self.METRICS[m_type]
         m_registry: str = kw.get('registry', registry)
         kw['registry'] = get_registry(m_registry)
         m_namespace: Optional[str] = kw.setdefault('namespace', namespace)
         cache_key: Tuple = (m_registry, m_namespace, m_name, m_type)
         cache_key += tuple(kw.get('labelnames', ()))
         metric: Optional[metrics.MetricWrapperBase] = self._cache.get(cache_key)
         if metric is None:
             metric = cls(**kw)
             self._cache[cache_key] = metric
         setattr(self, attr, metric)
Ejemplo n.º 6
0
def test_value_extractor():
    e = ValueExtractor(os.environ)
    if os.name == 'nt':
        os.environ.setdefault(
            'HOME',
            os.environ['HOMEDRIVE'] + os.environ['HOMEPATH'],
        )
    assert isinstance(e.get_path('HOME'), Path)
    v = ValueExtractor({'a': {'b': 1, 'c': None}, 'env': e})
    assert isinstance(v.get('a'), ValueExtractor)
    assert v.a.get_int('b') is v.a.b
    assert v.env._val == e._val

    with pytest.raises(RuntimeError):
        v.x = 1

    with pytest.raises(TypeError):
        v.a.get_int('c')
    assert v.a.get_int('c', null=True) is None

    with pytest.raises(AttributeError):
        getattr(v, '_a')

    assert repr(v)
Ejemplo n.º 7
0
def test_value_extractor_order_keys():
    v = ValueExtractor(MergeDict(a=1))
    assert list(v.new_child(b=1)) == ['a', 'b']
    assert list(v.new_parent(b=1)) == ['a', 'b']
    assert list(v.new_parent(b=1).new_child(c=1)) == ['a', 'b', 'c']