~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
import bzrlib
61
61
import bzrlib.errors as errors
62
62
import bzrlib.util.configobj.configobj as configobj
63
 
 
 
63
from StringIO import StringIO
64
64
 
65
65
CHECK_IF_POSSIBLE=0
66
66
CHECK_ALWAYS=1
483
483
        raise BzrError("%r doesn't seem to contain "
484
484
                       "a reasonable email address" % e)
485
485
    return m.group(0)
 
486
 
 
487
class TreeConfig(object):
 
488
    """Branch configuration data associated with its contents, not location"""
 
489
    def __init__(self, branch):
 
490
        self.branch = branch
 
491
 
 
492
    def _get_config(self):
 
493
        try:
 
494
            obj = ConfigObj(self.branch.controlfile('branch.conf',
 
495
                                                    'rb').readlines())
 
496
            obj.decode('UTF-8')
 
497
        except errors.NoSuchFile:
 
498
            obj = ConfigObj()
 
499
        return obj
 
500
 
 
501
    def get_option(self, name, section=None, default=None):
 
502
        self.branch.lock_read()
 
503
        try:
 
504
            obj = self._get_config()
 
505
            try:
 
506
                if section is not None:
 
507
                    obj[section]
 
508
                result = obj[name]
 
509
            except KeyError:
 
510
                result = default
 
511
        finally:
 
512
            self.branch.unlock()
 
513
        return result
 
514
 
 
515
    def set_option(self, value, name, section=None):
 
516
        """Set a per-branch configuration option"""
 
517
        self.branch.lock_write()
 
518
        try:
 
519
            cfg_obj = self._get_config()
 
520
            if section is None:
 
521
                obj = cfg_obj
 
522
            else:
 
523
                try:
 
524
                    obj = cfg_obj[section]
 
525
                except KeyError:
 
526
                    cfg_obj[section] = {}
 
527
                    obj = cfg_obj[section]
 
528
            obj[name] = value
 
529
            cfg_obj.encode('UTF-8')
 
530
            out_file = StringIO(''.join([l+'\n' for l in cfg_obj.write()]))
 
531
            out_file.seek(0)
 
532
            self.branch.put_controlfile('branch.conf', out_file, encode=False)
 
533
        finally:
 
534
            self.branch.unlock()