~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Robert Collins
  • Date: 2005-10-20 04:08:12 UTC
  • mfrom: (1185.12.68)
  • Revision ID: robertc@robertcollins.net-20051020040812-fecd1bc32aa3478e
Merge from Aaron Bentley.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
                    NB: This option is planned, but not implemented yet.
52
52
"""
53
53
 
54
 
from ConfigParser import ConfigParser
 
54
 
 
55
import errno
55
56
import os
56
57
from fnmatch import fnmatch
57
 
import errno
58
58
import re
59
59
 
60
60
import bzrlib
61
61
import bzrlib.errors as errors
 
62
import bzrlib.util.configobj.configobj as configobj
62
63
 
63
64
 
64
65
CHECK_IF_POSSIBLE=0
66
67
CHECK_NEVER=2
67
68
 
68
69
 
 
70
class ConfigObj(configobj.ConfigObj):
 
71
 
 
72
    def get_bool(self, section, key):
 
73
        val = self[section][key].lower()
 
74
        if val in ('1', 'yes', 'true', 'on'):
 
75
            return True
 
76
        elif val in ('0', 'no', 'false', 'off'):
 
77
            return False
 
78
        else:
 
79
            raise ValueError("Value %r is not boolean" % val)
 
80
 
 
81
    def get_value(self, section, name):
 
82
        # Try [] for the old DEFAULT section.
 
83
        if section == "DEFAULT":
 
84
            try:
 
85
                return self[name]
 
86
            except KeyError:
 
87
                pass
 
88
        return self[section][name]
 
89
 
 
90
 
69
91
class Config(object):
70
92
    """A configuration policy - what username, editor, gpg needs etc."""
71
93
 
170
192
    def _get_parser(self, file=None):
171
193
        if self._parser is not None:
172
194
            return self._parser
173
 
        parser = ConfigParser()
174
 
        if file is not None:
175
 
            parser.readfp(file)
 
195
        if file is None:
 
196
            input = self._get_filename()
176
197
        else:
177
 
            parser.read([self._get_filename()])
178
 
        self._parser = parser
179
 
        return parser
 
198
            input = file
 
199
        try:
 
200
            self._parser = ConfigObj(input)
 
201
        except configobj.ConfigObjError, e:
 
202
            raise errors.ParseConfigError(e.errors, e.config.filename)
 
203
        return self._parser
180
204
 
181
205
    def _get_section(self):
182
206
        """Override this to define the section used by the config."""
184
208
 
185
209
    def _get_signature_checking(self):
186
210
        """See Config._get_signature_checking."""
187
 
        section = self._get_section()
188
 
        if section is None:
189
 
            return None
190
 
        if self._get_parser().has_option(section, 'check_signatures'):
191
 
            return self._string_to_signature_policy(
192
 
                self._get_parser().get(section, 'check_signatures'))
 
211
        policy = self._get_user_option('check_signatures')
 
212
        if policy:
 
213
            return self._string_to_signature_policy(policy)
193
214
 
194
215
    def _get_user_id(self):
195
216
        """Get the user id from the 'email' key in the current section."""
196
 
        section = self._get_section()
197
 
        if section is not None:
198
 
            if self._get_parser().has_option(section, 'email'):
199
 
                return self._get_parser().get(section, 'email')
 
217
        return self._get_user_option('email')
200
218
 
201
219
    def _get_user_option(self, option_name):
202
220
        """See Config._get_user_option."""
203
 
        section = self._get_section()
204
 
        if section is not None:
205
 
            if self._get_parser().has_option(section, option_name):
206
 
                return self._get_parser().get(section, option_name)
 
221
        try:
 
222
            return self._get_parser().get_value(self._get_section(),
 
223
                                                option_name)
 
224
        except KeyError:
 
225
            pass
207
226
 
208
227
    def _gpg_signing_command(self):
209
228
        """See Config.gpg_signing_command."""
234
253
    """The configuration that should be used for a specific location."""
235
254
 
236
255
    def get_editor(self):
237
 
        if self._get_parser().has_option(self._get_section(), 'editor'):
238
 
            return self._get_parser().get(self._get_section(), 'editor')
 
256
        return self._get_user_option('editor')
239
257
 
240
258
    def __init__(self):
241
259
        super(GlobalConfig, self).__init__(config_filename)
261
279
        TODO: perhaps return a NullSection that thunks through to the 
262
280
              global config.
263
281
        """
264
 
        sections = self._get_parser().sections()
 
282
        sections = self._get_parser()
265
283
        location_names = self.location.split('/')
266
284
        if self.location.endswith('/'):
267
285
            del location_names[-1]
284
302
                continue
285
303
            # if path is longer, and recurse is not true, no match
286
304
            if len(section_names) < len(location_names):
287
 
                if (self._get_parser().has_option(section, 'recurse')
288
 
                    and not self._get_parser().getboolean(section, 'recurse')):
289
 
                    continue
 
305
                try:
 
306
                    if not self._get_parser().get_bool(section, 'recurse'):
 
307
                        continue
 
308
                except KeyError:
 
309
                    pass
290
310
            matches.append((len(section_names), section))
291
311
        if not len(matches):
292
312
            return None