~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Robert Collins
  • Date: 2005-10-14 01:59:24 UTC
  • mfrom: (1442.1.6)
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051014015924-3149bd0ff5e56a90
merge current config support - classes split out

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import re
24
24
 
25
25
import bzrlib
 
26
import bzrlib.errors as errors
 
27
 
 
28
 
 
29
class Config(object):
 
30
    """A configuration policy - what username, editor, gpg needs etc."""
 
31
 
 
32
    def get_editor(self):
 
33
        """Get the users pop up editor."""
 
34
        raise NotImplementedError
 
35
 
 
36
    def __init__(self):
 
37
        super(Config, self).__init__()
 
38
 
 
39
    def user_email(self):
 
40
        """Return just the email component of a username."""
 
41
        e = self.username()
 
42
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
 
43
        if not m:
 
44
            raise BzrError("%r doesn't seem to contain "
 
45
                           "a reasonable email address" % e)
 
46
        return m.group(0)
 
47
 
 
48
    def username(self):
 
49
        """Return email-style username.
 
50
    
 
51
        Something similar to 'Martin Pool <mbp@sourcefrog.net>'
 
52
        
 
53
        $BZREMAIL can be set to override this, then
 
54
        the concrete policy type is checked, and finally
 
55
        $EMAIL is examinged.
 
56
        but if none is found, a reasonable default is (hopefully)
 
57
        created.
 
58
    
 
59
        TODO: Check it's reasonably well-formed.
 
60
        """
 
61
        v = os.environ.get('BZREMAIL')
 
62
        if v:
 
63
            return v.decode(bzrlib.user_encoding)
 
64
    
 
65
        v = self._get_user_id()
 
66
        if v:
 
67
            return v
 
68
        
 
69
        v = os.environ.get('EMAIL')
 
70
        if v:
 
71
            return v.decode(bzrlib.user_encoding)
 
72
 
 
73
        name, email = _auto_user_id()
 
74
        if name:
 
75
            return '%s <%s>' % (name, email)
 
76
        else:
 
77
            return email
 
78
 
 
79
 
 
80
class GlobalConfig(Config):
 
81
    """The configuration that should be used for a specific location."""
 
82
 
 
83
    def _get_parser(self, filename=None, file=None):
 
84
        parser = ConfigParser()
 
85
        if file is not None:
 
86
            parser.readfp(file)
 
87
        else:
 
88
            parser.read([filename])
 
89
        return parser
 
90
 
 
91
    def _get_config_parser(self, file=None):
 
92
        if self._parser is None:
 
93
            self._parser =  self._get_parser(config_filename(), file)
 
94
        return self._parser
 
95
    
 
96
    def _get_branches_config_parser(self, file=None):
 
97
        if self._branches_parser is None:
 
98
            self._branches_parser = self._get_parser(
 
99
                branches_config_filename(), file)
 
100
        return self._branches_parser
 
101
 
 
102
    def get_editor(self):
 
103
        if self._get_config_parser().has_option('DEFAULT', 'editor'):
 
104
            return self._get_config_parser().get('DEFAULT', 'editor')
 
105
 
 
106
    def _get_user_id(self, branch=None):
 
107
        """Return the full user id from the global config file.
 
108
    
 
109
        e.g. "John Hacker <jhacker@foo.org>"
 
110
        from 
 
111
        [DEFAULT]
 
112
        email=John Hacker <jhacker@foo.org>
 
113
        """
 
114
        if self._get_config_parser().has_option('DEFAULT', 'email'):
 
115
            email = self._get_config_parser().get('DEFAULT', 'email')
 
116
            if email is not None:
 
117
                return email
 
118
    
 
119
    def __init__(self):
 
120
        super(GlobalConfig, self).__init__()
 
121
        self._branches_parser = None
 
122
        self._parser = None
 
123
 
 
124
 
 
125
class LocationConfig(Config):
 
126
    """A configuration object that gives the policy for a location."""
 
127
 
 
128
    def __init__(self, location):
 
129
        self._global_config = None
 
130
        self.location = location
 
131
 
 
132
    def _get_branches_config_parser(self, file=None):
 
133
        return self._get_global_config()._get_branches_config_parser(file)
 
134
 
 
135
    def _get_global_config(self):
 
136
        if self._global_config is None:
 
137
            self._global_config = GlobalConfig()
 
138
        return self._global_config
 
139
 
 
140
    def _get_user_id(self):
 
141
        return self._get_global_config()._get_user_id()
 
142
 
 
143
 
 
144
class BranchConfig(Config):
 
