26
import bzrlib.errors as errors
30
"""A configuration policy - what username, editor, gpg needs etc."""
33
"""Get the users pop up editor."""
34
raise NotImplementedError
37
super(Config, self).__init__()
40
"""Return just the email component of a username."""
42
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
44
raise BzrError("%r doesn't seem to contain "
45
"a reasonable email address" % e)
49
"""Return email-style username.
51
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
53
$BZREMAIL can be set to override this, then
54
the concrete policy type is checked, and finally
56
but if none is found, a reasonable default is (hopefully)
59
TODO: Check it's reasonably well-formed.
61
v = os.environ.get('BZREMAIL')
63
return v.decode(bzrlib.user_encoding)
65
v = self._get_user_id()
69
v = os.environ.get('EMAIL')
71
return v.decode(bzrlib.user_encoding)
73
name, email = _auto_user_id()
75
return '%s <%s>' % (name, email)
80
class GlobalConfig(Config):
81
"""The configuration that should be used for a specific location."""
83
def _get_parser(self, filename=None, file=None):
84
parser = ConfigParser()
88
parser.read([filename])
91
def _get_config_parser(self, file=None):
92
if self._parser is None:
93
self._parser = self._get_parser(config_filename(), file)
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
102
def get_editor(self):
103
if self._get_config_parser().has_option('DEFAULT', 'editor'):
104
return self._get_config_parser().get('DEFAULT', 'editor')
106
def _get_user_id(self, branch=None):
107
"""Return the full user id from the global config file.
109
e.g. "John Hacker <jhacker@foo.org>"
112
email=John Hacker <jhacker@foo.org>
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:
120
super(GlobalConfig, self).__init__()
121
self._branches_parser = None
125
class LocationConfig(Config):
126
"""A configuration object that gives the policy for a location."""
128
def __init__(self, location):
129
self._global_config = None
130
self.location = location
132
def _get_branches_config_parser(self, file=None):
133
return self._get_global_config()._get_branches_config_parser(file)
135
def _get_global_config(self):
136
if self._global_config is None:
137
self._global_config = GlobalConfig()
138
return self._global_config
140
def _get_user_id(self):
141
return self._get_global_config()._get_user_id()
144
class BranchConfig(Config):
145
"""A configuration object giving the policy for a branch."""
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
152
def _get_user_id(self):
153
"""Return the full user id for the branch.
155
e.g. "John Hacker <jhacker@foo.org>"
156
This is looked up in the email controlfile for the branch.
159
return (self.branch.controlfile("email", "r")
161
.decode(bzrlib.user_encoding)
163
except errors.NoSuchFile, e:
166
return self._get_location_config()._get_user_id()
168
def __init__(self, branch):
169
super(BranchConfig, self).__init__()
170
self._location_config = None
40
186
return os.path.join(config_dir(), 'bazaar.conf')
43
def _get_config_parser(file=None):
44
parser = ConfigParser()
48
parser.read([config_filename()])
52
def get_editor(parser=None):
54
parser = _get_config_parser()
55
if parser.has_option('DEFAULT', 'editor'):
56
return parser.get('DEFAULT', 'editor')
59
def _get_user_id(branch=None, parser = None):
60
"""Return the full user id from a file or environment variable.
62
e.g. "John Hacker <jhacker@foo.org>"
65
A branch to use for a per-branch configuration, or None.
67
The following are searched in order:
70
2. .bzr/email for this branch.
74
v = os.environ.get('BZREMAIL')
76
return v.decode(bzrlib.user_encoding)
80
return (branch.controlfile("email", "r")
82
.decode(bzrlib.user_encoding)
85
if e.errno != errno.ENOENT:
91
parser = _get_config_parser()
92
if parser.has_option('DEFAULT', 'email'):
93
email = parser.get('DEFAULT', 'email')
97
v = os.environ.get('EMAIL')
99
return v.decode(bzrlib.user_encoding)
189
def branches_config_filename():
190
"""Return per-user configuration ini file filename."""
191
return os.path.join(config_dir(), 'branches.conf')
104
194
def _auto_user_id():
137
227
return realname, (username + '@' + socket.gethostname())
140
def username(branch):
141
"""Return email-style username.
143
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
145
TODO: Check it's reasonably well-formed.
147
v = _get_user_id(branch)
151
name, email = _auto_user_id()
153
return '%s <%s>' % (name, email)
158
def user_email(branch):
159
"""Return just the email component of a username."""
160
e = _get_user_id(branch)
162
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
164
raise BzrError("%r doesn't seem to contain "
165
"a reasonable email address" % e)
168
return _auto_user_id()[1]