def _backend(self, name, options):
        klass = resolve_name(options[name])
        klass_options = {}
        for key, value in options.items():
            if not key.startswith(name + '.'):
                continue
            key = key.split('.', 1)[-1]
            klass_options[key] = value

        return klass(**klass_options)
示例#2
0
def load_from_config(section_name, config):
    """Load the plugin from the given section in a Config object.

    This function loads a plugin using the settings specified in a Config
    object section.  The key "backend" must be present in the section and
    gives the dotted name of the plugin class to load.  Any other keys in
    the secton will be passed as keyword arguments to the class.
    """
    kwargs = dict(config.items(section_name))
    klass = resolve_name(kwargs.pop("backend"))
    return klass(**kwargs)
示例#3
0
def load_from_config(section_name, config):
    """Load the plugin from the given section in a Config object.

    This function loads a plugin using the settings specified in a Config
    object section.  The key "backend" must be present in the section and
    gives the dotted name of the plugin class to load.  Any other keys in
    the secton will be passed as keyword arguments to the class.
    """
    kwargs = dict(config.items(section_name))
    klass = resolve_name(kwargs.pop("backend"))
    return klass(**kwargs)
示例#4
0
def load_from_settings(section_name, settings):
    """Load the plugin from the given section in a settings dict.

    This function loads a plugin using prefixed settings from the pyramid
    settings dict.  Any keys in the settings dict that start with the
    given section name plus a dot will be used.

    This a compatability function for use when a Config object is not
    available; load_from_config will usually be faster.
    """
    kwargs = {}
    prefix = section_name + "."
    for name, value in settings.iteritems():
        if name.startswith(prefix):
            kwargs[name[len(prefix) :]] = value
    klass = resolve_name(kwargs.pop("backend"))
    return klass(**kwargs)
示例#5
0
def load_from_settings(section_name, settings):
    """Load the plugin from the given section in a settings dict.

    This function loads a plugin using prefixed settings from the pyramid
    settings dict.  Any keys in the settings dict that start with the
    given section name plus a dot will be used.

    This a compatability function for use when a Config object is not
    available; load_from_config will usually be faster.
    """
    kwargs = {}
    prefix = section_name + "."
    for name, value in settings.iteritems():
        if name.startswith(prefix):
            kwargs[name[len(prefix):]] = value
    klass = resolve_name(kwargs.pop("backend"))
    return klass(**kwargs)
示例#6
0
    def test_resolve_name(self):

        # Resolving by absolute path
        self.assertEquals(os.path.abspath, resolve_name("os.path.abspath"))
        self.assertEquals(os.path.abspath, resolve_name("os.path:abspath"))

        # Resolving by relative path to package object
        self.assertEquals(os.path.abspath, resolve_name(".path.abspath", os))
        self.assertEquals(os.path.abspath, resolve_name(".path:abspath", os))

        # Resolving by relative path to package name
        self.assertEquals(os.path.abspath, resolve_name(".abspath", "os.path"))
        self.assertEquals(os.path.abspath, resolve_name(":abspath", "os.path"))
示例#7
0
    def test_resolve_name(self):

        # Resolving by absolute path
        self.assertEquals(os.path.abspath, resolve_name("os.path.abspath"))
        self.assertEquals(os.path.abspath, resolve_name("os.path:abspath"))

        # Resolving by relative path to package object
        self.assertEquals(os.path.abspath, resolve_name(".path.abspath", os))
        self.assertEquals(os.path.abspath, resolve_name(".path:abspath", os))

        # Resolving by relative path to package name
        self.assertEquals(os.path.abspath, resolve_name(".abspath", "os.path"))
        self.assertEquals(os.path.abspath, resolve_name(":abspath", "os.path"))
示例#8
0
 def __init__(self, secrets=None, **kwds):
     if not secrets:
         # Using secret=None will cause tokenlib to use a randomly-generated
         # secret.  This is useful for getting started without having to
         # twiddle any configuration files, but probably not what anyone
         # wants to use long-term.
         secrets = None
         msgs = [
             "WARNING: using a randomly-generated token secret.",
             "You probably want to set 'secret' or 'secrets_file' in "
             "the [hawkauth] section of your configuration"
         ]
         for msg in msgs:
             mozsvc.logger.warn(msg)
     elif isinstance(secrets, (str, list)):
         secrets = mozsvc.secrets.FixedSecrets(secrets)
     elif isinstance(secrets, dict):
         secrets = resolve_name(secrets.pop("backend"))(**secrets)
     self.secrets = secrets
     if kwds.get("nonce_cache") is None:
         kwds["nonce_cache"] = PermissiveNonceCache()
     super(TokenServerAuthenticationPolicy, self).__init__(**kwds)
示例#9
0
 def __init__(self, secrets=None, **kwds):
     if not secrets:
         # Using secret=None will cause tokenlib to use a randomly-generated
         # secret.  This is useful for getting started without having to
         # twiddle any configuration files, but probably not what anyone
         # wants to use long-term.
         secrets = None
         msgs = [
             "WARNING: using a randomly-generated token secret.",
             "You probably want to set 'secret' or 'secrets_file' in "
             "the [hawkauth] section of your configuration",
         ]
         for msg in msgs:
             mozsvc.logger.warn(msg)
     elif isinstance(secrets, (basestring, list)):
         secrets = mozsvc.secrets.FixedSecrets(secrets)
     elif isinstance(secrets, dict):
         secrets = resolve_name(secrets.pop("backend"))(**secrets)
     self.secrets = secrets
     if kwds.get("nonce_cache") is None:
         kwds["nonce_cache"] = PermissiveNonceCache()
     super(TokenServerAuthenticationPolicy, self).__init__(**kwds)