コード例 #1
0
def test_sx_adaptor_settings_name_not_type(caplog):
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [
        {
            'type': 'my-type1',
            'name': 'my-fork1',
            'endpoint': 'http://my-left-foot1.com:5000',
            'token': 'forkingshirtballs.thegoodplace.bortles'
        },
        {
            'type': 'my-type2',
            'name': 'my-type1',
            'endpoint': 'http://my-left-foot2.com:5000',
            'token': 'forkingshirtballs.thegoodplace.bortles'
        },
    ]
    x = ServiceXConfigAdaptor(c)
    endpoint, token = x.get_servicex_adaptor_config('my-type1')

    assert endpoint == 'http://my-left-foot2.com:5000'
    assert token == 'forkingshirtballs.thegoodplace.bortles'

    assert len(caplog.record_tuples) == 0
コード例 #2
0
def test_configured_cache_location():
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    here = Path('./servicex-dude').absolute()
    c['cache_path'] = str(here)

    p = get_configured_cache_path(c)

    # Should default to temp directory - should work on all platforms!
    assert p.exists()
    assert str(p) == str(here)
コード例 #3
0
def test_configured_cache_temp_location():
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['cache_path'] = '/tmp/servicex-dude'

    p = get_configured_cache_path(c)

    # Should default to temp directory - should work on all platforms!
    assert p.exists()
    assert str(p).startswith(tempfile.gettempdir())
    assert 'servicex-dude' in str(p)
コード例 #4
0
def test_sx_adaptor_settings_backend_name_requested_with_unlabeled_type(
        caplog):
    'Request None for a backend name'
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'endpoint': 'http://my-left-foot.com:5000',
        'token': 'forkingshirtballs.thegoodplace.bortles'
    }]
    x = ServiceXConfigAdaptor(c)
    with pytest.raises(ServiceXException) as e:
        _ = x.get_servicex_adaptor_config('xaod')

    assert 'Unable to find' in str(e)
コード例 #5
0
def test_sx_adaptor_settings_name_worng(caplog):
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'type': 'my-type',
        'name': 'my-fork',
        'endpoint': 'http://my-left-foot.com:5000',
        'token': 'forkingshirtballs.thegoodplace.bortles'
    }]
    x = ServiceXConfigAdaptor(c)
    with pytest.raises(ServiceXException) as e:
        x.get_servicex_adaptor_config('my-type')

    assert 'Unable to find type my-type' in str(e)
コード例 #6
0
def test_cache_expansion_username():
    '''On windows this will expand one way, and on linux another. So need to be a little careful here!
    '''
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['cache_path'] = '/tmp/servicex_${UserName}'

    # Get the right answer, depending on the definition of USER
    u_name = os.environ['USER'] if 'USER' in os.environ else os.environ[
        'UserName']
    path_name = f'servicex_{u_name}'

    p = get_configured_cache_path(c)

    # Should default to temp directory - should work on all platforms!
    assert p.name == path_name
コード例 #7
0
def test_sx_adaptor_settings_backend_name_unlabeled_type():
    'Request None for a backend name'
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'type': 'xaod',
        'endpoint': 'http://my-left-foot.com:5000',
        'token': 'forkingshirtballs.thegoodplace.bortles'
    }, {
        'endpoint': 'http://my-left-foot.com:5001',
        'token': 'forkingshirtballs.thegoodplace.bortles1'
    }]
    x = ServiceXConfigAdaptor(c)
    endpoint, token = x.get_servicex_adaptor_config()

    assert endpoint == 'http://my-left-foot.com:5001'
    assert token == 'forkingshirtballs.thegoodplace.bortles1'
コード例 #8
0
def test_sx_adaptor_settings_backend_name_requested_with_unlabeled_type(
        caplog):
    'Request None for a backend name'
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'endpoint': 'http://my-left-foot.com:5000',
        'token': 'forkingshirtballs.thegoodplace.bortles'
    }]
    x = ServiceXConfigAdaptor(c)
    endpoint, token = x.get_servicex_adaptor_config('xaod')

    assert endpoint == 'http://my-left-foot.com:5000'
    assert token == 'forkingshirtballs.thegoodplace.bortles'

    assert caplog.record_tuples[0][2] == "No 'xaod' backend type found, " \
                                         "using http://my-left-foot.com:5000 - please add to " \
                                         "the configuration file (e.g. servicex.yaml)"
コード例 #9
0
def test_sx_adaptor_settings_no_backend_name_requested_or_listed(caplog):
    'Request None for a backend name'
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'endpoint': 'http://my-left-foot.com:5000',
        'token': 'forkingshirtballs.thegoodplace.bortles'
    }]
    x = ServiceXConfigAdaptor(c)
    endpoint, token = x.get_servicex_adaptor_config()

    assert endpoint == 'http://my-left-foot.com:5000'
    assert token == 'forkingshirtballs.thegoodplace.bortles'

    assert caplog.record_tuples[0][2] == "No backend type requested, " \
                                         "using http://my-left-foot.com:5000 - please be " \
                                         "explicit " \
                                         "in the ServiceXDataset constructor"
コード例 #10
0
def test_sx_adaptor_settings_env():
    from confuse import Configuration
    c = Configuration('bogus', 'bogus')
    c.clear()
    c['api_endpoints'] = [{
        'type': '${SXTYPE}',
        'endpoint': '${ENDPOINT}:5000',
        'token': '${SXTOKEN}',
    }]

    from os import environ
    environ['ENDPOINT'] = 'http://tachi.com'
    environ['SXTYPE'] = 'mcrn'
    environ['SXTOKEN'] = 'protomolecule'

    x = ServiceXConfigAdaptor(c)
    endpoint, token = x.get_servicex_adaptor_config('mcrn')

    assert endpoint == 'http://tachi.com:5000'
    assert token == 'protomolecule'
コード例 #11
0
ファイル: configuration.py プロジェクト: mozilla/PRESC
class PrescConfig:
    """
    Wrapper around a confuse Configuration object.

    This is used for managing config options in PRESC, including the global config.

    Attributes
    ----------
    from_config : PrescConfig
        A PrescConfig instance to override. If None, the config is initialized to the default settings.
    """
    def __init__(self, from_config=None):
        if from_config:
            self._config = LocalConfig(from_config.settings)
        else:
            self._config = Configuration("PRESC", read=False)
            self.reset_defaults()

    def reset_defaults(self):
        """Reset all options to their defaults."""
        self._config.clear()
        self.update_from_file(DEFAULT_CONFIG_PATH)

    def update_from_file(self, file_path):
        """Override current settings with those in the given YAML file."""
        self._config.set_file(str(file_path))

    def set(self, settings):
        """Update one or more config options.

        These should be specified in a dict, either mirroring the nested
        structure of the configuration file, or as flat key-value pairs using
        dots to indicate nested namespaces.

        Examples
        --------
        ``config.set({"report": {"title": "My Report", "author": "Me"}})``
        ``config.set({"report.title": "My Report", "report.author": "Me"})``

        """
        if not isinstance(settings, dict):
            raise PrescError("Config settings must be specified in a dict")
        self._config.set_args(settings, dots=True)

    @property
    def settings(self):
        """Access the underlying confuse object."""
        return self._config

    def dump(self):
        """Dump the current config in YAML format."""
        return self._config.dump()

    # Make option access work on the PrescConfig:
    def __getitem__(self, key):
        return self._config.__getitem__(key)

    def get(self, template=None):
        # If template is None, defer to the underlying default arg.
        template_arg = {}
        if template:
            template_arg["template"] = template
        return self._config.get(**template_arg)

    def flatten(self):
        return self._config.flatten()