~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Sidnei da Silva
  • Date: 2009-05-29 14:19:29 UTC
  • mto: (4531.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4532.
  • Revision ID: sidnei.da.silva@canonical.com-20090529141929-3heywbvj36po72a5
- Add initial config

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2007, 2008 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#            and others
4
4
#
146
146
class Config(object):
147
147
    """A configuration policy - what username, editor, gpg needs etc."""
148
148
 
149
 
    def __init__(self):
150
 
        super(Config, self).__init__()
151
 
 
152
149
    def get_editor(self):
153
150
        """Get the users pop up editor."""
154
151
        raise NotImplementedError
155
152
 
156
 
    def get_change_editor(self, old_tree, new_tree):
157
 
        from bzrlib import diff
158
 
        cmd = self._get_change_editor()
159
 
        if cmd is None:
160
 
            return None
161
 
        return diff.DiffFromTool.from_string(cmd, old_tree, new_tree,
162
 
                                             sys.stdout)
163
 
 
164
 
 
165
153
    def get_mail_client(self):
166
154
        """Get a mail client to use"""
167
155
        selected_client = self.get_user_option('mail_client')
186
174
        """Get a generic option - no special process, no default."""
187
175
        return self._get_user_option(option_name)
188
176
 
189
 
    def get_user_option_as_bool(self, option_name):
190
 
        """Get a generic option as a boolean - no special process, no default.
191
 
 
192
 
        :return None if the option doesn't exist or its value can't be
193
 
            interpreted as a boolean. Returns True or False otherwise.
194
 
        """
195
 
        s = self._get_user_option(option_name)
196
 
        return ui.bool_from_string(s)
197
 
 
198
 
    def get_user_option_as_list(self, option_name):
199
 
        """Get a generic option as a list - no special process, no default.
200
 
 
201
 
        :return None if the option doesn't exist. Returns the value as a list
202
 
            otherwise.
203
 
        """
204
 
        l = self._get_user_option(option_name)
205
 
        if isinstance(l, (str, unicode)):
206
 
            # A single value, most probably the user forgot the final ','
207
 
            l = [l]
208
 
        return l
209
 
 
210
177
    def gpg_signing_command(self):
211
178
        """What program should be used to sign signatures?"""
212
179
        result = self._gpg_signing_command()
229
196
        """See log_format()."""
230
197
        return None
231
198
 
 
199
    def __init__(self):
 
200
        super(Config, self).__init__()
 
201
 
232
202
    def post_commit(self):
233
203
        """An ordered list of python functions to call.
234
204
 
325
295
                path = 'bzr'
326
296
            return path
327
297
 
328
 
    def suppress_warning(self, warning):
329
 
        """Should the warning be suppressed or emitted.
330
 
 
331
 
        :param warning: The name of the warning being tested.
332
 
 
333
 
        :returns: True if the warning should be suppressed, False otherwise.
334
 
        """
335
 
        warnings = self.get_user_option_as_list('suppress_warnings')
336
 
        if warnings is None or warning not in warnings:
337
 
            return False
338
 
        else:
339
 
            return True
340
 
 
341
298
 
342
299
class IniBasedConfig(Config):
343
300
    """A configuration policy that draws from ini files."""
344
301
 
345
 
    def __init__(self, get_filename):
346
 
        super(IniBasedConfig, self).__init__()
347
 
        self._get_filename = get_filename
348
 
        self._parser = None
349
 
 
350
302
    def _get_parser(self, file=None):
351
303
        if self._parser is not None:
352
304
            return self._parser
380
332
        """Return the policy for the given (section, option_name) pair."""
381
333
        return POLICY_NONE
382
334
 
383
 
    def _get_change_editor(self):
384
 
        return self.get_user_option('change_editor')
385
 
 
386
335
    def _get_signature_checking(self):
387
336
        """See Config._get_signature_checking."""
388
337
        policy = self._get_user_option('check_signatures')
432
381
        """See Config.log_format."""
433
382
        return self._get_user_option('log_format')
434
383
 
 
384
    def __init__(self, get_filename):
 
385
        super(IniBasedConfig, self).__init__()
 
386
        self._get_filename = get_filename
 
387
        self._parser = None
 
388
 
435
389
    def _post_commit(self):
436
390
        """See Config.post_commit."""
437
391
        return self._get_user_option('post_commit')
716
670
 
717
671
        return self._get_best_value('_get_user_id')
718
672
 
719
 
    def _get_change_editor(self):
720
 
        return self._get_best_value('_get_change_editor')
721
 
 
722
673
    def _get_signature_checking(self):
723
674
        """See Config._get_signature_checking."""
724
675
        return self._get_best_value('_get_signature_checking')
861
812
    return osutils.pathjoin(config_dir(), 'ignore')
862
813
 
863
814
 
864
 
def crash_dir():
865
 
    """Return the directory name to store crash files.
866
 
 
867
 
    This doesn't implicitly create it.
868
 
 
869
 
    On Windows it's in the config directory; elsewhere it's /var/crash
870
 
    which may be monitored by apport.  It can be overridden by
871
 
    $APPORT_CRASH_DIR.
872
 
    """
873
 
    if sys.platform == 'win32':
874
 
        return osutils.pathjoin(config_dir(), 'Crash')
875
 
    else:
876
 
        # XXX: hardcoded in apport_python_hook.py; therefore here too -- mbp
877
 
        # 2010-01-31
878
 
        return os.environ.get('APPORT_CRASH_DIR', '/var/crash')
879
 
 
880
 
 
881
 
def xdg_cache_dir():
882
 
    # See http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
883
 
    # Possibly this should be different on Windows?
884
 
    e = os.environ.get('XDG_CACHE_DIR', None)
885
 
    if e:
886
 
        return e
887
 
    else:
888
 
        return os.path.expanduser('~/.cache')
889
 
 
890
 
 
891
815
def _auto_user_id():
892
816
    """Calculate automatic user identification.
893
817
 
1006
930
            return self._config.get_option(name, section, default)
1007
931
        finally:
1008
932
            self.branch.unlock()
 
933
        return result
1009
934
 
1010
935
    def set_option(self, value, name, section=None):
1011
936
        """Set a per-branch configuration option"""
1501
1426
 
1502
1427
    def _get_config_file(self):
1503
1428
        try:
1504
 
            return StringIO(self._transport.get_bytes(self._filename))
 
1429
            return self._transport.get(self._filename)
1505
1430
        except errors.NoSuchFile:
1506
1431
            return StringIO()
1507
1432