~ubuntu-branches/ubuntu/precise/python-keyring/precise-updates

« back to all changes in this revision

Viewing changes to keyring/core.py

  • Committer: Package Import Robot
  • Author(s): Carl Chenet
  • Date: 2012-02-14 12:07:30 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120214120730-ekuysup9a2s5yl9d
Tags: 0.7.1-1
* New upstream version (Closes: #656680, #624690)
* debian/control
  - Add X-Python-Version for Python 3.2
  - Add B-D for Python 3.2
  - Add unzip for B-D to repack upstream sources
  - Add python3-keyring description
  - Recommends python3-crypto for Python3 binary package
* Add python-keyring.install and python3-keyring.install files
* debian/rules
  - Execute unit tests if available
  - repack upstream sources
* debian/watch
  - New URL to catch upstream sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
Created by Kang Zhang on 2009-07-09
5
5
"""
6
6
import os
7
 
import ConfigParser
 
7
try:
 
8
        import configparser as config_parser
 
9
except ImportError:
 
10
        import ConfigParser as config_parser
8
11
import imp
9
12
import sys
10
13
 
17
20
    global _keyring_backend
18
21
    if isinstance(keyring, backend.KeyringBackend):
19
22
        _keyring_backend = keyring
20
 
    else: raise TypeError("The keyring must be a subclass of KeyringBackend")
 
23
    else:
 
24
        raise TypeError("The keyring must be a subclass of KeyringBackend")
21
25
 
22
26
def get_keyring():
23
27
    """Get current keyring backend.
30
34
    return _keyring_backend.get_password(service_name, username)
31
35
 
32
36
def set_password(service_name, username, password):
33
 
    """Set password for the user in the spcified service
 
37
    """Set password for the user in the specified service
34
38
    """
35
39
    _keyring_backend.set_password(service_name, username, password)
36
40
 
37
41
def init_backend():
38
 
    """first try to load the keyring in the config file, if it has not
39
 
    been decleared, assign a defult keyring according to the platform.
 
42
    """Load a keyring from a config file or for the default platform.
 
43
 
 
44
    First try to load the keyring in the config file, if it has not
 
45
    been declared, assign a default keyring according to the platform.
40
46
    """
41
 
    #select a backend according to the config file
 
47
    # select a backend according to the config file
42
48
    keyring = load_config()
43
49
 
44
 
    # if the user dose not specify a keyring, we apply a default one
 
50
    # if the user doesn't specify a keyring, we apply a default one
45
51
    if keyring is None:
46
52
 
47
53
        keyrings = backend.get_all_keyring()
48
 
        # rank according the supported
49
 
        keyrings.sort(lambda x, y: y.supported() - x.supported())
50
 
        # get the most recommend one
 
54
        # rank according to the supported result
 
55
        keyrings.sort(key = lambda x: -x.supported())
 
56
        # get the most recommended one
51
57
        keyring = keyrings[0]
52
58
 
53
59
    set_keyring(keyring)
56
62
def load_keyring(keyring_path, keyring_name):
57
63
    """Load the specified keyring name from the specified path
58
64
 
59
 
    `keyring_path` can be None and it will not interfer with the loading
 
65
    `keyring_path` can be None and it will not interfere with the loading
60
66
    process.
61
67
    """
62
68
 
92
98
        module = load_module(keyring_name, sys.path+[keyring_path])
93
99
 
94
100
    keyring_class = keyring_name.split('.')[-1].strip()
95
 
    exec  "keyring_temp = module." + keyring_class + "() " in locals()
 
101
    keyring_temp = getattr(module, keyring_class)()
96
102
 
97
103
    return keyring_temp
98
104
 
99
105
 
100
106
def load_config():
101
 
    """load a keyring using the config file, the config file can be
102
 
    in the current working directory, or in the user's home directory.
 
107
    """Load a keyring using the config file.
 
108
 
 
109
    The config file can be in the current working directory, or in the user's
 
110
    home directory.
103
111
    """
104
112
    keyring = None
105
113
 
106
114
    # search from current working directory and the home folder
107
115
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
108
116
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
109
 
    # initial the keyring_cfg with the fist detected config file
 
117
 
 
118
    # initialize the keyring_config with the first detected config file
110
119
    keyring_cfg = None
111
120
    for path in keyring_cfg_list:
112
121
        keyring_cfg = path
114
123
            break
115
124
 
116
125
    if os.path.exists(keyring_cfg):
117
 
        config = ConfigParser.RawConfigParser()
 
126
        config = config_parser.RawConfigParser()
118
127
        config.read(keyring_cfg)
119
128
        # load the keyring-path option
120
129
        try:
122
131
                keyring_path = config.get("backend", "keyring-path").strip()
123
132
            else:
124
133
                keyring_path = None
125
 
        except ConfigParser.NoOptionError:
 
134
        except config_parser.NoOptionError:
126
135
            keyring_path = None
127
136
 
128
 
        # load the keyring class name, and load it
 
137
        # load the keyring class name, and then load this keyring
129
138
        try:
130
139
            if config.has_section("backend"):
131
140
                keyring_name = config.get("backend", "default-keyring").strip()
132
141
            else:
133
 
                raise ConfigParser.NoOptionError('backend', 'default-keyring')
 
142
                raise config_parser.NoOptionError('backend', 'default-keyring')
134
143
 
135
144
            keyring = load_keyring(keyring_path, keyring_name)
136
 
        except (ConfigParser.NoOptionError, ImportError):
137
 
            logger.warning("Keyring Config file does not write correctly.\n" + \
 
145
        except (config_parser.NoOptionError, ImportError):
 
146
            logger.warning("Keyring config file contains incorrect values.\n" +
138
147
                           "Config file: %s" % keyring_cfg)
139
148
 
140
149
    return keyring
141
150
 
142
151
# init the _keyring_backend
143
152
init_backend()
144