93
93
return CHECK_IF_POSSIBLE
96
class GlobalConfig(Config):
97
"""The configuration that should be used for a specific location."""
96
class IniBasedConfig(Config):
97
"""A configuration policy that draws from ini files."""
99
def _get_parser(self, filename=None, file=None):
99
def _get_parser(self, file=None):
100
if self._parser is not None:
100
102
parser = ConfigParser()
101
103
if file is not None:
102
104
parser.readfp(file)
104
parser.read([filename])
106
parser.read([self._get_filename()])
107
self._parser = parser
107
def _get_config_parser(self, file=None):
108
if self._parser is None:
109
self._parser = self._get_parser(config_filename(), file)
112
def _get_branches_config_parser(self, file=None):
113
if self._branches_parser is None:
114
self._branches_parser = self._get_parser(
115
branches_config_filename(), file)
116
return self._branches_parser
118
def get_editor(self):
119
if self._get_config_parser().has_option('DEFAULT', 'editor'):
120
return self._get_config_parser().get('DEFAULT', 'editor')
122
def _get_user_id(self, branch=None):
123
"""Return the full user id from the global config file.
125
e.g. "John Hacker <jhacker@foo.org>"
128
email=John Hacker <jhacker@foo.org>
130
if self._get_config_parser().has_option('DEFAULT', 'email'):
131
return self._get_config_parser().get('DEFAULT', 'email')
110
def _get_section(self):
111
"""Override this to define the section used by the config."""
133
114
def _get_signature_checking(self):
134
115
"""See Config._get_signature_checking."""
135
if self._get_config_parser().has_option('DEFAULT', 'signatures'):
116
section = self._get_section()
119
if self._get_parser().has_option(section, 'signatures'):
136
120
return self._string_to_signature_policy(
137
self._get_config_parser().get('DEFAULT', 'signatures'))
140
super(GlobalConfig, self).__init__()
141
self._branches_parser = None
121
self._get_parser().get(section, 'signatures'))
123
def _get_user_id(self):
124
"""Get the user id from the 'email' key in the current section."""
125
section = self._get_section()
126
if section is not None:
127
if self._get_parser().has_option(section, 'email'):
128
return self._get_parser().get(section, 'email')
130
def __init__(self, get_filename):
131
super(IniBasedConfig, self).__init__()
132
self._get_filename = get_filename
142
133
self._parser = None
144
135
def _string_to_signature_policy(self, signature_string):
152
143
raise errors.BzrError("Invalid signatures policy '%s'"
153
144
% signature_string)
155
class LocationConfig(Config):
147
class GlobalConfig(IniBasedConfig):
148
"""The configuration that should be used for a specific location."""
150
def get_editor(self):
151
if self._get_parser().has_option(self._get_section(), 'editor'):
152
return self._get_parser().get(self._get_section(), 'editor')
155
super(GlobalConfig, self).__init__(config_filename)
158
class LocationConfig(IniBasedConfig):
156
159
"""A configuration object that gives the policy for a location."""
158
161
def __init__(self, location):
162
super(LocationConfig, self).__init__(branches_config_filename)
159
163
self._global_config = None
160
164
self.location = location
162
def _get_branches_config_parser(self, file=None):
163
return self._get_global_config()._get_branches_config_parser(file)
165
166
def _get_global_config(self):
166
167
if self._global_config is None:
167
168
self._global_config = GlobalConfig()
197
199
# if path is longer, and recurse is not true, no match
198
200
if len(section_names) < len(location_names):
199
if (parser.has_option(section, 'recurse')
200
and not parser.getboolean(section, 'recurse')):
201
if (self._get_parser().has_option(section, 'recurse')
202
and not self._get_parser().getboolean(section, 'recurse')):
202
204
matches.append((len(section_names), section))
203
205
if not len(matches):
206
208
return matches[0][1]
208
210
def _get_user_id(self):
209
section = self._get_section()
210
if section is not None:
211
if self._get_branches_config_parser().has_option(section, 'email'):
212
return self._get_branches_config_parser().get(section, 'email')
211
user_id = super(LocationConfig, self)._get_user_id()
212
if user_id is not None:
213
214
return self._get_global_config()._get_user_id()
216
def _get_signature_checking(self):
217
"""See Config._get_signature_checking."""
218
check = super(LocationConfig, self)._get_signature_checking()
219
if check is not None:
221
return self._get_global_config()._get_signature_checking()
216
224
class BranchConfig(Config):
217
225
"""A configuration object giving the policy for a branch."""