145
    """A configuration object giving the policy for a branch."""
 
146
 
 
147
    def _get_location_config(self):
 
148
        if self._location_config is None:
 
149
            self._location_config = LocationConfig(self.branch.base)
 
150
        return self._location_config
 
151
 
 
152
    def _get_user_id(self):
 
153
        """Return the full user id for the branch.
 
154
    
 
155
        e.g. "John Hacker <jhacker@foo.org>"
 
156
        This is looked up in the email controlfile for the branch.
 
157
        """
 
158
        try:
 
159
            return (self.branch.controlfile("email", "r") 
 
160
                    .read()
 
161
                    .decode(bzrlib.user_encoding)
 
162
                    .rstrip("\r\n"))
 
163
        except errors.NoSuchFile, e:
 
164
            pass
 
165
        
 
166
        return self._get_location_config()._get_user_id()
 
167
 
 
168
    def __init__(self, branch):
 
169
        super(BranchConfig, self).__init__()
 
170
        self._location_config = None
 
171
        self.branch = branch
26
172
 
27
173
 
28
174
def config_dir():
40
186
    return os.path.join(config_dir(), 'bazaar.conf')
41
187
 
42
188
 
43
 
def _get_config_parser(file=None):
44
 
    parser = ConfigParser()
45
 
    if file is not None:
46
 
        parser.readfp(file)
47
 
    else:
48
 
        parser.read([config_filename()])
49
 
    return parser
50
 
 
51
 
 
52
 
def get_editor(parser=None):
53
 
    if parser is None:
54
 
        parser = _get_config_parser()
55
 
    if parser.has_option('DEFAULT', 'editor'):
56
 
        return parser.get('DEFAULT', 'editor')
57
 
 
58
 
 
59
 
def _get_user_id(branch=None, parser = None):
60
 
    """Return the full user id from a file or environment variable.
61
 
 
62
 
    e.g. "John Hacker <jhacker@foo.org>"
63
 
 
64
 
    branch
65
 
        A branch to use for a per-branch configuration, or None.
66
 
 
67
 
    The following are searched in order:
68
 
 
69
 
    1. $BZREMAIL
70
 
    2. .bzr/email for this branch.
71
 
    3. ~/.bzr.conf/email
72
 
    4. $EMAIL
73
 
    """
74
 
    v = os.environ.get('BZREMAIL')
75
 
    if v:
76
 
        return v.decode(bzrlib.user_encoding)
77
 
 
78
 
    if branch:
79
 
        try:
80
 
            return (branch.controlfile("email", "r") 
81
 
                    .read()
82
 
                    .decode(bzrlib.user_encoding)
83
 
                    .rstrip("\r\n"))
84
 
        except IOError, e:
85
 
            if e.errno != errno.ENOENT:
86
 
                raise
87
 
        except BzrError, e:
88
 
            pass
89
 
    
90
 
    if parser is None:
91
 
        parser = _get_config_parser()
92
 
    if parser.has_option('DEFAULT', 'email'):
93
 
        email = parser.get('DEFAULT', 'email')
94
 
        if email is not None:
95
 
            return email
96
 
 
97
 
    v = os.environ.get('EMAIL')
98
 
    if v:
99
 
        return v.decode(bzrlib.user_encoding)
100
 
    else:    
101
 
        return None
 
189
def branches_config_filename():
 
190
    """Return per-user configuration ini file filename."""
 
191
    return os.path.join(config_dir(), 'branches.conf')
102
192
 
103
193
 
104
194
def _auto_user_id():
137
227
    return realname, (username + '@' + socket.gethostname())
138
228
 
139
229
 
140
 
def username(branch):
141
 
    """Return email-style username.
142
 
 
143
 
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
144
 
 
145
 
    TODO: Check it's reasonably well-formed.
146
 
    """
147
 
    v = _get_user_id(branch)
148
 
    if v:
149
 
        return v
150
 
    
151
 
    name, email = _auto_user_id()
152
 
    if name:
153
 
        return '%s <%s>' % (name, email)
154
 
    else:
155
 
        return email
156
 
 
157
 
 
158
 
def user_email(branch):
159
 
    """Return just the email component of a username."""
160
 
    e = _get_user_id(branch)
161
 
    if e:
162
 
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
163
 
        if not m:
164
 
            raise BzrError("%r doesn't seem to contain "
165
 
                           "a reasonable email address" % e)
166
 
        return m.group(0)
167
 
 
168
 
    return _auto_user_id()[1]
169
 
 
170