~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Added support for branch nicks

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
466
466
        raise BzrError("%r doesn't seem to contain "
467
467
                       "a reasonable email address" % e)
468
468
    return m.group(0)
 
469
 
 
470
class TreeConfig(object):
 
471
    """Branch configuration data associated with its contents, not location"""
 
472
    def __init__(self, branch):
 
473
        self.branch = branch
 
474
 
 
475
    def _get_config(self):
 
476
        try:
 
477
            obj = ConfigObj(self.branch.controlfile('branch.conf', 'rb'))
 
478
        except errors.NoSuchFile:
 
479
            obj = ConfigObj()
 
480
        return obj
 
481
 
 
482
    def get_option(self, name, section=None, default=None):
 
483
        self.branch.lock_read()
 
484
        try:
 
485
            obj = self._get_config()
 
486
            try:
 
487
                if section is not None:
 
488
                    obj[section]
 
489
                result = obj[name]
 
490
            except KeyError:
 
491
                result = default
 
492
        finally:
 
493
            self.branch.unlock()
 
494
        return result
 
495
 
 
496
    def set_option(self, value, name, section=None):
 
497
        """Set a per-branch configuration option"""
 
498
        self.branch.lock_write()
 
499
        try:
 
500
            cfg_obj = self._get_config()
 
501
            if section is None:
 
502
                obj = cfg_obj
 
503
            else:
 
504
                try:
 
505
                    obj = cfg_obj[section]
 
506
                except KeyError:
 
507
                    cfg_obj[section] = {}
 
508
                    obj = cfg_obj[section]
 
509
            obj[name] = value
 
510
            out_file = StringIO(''.join([l+'\n' for l in cfg_obj.write()]))
 
511
            out_file.seek(0)
 
512
            self.branch.put_controlfile('branch.conf', out_file, encode=True)
 
513
        finally:
 
514
            self.branch.unlock()