~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2010-10-02 21:08:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5499.
  • Revision ID: v.ladeuil+lp@free.fr-20101002210835-nqy0fqas09e7ijj1
Implement config.get_sections() to clarify how sections can be used.

* bzrlib/tests/test_config.py:
(TestConfigGetSections): Expose the various supported section
use-cases.

* bzrlib/config.py:
(IniBasedConfig.get_sections, GlobalConfig.get_sections)
(LocationConfig.get_sections, BranchConfig.get_sections): Properly
capture the different section usages exposed by the various config
objects.

Show diffs side-by-side

added added

removed removed

Lines of Context:
450
450
        """Override this to define the section used by the config."""
451
451
        return "DEFAULT"
452
452
 
 
453
    def get_sections(self, name=None):
 
454
        """Returns an iterator of the sections specified by ``name``.
 
455
 
 
456
        :param name: The section name. If None is supplied, the default
 
457
            configurations are yielded.
 
458
 
 
459
        :return: A tuple (name, section) for all sections that will we walked
 
460
            by user_get_option() in the 'right' order. The first one is where
 
461
            set_user_option() will update the value.
 
462
        """
 
463
        parser = self._get_parser()
 
464
        if name is not None:
 
465
            yield (name, parser[name])
 
466
        else:
 
467
            # No section name has been given so we fallback to the configobj
 
468
            # itself which holds the variables defined outside of any section.
 
469
            yield (None, parser)
 
470
 
453
471
    def get_options(self, sections=None):
454
472
        """Return an ordered list of (name, value, section, config_id) tuples.
455
473
 
706
724
        self._write_config_file()
707
725
 
708
726
 
 
727
    def get_sections(self, name=None):
 
728
        """See IniBasedConfig.get_sections()."""
 
729
        parser = self._get_parser()
 
730
        # We don't give access to options defined outside of any section, we
 
731
        # used the DEFAULT section by... default.
 
732
        if name in (None, 'DEFAULT'):
 
733
            # This could happen for an empty file where the DEFAULT section
 
734
            # doesn't exist yet. So we force DEFAULT when yielding
 
735
            name = 'DEFAULT'
 
736
            parser[name]= {}
 
737
        yield (name, parser[name])
 
738
 
 
739
 
709
740
class LocationConfig(LockableConfig):
710
741
    """A configuration object that gives the policy for a location."""
711
742
 
783
814
                pass
784
815
        return sections
785
816
 
 
817
    def get_sections(self, name=None):
 
818
        """See IniBasedConfig.get_sections()."""
 
819
        # We ignore the name here as the only sections handled are named with
 
820
        # the location path and we don't expose embedded sections either.
 
821
        parser = self._get_parser()
 
822
        for name, extra_path in self._get_matching_sections():
 
823
            yield (name, parser[name])
 
824
 
786
825
    def _get_option_policy(self, section, option_name):
787
826
        """Return the policy for the given (section, option_name) pair."""
788
827
        # check for the old 'recurse=False' flag
946
985
                return value
947
986
        return None
948
987
 
 
988
    def get_sections(self, name=None):
 
989
        """See IniBasedConfig.get_sections()."""
 
990
        for source in self.option_sources:
 
991
            for section in source().get_sections(name):
 
992
                yield section
 
993
 
949
994
    def get_options(self, sections=None):
950
995
        opts = []
951
996
        # First the locations options
1605
1650
    """A Config that reads/writes a config file on a Transport.
1606
1651
 
1607
1652
    It is a low-level object that considers config data to be name/value pairs
1608
 
    that may be associated with a section.  Assigning meaning to the these
1609
 
    values is done at higher levels like TreeConfig.
 
1653
    that may be associated with a section.  Assigning meaning to these values
 
1654
    is done at higher levels like TreeConfig.
1610
1655
    """
1611
1656
 
1612
1657
    def __init__(self, transport, filename):