~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/util/configobj/configobj.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
353
353
        if not isinstance(key, StringTypes):
354
354
            raise ValueError, 'The key "%s" is not a string.' % key
355
355
        # add the comment
356
 
        if key not in self.comments:
 
356
        if not self.comments.has_key(key):
357
357
            self.comments[key] = []
358
358
            self.inline_comments[key] = ''
359
359
        # remove the entry from defaults
361
361
            self.defaults.remove(key)
362
362
        #
363
363
        if isinstance(value, Section):
364
 
            if key not in self:
 
364
            if not self.has_key(key):
365
365
                self.sections.append(key)
366
366
            dict.__setitem__(self, key, value)
367
367
        elif isinstance(value, dict):
368
368
            # First create the new depth level,
369
369
            # then create the section
370
 
            if key not in self:
 
370
            if not self.has_key(key):
371
371
                self.sections.append(key)
372
372
            new_depth = self.depth + 1
373
373
            dict.__setitem__(
380
380
                    indict=value,
381
381
                    name=key))
382
382
        else:
383
 
            if key not in self:
 
383
            if not self.has_key(key):
384
384
                self.scalars.append(key)
385
385
            if not self.main.stringify:
386
386
                if isinstance(value, StringTypes):
678
678
        >>> def testuni(val):
679
679
        ...     for entry in val:
680
680
        ...         if not isinstance(entry, unicode):
681
 
        ...             sys.stderr.write(type(entry))
682
 
        ...             sys.stderr.write('\n')
 
681
        ...             print >> sys.stderr, type(entry)
683
682
        ...             raise AssertionError, 'decode failed.'
684
683
        ...         if isinstance(val[entry], dict):
685
684
        ...             testuni(val[entry])
1025
1024
            else:
1026
1025
                self.configspec = None
1027
1026
            return
1028
 
        elif getattr(infile, 'read', None) is not None:
 
1027
        elif hasattr(infile, 'read'):
1029
1028
            # This supports file like objects
1030
1029
            infile = infile.read() or []
1031
1030
            # needs splitting into lines - but needs doing *after* decoding
1309
1308
            reset_comment = True
1310
1309
            # first we check if it's a section marker
1311
1310
            mat = self._sectionmarker.match(line)
1312
 
##            sys.stderr.write('%s %s\n' % (sline, mat))
 
1311
##            print >> sys.stderr, sline, mat
1313
1312
            if mat is not None:
1314
1313
                # is a section line
1315
1314
                (indent, sect_open, sect_name, sect_close, comment) = (
1345
1344
                        NestingError, infile, cur_index)
1346
1345
                #
1347
1346
                sect_name = self._unquote(sect_name)
1348
 
                if sect_name in parent:
1349
 
##                    sys.stderr.write(sect_name)
1350
 
##                    sys.stderr.write('\n')
 
1347
                if parent.has_key(sect_name):
 
1348
##                    print >> sys.stderr, sect_name
1351
1349
                    self._handle_error(
1352
1350
                        'Duplicate section name at line %s.',
1353
1351
                        DuplicateError, infile, cur_index)
1361
1359
                parent[sect_name] = this_section
1362
1360
                parent.inline_comments[sect_name] = comment
1363
1361
                parent.comments[sect_name] = comment_list
1364
 
##                sys.stderr.write(parent[sect_name] is this_section)
1365
 
##                sys.stderr.write('\n')
 
1362
##                print >> sys.stderr, parent[sect_name] is this_section
1366
1363
                continue
1367
1364
            #
1368
1365
            # it's not a section marker,
1369
1366
            # so it should be a valid ``key = value`` line
1370
1367
            mat = self._keyword.match(line)
1371
 
##            sys.stderr.write('%s %s\n' % (sline, mat))
 
1368
##            print >> sys.stderr, sline, mat
1372
1369
            if mat is not None:
1373
1370
                # is a keyword value
1374
1371
                # value will include any inline comment
1395
1392
                            ParseError, infile, cur_index)
1396
1393
                        continue
1397
1394
                #
1398
 
##                sys.stderr.write(sline)
1399
 
##                sys.stderr.write('\n')
 
1395
##                print >> sys.stderr, sline
1400
1396
                key = self._unquote(key)
1401
 
                if key in this_section:
 
1397
                if this_section.has_key(key):
1402
1398
                    self._handle_error(
1403
1399
                        'Duplicate keyword name at line %s.',
1404
1400
                        DuplicateError, infile, cur_index)
1405
1401
                    continue
1406
1402
                # add the key
1407
 
##                sys.stderr.write(this_section.name + '\n')
 
1403
##                print >> sys.stderr, this_section.name
1408
1404
                this_section[key] = value
1409
1405
                this_section.inline_comments[key] = comment
1410
1406
                this_section.comments[key] = comment_list
1411
 
##                sys.stderr.write('%s %s\n' % (key, this_section[key]))
 
1407
##                print >> sys.stderr, key, this_section[key]
1412
1408
##                if this_section.name is not None:
1413
 
##                    sys.stderr.write(this_section + '\n')
1414
 
##                    sys.stderr.write(this_section.parent + '\n')
1415
 
##                    sys.stderr.write(this_section.parent[this_section.name])
1416
 
##                    sys.stderr.write('\n')
 
1409
##                    print >> sys.stderr, this_section
 
1410
##                    print >> sys.stderr, this_section.parent
 
1411
##                    print >> sys.stderr, this_section.parent[this_section.name]
1417
1412
                continue
1418
1413
            #
1419
1414
            # it neither matched as a keyword
1456
1451
        The error will have occured at ``cur_index``
1457
1452
        """
1458
1453
        line = infile[cur_index]
1459
 
        cur_index += 1
1460
1454
        message = text % cur_index
1461
1455
        error = ErrorClass(message, cur_index, line)
1462
1456
        if self.raise_errors:
1751
1745
        for entry in configspec.sections:
1752
1746
            if entry == '__many__':
1753
1747
                continue
1754
 
            if entry not in section:
 
1748
            if not section.has_key(entry):
1755
1749
                section[entry] = {}
1756
1750
            self._set_configspec_value(configspec[entry], section[entry])
1757
1751
 
1782
1776
        #
1783
1777
        section.configspec = scalars
1784
1778
        for entry in sections:
1785
 
            if entry not in section:
 
1779
            if not section.has_key(entry):
1786
1780
                section[entry] = {}
1787
1781
            self._handle_repeat(section[entry], sections[entry])
1788
1782
 
2013
2007
        >>> try:
2014
2008
        ...     from validate import Validator
2015
2009
        ... except ImportError:
2016
 
        ...     sys.stderr.write('Cannot import the Validator object, skipping the related tests\n')
 
2010
        ...     print >> sys.stderr, 'Cannot import the Validator object, skipping the related tests'
2017
2011
        ... else:
2018
2012
        ...     config = '''
2019
2013
        ...     test1=40
2858
2852
    >>> uc2 = ConfigObj(file_like)
2859
2853
    >>> uc2 == uc
2860
2854
    1
2861
 
    >>> uc2.filename is None
 
2855
    >>> uc2.filename == None
2862
2856
    1
2863
2857
    >>> uc2.newlines == '\\r'
2864
2858
    1