~ubuntu-branches/ubuntu/oneiric/python-keyring/oneiric

« back to all changes in this revision

Viewing changes to keyring/core.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet
  • Date: 2009-09-24 23:24:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090924232402-5vkfmi0os43m214w
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
core.py
 
3
 
 
4
Created by Kang Zhang on 2009-07-09
 
5
"""
 
6
import os
 
7
import ConfigParser
 
8
import imp
 
9
import sys
 
10
import backend
 
11
 
 
12
def set_keyring(keyring):
 
13
    """Set current keyring backend.
 
14
    """
 
15
    global _keyring_backend
 
16
    if isinstance(keyring, backend.KeyringBackend):
 
17
        _keyring_backend = keyring
 
18
    else: raise TypeError("The keyring must be a subclass of KeyringBackend")
 
19
 
 
20
def get_keyring():
 
21
    """Get current keyring backend.
 
22
    """
 
23
    return _keyring_backend
 
24
 
 
25
def get_password(service_name, username):
 
26
    """Get password from the specified service
 
27
    """
 
28
    return _keyring_backend.get_password(service_name, username)
 
29
 
 
30
def set_password(service_name, username, password):
 
31
    """Set password for the user in the spcified service
 
32
    """
 
33
    return _keyring_backend.set_password(service_name, username, password)
 
34
 
 
35
def init_backend():
 
36
    """first try to load the keyring in the config file, if it has not
 
37
    been decleared, assign a defult keyring according to the platform.
 
38
    """
 
39
    #select a backend according to the config file
 
40
    keyring_impl = load_config()
 
41
 
 
42
    # if the user dose not specify a keyring, we apply a default one
 
43
    if keyring_impl is None:
 
44
 
 
45
        keyrings = backend.get_all_keyring()
 
46
        # rank according the supported
 
47
        keyrings.sort(lambda x, y: y.supported() - x.supported())
 
48
        # get the most recommend one
 
49
        keyring_impl = keyrings[0]
 
50
 
 
51
    set_keyring(keyring_impl)
 
52
 
 
53
def load_config():
 
54
    """load a keyring using the config file, the config file can be
 
55
    in the current working directory, or in the user's home directory.
 
56
    """
 
57
    keyring_impl = None
 
58
 
 
59
    # search from current working directory and the home folder
 
60
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
 
61
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
 
62
    # initial the keyring_cfg with the fist detected config file
 
63
    keyring_cfg = None
 
64
    for path in keyring_cfg_list:
 
65
        keyring_cfg = path
 
66
        if os.path.exists(path):
 
67
            break
 
68
 
 
69
    if os.path.exists(keyring_cfg):
 
70
        config = ConfigParser.RawConfigParser()
 
71
        config.read(keyring_cfg)
 
72
        # load the keyring-path option
 
73
        try:
 
74
            keyring_path = config.get("backend", "keyring-path").strip()
 
75
        except ConfigParser.NoOptionError:
 
76
            keyring_path = None
 
77
        # load the keyring class name, and load it
 
78
        try:
 
79
            keyring_name = config.get("backend", "default-keyring").strip()
 
80
 
 
81
            def load_module(name, path):
 
82
                """Load the specified module from the disk.
 
83
                """
 
84
                path_list = name.split('.')
 
85
                module_info = imp.find_module(path_list[0], path)
 
86
                module_file, pathname, description = module_info
 
87
                module = imp.load_module(path_list[0], module_file, \
 
88
                                                          pathname, description)
 
89
                if module_file:
 
90
                    module_file.close()
 
91
 
 
92
                if len(path_list) > 1:
 
93
                    # for the class name containing dots
 
94
                    sub_name = '.'.join(path_list[1:])
 
95
                    sub_path = path
 
96
 
 
97
                    try:
 
98
                        sub_path = path + module.__path__
 
99
                    except AttributeError:
 
100
                        return module
 
101
 
 
102
                    return load_module(sub_name, sub_path)
 
103
                return module
 
104
 
 
105
            try:
 
106
                # avoid import the imported modules
 
107
                module = sys.modules[keyring_name[:keyring_name.rfind('.')]]
 
108
            except KeyError:
 
109
                module = load_module( keyring_name, sys.path+[keyring_path])
 
110
 
 
111
            keyring_class = keyring_name.split('.')[-1].strip()
 
112
            exec  "keyring_temp = module." + keyring_class + "() " in locals()
 
113
 
 
114
            keyring_impl = keyring_temp
 
115
        except (ConfigParser.NoOptionError, ImportError):
 
116
            logger.warning("Keyring Config file does not write correctly.\n" + \
 
117
                           "Config file: %s" % keyring_cfg)
 
118
 
 
119
    return keyring_impl
 
120
 
 
121
# init the _keyring_backend
 
122
init_backend()
 
123