~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Robert Collins
  • Date: 2005-11-22 21:28:30 UTC
  • mfrom: (1185.33.32 bzr.dev)
  • Revision ID: robertc@robertcollins.net-20051122212830-885c284847f0b17b
Merge from mpool.

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
133
133
 
134
134
    def user_email(self):
135
135
        """Return just the email component of a username."""
136
 
        e = self.username()
137
 
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
138
 
        if not m:
139
 
            raise BzrError("%r doesn't seem to contain "
140
 
                           "a reasonable email address" % e)
141
 
        return m.group(0)
 
136
        return extract_email_address(self.username())
142
137
 
143
138
    def username(self):
144
139
        """Return email-style username.
147
142
        
148
143
        $BZREMAIL can be set to override this, then
149
144
        the concrete policy type is checked, and finally
150
 
        $EMAIL is examinged.
151
 
        but if none is found, a reasonable default is (hopefully)
 
145
        $EMAIL is examined.
 
146
        If none is found, a reasonable default is (hopefully)
152
147
        created.
153
148
    
154
149
        TODO: Check it's reasonably well-formed.
480
475
    """
481
476
    m = re.search(r'[\w+.-]+@[\w+.-]+', e)
482
477
    if not m:
483
 
        raise BzrError("%r doesn't seem to contain "
484
 
                       "a reasonable email address" % e)
 
478
        raise errors.BzrError("%r doesn't seem to contain "
 
479
                              "a reasonable email address" % e)
485
480
    return m.group(0)
 
481
 
 
482
class TreeConfig(object):
 
483
    """Branch configuration data associated with its contents, not location"""
 
484
    def __init__(self, branch):
 
485
        self.branch = branch
 
486
 
 
487
    def _get_config(self):
 
488
        try:
 
489
            obj = ConfigObj(self.branch.controlfile('branch.conf',
 
490
                                                    'rb').readlines())
 
491
            obj.decode('UTF-8')
 
492
        except errors.NoSuchFile:
 
493
            obj = ConfigObj()
 
494
        return obj
 
495
 
 
496
    def get_option(self, name, section=None, default=None):
 
497
        self.branch.lock_read()
 
498
        try:
 
499
            obj = self._get_config()
 
500
            try:
 
501
                if section is not None:
 
502
                    obj[section]
 
503
                result = obj[name]
 
504
            except KeyError:
 
505
                result = default
 
506
        finally:
 
507
            self.branch.unlock()
 
508
        return result
 
509
 
 
510
    def set_option(self, value, name, section=None):
 
511
        """Set a per-branch configuration option"""
 
512
        self.branch.lock_write()
 
513
        try:
 
514
            cfg_obj = self._get_config()
 
515
            if section is None:
 
516
                obj = cfg_obj
 
517
            else:
 
518
                try:
 
519
                    obj = cfg_obj[section]
 
520
                except KeyError:
 
521
                    cfg_obj[section] = {}
 
522
                    obj = cfg_obj[section]
 
523
            obj[name] = value
 
524
            cfg_obj.encode('UTF-8')
 
525
            out_file = StringIO(''.join([l+'\n' for l in cfg_obj.write()]))
 
526
            out_file.seek(0)
 
527
            self.branch.put_controlfile('branch.conf', out_file, encode=False)
 
528
        finally:
 
529
            self.branch.unlock()