Beispiel #1
0
def load_config():
    """Load a keyring using the config file.

    The config file can be in the current working directory, or in the user's
    home directory.
    """
    keyring = None

    filename = 'keyringrc.cfg'

    local_path = os.path.join(os.getcwd(), filename)
    legacy_path = os.path.join(os.path.expanduser("~"), filename)
    config_path = os.path.join(platform.data_root(), filename)
    loc_compat.relocate_file(legacy_path, config_path)

    # search from current working directory and the data root
    keyring_cfg_candidates = [local_path, config_path]

    # initialize the keyring_config with the first detected config file
    keyring_cfg = None
    for path in keyring_cfg_candidates:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = config_parser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except config_parser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and then load this keyring
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise config_parser.NoOptionError('backend', 'default-keyring')

            keyring = load_keyring(keyring_path, keyring_name)
        except (config_parser.NoOptionError, ImportError):
            logger.warning("Keyring config file contains incorrect values.\n" +
                           "Config file: %s" % keyring_cfg)

    return keyring
Beispiel #2
0
def load_config():
    """Load a keyring using the config file.

    The config file can be in the current working directory, or in the user's
    home directory.
    """
    keyring = None

    # search from current working directory and the home folder
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]

    # initialize the keyring_config with the first detected config file
    keyring_cfg = None
    for path in keyring_cfg_list:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = config_parser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except config_parser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and then load this keyring
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise config_parser.NoOptionError('backend', 'default-keyring')

            keyring = load_keyring(keyring_path, keyring_name)
        except (config_parser.NoOptionError, ImportError):
            logger.warning("Keyring config file contains incorrect values.\n" +
                           "Config file: %s" % keyring_cfg)

    return keyring
Beispiel #3
0
def load_config():
    """load a keyring using the config file, the config file can be
    in the current working directory, or in the user's home directory.
    """
    keyring = None

    # search from current working directory and the home folder
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
    # initial the keyring_cfg with the fist detected config file
    keyring_cfg = None
    for path in keyring_cfg_list:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = ConfigParser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except ConfigParser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and load it
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise ConfigParser.NoOptionError('backend', 'default-keyring')

            keyring = load_keyring(keyring_path, keyring_name)
        except (ConfigParser.NoOptionError, ImportError):
            logger.warning("Keyring Config file does not write correctly.\n" + \
                           "Config file: %s" % keyring_cfg)

    return keyring
Beispiel #4
0
def load_config():
    """Load a keyring using the config file.

    The config file can be in the current working directory, or in the user's
    home directory.
    """
    keyring = None

    filename = 'keyringrc.cfg'

    local_path = os.path.join(os.getcwd(), filename)
    config_path = os.path.join(platform.config_root(), filename)

    # search from current working directory and the data root
    keyring_cfg_candidates = [local_path, config_path]

    # initialize the keyring_config with the first detected config file
    keyring_cfg = None
    for path in keyring_cfg_candidates:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = configparser.RawConfigParser()
        config.read(keyring_cfg)
        _load_keyring_path(config)

        # load the keyring class name, and then load this keyring
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise configparser.NoOptionError('backend', 'default-keyring')

            keyring = load_keyring(None, keyring_name)
        except (configparser.NoOptionError, ImportError):
            logger.warning("Keyring config file contains incorrect values.\n" +
                           "Config file: %s" % keyring_cfg)

    return keyring
Beispiel #5
0
def load_config():
    """load a keyring using the config file, the config file can be
    in the current working directory, or in the user's home directory.
    """
    keyring = None

    # search from current working directory and the home folder
    keyring_cfg_list = [
        os.path.join(os.getcwd(), "keyringrc.cfg"),
        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")
    ]
    # initial the keyring_cfg with the fist detected config file
    keyring_cfg = None
    for path in keyring_cfg_list:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = ConfigParser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except ConfigParser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and load it
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise ConfigParser.NoOptionError('backend', 'default-keyring')

            def load_module(name, path):
                """Load the specified module from the disk.
                """
                path_list = name.split('.')
                module_info = imp.find_module(path_list[0], path)
                module_file, pathname, description = module_info
                module = imp.load_module(path_list[0], module_file, \
                                                          pathname, description)
                if module_file:
                    module_file.close()

                if len(path_list) > 1:
                    # for the class name containing dots
                    sub_name = '.'.join(path_list[1:])
                    sub_path = path

                    try:
                        sub_path = path + module.__path__
                    except AttributeError:
                        return module

                    return load_module(sub_name, sub_path)
                return module

            try:
                # avoid import the imported modules
                module = sys.modules[keyring_name[:keyring_name.rfind('.')]]
            except KeyError:
                module = load_module(keyring_name, sys.path + [keyring_path])

            keyring_class = keyring_name.split('.')[-1].strip()
            exec "keyring_temp = module." + keyring_class + "() " in locals()

            keyring = keyring_temp
        except (ConfigParser.NoOptionError, ImportError):
            logger.warning("Keyring Config file does not write correctly.\n" + \
                           "Config file: %s" % keyring_cfg)

    return keyring
Beispiel #6
0
def load_config():
    """load a keyring using the config file, the config file can be
    in the current working directory, or in the user's home directory.
    """
    keyring = None

    # search from current working directory and the home folder
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
    # initial the keyring_cfg with the fist detected config file
    keyring_cfg = None
    for path in keyring_cfg_list:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = ConfigParser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except ConfigParser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and load it
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise ConfigParser.NoOptionError('backend', 'default-keyring')

            def load_module(name, path):
                """Load the specified module from the disk.
                """
                path_list = name.split('.')
                module_info = imp.find_module(path_list[0], path)
                module_file, pathname, description = module_info
                module = imp.load_module(path_list[0], module_file, \
                                                          pathname, description)
                if module_file:
                    module_file.close()

                if len(path_list) > 1:
                    # for the class name containing dots
                    sub_name = '.'.join(path_list[1:])
                    sub_path = path

                    try:
                        sub_path = path + module.__path__
                    except AttributeError:
                        return module

                    return load_module(sub_name, sub_path)
                return module

            try:
                # avoid import the imported modules
                module = sys.modules[keyring_name[:keyring_name.rfind('.')]]
            except KeyError:
                module = load_module( keyring_name, sys.path+[keyring_path])

            keyring_class = keyring_name.split('.')[-1].strip()
            exec  "keyring_temp = module." + keyring_class + "() " in locals()

            keyring = keyring_temp
        except (ConfigParser.NoOptionError, ImportError):
            logger.warning("Keyring Config file does not write correctly.\n" + \
                           "Config file: %s" % keyring_cfg)

    return keyring