~ubuntu-branches/ubuntu/quantal/python2.7/quantal

« back to all changes in this revision

Viewing changes to Lib/ConfigParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-30 06:44:23 UTC
  • mto: (27.1.6 oneiric) (36.1.11 sid)
  • mto: This revision was merged to the branch mainline in revision 32.
  • Revision ID: james.westby@ubuntu.com-20110530064423-zj6auh76957umtm4
Tags: upstream-2.7.2~rc1
ImportĀ upstreamĀ versionĀ 2.7.2~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
545
545
                if isinstance(val, list):
546
546
                    options[name] = '\n'.join(val)
547
547
 
 
548
import UserDict as _UserDict
 
549
 
 
550
class _Chainmap(_UserDict.DictMixin):
 
551
    """Combine multiple mappings for successive lookups.
 
552
 
 
553
    For example, to emulate Python's normal lookup sequence:
 
554
 
 
555
        import __builtin__
 
556
        pylookup = _Chainmap(locals(), globals(), vars(__builtin__))
 
557
    """
 
558
 
 
559
    def __init__(self, *maps):
 
560
        self._maps = maps
 
561
 
 
562
    def __getitem__(self, key):
 
563
        for mapping in self._maps:
 
564
            try:
 
565
                return mapping[key]
 
566
            except KeyError:
 
567
                pass
 
568
        raise KeyError(key)
 
569
 
 
570
    def keys(self):
 
571
        result = []
 
572
        seen = set()
 
573
        for mapping in self_maps:
 
574
            for key in mapping:
 
575
                if key not in seen:
 
576
                    result.append(key)
 
577
                    seen.add(key)
 
578
        return result
 
579
 
548
580
class ConfigParser(RawConfigParser):
549
581
 
550
582
    def get(self, section, option, raw=False, vars=None):
559
591
 
560
592
        The section DEFAULT is special.
561
593
        """
562
 
        d = self._defaults.copy()
 
594
        sectiondict = {}
563
595
        try:
564
 
            d.update(self._sections[section])
 
596
            sectiondict = self._sections[section]
565
597
        except KeyError:
566
598
            if section != DEFAULTSECT:
567
599
                raise NoSectionError(section)
568
600
        # Update with the entry specific variables
 
601
        vardict = {}
569
602
        if vars:
570
603
            for key, value in vars.items():
571
 
                d[self.optionxform(key)] = value
 
604
                vardict[self.optionxform(key)] = value
 
605
        d = _Chainmap(vardict, sectiondict, self._defaults)
572
606
        option = self.optionxform(option)
573
607
        try:
574
608
            value = d[option]