~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: 2007-11-03 09:39:11 UTC
  • mfrom: (2949.6.2 win32.os.lstat)
  • Revision ID: pqm@pqm.ubuntu.com-20071103093911-4alf7wiad3n3vfz6
windows python has os.lstat

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# configobj.py
2
2
# A config file reader/writer that supports nested sections in config files.
3
 
# Copyright (C) 2005-2008 Michael Foord, Nicola Larosa
 
3
# Copyright (C) 2005 Michael Foord, Nicola Larosa
4
4
# E-mail: fuzzyman AT voidspace DOT org DOT uk
5
5
#         nico AT tekNico DOT net
6
6
 
18
18
 
19
19
from __future__ import generators
20
20
 
 
21
"""
 
22
    >>> z = ConfigObj()
 
23
    >>> z['a'] = 'a'
 
24
    >>> z['sect'] = {
 
25
    ...    'subsect': {
 
26
    ...         'a': 'fish',
 
27
    ...         'b': 'wobble',
 
28
    ...     },
 
29
    ...     'member': 'value',
 
30
    ... }
 
31
    >>> x = ConfigObj(z.write())
 
32
    >>> z == x
 
33
    1
 
34
"""
 
35
 
21
36
import sys
22
37
INTP_VER = sys.version_info[:2]
23
38
if INTP_VER < (2, 2):
24
39
    raise RuntimeError("Python v.2.2 or later needed")
25
40
 
26
41
import os, re
27
 
compiler = None
28
 
try:
29
 
    import compiler
30
 
except ImportError:
31
 
    # for IronPython
32
 
    pass
33
42
from types import StringTypes
34
43
from warnings import warn
35
 
try:
36
 
    from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
37
 
except ImportError:
38
 
    # Python 2.2 does not have these
39
 
    # UTF-8
40
 
    BOM_UTF8 = '\xef\xbb\xbf'
41
 
    # UTF-16, little endian
42
 
    BOM_UTF16_LE = '\xff\xfe'
43
 
    # UTF-16, big endian
44
 
    BOM_UTF16_BE = '\xfe\xff'
45
 
    if sys.byteorder == 'little':
46
 
        # UTF-16, native endianness
47
 
        BOM_UTF16 = BOM_UTF16_LE
48
 
    else:
49
 
        # UTF-16, native endianness
50
 
        BOM_UTF16 = BOM_UTF16_BE
 
44
from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF16_BE, BOM_UTF16_LE
51
45
 
52
46
# A dictionary mapping BOM to
53
47
# the encoding to decode with, and what to set the
88
82
    None: BOM_UTF8
89
83
    }
90
84
 
91
 
 
92
 
def match_utf8(encoding):
93
 
    return BOM_LIST.get(encoding.lower()) == 'utf_8'
94
 
 
95
 
 
96
 
# Quote strings used for writing values
97
 
squot = "'%s'"
98
 
dquot = '"%s"'
99
 
noquot = "%s"
100
 
wspace_plus = ' \r\t\n\v\t\'"'
101
 
tsquot = '"""%s"""'
102
 
tdquot = "'''%s'''"
 
85
try:
 
86
    from validate import VdtMissingValue
 
87
except ImportError:
 
88
    VdtMissingValue = None
103
89
 
104
90
try:
105
91
    enumerate
117
103
    True, False = 1, 0
118
104
 
119
105
 
120
 
__version__ = '4.5.2'
 
106
__version__ = '4.2.0beta2'
121
107
 
122
108
__revision__ = '$Id: configobj.py 156 2006-01-31 14:57:08Z fuzzyman $'
123
109
 
124
110
__docformat__ = "restructuredtext en"
125
111
 
 
112
# NOTE: Does it make sense to have the following in __all__ ?
 
113
# NOTE: DEFAULT_INDENT_TYPE, NUM_INDENT_SPACES, MAX_INTERPOL_DEPTH
 
114
# NOTE: If used as from configobj import...
 
115
# NOTE: They are effectively read only
126
116
__all__ = (
127
117
    '__version__',
128
118
    'DEFAULT_INDENT_TYPE',
129
 
    'DEFAULT_INTERPOLATION',
 
119
    'NUM_INDENT_SPACES',
 
120
    'MAX_INTERPOL_DEPTH',
130
121
    'ConfigObjError',
131
122
    'NestingError',
132
123
    'ParseError',
135
126
    'ConfigObj',
136
127
    'SimpleVal',
137
128
    'InterpolationError',
138
 
    'InterpolationLoopError',
 
129
    'InterpolationDepthError',
139
130
    'MissingInterpolationOption',
140
131
    'RepeatSectionError',
141
 
    'ReloadError',
142
 
    'UnreprError',
143
 
    'UnknownType',
144
132
    '__docformat__',
145
133
    'flatten_errors',
146
134
)
147
135
 
148
 
DEFAULT_INTERPOLATION = 'configparser'
149
 
DEFAULT_INDENT_TYPE = '    '
 
136
DEFAULT_INDENT_TYPE = ' '
 
137
NUM_INDENT_SPACES = 4
150
138
MAX_INTERPOL_DEPTH = 10
151
139
 
152
140
OPTION_DEFAULTS = {
161
149
    'indent_type': None,
162
150
    'encoding': None,
163
151
    'default_encoding': None,
164
 
    'unrepr': False,
165
 
    'write_empty_values': False,
166
152
}
167
153
 
168
 
 
169
 
 
170
 
def getObj(s):
171
 
    s = "a=" + s
172
 
    if compiler is None:
173
 
        raise ImportError('compiler module not available')
174
 
    p = compiler.parse(s)
175
 
    return p.getChildren()[1].getChildren()[0].getChildren()[1]
176
 
 
177
 
 
178
 
class UnknownType(Exception):
179
 
    pass
180
 
 
181
 
 
182
 
class Builder(object):
183
 
    
184
 
    def build(self, o):
185
 
        m = getattr(self, 'build_' + o.__class__.__name__, None)
186
 
        if m is None:
187
 
            raise UnknownType(o.__class__.__name__)
188
 
        return m(o)
189
 
    
190
 
    def build_List(self, o):
191
 
        return map(self.build, o.getChildren())
192
 
    
193
 
    def build_Const(self, o):
194
 
        return o.value
195
 
    
196
 
    def build_Dict(self, o):
197
 
        d = {}
198
 
        i = iter(map(self.build, o.getChildren()))
199
 
        for el in i:
200
 
            d[el] = i.next()
201
 
        return d
202
 
    
203
 
    def build_Tuple(self, o):
204
 
        return tuple(self.build_List(o))
205
 
    
206
 
    def build_Name(self, o):
207
 
        if o.name == 'None':
208
 
            return None
209
 
        if o.name == 'True':
210
 
            return True
211
 
        if o.name == 'False':
212
 
            return False
213
 
        
214
 
        # An undefined Name
215
 
        raise UnknownType('Undefined Name')
216
 
    
217
 
    def build_Add(self, o):
218
 
        real, imag = map(self.build_Const, o.getChildren())
219
 
        try:
220
 
            real = float(real)
221
 
        except TypeError:
222
 
            raise UnknownType('Add')
223
 
        if not isinstance(imag, complex) or imag.real != 0.0:
224
 
            raise UnknownType('Add')
225
 
        return real+imag
226
 
    
227
 
    def build_Getattr(self, o):
228
 
        parent = self.build(o.expr)
229
 
        return getattr(parent, o.attrname)
230
 
    
231
 
    def build_UnarySub(self, o):
232
 
        return -self.build_Const(o.getChildren()[0])
233
 
    
234
 
    def build_UnaryAdd(self, o):
235
 
        return self.build_Const(o.getChildren()[0])
236
 
 
237
 
 
238
 
_builder = Builder()
239
 
 
240
 
 
241
 
def unrepr(s):
242
 
    if not s:
243
 
        return s
244
 
    return _builder.build(getObj(s))
245
 
 
246
 
 
247
 
 
248
154
class ConfigObjError(SyntaxError):
249
155
    """
250
156
    This is the base class for all errors that ConfigObj raises.
251
157
    It is a subclass of SyntaxError.
 
158
    
 
159
    >>> raise ConfigObjError
 
160
    Traceback (most recent call last):
 
161
    ConfigObjError
252
162
    """
253
163
    def __init__(self, message='', line_number=None, line=''):
254
164
        self.line = line
256
166
        self.message = message
257
167
        SyntaxError.__init__(self, message)
258
168
 
259
 
 
260
169
class NestingError(ConfigObjError):
261
170
    """
262
171
    This error indicates a level of nesting that doesn't match.
 
172
    
 
173
    >>> raise NestingError
 
174
    Traceback (most recent call last):
 
175
    NestingError
263
176
    """
264
177
 
265
 
 
266
178
class ParseError(ConfigObjError):
267
179
    """
268
180
    This error indicates that a line is badly written.
269
181
    It is neither a valid ``key = value`` line,
270
182
    nor a valid section marker line.
271
 
    """
272
 
 
273
 
 
274
 
class ReloadError(IOError):
275
 
    """
276
 
    A 'reload' operation failed.
277
 
    This exception is a subclass of ``IOError``.
278
 
    """
279
 
    def __init__(self):
280
 
        IOError.__init__(self, 'reload failed, filename is not set.')
281
 
 
 
183
    
 
184
    >>> raise ParseError
 
185
    Traceback (most recent call last):
 
186
    ParseError
 
187
    """
282
188
 
283
189
class DuplicateError(ConfigObjError):
284
190
    """
285
191
    The keyword or section specified already exists.
 
192
    
 
193
    >>> raise DuplicateError
 
194
    Traceback (most recent call last):
 
195
    DuplicateError
286
196
    """
287
197
 
288
 
 
289
198
class ConfigspecError(ConfigObjError):
290
199
    """
291
200
    An error occured whilst parsing a configspec.
 
201
    
 
202
    >>> raise ConfigspecError
 
203
    Traceback (most recent call last):
 
204
    ConfigspecError
292
205
    """
293
206
 
294
 
 
295
207
class InterpolationError(ConfigObjError):
296
208
    """Base class for the two interpolation errors."""
297
209
 
298
 
 
299
 
class InterpolationLoopError(InterpolationError):
 
210
class InterpolationDepthError(InterpolationError):
300
211
    """Maximum interpolation depth exceeded in string interpolation."""
301
212
 
302
213
    def __init__(self, option):
 
214
        """
 
215
        >>> raise InterpolationDepthError('yoda')
 
216
        Traceback (most recent call last):
 
217
        InterpolationDepthError: max interpolation depth exceeded in value "yoda".
 
218
        """
303
219
        InterpolationError.__init__(
304
220
            self,
305
 
            'interpolation loop detected in value "%s".' % option)
306
 
 
 
221
            'max interpolation depth exceeded in value "%s".' % option)
307
222
 
308
223
class RepeatSectionError(ConfigObjError):
309
224
    """
310
225
    This error indicates additional sections in a section with a
311
226
    ``__many__`` (repeated) section.
 
227
    
 
228
    >>> raise RepeatSectionError
 
229
    Traceback (most recent call last):
 
230
    RepeatSectionError
312
231
    """
313
232
 
314
 
 
315
233
class MissingInterpolationOption(InterpolationError):
316
234
    """A value specified for interpolation was missing."""
317
235
 
318
236
    def __init__(self, option):
 
237
        """
 
238
        >>> raise MissingInterpolationOption('yoda')
 
239
        Traceback (most recent call last):
 
240
        MissingInterpolationOption: missing option "yoda" in interpolation.
 
241
        """
319
242
        InterpolationError.__init__(
320
243
            self,
321
244
            'missing option "%s" in interpolation.' % option)
322
245
 
323
 
 
324
 
class UnreprError(ConfigObjError):
325
 
    """An error parsing in unrepr mode."""
326
 
 
327
 
 
328
 
 
329
 
class InterpolationEngine(object):
330
 
    """
331
 
    A helper class to help perform string interpolation.
332
 
 
333
 
    This class is an abstract base class; its descendants perform
334
 
    the actual work.
335
 
    """
336
 
 
337
 
    # compiled regexp to use in self.interpolate()
338
 
    _KEYCRE = re.compile(r"%\(([^)]*)\)s")
339
 
 
340
 
    def __init__(self, section):
341
 
        # the Section instance that "owns" this engine
342
 
        self.section = section
343
 
 
344
 
 
345
 
    def interpolate(self, key, value):
346
 
        def recursive_interpolate(key, value, section, backtrail):
347
 
            """The function that does the actual work.
348
 
 
349
 
            ``value``: the string we're trying to interpolate.
350
 
            ``section``: the section in which that string was found
351
 
            ``backtrail``: a dict to keep track of where we've been,
352
 
            to detect and prevent infinite recursion loops
353
 
 
354
 
            This is similar to a depth-first-search algorithm.
355
 
            """
356
 
            # Have we been here already?
357
 
            if backtrail.has_key((key, section.name)):
358
 
                # Yes - infinite loop detected
359
 
                raise InterpolationLoopError(key)
360
 
            # Place a marker on our backtrail so we won't come back here again
361
 
            backtrail[(key, section.name)] = 1
362
 
 
363
 
            # Now start the actual work
364
 
            match = self._KEYCRE.search(value)
365
 
            while match:
366
 
                # The actual parsing of the match is implementation-dependent,
367
 
                # so delegate to our helper function
368
 
                k, v, s = self._parse_match(match)
369
 
                if k is None:
370
 
                    # That's the signal that no further interpolation is needed
371
 
                    replacement = v
372
 
                else:
373
 
                    # Further interpolation may be needed to obtain final value
374
 
                    replacement = recursive_interpolate(k, v, s, backtrail)
375
 
                # Replace the matched string with its final value
376
 
                start, end = match.span()
377
 
                value = ''.join((value[:start], replacement, value[end:]))
378
 
                new_search_start = start + len(replacement)
379
 
                # Pick up the next interpolation key, if any, for next time
380
 
                # through the while loop
381
 
                match = self._KEYCRE.search(value, new_search_start)
382
 
 
383
 
            # Now safe to come back here again; remove marker from backtrail
384
 
            del backtrail[(key, section.name)]
385
 
 
386
 
            return value
387
 
 
388
 
        # Back in interpolate(), all we have to do is kick off the recursive
389
 
        # function with appropriate starting values
390
 
        value = recursive_interpolate(key, value, self.section, {})
391
 
        return value
392
 
 
393
 
 
394
 
    def _fetch(self, key):
395
 
        """Helper function to fetch values from owning section.
396
 
 
397
 
        Returns a 2-tuple: the value, and the section where it was found.
398
 
        """
399
 
        # switch off interpolation before we try and fetch anything !
400
 
        save_interp = self.section.main.interpolation
401
 
        self.section.main.interpolation = False
402
 
 
403
 
        # Start at section that "owns" this InterpolationEngine
404
 
        current_section = self.section
405
 
        while True:
406
 
            # try the current section first
407
 
            val = current_section.get(key)
408
 
            if val is not None:
409
 
                break
410
 
            # try "DEFAULT" next
411
 
            val = current_section.get('DEFAULT', {}).get(key)
412
 
            if val is not None:
413
 
                break
414
 
            # move up to parent and try again
415
 
            # top-level's parent is itself
416
 
            if current_section.parent is current_section:
417
 
                # reached top level, time to give up
418
 
                break
419
 
            current_section = current_section.parent
420
 
 
421
 
        # restore interpolation to previous value before returning
422
 
        self.section.main.interpolation = save_interp
423
 
        if val is None:
424
 
            raise MissingInterpolationOption(key)
425
 
        return val, current_section
426
 
 
427
 
 
428
 
    def _parse_match(self, match):
429
 
        """Implementation-dependent helper function.
430
 
 
431
 
        Will be passed a match object corresponding to the interpolation
432
 
        key we just found (e.g., "%(foo)s" or "$foo"). Should look up that
433
 
        key in the appropriate config file section (using the ``_fetch()``
434
 
        helper function) and return a 3-tuple: (key, value, section)
435
 
 
436
 
        ``key`` is the name of the key we're looking for
437
 
        ``value`` is the value found for that key
438
 
        ``section`` is a reference to the section where it was found
439
 
 
440
 
        ``key`` and ``section`` should be None if no further
441
 
        interpolation should be performed on the resulting value
442
 
        (e.g., if we interpolated "$$" and returned "$").
443
 
        """
444
 
        raise NotImplementedError()
445
 
    
446
 
 
447
 
 
448
 
class ConfigParserInterpolation(InterpolationEngine):
449
 
    """Behaves like ConfigParser."""
450
 
    _KEYCRE = re.compile(r"%\(([^)]*)\)s")
451
 
 
452
 
    def _parse_match(self, match):
453
 
        key = match.group(1)
454
 
        value, section = self._fetch(key)
455
 
        return key, value, section
456
 
 
457
 
 
458
 
 
459
 
class TemplateInterpolation(InterpolationEngine):
460
 
    """Behaves like string.Template."""
461
 
    _delimiter = '$'
462
 
    _KEYCRE = re.compile(r"""
463
 
        \$(?:
464
 
          (?P<escaped>\$)              |   # Two $ signs
465
 
          (?P<named>[_a-z][_a-z0-9]*)  |   # $name format
466
 
          {(?P<braced>[^}]*)}              # ${name} format
467
 
        )
468
 
        """, re.IGNORECASE | re.VERBOSE)
469
 
 
470
 
    def _parse_match(self, match):
471
 
        # Valid name (in or out of braces): fetch value from section
472
 
        key = match.group('named') or match.group('braced')
473
 
        if key is not None:
474
 
            value, section = self._fetch(key)
475
 
            return key, value, section
476
 
        # Escaped delimiter (e.g., $$): return single delimiter
477
 
        if match.group('escaped') is not None:
478
 
            # Return None for key and section to indicate it's time to stop
479
 
            return None, self._delimiter, None
480
 
        # Anything else: ignore completely, just return it unchanged
481
 
        return None, match.group(), None
482
 
 
483
 
 
484
 
interpolation_engines = {
485
 
    'configparser': ConfigParserInterpolation,
486
 
    'template': TemplateInterpolation,
487
 
}
488
 
 
489
 
 
490
 
 
491
246
class Section(dict):
492
247
    """
493
248
    A dictionary-like object that represents a section in a config file.
494
249
    
495
 
    It does string interpolation if the 'interpolation' attribute
 
250
    It does string interpolation if the 'interpolate' attribute
496
251
    of the 'main' object is set to True.
497
252
    
498
 
    Interpolation is tried first from this object, then from the 'DEFAULT'
499
 
    section of this object, next from the parent and its 'DEFAULT' section,
500
 
    and so on until the main object is reached.
 
253
    Interpolation is tried first from the 'DEFAULT' section of this object,
 
254
    next from the 'DEFAULT' section of the parent, lastly the main object.
501
255
    
502
256
    A Section will behave like an ordered dictionary - following the
503
257
    order of the ``scalars`` and ``sections`` attributes.
506
260
    Iteration follows the order: scalars, then sections.
507
261
    """
508
262
 
 
263
    _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
 
264
 
509
265
    def __init__(self, parent, depth, main, indict=None, name=None):
510
266
        """
511
267
        * parent is the section above
522
278
        self.main = main
523
279
        # level of nesting depth of this Section
524
280
        self.depth = depth
 
281
        # the sequence of scalar values in this Section
 
282
        self.scalars = []
 
283
        # the sequence of sections in this Section
 
284
        self.sections = []
525
285
        # purely for information
526
286
        self.name = name
527
 
        #
528
 
        self._initialise()
529
 
        # we do this explicitly so that __setitem__ is used properly
530
 
        # (rather than just passing to ``dict.__init__``)
531
 
        for entry, value in indict.iteritems():
532
 
            self[entry] = value
533
 
            
534
 
            
535
 
    def _initialise(self):
536
 
        # the sequence of scalar values in this Section
537
 
        self.scalars = []
538
 
        # the sequence of sections in this Section
539
 
        self.sections = []
540
287
        # for comments :-)
541
288
        self.comments = {}
542
289
        self.inline_comments = {}
543
290
        # for the configspec
544
291
        self.configspec = {}
545
 
        self._order = []
546
 
        self._configspec_comments = {}
547
 
        self._configspec_inline_comments = {}
548
 
        self._cs_section_comments = {}
549
 
        self._cs_section_inline_comments = {}
550
292
        # for defaults
551
293
        self.defaults = []
552
 
        self.default_values = {}
553
 
 
554
 
 
555
 
    def _interpolate(self, key, value):
556
 
        try:
557
 
            # do we already have an interpolation engine?
558
 
            engine = self._interpolation_engine
559
 
        except AttributeError:
560
 
            # not yet: first time running _interpolate(), so pick the engine
561
 
            name = self.main.interpolation
562
 
            if name == True:  # note that "if name:" would be incorrect here
563
 
                # backwards-compatibility: interpolation=True means use default
564
 
                name = DEFAULT_INTERPOLATION
565
 
            name = name.lower()  # so that "Template", "template", etc. all work
566
 
            class_ = interpolation_engines.get(name, None)
567
 
            if class_ is None:
568
 
                # invalid value for self.main.interpolation
569
 
                self.main.interpolation = False
570
 
                return value
 
294
        #
 
295
        # we do this explicitly so that __setitem__ is used properly
 
296
        # (rather than just passing to ``dict.__init__``)
 
297
        for entry in indict:
 
298
            self[entry] = indict[entry]
 
299
 
 
300
    def _interpolate(self, value):
 
301
        """Nicked from ConfigParser."""
 
302
        depth = MAX_INTERPOL_DEPTH
 
303
        # loop through this until it's done
 
304
        while depth:
 
305
            depth -= 1
 
306
            if value.find("%(") != -1:
 
307
                value = self._KEYCRE.sub(self._interpolation_replace, value)
571
308
            else:
572
 
                # save reference to engine so we don't have to do this again
573
 
                engine = self._interpolation_engine = class_(self)
574
 
        # let the engine do the actual work
575
 
        return engine.interpolate(key, value)
 
309
                break
 
310
        else:
 
311
            raise InterpolationDepthError(value)
 
312
        return value
576
313
 
 
314
    def _interpolation_replace(self, match):
 
315
        """ """
 
316
        s = match.group(1)
 
317
        if s is None:
 
318
            return match.group()
 
319
        else:
 
320
            # switch off interpolation before we try and fetch anything !
 
321
            self.main.interpolation = False
 
322
            # try the 'DEFAULT' member of *this section* first
 
323
            val = self.get('DEFAULT', {}).get(s)
 
324
            # try the 'DEFAULT' member of the *parent section* next
 
325
            if val is None:
 
326
                val = self.parent.get('DEFAULT', {}).get(s)
 
327
            # last, try the 'DEFAULT' member of the *main section*
 
328
            if val is None:
 
329
                val = self.main.get('DEFAULT', {}).get(s)
 
330
            self.main.interpolation = True
 
331
            if val is None:
 
332
                raise MissingInterpolationOption(s)
 
333
            return val
577
334
 
578
335
    def __getitem__(self, key):
579
336
        """Fetch the item and do string interpolation."""
580
337
        val = dict.__getitem__(self, key)
581
338
        if self.main.interpolation and isinstance(val, StringTypes):
582
 
            return self._interpolate(key, val)
 
339
            return self._interpolate(val)
583
340
        return val
584
341
 
585
 
 
586
 
    def __setitem__(self, key, value, unrepr=False):
 
342
    def __setitem__(self, key, value):
587
343
        """
588
344
        Correctly set a value.
589
345
        
593
349
        Keys must be strings.
594
350
        Values need only be strings (or lists of strings) if
595
351
        ``main.stringify`` is set.
596
 
        
597
 
        `unrepr`` must be set when setting a value to a dictionary, without
598
 
        creating a new sub-section.
599
352
        """
600
353
        if not isinstance(key, StringTypes):
601
 
            raise ValueError('The key "%s" is not a string.' % key)
602
 
        
 
354
            raise ValueError, 'The key "%s" is not a string.' % key
603
355
        # add the comment
604
 
        if not self.comments.has_key(key):
 
356
        if key not in self.comments:
605
357
            self.comments[key] = []
606
358
            self.inline_comments[key] = ''
607
359
        # remove the entry from defaults
609
361
            self.defaults.remove(key)
610
362
        #
611
363
        if isinstance(value, Section):
612
 
            if not self.has_key(key):
 
364
            if key not in self:
613
365
                self.sections.append(key)
614
366
            dict.__setitem__(self, key, value)
615
 
        elif isinstance(value, dict) and not unrepr:
 
367
        elif isinstance(value, dict):
616
368
            # First create the new depth level,
617
369
            # then create the section
618
 
            if not self.has_key(key):
 
370
            if key not in self:
619
371
                self.sections.append(key)
620
372
            new_depth = self.depth + 1
621
373
            dict.__setitem__(
628
380
                    indict=value,
629
381
                    name=key))
630
382
        else:
631
 
            if not self.has_key(key):
 
383
            if key not in self:
632
384
                self.scalars.append(key)
633
385
            if not self.main.stringify:
634
386
                if isinstance(value, StringTypes):
636
388
                elif isinstance(value, (list, tuple)):
637
389
                    for entry in value:
638
390
                        if not isinstance(entry, StringTypes):
639
 
                            raise TypeError('Value is not a string "%s".' % entry)
 
391
                            raise TypeError, (
 
392
                                'Value is not a string "%s".' % entry)
640
393
                else:
641
 
                    raise TypeError('Value is not a string "%s".' % value)
 
394
                    raise TypeError, 'Value is not a string "%s".' % value
642
395
            dict.__setitem__(self, key, value)
643
396
 
644
 
 
645
397
    def __delitem__(self, key):
646
398
        """Remove items from the sequence when deleting."""
647
399
        dict. __delitem__(self, key)
652
404
        del self.comments[key]
653
405
        del self.inline_comments[key]
654
406
 
655
 
 
656
407
    def get(self, key, default=None):
657
408
        """A version of ``get`` that doesn't bypass string interpolation."""
658
409
        try:
660
411
        except KeyError:
661
412
            return default
662
413
 
663
 
 
664
414
    def update(self, indict):
665
415
        """
666
416
        A version of update that uses our ``__setitem__``.
670
420
 
671
421
 
672
422
    def pop(self, key, *args):
673
 
        """
674
 
        'D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
675
 
        If key is not found, d is returned if given, otherwise KeyError is raised'
676
 
        """
 
423
        """ """
677
424
        val = dict.pop(self, key, *args)
678
425
        if key in self.scalars:
679
426
            del self.comments[key]
684
431
            del self.inline_comments[key]
685
432
            self.sections.remove(key)
686
433
        if self.main.interpolation and isinstance(val, StringTypes):
687
 
            return self._interpolate(key, val)
 
434
            return self._interpolate(val)
688
435
        return val
689
436
 
690
 
 
691
437
    def popitem(self):
692
438
        """Pops the first (key,val)"""
693
439
        sequence = (self.scalars + self.sections)
694
440
        if not sequence:
695
 
            raise KeyError(": 'popitem(): dictionary is empty'")
 
441
            raise KeyError, ": 'popitem(): dictionary is empty'"
696
442
        key = sequence[0]
697
443
        val =  self[key]
698
444
        del self[key]
699
445
        return key, val
700
446
 
701
 
 
702
447
    def clear(self):
703
448
        """
704
449
        A version of clear that also affects scalars/sections
714
459
        self.inline_comments = {}
715
460
        self.configspec = {}
716
461
 
717
 
 
718
462
    def setdefault(self, key, default=None):
719
463
        """A version of setdefault that sets sequence if appropriate."""
720
464
        try:
723
467
            self[key] = default
724
468
            return self[key]
725
469
 
726
 
 
727
470
    def items(self):
728
 
        """D.items() -> list of D's (key, value) pairs, as 2-tuples"""
 
471
        """ """
729
472
        return zip((self.scalars + self.sections), self.values())
730
473
 
731
 
 
732
474
    def keys(self):
733
 
        """D.keys() -> list of D's keys"""
 
475
        """ """
734
476
        return (self.scalars + self.sections)
735
477
 
736
 
 
737
478
    def values(self):
738
 
        """D.values() -> list of D's values"""
 
479
        """ """
739
480
        return [self[key] for key in (self.scalars + self.sections)]
740
481
 
741
 
 
742
482
    def iteritems(self):
743
 
        """D.iteritems() -> an iterator over the (key, value) items of D"""
 
483
        """ """
744
484
        return iter(self.items())
745
485
 
746
 
 
747
486
    def iterkeys(self):
748
 
        """D.iterkeys() -> an iterator over the keys of D"""
 
487
        """ """
749
488
        return iter((self.scalars + self.sections))
750
489
 
751
490
    __iter__ = iterkeys
752
491
 
753
 
 
754
492
    def itervalues(self):
755
 
        """D.itervalues() -> an iterator over the values of D"""
 
493
        """ """
756
494
        return iter(self.values())
757
495
 
758
 
 
759
496
    def __repr__(self):
760
 
        """x.__repr__() <==> repr(x)"""
761
497
        return '{%s}' % ', '.join([('%s: %s' % (repr(key), repr(self[key])))
762
498
            for key in (self.scalars + self.sections)])
763
499
 
764
500
    __str__ = __repr__
765
 
    __str__.__doc__ = "x.__str__() <==> str(x)"
766
 
 
767
501
 
768
502
    # Extra methods - not in a normal dictionary
769
503
 
785
519
            this_entry = self[entry]
786
520
            if isinstance(this_entry, Section):
787
521
                this_entry = this_entry.dict()
788
 
            elif isinstance(this_entry, list):
 
522
            elif isinstance(this_entry, (list, tuple)):
789
523
                # create a copy rather than a reference
790
524
                this_entry = list(this_entry)
791
 
            elif isinstance(this_entry, tuple):
792
 
                # create a copy rather than a reference
793
 
                this_entry = tuple(this_entry)
794
525
            newdict[entry] = this_entry
795
526
        return newdict
796
527
 
797
 
 
798
528
    def merge(self, indict):
799
529
        """
800
530
        A recursive update - useful for merging config files.
821
551
            else:   
822
552
                self[key] = val
823
553
 
824
 
 
825
554
    def rename(self, oldkey, newkey):
826
555
        """
827
556
        Change a keyname to another, without changing position in sequence.
836
565
        elif oldkey in self.sections:
837
566
            the_list = self.sections
838
567
        else:
839
 
            raise KeyError('Key "%s" not found.' % oldkey)
 
568
            raise KeyError, 'Key "%s" not found.' % oldkey
840
569
        pos = the_list.index(oldkey)
841
570
        #
842
571
        val = self[oldkey]
851
580
        self.comments[newkey] = comm
852
581
        self.inline_comments[newkey] = inline_comment
853
582
 
854
 
 
855
583
    def walk(self, function, raise_errors=True,
856
584
            call_on_sections=False, **keywargs):
857
585
        """
936
664
                **keywargs)
937
665
        return out
938
666
 
939
 
 
940
667
    def decode(self, encoding):
941
668
        """
942
669
        Decode all strings and values to unicode, using the specified encoding.
951
678
        >>> def testuni(val):
952
679
        ...     for entry in val:
953
680
        ...         if not isinstance(entry, unicode):
954
 
        ...             print >> sys.stderr, type(entry)
 
681
        ...             sys.stderr.write(type(entry))
 
682
        ...             sys.stderr.write('\n')
955
683
        ...             raise AssertionError, 'decode failed.'
956
684
        ...         if isinstance(val[entry], dict):
957
685
        ...             testuni(val[entry])
962
690
        >>> a == m
963
691
        1
964
692
        """
965
 
        warn('use of ``decode`` is deprecated.', DeprecationWarning)
966
 
        def decode(section, key, encoding=encoding, warn=True):
 
693
        def decode(section, key, encoding=encoding):
967
694
            """ """
968
695
            val = section[key]
969
696
            if isinstance(val, (list, tuple)):
980
707
        # using ``call_on_sections`` allows us to modify section names
981
708
        self.walk(decode, call_on_sections=True)
982
709
 
983
 
 
984
710
    def encode(self, encoding):
985
711
        """
986
712
        Encode all strings and values from unicode,
989
715
        Works with subsections and list values.
990
716
        Uses the ``walk`` method.
991
717
        """
992
 
        warn('use of ``encode`` is deprecated.', DeprecationWarning)
993
718
        def encode(section, key, encoding=encoding):
994
719
            """ """
995
720
            val = section[key]
1006
731
            section[newkey] = newval
1007
732
        self.walk(encode, call_on_sections=True)
1008
733
 
1009
 
 
1010
734
    def istrue(self, key):
1011
735
        """A deprecated version of ``as_bool``."""
1012
736
        warn('use of ``istrue`` is deprecated. Use ``as_bool`` method '
1013
737
                'instead.', DeprecationWarning)
1014
738
        return self.as_bool(key)
1015
739
 
1016
 
 
1017
740
    def as_bool(self, key):
1018
741
        """
1019
742
        Accepts a key as input. The corresponding value must be a string or
1050
773
        else:
1051
774
            try:
1052
775
                if not isinstance(val, StringTypes):
1053
 
                    # TODO: Why do we raise a KeyError here?
1054
 
                    raise KeyError()
 
776
                    raise KeyError
1055
777
                else:
1056
778
                    return self.main._bools[val.lower()]
1057
779
            except KeyError:
1058
780
                raise ValueError('Value "%s" is neither True nor False' % val)
1059
781
 
1060
 
 
1061
782
    def as_int(self, key):
1062
783
        """
1063
784
        A convenience method which coerces the specified value to an integer.
1080
801
        """
1081
802
        return int(self[key])
1082
803
 
1083
 
 
1084
804
    def as_float(self, key):
1085
805
        """
1086
806
        A convenience method which coerces the specified value to a float.
1101
821
        3.2000000000000002
1102
822
        """
1103
823
        return float(self[key])
1104
 
 
1105
 
 
1106
 
    def restore_default(self, key):
1107
 
        """
1108
 
        Restore (and return) default value for the specified key.
1109
 
        
1110
 
        This method will only work for a ConfigObj that was created
1111
 
        with a configspec and has been validated.
1112
 
        
1113
 
        If there is no default value for this key, ``KeyError`` is raised.
1114
 
        """
1115
 
        default = self.default_values[key]
1116
 
        dict.__setitem__(self, key, default)
1117
 
        if key not in self.defaults:
1118
 
            self.defaults.append(key)
1119
 
        return default
1120
 
 
1121
824
    
1122
 
    def restore_defaults(self):
1123
 
        """
1124
 
        Recursively restore default values to all members
1125
 
        that have them.
1126
 
        
1127
 
        This method will only work for a ConfigObj that was created
1128
 
        with a configspec and has been validated.
1129
 
        
1130
 
        It doesn't delete or modify entries without default values.
1131
 
        """
1132
 
        for key in self.default_values:
1133
 
            self.restore_default(key)
1134
 
            
1135
 
        for section in self.sections:
1136
 
            self[section].restore_defaults()
1137
 
 
1138
825
 
1139
826
class ConfigObj(Section):
1140
 
    """An object to read, create, and write config files."""
 
827
    """
 
828
    An object to read, create, and write config files.
 
829
    
 
830
    Testing with duplicate keys and sections.
 
831
    
 
832
    >>> c = '''
 
833
    ... [hello]
 
834
    ... member = value
 
835
    ... [hello again]
 
836
    ... member = value
 
837
    ... [ "hello" ]
 
838
    ... member = value
 
839
    ... '''
 
840
    >>> ConfigObj(c.split('\\n'), raise_errors = True)
 
841
    Traceback (most recent call last):
 
842
    DuplicateError: Duplicate section name at line 5.
 
843
    
 
844
    >>> d = '''
 
845
    ... [hello]
 
846
    ... member = value
 
847
    ... [hello again]
 
848
    ... member1 = value
 
849
    ... member2 = value
 
850
    ... 'member1' = value
 
851
    ... [ "and again" ]
 
852
    ... member = value
 
853
    ... '''
 
854
    >>> ConfigObj(d.split('\\n'), raise_errors = True)
 
855
    Traceback (most recent call last):
 
856
    DuplicateError: Duplicate keyword name at line 6.
 
857
    """
1141
858
 
1142
859
    _keyword = re.compile(r'''^ # line start
1143
860
        (\s*)                   # indentation
1167
884
 
1168
885
    # this regexp pulls list values out as a single string
1169
886
    # or single values and comments
1170
 
    # FIXME: this regex adds a '' to the end of comma terminated lists
1171
 
    #   workaround in ``_handle_value``
1172
887
    _valueexp = re.compile(r'''^
1173
888
        (?:
1174
889
            (?:
1177
892
                        (?:
1178
893
                            (?:".*?")|              # double quotes
1179
894
                            (?:'.*?')|              # single quotes
1180
 
                            (?:[^'",\#][^,\#]*?)    # unquoted
 
895
                            (?:[^'",\#][^,\#]*?)       # unquoted
1181
896
                        )
1182
897
                        \s*,\s*                     # comma
1183
898
                    )*      # match all list items ending in a comma (if any)
1185
900
                (
1186
901
                    (?:".*?")|                      # double quotes
1187
902
                    (?:'.*?')|                      # single quotes
1188
 
                    (?:[^'",\#\s][^,]*?)|           # unquoted
1189
 
                    (?:(?<!,))                      # Empty value
 
903
                    (?:[^'",\#\s][^,]*?)             # unquoted
1190
904
                )?          # last item in a list - or string value
1191
905
            )|
1192
906
            (,)             # alternatively a single comma - empty list
1212
926
        (
1213
927
            (?:".*?")|          # double quotes
1214
928
            (?:'.*?')|          # single quotes
1215
 
            (?:[^'"\#].*?)|     # unquoted
1216
 
            (?:)                # Empty value
 
929
            (?:[^'"\#].*?)      # unquoted
1217
930
        )
1218
931
        \s*(\#.*)?              # optional comment
1219
932
        $''',
1238
951
        'true': True, 'false': False,
1239
952
        }
1240
953
 
1241
 
 
1242
954
    def __init__(self, infile=None, options=None, **kwargs):
1243
955
        """
1244
 
        Parse a config file or create a config file object.
 
956
        Parse or create a config file object.
1245
957
        
1246
958
        ``ConfigObj(infile=None, options=None, **kwargs)``
1247
959
        """
1248
 
        # init the superclass
1249
 
        Section.__init__(self, self, 0, self)
1250
 
        
1251
960
        if infile is None:
1252
961
            infile = []
1253
962
        if options is None:
1254
963
            options = {}
1255
 
        else:
1256
 
            options = dict(options)
1257
 
            
1258
964
        # keyword arguments take precedence over an options dictionary
1259
965
        options.update(kwargs)
1260
 
        
 
966
        # init the superclass
 
967
        Section.__init__(self, self, 0, self)
 
968
        #
1261
969
        defaults = OPTION_DEFAULTS.copy()
 
970
        for entry in options.keys():
 
971
            if entry not in defaults.keys():
 
972
                raise TypeError, 'Unrecognised option "%s".' % entry
1262
973
        # TODO: check the values too.
1263
 
        for entry in options:
1264
 
            if entry not in defaults:
1265
 
                raise TypeError('Unrecognised option "%s".' % entry)
1266
 
        
 
974
        #
1267
975
        # Add any explicit options to the defaults
1268
976
        defaults.update(options)
1269
 
        self._initialise(defaults)
1270
 
        configspec = defaults['configspec']
1271
 
        self._original_configspec = configspec
1272
 
        self._load(infile, configspec)
1273
 
        
1274
 
        
1275
 
    def _load(self, infile, configspec):
 
977
        #
 
978
        # initialise a few variables
 
979
        self.filename = None
 
980
        self._errors = []
 
981
        self.raise_errors = defaults['raise_errors']
 
982
        self.interpolation = defaults['interpolation']
 
983
        self.list_values = defaults['list_values']
 
984
        self.create_empty = defaults['create_empty']
 
985
        self.file_error = defaults['file_error']
 
986
        self.stringify = defaults['stringify']
 
987
        self.indent_type = defaults['indent_type']
 
988
        self.encoding = defaults['encoding']
 
989
        self.default_encoding = defaults['default_encoding']
 
990
        self.BOM = False
 
991
        self.newlines = None
 
992
        #
 
993
        self.initial_comment = []
 
994
        self.final_comment = []
 
995
        #
1276
996
        if isinstance(infile, StringTypes):
1277
997
            self.filename = infile
1278
998
            if os.path.isfile(infile):
1279
 
                h = open(infile, 'rb')
1280
 
                infile = h.read() or []
1281
 
                h.close()
 
999
                infile = open(infile).read() or []
1282
1000
            elif self.file_error:
1283
1001
                # raise an error if the file doesn't exist
1284
 
                raise IOError('Config file not found: "%s".' % self.filename)
 
1002
                raise IOError, 'Config file not found: "%s".' % self.filename
1285
1003
            else:
1286
1004
                # file doesn't already exist
1287
1005
                if self.create_empty:
1288
1006
                    # this is a good test that the filename specified
1289
 
                    # isn't impossible - like on a non-existent device
 
1007
                    # isn't impossible - like on a non existent device
1290
1008
                    h = open(infile, 'w')
1291
1009
                    h.write('')
1292
1010
                    h.close()
1293
1011
                infile = []
1294
 
                
1295
1012
        elif isinstance(infile, (list, tuple)):
1296
1013
            infile = list(infile)
1297
 
            
1298
1014
        elif isinstance(infile, dict):
1299
1015
            # initialise self
1300
1016
            # the Section class handles creating subsections
1301
1017
            if isinstance(infile, ConfigObj):
1302
1018
                # get a copy of our ConfigObj
1303
1019
                infile = infile.dict()
1304
 
                
1305
1020
            for entry in infile:
1306
1021
                self[entry] = infile[entry]
1307
1022
            del self._errors
1308
 
            
1309
 
            if configspec is not None:
1310
 
                self._handle_configspec(configspec)
 
1023
            if defaults['configspec'] is not None:
 
1024
                self._handle_configspec(defaults['configspec'])
1311
1025
            else:
1312
1026
                self.configspec = None
1313
1027
            return
1314
 
        
1315
1028
        elif getattr(infile, 'read', None) is not None:
1316
1029
            # This supports file like objects
1317
1030
            infile = infile.read() or []
1318
1031
            # needs splitting into lines - but needs doing *after* decoding
1319
1032
            # in case it's not an 8 bit encoding
1320
1033
        else:
1321
 
            raise TypeError('infile must be a filename, file like object, or list of lines.')
1322
 
        
 
1034
            raise TypeError, ('infile must be a filename,'
 
1035
                ' file like object, or list of lines.')
 
1036
        #
1323
1037
        if infile:
1324
1038
            # don't do it for the empty ConfigObj
1325
1039
            infile = self._handle_bom(infile)
1328
1042
            # Set the newlines attribute (first line ending it finds)
1329
1043
            # and strip trailing '\n' or '\r' from lines
1330
1044
            for line in infile:
1331
 
                if (not line) or (line[-1] not in ('\r', '\n', '\r\n')):
 
1045
                if (not line) or (line[-1] not in '\r\n'):
1332
1046
                    continue
1333
1047
                for end in ('\r\n', '\n', '\r'):
1334
1048
                    if line.endswith(end):
1335
1049
                        self.newlines = end
1336
1050
                        break
1337
1051
                break
1338
 
 
1339
1052
            infile = [line.rstrip('\r\n') for line in infile]
1340
 
            
 
1053
        #
1341
1054
        self._parse(infile)
1342
1055
        # if we had any errors, now is the time to raise them
1343
1056
        if self._errors:
1344
 
            info = "at line %s." % self._errors[0].line_number
1345
 
            if len(self._errors) > 1:
1346
 
                msg = "Parsing failed with several errors.\nFirst error %s" % info
1347
 
                error = ConfigObjError(msg)
1348
 
            else:
1349
 
                error = self._errors[0]
 
1057
            error = ConfigObjError("Parsing failed.")
1350
1058
            # set the errors attribute; it's a list of tuples:
1351
1059
            # (error_type, message, line_number)
1352
1060
            error.errors = self._errors
1355
1063
            raise error
1356
1064
        # delete private attributes
1357
1065
        del self._errors
1358
 
        
1359
 
        if configspec is None:
 
1066
        #
 
1067
        if defaults['configspec'] is None:
1360
1068
            self.configspec = None
1361
1069
        else:
1362
 
            self._handle_configspec(configspec)
1363
 
    
1364
 
    
1365
 
    def _initialise(self, options=None):
1366
 
        if options is None:
1367
 
            options = OPTION_DEFAULTS
1368
 
            
1369
 
        # initialise a few variables
1370
 
        self.filename = None
1371
 
        self._errors = []
1372
 
        self.raise_errors = options['raise_errors']
1373
 
        self.interpolation = options['interpolation']
1374
 
        self.list_values = options['list_values']
1375
 
        self.create_empty = options['create_empty']
1376
 
        self.file_error = options['file_error']
1377
 
        self.stringify = options['stringify']
1378
 
        self.indent_type = options['indent_type']
1379
 
        self.encoding = options['encoding']
1380
 
        self.default_encoding = options['default_encoding']
1381
 
        self.BOM = False
1382
 
        self.newlines = None
1383
 
        self.write_empty_values = options['write_empty_values']
1384
 
        self.unrepr = options['unrepr']
1385
 
        
1386
 
        self.initial_comment = []
1387
 
        self.final_comment = []
1388
 
        self.configspec = {}
1389
 
        
1390
 
        # Clear section attributes as well
1391
 
        Section._initialise(self)
1392
 
        
1393
 
        
1394
 
    def __repr__(self):
1395
 
        return ('ConfigObj({%s})' % 
1396
 
                ', '.join([('%s: %s' % (repr(key), repr(self[key]))) 
1397
 
                for key in (self.scalars + self.sections)]))
1398
 
    
1399
 
    
 
1070
            self._handle_configspec(defaults['configspec'])
 
1071
 
1400
1072
    def _handle_bom(self, infile):
1401
1073
        """
1402
1074
        Handle any BOM, and decode if necessary.
1422
1094
        if ((self.encoding is not None) and
1423
1095
            (self.encoding.lower() not in BOM_LIST)):
1424
1096
            # No need to check for a BOM
1425
 
            # the encoding specified doesn't have one
 
1097
            # encoding specified doesn't have one
1426
1098
            # just decode
1427
1099
            return self._decode(infile, self.encoding)
1428
 
        
 
1100
        #
1429
1101
        if isinstance(infile, (list, tuple)):
1430
1102
            line = infile[0]
1431
1103
        else:
1447
1119
                        ##self.BOM = True
1448
1120
                        # Don't need to remove BOM
1449
1121
                        return self._decode(infile, encoding)
1450
 
                    
 
1122
                #
1451
1123
                # If we get this far, will *probably* raise a DecodeError
1452
1124
                # As it doesn't appear to start with a BOM
1453
1125
                return self._decode(infile, self.encoding)
1454
 
            
 
1126
            #
1455
1127
            # Must be UTF8
1456
1128
            BOM = BOM_SET[enc]
1457
1129
            if not line.startswith(BOM):
1458
1130
                return self._decode(infile, self.encoding)
1459
 
            
 
1131
            #
1460
1132
            newline = line[len(BOM):]
1461
 
            
 
1133
            #
1462
1134
            # BOM removed
1463
1135
            if isinstance(infile, (list, tuple)):
1464
1136
                infile[0] = newline
1466
1138
                infile = newline
1467
1139
            self.BOM = True
1468
1140
            return self._decode(infile, self.encoding)
1469
 
        
 
1141
        #
1470
1142
        # No encoding specified - so we need to check for UTF8/UTF16
1471
1143
        for BOM, (encoding, final_encoding) in BOMS.items():
1472
1144
            if not line.startswith(BOM):
1490
1162
                        return infile
1491
1163
                # UTF16 - have to decode
1492
1164
                return self._decode(infile, encoding)
1493
 
            
 
1165
        #
1494
1166
        # No BOM discovered and no encoding specified, just return
1495
1167
        if isinstance(infile, StringTypes):
1496
1168
            # infile read from a file will be a single string
1497
1169
            return infile.splitlines(True)
1498
 
        return infile
1499
 
 
1500
 
 
1501
 
    def _a_to_u(self, aString):
1502
 
        """Decode ASCII strings to unicode if a self.encoding is specified."""
1503
 
        if self.encoding:
1504
 
            return aString.decode('ascii')
1505
 
        else:
1506
 
            return aString
1507
 
 
 
1170
        else:
 
1171
            return infile
 
1172
 
 
1173
    def _a_to_u(self, string):
 
1174
        """Decode ascii strings to unicode if a self.encoding is specified."""
 
1175
        if not self.encoding:
 
1176
            return string
 
1177
        else:
 
1178
            return string.decode('ascii')
1508
1179
 
1509
1180
    def _decode(self, infile, encoding):
1510
1181
        """
1524
1195
                infile[i] = line.decode(encoding)
1525
1196
        return infile
1526
1197
 
1527
 
 
1528
1198
    def _decode_element(self, line):
1529
1199
        """Decode element to unicode if necessary."""
1530
1200
        if not self.encoding:
1533
1203
            return line.decode(self.default_encoding)
1534
1204
        return line
1535
1205
 
1536
 
 
1537
1206
    def _str(self, value):
1538
1207
        """
1539
1208
        Used by ``stringify`` within validate, to turn non-string values
1544
1213
        else:
1545
1214
            return value
1546
1215
 
1547
 
 
1548
1216
    def _parse(self, infile):
1549
 
        """Actually parse the config file."""
1550
 
        temp_list_values = self.list_values
1551
 
        if self.unrepr:
1552
 
            self.list_values = False
1553
 
            
 
1217
        """
 
1218
        Actually parse the config file
 
1219
        
 
1220
        Testing Interpolation
 
1221
        
 
1222
        >>> c = ConfigObj()
 
1223
        >>> c['DEFAULT'] = {
 
1224
        ...     'b': 'goodbye',
 
1225
        ...     'userdir': 'c:\\\\home',
 
1226
        ...     'c': '%(d)s',
 
1227
        ...     'd': '%(c)s'
 
1228
        ... }
 
1229
        >>> c['section'] = {
 
1230
        ...     'a': '%(datadir)s\\\\some path\\\\file.py',
 
1231
        ...     'b': '%(userdir)s\\\\some path\\\\file.py',
 
1232
        ...     'c': 'Yo %(a)s',
 
1233
        ...     'd': '%(not_here)s',
 
1234
        ...     'e': '%(c)s',
 
1235
        ... }
 
1236
        >>> c['section']['DEFAULT'] = {
 
1237
        ...     'datadir': 'c:\\\\silly_test',
 
1238
        ...     'a': 'hello - %(b)s',
 
1239
        ... }
 
1240
        >>> c['section']['a'] == 'c:\\\\silly_test\\\\some path\\\\file.py'
 
1241
        1
 
1242
        >>> c['section']['b'] == 'c:\\\\home\\\\some path\\\\file.py'
 
1243
        1
 
1244
        >>> c['section']['c'] == 'Yo hello - goodbye'
 
1245
        1
 
1246
        
 
1247
        Switching Interpolation Off
 
1248
        
 
1249
        >>> c.interpolation = False
 
1250
        >>> c['section']['a'] == '%(datadir)s\\\\some path\\\\file.py'
 
1251
        1
 
1252
        >>> c['section']['b'] == '%(userdir)s\\\\some path\\\\file.py'
 
1253
        1
 
1254
        >>> c['section']['c'] == 'Yo %(a)s'
 
1255
        1
 
1256
        
 
1257
        Testing the interpolation errors.
 
1258
        
 
1259
        >>> c.interpolation = True
 
1260
        >>> c['section']['d']
 
1261
        Traceback (most recent call last):
 
1262
        MissingInterpolationOption: missing option "not_here" in interpolation.
 
1263
        >>> c['section']['e']
 
1264
        Traceback (most recent call last):
 
1265
        InterpolationDepthError: max interpolation depth exceeded in value "%(c)s".
 
1266
        
 
1267
        Testing our quoting.
 
1268
        
 
1269
        >>> i._quote('\"""\'\'\'')
 
1270
        Traceback (most recent call last):
 
1271
        SyntaxError: EOF while scanning triple-quoted string
 
1272
        >>> try:
 
1273
        ...     i._quote('\\n', multiline=False)
 
1274
        ... except ConfigObjError, e:
 
1275
        ...    e.msg
 
1276
        'Value "\\n" cannot be safely quoted.'
 
1277
        >>> k._quote(' "\' ', multiline=False)
 
1278
        Traceback (most recent call last):
 
1279
        SyntaxError: EOL while scanning single-quoted string
 
1280
        
 
1281
        Testing with "stringify" off.
 
1282
        >>> c.stringify = False
 
1283
        >>> c['test'] = 1
 
1284
        Traceback (most recent call last):
 
1285
        TypeError: Value is not a string "1".
 
1286
        """
1554
1287
        comment_list = []
1555
1288
        done_start = False
1556
1289
        this_section = self
1557
1290
        maxline = len(infile) - 1
1558
1291
        cur_index = -1
1559
1292
        reset_comment = False
1560
 
        
1561
1293
        while cur_index < maxline:
1562
1294
            if reset_comment:
1563
1295
                comment_list = []
1569
1301
                reset_comment = False
1570
1302
                comment_list.append(line)
1571
1303
                continue
1572
 
            
1573
1304
            if not done_start:
1574
1305
                # preserve initial comment
1575
1306
                self.initial_comment = comment_list
1576
1307
                comment_list = []
1577
1308
                done_start = True
1578
 
                
1579
1309
            reset_comment = True
1580
1310
            # first we check if it's a section marker
1581
1311
            mat = self._sectionmarker.match(line)
 
1312
##            sys.stderr.write('%s %s\n' % (sline, mat))
1582
1313
            if mat is not None:
1583
1314
                # is a section line
1584
 
                (indent, sect_open, sect_name, sect_close, comment) = mat.groups()
 
1315
                (indent, sect_open, sect_name, sect_close, comment) = (
 
1316
                    mat.groups())
1585
1317
                if indent and (self.indent_type is None):
1586
 
                    self.indent_type = indent
 
1318
                    self.indent_type = indent[0]
1587
1319
                cur_depth = sect_open.count('[')
1588
1320
                if cur_depth != sect_close.count(']'):
1589
 
                    self._handle_error("Cannot compute the section depth at line %s.",
1590
 
                                       NestingError, infile, cur_index)
 
1321
                    self._handle_error(
 
1322
                        "Cannot compute the section depth at line %s.",
 
1323
                        NestingError, infile, cur_index)
1591
1324
                    continue
1592
 
                
1593
1325
                if cur_depth < this_section.depth:
1594
1326
                    # the new section is dropping back to a previous level
1595
1327
                    try:
1596
 
                        parent = self._match_depth(this_section,
1597
 
                                                   cur_depth).parent
 
1328
                        parent = self._match_depth(
 
1329
                            this_section,
 
1330
                            cur_depth).parent
1598
1331
                    except SyntaxError:
1599
 
                        self._handle_error("Cannot compute nesting level at line %s.",
1600
 
                                           NestingError, infile, cur_index)
 
1332
                        self._handle_error(
 
1333
                            "Cannot compute nesting level at line %s.",
 
1334
                            NestingError, infile, cur_index)
1601
1335
                        continue
1602
1336
                elif cur_depth == this_section.depth:
1603
1337
                    # the new section is a sibling of the current section
1606
1340
                    # the new section is a child the current section
1607
1341
                    parent = this_section
1608
1342
                else:
1609
 
                    self._handle_error("Section too nested at line %s.",
1610
 
                                       NestingError, infile, cur_index)
1611
 
                    
 
1343
                    self._handle_error(
 
1344
                        "Section too nested at line %s.",
 
1345
                        NestingError, infile, cur_index)
 
1346
                #
1612
1347
                sect_name = self._unquote(sect_name)
1613
 
                if parent.has_key(sect_name):
1614
 
                    self._handle_error('Duplicate section name at line %s.',
1615
 
                                       DuplicateError, infile, cur_index)
 
1348
                if sect_name in parent:
 
1349
##                    sys.stderr.write(sect_name)
 
1350
##                    sys.stderr.write('\n')
 
1351
                    self._handle_error(
 
1352
                        'Duplicate section name at line %s.',
 
1353
                        DuplicateError, infile, cur_index)
1616
1354
                    continue
1617
 
                
1618
1355
                # create the new section
1619
1356
                this_section = Section(
1620
1357
                    parent,
1624
1361
                parent[sect_name] = this_section
1625
1362
                parent.inline_comments[sect_name] = comment
1626
1363
                parent.comments[sect_name] = comment_list
 
1364
##                sys.stderr.write(parent[sect_name] is this_section)
 
1365
##                sys.stderr.write('\n')
1627
1366
                continue
1628
1367
            #
1629
1368
            # it's not a section marker,
1630
1369
            # so it should be a valid ``key = value`` line
1631
1370
            mat = self._keyword.match(line)
1632
 
            if mat is None:
1633
 
                # it neither matched as a keyword
1634
 
                # or a section marker
1635
 
                self._handle_error(
1636
 
                    'Invalid line at line "%s".',
1637
 
                    ParseError, infile, cur_index)
1638
 
            else:
 
1371
##            sys.stderr.write('%s %s\n' % (sline, mat))
 
1372
            if mat is not None:
1639
1373
                # is a keyword value
1640
1374
                # value will include any inline comment
1641
1375
                (indent, key, value) = mat.groups()
1642
1376
                if indent and (self.indent_type is None):
1643
 
                    self.indent_type = indent
 
1377
                    self.indent_type = indent[0]
1644
1378
                # check for a multiline value
1645
1379
                if value[:3] in ['"""', "'''"]:
1646
1380
                    try:
1651
1385
                            'Parse error in value at line %s.',
1652
1386
                            ParseError, infile, cur_index)
1653
1387
                        continue
1654
 
                    else:
1655
 
                        if self.unrepr:
1656
 
                            comment = ''
1657
 
                            try:
1658
 
                                value = unrepr(value)
1659
 
                            except Exception, e:
1660
 
                                if type(e) == UnknownType:
1661
 
                                    msg = 'Unknown name or type in value at line %s.'
1662
 
                                else:
1663
 
                                    msg = 'Parse error in value at line %s.'
1664
 
                                self._handle_error(msg, UnreprError, infile,
1665
 
                                    cur_index)
1666
 
                                continue
1667
1388
                else:
1668
 
                    if self.unrepr:
1669
 
                        comment = ''
1670
 
                        try:
1671
 
                            value = unrepr(value)
1672
 
                        except Exception, e:
1673
 
                            if isinstance(e, UnknownType):
1674
 
                                msg = 'Unknown name or type in value at line %s.'
1675
 
                            else:
1676
 
                                msg = 'Parse error in value at line %s.'
1677
 
                            self._handle_error(msg, UnreprError, infile,
1678
 
                                cur_index)
1679
 
                            continue
1680
 
                    else:
1681
 
                        # extract comment and lists
1682
 
                        try:
1683
 
                            (value, comment) = self._handle_value(value)
1684
 
                        except SyntaxError:
1685
 
                            self._handle_error(
1686
 
                                'Parse error in value at line %s.',
1687
 
                                ParseError, infile, cur_index)
1688
 
                            continue
 
1389
                    # extract comment and lists
 
1390
                    try:
 
1391
                        (value, comment) = self._handle_value(value)
 
1392
                    except SyntaxError:
 
1393
                        self._handle_error(
 
1394
                            'Parse error in value at line %s.',
 
1395
                            ParseError, infile, cur_index)
 
1396
                        continue
1689
1397
                #
 
1398
##                sys.stderr.write(sline)
 
1399
##                sys.stderr.write('\n')
1690
1400
                key = self._unquote(key)
1691
 
                if this_section.has_key(key):
 
1401
                if key in this_section:
1692
1402
                    self._handle_error(
1693
1403
                        'Duplicate keyword name at line %s.',
1694
1404
                        DuplicateError, infile, cur_index)
1695
1405
                    continue
1696
 
                # add the key.
1697
 
                # we set unrepr because if we have got this far we will never
1698
 
                # be creating a new section
1699
 
                this_section.__setitem__(key, value, unrepr=True)
 
1406
                # add the key
 
1407
##                sys.stderr.write(this_section.name + '\n')
 
1408
                this_section[key] = value
1700
1409
                this_section.inline_comments[key] = comment
1701
1410
                this_section.comments[key] = comment_list
 
1411
##                sys.stderr.write('%s %s\n' % (key, this_section[key]))
 
1412
##                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')
1702
1417
                continue
1703
 
        #
 
1418
            #
 
1419
            # it neither matched as a keyword
 
1420
            # or a section marker
 
1421
            self._handle_error(
 
1422
                'Invalid line at line "%s".',
 
1423
                ParseError, infile, cur_index)
1704
1424
        if self.indent_type is None:
1705
1425
            # no indentation used, set the type accordingly
1706
1426
            self.indent_type = ''
1707
 
 
1708
1427
        # preserve the final comment
1709
1428
        if not self and not self.initial_comment:
1710
1429
            self.initial_comment = comment_list
1711
 
        elif not reset_comment:
 
1430
        else:
1712
1431
            self.final_comment = comment_list
1713
 
        self.list_values = temp_list_values
1714
 
 
1715
1432
 
1716
1433
    def _match_depth(self, sect, depth):
1717
1434
        """
1724
1441
        while depth < sect.depth:
1725
1442
            if sect is sect.parent:
1726
1443
                # we've reached the top level already
1727
 
                raise SyntaxError()
 
1444
                raise SyntaxError
1728
1445
            sect = sect.parent
1729
1446
        if sect.depth == depth:
1730
1447
            return sect
1731
1448
        # shouldn't get here
1732
 
        raise SyntaxError()
1733
 
 
 
1449
        raise SyntaxError
1734
1450
 
1735
1451
    def _handle_error(self, text, ErrorClass, infile, cur_index):
1736
1452
        """
1750
1466
        # reraise when parsing has finished
1751
1467
        self._errors.append(error)
1752
1468
 
1753
 
 
1754
1469
    def _unquote(self, value):
1755
1470
        """Return an unquoted version of a value"""
1756
1471
        if (value[0] == value[-1]) and (value[0] in ('"', "'")):
1757
1472
            value = value[1:-1]
1758
1473
        return value
1759
1474
 
1760
 
 
1761
1475
    def _quote(self, value, multiline=True):
1762
1476
        """
1763
1477
        Return a safely quoted version of a value.
1772
1486
        Obey list syntax for empty and single member lists.
1773
1487
        
1774
1488
        If ``list_values=False`` then the value is only quoted if it contains
1775
 
        a ``\n`` (is multiline) or '#'.
1776
 
        
1777
 
        If ``write_empty_values`` is set, and the value is an empty string, it
1778
 
        won't be quoted.
 
1489
        a ``\n`` (is multiline).
1779
1490
        """
1780
 
        if multiline and self.write_empty_values and value == '':
1781
 
            # Only if multiline is set, so that it is used for values not
1782
 
            # keys, and not values that are part of a list
1783
 
            return ''
1784
 
        
1785
 
        if multiline and isinstance(value, (list, tuple)):
 
1491
        if isinstance(value, (list, tuple)):
1786
1492
            if not value:
1787
1493
                return ','
1788
1494
            elif len(value) == 1:
1793
1499
            if self.stringify:
1794
1500
                value = str(value)
1795
1501
            else:
1796
 
                raise TypeError('Value "%s" is not a string.' % value)
1797
 
 
 
1502
                raise TypeError, 'Value "%s" is not a string.' % value
 
1503
        squot = "'%s'"
 
1504
        dquot = '"%s"'
 
1505
        noquot = "%s"
 
1506
        wspace_plus = ' \r\t\n\v\t\'"'
 
1507
        tsquot = '"""%s"""'
 
1508
        tdquot = "'''%s'''"
1798
1509
        if not value:
1799
1510
            return '""'
1800
 
        
1801
 
        no_lists_no_quotes = not self.list_values and '\n' not in value and '#' not in value
1802
 
        need_triple = multiline and ((("'" in value) and ('"' in value)) or ('\n' in value ))
1803
 
        hash_triple_quote = multiline and not need_triple and ("'" in value) and ('"' in value) and ('#' in value)
1804
 
        check_for_single = (no_lists_no_quotes or not need_triple) and not hash_triple_quote
1805
 
        
1806
 
        if check_for_single:
 
1511
        if (not self.list_values and '\n' not in value) or not (multiline and
 
1512
                ((("'" in value) and ('"' in value)) or ('\n' in value))):
1807
1513
            if not self.list_values:
1808
1514
                # we don't quote if ``list_values=False``
1809
1515
                quot = noquot
1810
1516
            # for normal values either single or double quotes will do
1811
1517
            elif '\n' in value:
1812
1518
                # will only happen if multiline is off - e.g. '\n' in key
1813
 
                raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
 
1519
                raise ConfigObjError, ('Value "%s" cannot be safely quoted.' %
 
1520
                    value)
1814
1521
            elif ((value[0] not in wspace_plus) and
1815
1522
                    (value[-1] not in wspace_plus) and
1816
1523
                    (',' not in value)):
1817
1524
                quot = noquot
1818
1525
            else:
1819
 
                quot = self._get_single_quote(value)
 
1526
                if ("'" in value) and ('"' in value):
 
1527
                    raise ConfigObjError, (
 
1528
                        'Value "%s" cannot be safely quoted.' % value)
 
1529
                elif '"' in value:
 
1530
                    quot = squot
 
1531
                else:
 
1532
                    quot = dquot
1820
1533
        else:
1821
1534
            # if value has '\n' or "'" *and* '"', it will need triple quotes
1822
 
            quot = self._get_triple_quote(value)
1823
 
        
1824
 
        if quot == noquot and '#' in value and self.list_values:
1825
 
            quot = self._get_single_quote(value)
1826
 
                
 
1535
            if (value.find('"""') != -1) and (value.find("'''") != -1):
 
1536
                raise ConfigObjError, (
 
1537
                    'Value "%s" cannot be safely quoted.' % value)
 
1538
            if value.find('"""') == -1:
 
1539
                quot = tdquot
 
1540
            else:
 
1541
                quot = tsquot
1827
1542
        return quot % value
1828
 
    
1829
 
    
1830
 
    def _get_single_quote(self, value):
1831
 
        if ("'" in value) and ('"' in value):
1832
 
            raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
1833
 
        elif '"' in value:
1834
 
            quot = squot
1835
 
        else:
1836
 
            quot = dquot
1837
 
        return quot
1838
 
    
1839
 
    
1840
 
    def _get_triple_quote(self, value):
1841
 
        if (value.find('"""') != -1) and (value.find("'''") != -1):
1842
 
            raise ConfigObjError('Value "%s" cannot be safely quoted.' % value)
1843
 
        if value.find('"""') == -1:
1844
 
            quot = tdquot
1845
 
        else:
1846
 
            quot = tsquot 
1847
 
        return quot
1848
 
 
1849
1543
 
1850
1544
    def _handle_value(self, value):
1851
1545
        """
1852
1546
        Given a value string, unquote, remove comment,
1853
1547
        handle lists. (including empty and single member lists)
 
1548
        
 
1549
        Testing list values.
 
1550
        
 
1551
        >>> testconfig3 = '''
 
1552
        ... a = ,
 
1553
        ... b = test,
 
1554
        ... c = test1, test2   , test3
 
1555
        ... d = test1, test2, test3,
 
1556
        ... '''
 
1557
        >>> d = ConfigObj(testconfig3.split('\\n'), raise_errors=True)
 
1558
        >>> d['a'] == []
 
1559
        1
 
1560
        >>> d['b'] == ['test']
 
1561
        1
 
1562
        >>> d['c'] == ['test1', 'test2', 'test3']
 
1563
        1
 
1564
        >>> d['d'] == ['test1', 'test2', 'test3']
 
1565
        1
 
1566
        
 
1567
        Testing with list values off.
 
1568
        
 
1569
        >>> e = ConfigObj(
 
1570
        ...     testconfig3.split('\\n'),
 
1571
        ...     raise_errors=True,
 
1572
        ...     list_values=False)
 
1573
        >>> e['a'] == ','
 
1574
        1
 
1575
        >>> e['b'] == 'test,'
 
1576
        1
 
1577
        >>> e['c'] == 'test1, test2   , test3'
 
1578
        1
 
1579
        >>> e['d'] == 'test1, test2, test3,'
 
1580
        1
 
1581
        
 
1582
        Testing creating from a dictionary.
 
1583
        
 
1584
        >>> f = {
 
1585
        ...     'key1': 'val1',
 
1586
        ...     'key2': 'val2',
 
1587
        ...     'section 1': {
 
1588
        ...         'key1': 'val1',
 
1589
        ...         'key2': 'val2',
 
1590
        ...         'section 1b': {
 
1591
        ...             'key1': 'val1',
 
1592
        ...             'key2': 'val2',
 
1593
        ...         },
 
1594
        ...     },
 
1595
        ...     'section 2': {
 
1596
        ...         'key1': 'val1',
 
1597
        ...         'key2': 'val2',
 
1598
        ...         'section 2b': {
 
1599
        ...             'key1': 'val1',
 
1600
        ...             'key2': 'val2',
 
1601
        ...         },
 
1602
        ...     },
 
1603
        ...      'key3': 'val3',
 
1604
        ... }
 
1605
        >>> g = ConfigObj(f)
 
1606
        >>> f == g
 
1607
        1
 
1608
        
 
1609
        Testing we correctly detect badly built list values (4 of them).
 
1610
        
 
1611
        >>> testconfig4 = '''
 
1612
        ... config = 3,4,,
 
1613
        ... test = 3,,4
 
1614
        ... fish = ,,
 
1615
        ... dummy = ,,hello, goodbye
 
1616
        ... '''
 
1617
        >>> try:
 
1618
        ...     ConfigObj(testconfig4.split('\\n'))
 
1619
        ... except ConfigObjError, e:
 
1620
        ...     len(e.errors)
 
1621
        4
 
1622
        
 
1623
        Testing we correctly detect badly quoted values (4 of them).
 
1624
        
 
1625
        >>> testconfig5 = '''
 
1626
        ... config = "hello   # comment
 
1627
        ... test = 'goodbye
 
1628
        ... fish = 'goodbye   # comment
 
1629
        ... dummy = "hello again
 
1630
        ... '''
 
1631
        >>> try:
 
1632
        ...     ConfigObj(testconfig5.split('\\n'))
 
1633
        ... except ConfigObjError, e:
 
1634
        ...     len(e.errors)
 
1635
        4
1854
1636
        """
1855
1637
        # do we look for lists in values ?
1856
1638
        if not self.list_values:
1857
1639
            mat = self._nolistvalue.match(value)
1858
1640
            if mat is None:
1859
 
                raise SyntaxError()
 
1641
                raise SyntaxError
 
1642
            (value, comment) = mat.groups()
1860
1643
            # NOTE: we don't unquote here
1861
 
            return mat.groups()
1862
 
        #
 
1644
            return (value, comment)
1863
1645
        mat = self._valueexp.match(value)
1864
1646
        if mat is None:
1865
1647
            # the value is badly constructed, probably badly quoted,
1866
1648
            # or an invalid list
1867
 
            raise SyntaxError()
 
1649
            raise SyntaxError
1868
1650
        (list_values, single, empty_list, comment) = mat.groups()
1869
1651
        if (list_values == '') and (single is None):
1870
1652
            # change this if you want to accept empty values
1871
 
            raise SyntaxError()
 
1653
            raise SyntaxError
1872
1654
        # NOTE: note there is no error handling from here if the regex
1873
1655
        # is wrong: then incorrect values will slip through
1874
1656
        if empty_list is not None:
1875
1657
            # the single comma - meaning an empty list
1876
1658
            return ([], comment)
1877
1659
        if single is not None:
1878
 
            # handle empty values
1879
 
            if list_values and not single:
1880
 
                # FIXME: the '' is a workaround because our regex now matches
1881
 
                #   '' at the end of a list if it has a trailing comma
1882
 
                single = None
1883
 
            else:
1884
 
                single = single or '""'
1885
 
                single = self._unquote(single)
 
1660
            single = self._unquote(single)
1886
1661
        if list_values == '':
1887
1662
            # not a list value
1888
1663
            return (single, comment)
1892
1667
            the_list += [single]
1893
1668
        return (the_list, comment)
1894
1669
 
1895
 
 
1896
1670
    def _multiline(self, value, infile, cur_index, maxline):
1897
 
        """Extract the value, where we are in a multiline situation."""
 
1671
        """
 
1672
        Extract the value, where we are in a multiline situation
 
1673
        
 
1674
        Testing multiline values.
 
1675
        
 
1676
        >>> i == {
 
1677
        ...     'name4': ' another single line value ',
 
1678
        ...     'multi section': {
 
1679
        ...         'name4': '\\n        Well, this is a\\n        multiline '
 
1680
        ...             'value\\n        ',
 
1681
        ...         'name2': '\\n        Well, this is a\\n        multiline '
 
1682
        ...             'value\\n        ',
 
1683
        ...         'name3': '\\n        Well, this is a\\n        multiline '
 
1684
        ...             'value\\n        ',
 
1685
        ...         'name1': '\\n        Well, this is a\\n        multiline '
 
1686
        ...             'value\\n        ',
 
1687
        ...     },
 
1688
        ...     'name2': ' another single line value ',
 
1689
        ...     'name3': ' a single line value ',
 
1690
        ...     'name1': ' a single line value ',
 
1691
        ... }
 
1692
        1
 
1693
        """
1898
1694
        quot = value[:3]
1899
1695
        newvalue = value[3:]
1900
1696
        single_line = self._triple_quote[quot][0]
1906
1702
            return retval
1907
1703
        elif newvalue.find(quot) != -1:
1908
1704
            # somehow the triple quote is missing
1909
 
            raise SyntaxError()
 
1705
            raise SyntaxError
1910
1706
        #
1911
1707
        while cur_index < maxline:
1912
1708
            cur_index += 1
1919
1715
                break
1920
1716
        else:
1921
1717
            # we've got to the end of the config, oops...
1922
 
            raise SyntaxError()
 
1718
            raise SyntaxError
1923
1719
        mat = multi_line.match(line)
1924
1720
        if mat is None:
1925
1721
            # a badly formed line
1926
 
            raise SyntaxError()
 
1722
            raise SyntaxError
1927
1723
        (value, comment) = mat.groups()
1928
1724
        return (newvalue + value, comment, cur_index)
1929
1725
 
1930
 
 
1931
1726
    def _handle_configspec(self, configspec):
1932
1727
        """Parse the configspec."""
1933
 
        # FIXME: Should we check that the configspec was created with the 
1934
 
        #        correct settings ? (i.e. ``list_values=False``)
1935
 
        if not isinstance(configspec, ConfigObj):
1936
 
            try:
1937
 
                configspec = ConfigObj(configspec,
1938
 
                                       raise_errors=True,
1939
 
                                       file_error=True,
1940
 
                                       list_values=False)
1941
 
            except ConfigObjError, e:
1942
 
                # FIXME: Should these errors have a reference
1943
 
                #        to the already parsed ConfigObj ?
1944
 
                raise ConfigspecError('Parsing configspec failed: %s' % e)
1945
 
            except IOError, e:
1946
 
                raise IOError('Reading configspec failed: %s' % e)
1947
 
        
 
1728
        try:
 
1729
            configspec = ConfigObj(
 
1730
                configspec,
 
1731
                raise_errors=True,
 
1732
                file_error=True,
 
1733
                list_values=False)
 
1734
        except ConfigObjError, e:
 
1735
            # FIXME: Should these errors have a reference
 
1736
            # to the already parsed ConfigObj ?
 
1737
            raise ConfigspecError('Parsing configspec failed: %s' % e)
 
1738
        except IOError, e:
 
1739
            raise IOError('Reading configspec failed: %s' % e)
1948
1740
        self._set_configspec_value(configspec, self)
1949
1741
 
1950
 
 
1951
1742
    def _set_configspec_value(self, configspec, section):
1952
1743
        """Used to recursively set configspec values."""
1953
1744
        if '__many__' in configspec.sections:
1954
1745
            section.configspec['__many__'] = configspec['__many__']
1955
1746
            if len(configspec.sections) > 1:
1956
1747
                # FIXME: can we supply any useful information here ?
1957
 
                raise RepeatSectionError()
1958
 
            
1959
 
        if getattr(configspec, 'initial_comment', None) is not None:
1960
 
            section._configspec_initial_comment = configspec.initial_comment
1961
 
            section._configspec_final_comment = configspec.final_comment
1962
 
            section._configspec_encoding = configspec.encoding
1963
 
            section._configspec_BOM = configspec.BOM
1964
 
            section._configspec_newlines = configspec.newlines
1965
 
            section._configspec_indent_type = configspec.indent_type
1966
 
            
 
1748
                raise RepeatSectionError
1967
1749
        for entry in configspec.scalars:
1968
 
            section._configspec_comments[entry] = configspec.comments[entry]
1969
 
            section._configspec_inline_comments[entry] = configspec.inline_comments[entry]
1970
1750
            section.configspec[entry] = configspec[entry]
1971
 
            section._order.append(entry)
1972
 
            
1973
1751
        for entry in configspec.sections:
1974
1752
            if entry == '__many__':
1975
1753
                continue
1976
 
            
1977
 
            section._cs_section_comments[entry] = configspec.comments[entry]
1978
 
            section._cs_section_inline_comments[entry] = configspec.inline_comments[entry]
1979
 
            if not section.has_key(entry):
 
1754
            if entry not in section:
1980
1755
                section[entry] = {}
1981
1756
            self._set_configspec_value(configspec[entry], section[entry])
1982
1757
 
1983
 
 
1984
1758
    def _handle_repeat(self, section, configspec):
1985
1759
        """Dynamically assign configspec for repeated section."""
1986
1760
        try:
1991
1765
                                if isinstance(configspec[entry], dict)]
1992
1766
            scalar_keys = [entry for entry in configspec 
1993
1767
                                if not isinstance(configspec[entry], dict)]
1994
 
            
1995
1768
        if '__many__' in section_keys and len(section_keys) > 1:
1996
1769
            # FIXME: can we supply any useful information here ?
1997
 
            raise RepeatSectionError()
1998
 
        
 
1770
            raise RepeatSectionError
1999
1771
        scalars = {}
2000
1772
        sections = {}
2001
1773
        for entry in scalar_keys:
2007
1779
                scalars[entry] = val
2008
1780
                continue
2009
1781
            sections[entry] = val
2010
 
            
 
1782
        #
2011
1783
        section.configspec = scalars
2012
1784
        for entry in sections:
2013
 
            if not section.has_key(entry):
 
1785
            if entry not in section:
2014
1786
                section[entry] = {}
2015
1787
            self._handle_repeat(section[entry], sections[entry])
2016
1788
 
2017
 
 
2018
1789
    def _write_line(self, indent_string, entry, this_entry, comment):
2019
1790
        """Write an individual line, for the write method"""
2020
1791
        # NOTE: the calls to self._quote here handles non-StringType values.
2021
 
        if not self.unrepr:
2022
 
            val = self._decode_element(self._quote(this_entry))
2023
 
        else:
2024
 
            val = repr(this_entry)
2025
 
        return '%s%s%s%s%s' % (indent_string,
2026
 
                               self._decode_element(self._quote(entry, multiline=False)),
2027
 
                               self._a_to_u(' = '),
2028
 
                               val,
2029
 
                               self._decode_element(comment))
2030
 
 
 
1792
        return '%s%s%s%s%s' % (
 
1793
            indent_string,
 
1794
            self._decode_element(self._quote(entry, multiline=False)),
 
1795
            self._a_to_u(' = '),
 
1796
            self._decode_element(self._quote(this_entry)),
 
1797
            self._decode_element(comment))
2031
1798
 
2032
1799
    def _write_marker(self, indent_string, depth, entry, comment):
2033
1800
        """Write a section marker line"""
2034
 
        return '%s%s%s%s%s' % (indent_string,
2035
 
                               self._a_to_u('[' * depth),
2036
 
                               self._quote(self._decode_element(entry), multiline=False),
2037
 
                               self._a_to_u(']' * depth),
2038
 
                               self._decode_element(comment))
2039
 
 
 
1801
        return '%s%s%s%s%s' % (
 
1802
            indent_string,
 
1803
            self._a_to_u('[' * depth),
 
1804
            self._quote(self._decode_element(entry), multiline=False),
 
1805
            self._a_to_u(']' * depth),
 
1806
            self._decode_element(comment))
2040
1807
 
2041
1808
    def _handle_comment(self, comment):
2042
 
        """Deal with a comment."""
 
1809
        """
 
1810
        Deal with a comment.
 
1811
        
 
1812
        >>> filename = a.filename
 
1813
        >>> a.filename = None
 
1814
        >>> values = a.write()
 
1815
        >>> index = 0
 
1816
        >>> while index < 23:
 
1817
        ...     index += 1
 
1818
        ...     line = values[index-1]
 
1819
        ...     assert line.endswith('# comment ' + str(index))
 
1820
        >>> a.filename = filename
 
1821
        
 
1822
        >>> start_comment = ['# Initial Comment', '', '#']
 
1823
        >>> end_comment = ['', '#', '# Final Comment']
 
1824
        >>> newconfig = start_comment + testconfig1.split('\\n') + end_comment
 
1825
        >>> nc = ConfigObj(newconfig)
 
1826
        >>> nc.initial_comment
 
1827
        ['# Initial Comment', '', '#']
 
1828
        >>> nc.final_comment
 
1829
        ['', '#', '# Final Comment']
 
1830
        >>> nc.initial_comment == start_comment
 
1831
        1
 
1832
        >>> nc.final_comment == end_comment
 
1833
        1
 
1834
        """
2043
1835
        if not comment:
2044
1836
            return ''
2045
 
        start = self.indent_type
 
1837
        if self.indent_type == '\t':
 
1838
            start = self._a_to_u('\t')
 
1839
        else:
 
1840
            start = self._a_to_u(' ' * NUM_INDENT_SPACES)
2046
1841
        if not comment.startswith('#'):
2047
 
            start += self._a_to_u(' # ')
 
1842
            start += _a_to_u('# ')
2048
1843
        return (start + comment)
2049
1844
 
 
1845
    def _compute_indent_string(self, depth):
 
1846
        """
 
1847
        Compute the indent string, according to current indent_type and depth
 
1848
        """
 
1849
        if self.indent_type == '':
 
1850
            # no indentation at all
 
1851
            return ''
 
1852
        if self.indent_type == '\t':
 
1853
            return '\t' * depth
 
1854
        if self.indent_type == ' ':
 
1855
            return ' ' * NUM_INDENT_SPACES * depth
 
1856
        raise SyntaxError
2050
1857
 
2051
1858
    # Public methods
2052
1859
 
2062
1869
        >>> a.filename = filename
2063
1870
        >>> a == ConfigObj('test.ini', raise_errors=True)
2064
1871
        1
 
1872
        >>> os.remove('test.ini')
 
1873
        >>> b.filename = 'test.ini'
 
1874
        >>> b.write()
 
1875
        >>> b == ConfigObj('test.ini', raise_errors=True)
 
1876
        1
 
1877
        >>> os.remove('test.ini')
 
1878
        >>> i.filename = 'test.ini'
 
1879
        >>> i.write()
 
1880
        >>> i == ConfigObj('test.ini', raise_errors=True)
 
1881
        1
 
1882
        >>> os.remove('test.ini')
 
1883
        >>> a = ConfigObj()
 
1884
        >>> a['DEFAULT'] = {'a' : 'fish'}
 
1885
        >>> a['a'] = '%(a)s'
 
1886
        >>> a.write()
 
1887
        ['a = %(a)s', '[DEFAULT]', 'a = fish']
2065
1888
        """
2066
1889
        if self.indent_type is None:
2067
1890
            # this can be true if initialised from a dictionary
2068
1891
            self.indent_type = DEFAULT_INDENT_TYPE
2069
 
            
 
1892
        #
2070
1893
        out = []
2071
1894
        cs = self._a_to_u('#')
2072
1895
        csp = self._a_to_u('# ')
2080
1903
                if stripped_line and not stripped_line.startswith(cs):
2081
1904
                    line = csp + line
2082
1905
                out.append(line)
2083
 
                
2084
 
        indent_string = self.indent_type * section.depth
 
1906
        #
 
1907
        indent_string = self._a_to_u(
 
1908
            self._compute_indent_string(section.depth))
2085
1909
        for entry in (section.scalars + section.sections):
2086
1910
            if entry in section.defaults:
2087
1911
                # don't write out default values
2093
1917
                out.append(indent_string + comment_line)
2094
1918
            this_entry = section[entry]
2095
1919
            comment = self._handle_comment(section.inline_comments[entry])
2096
 
            
 
1920
            #
2097
1921
            if isinstance(this_entry, dict):
2098
1922
                # a section
2099
1923
                out.append(self._write_marker(
2108
1932
                    entry,
2109
1933
                    this_entry,
2110
1934
                    comment))
2111
 
                
 
1935
        #
2112
1936
        if section is self:
2113
1937
            for line in self.final_comment:
2114
1938
                line = self._decode_element(line)
2117
1941
                    line = csp + line
2118
1942
                out.append(line)
2119
1943
            self.interpolation = int_val
2120
 
            
 
1944
        #
2121
1945
        if section is not self:
2122
1946
            return out
2123
 
        
 
1947
        #
2124
1948
        if (self.filename is None) and (outfile is None):
2125
1949
            # output a list of lines
2126
1950
            # might need to encode
2134
1958
                    out.append('')
2135
1959
                out[0] = BOM_UTF8 + out[0]
2136
1960
            return out
2137
 
        
 
1961
        #
2138
1962
        # Turn the list to a string, joined with correct newlines
2139
 
        newline = self.newlines or os.linesep
2140
 
        output = self._a_to_u(newline).join(out)
 
1963
        output = (self._a_to_u(self.newlines or os.linesep)
 
1964
            ).join(out)
2141
1965
        if self.encoding:
2142
1966
            output = output.encode(self.encoding)
2143
 
        if self.BOM and ((self.encoding is None) or match_utf8(self.encoding)):
 
1967
        if (self.BOM and ((self.encoding is None) or
 
1968
            (BOM_LIST.get(self.encoding.lower()) == 'utf_8'))):
2144
1969
            # Add the UTF8 BOM
2145
1970
            output = BOM_UTF8 + output
2146
 
            
2147
 
        if not output.endswith(newline):
2148
 
            output += newline
2149
1971
        if outfile is not None:
2150
1972
            outfile.write(output)
2151
1973
        else:
2152
 
            h = open(self.filename, 'wb')
 
1974
            h = open(self.filename, 'w')
2153
1975
            h.write(output)
2154
1976
            h.close()
2155
1977
 
2156
 
 
2157
 
    def validate(self, validator, preserve_errors=False, copy=False,
2158
 
                 section=None):
 
1978
    def validate(self, validator, preserve_errors=False, section=None):
2159
1979
        """
2160
1980
        Test the ConfigObj against a configspec.
2161
1981
        
2180
2000
        If ``preserve_errors`` is ``True`` (``False`` is default) then instead
2181
2001
        of a marking a fail with a ``False``, it will preserve the actual
2182
2002
        exception object. This can contain info about the reason for failure.
2183
 
        For example the ``VdtValueTooSmallError`` indicates that the value
 
2003
        For example the ``VdtValueTooSmallError`` indeicates that the value
2184
2004
        supplied was too small. If a value (or section) is missing it will
2185
2005
        still be marked as ``False``.
2186
2006
        
2189
2009
        You can then use the ``flatten_errors`` function to turn your nested
2190
2010
        results dictionary into a flattened list of failures - useful for
2191
2011
        displaying meaningful error messages.
 
2012
        
 
2013
        >>> try:
 
2014
        ...     from validate import Validator
 
2015
        ... except ImportError:
 
2016
        ...     sys.stderr.write('Cannot import the Validator object, skipping the related tests\n')
 
2017
        ... else:
 
2018
        ...     config = '''
 
2019
        ...     test1=40
 
2020
        ...     test2=hello
 
2021
        ...     test3=3
 
2022
        ...     test4=5.0
 
2023
        ...     [section]
 
2024
        ...         test1=40
 
2025
        ...         test2=hello
 
2026
        ...         test3=3
 
2027
        ...         test4=5.0
 
2028
        ...         [[sub section]]
 
2029
        ...             test1=40
 
2030
        ...             test2=hello
 
2031
        ...             test3=3
 
2032
        ...             test4=5.0
 
2033
        ... '''.split('\\n')
 
2034
        ...     configspec = '''
 
2035
        ...     test1= integer(30,50)
 
2036
        ...     test2= string
 
2037
        ...     test3=integer
 
2038
        ...     test4=float(6.0)
 
2039
        ...     [section ]
 
2040
        ...         test1=integer(30,50)
 
2041
        ...         test2=string
 
2042
        ...         test3=integer
 
2043
        ...         test4=float(6.0)
 
2044
        ...         [[sub section]]
 
2045
        ...             test1=integer(30,50)
 
2046
        ...             test2=string
 
2047
        ...             test3=integer
 
2048
        ...             test4=float(6.0)
 
2049
        ...     '''.split('\\n')
 
2050
        ...     val = Validator()
 
2051
        ...     c1 = ConfigObj(config, configspec=configspec)
 
2052
        ...     test = c1.validate(val)
 
2053
        ...     test == {
 
2054
        ...         'test1': True,
 
2055
        ...         'test2': True,
 
2056
        ...         'test3': True,
 
2057
        ...         'test4': False,
 
2058
        ...         'section': {
 
2059
        ...             'test1': True,
 
2060
        ...             'test2': True,
 
2061
        ...             'test3': True,
 
2062
        ...             'test4': False,
 
2063
        ...             'sub section': {
 
2064
        ...                 'test1': True,
 
2065
        ...                 'test2': True,
 
2066
        ...                 'test3': True,
 
2067
        ...                 'test4': False,
 
2068
        ...             },
 
2069
        ...         },
 
2070
        ...     }
 
2071
        1
 
2072
        >>> val.check(c1.configspec['test4'], c1['test4'])
 
2073
        Traceback (most recent call last):
 
2074
        VdtValueTooSmallError: the value "5.0" is too small.
 
2075
        
 
2076
        >>> val_test_config = '''
 
2077
        ...     key = 0
 
2078
        ...     key2 = 1.1
 
2079
        ...     [section]
 
2080
        ...     key = some text
 
2081
        ...     key2 = 1.1, 3.0, 17, 6.8
 
2082
        ...         [[sub-section]]
 
2083
        ...         key = option1
 
2084
        ...         key2 = True'''.split('\\n')
 
2085
        >>> val_test_configspec = '''
 
2086
        ...     key = integer
 
2087
        ...     key2 = float
 
2088
        ...     [section]
 
2089
        ...     key = string
 
2090
        ...     key2 = float_list(4)
 
2091
        ...        [[sub-section]]
 
2092
        ...        key = option(option1, option2)
 
2093
        ...        key2 = boolean'''.split('\\n')
 
2094
        >>> val_test = ConfigObj(val_test_config, configspec=val_test_configspec)
 
2095
        >>> val_test.validate(val)
 
2096
        1
 
2097
        >>> val_test['key'] = 'text not a digit'
 
2098
        >>> val_res = val_test.validate(val)
 
2099
        >>> val_res == {'key2': True, 'section': True, 'key': False}
 
2100
        1
 
2101
        >>> configspec = '''
 
2102
        ...     test1=integer(30,50, default=40)
 
2103
        ...     test2=string(default="hello")
 
2104
        ...     test3=integer(default=3)
 
2105
        ...     test4=float(6.0, default=6.0)
 
2106
        ...     [section ]
 
2107
        ...         test1=integer(30,50, default=40)
 
2108
        ...         test2=string(default="hello")
 
2109
        ...         test3=integer(default=3)
 
2110
        ...         test4=float(6.0, default=6.0)
 
2111
        ...         [[sub section]]
 
2112
        ...             test1=integer(30,50, default=40)
 
2113
        ...             test2=string(default="hello")
 
2114
        ...             test3=integer(default=3)
 
2115
        ...             test4=float(6.0, default=6.0)
 
2116
        ...     '''.split('\\n')
 
2117
        >>> default_test = ConfigObj(['test1=30'], configspec=configspec)
 
2118
        >>> default_test
 
2119
        {'test1': '30', 'section': {'sub section': {}}}
 
2120
        >>> default_test.validate(val)
 
2121
        1
 
2122
        >>> default_test == {
 
2123
        ...     'test1': 30,
 
2124
        ...     'test2': 'hello',
 
2125
        ...     'test3': 3,
 
2126
        ...     'test4': 6.0,
 
2127
        ...     'section': {
 
2128
        ...         'test1': 40,
 
2129
        ...         'test2': 'hello',
 
2130
        ...         'test3': 3,
 
2131
        ...         'test4': 6.0,
 
2132
        ...         'sub section': {
 
2133
        ...             'test1': 40,
 
2134
        ...             'test3': 3,
 
2135
        ...             'test2': 'hello',
 
2136
        ...             'test4': 6.0,
 
2137
        ...         },
 
2138
        ...     },
 
2139
        ... }
 
2140
        1
 
2141
        
 
2142
        Now testing with repeated sections : BIG TEST
 
2143
        
 
2144
        >>> repeated_1 = '''
 
2145
        ... [dogs]
 
2146
        ...     [[__many__]] # spec for a dog
 
2147
        ...         fleas = boolean(default=True)
 
2148
        ...         tail = option(long, short, default=long)
 
2149
        ...         name = string(default=rover)
 
2150
        ...         [[[__many__]]]  # spec for a puppy
 
2151
        ...             name = string(default="son of rover")
 
2152
        ...             age = float(default=0.0)
 
2153
        ... [cats]
 
2154
        ...     [[__many__]] # spec for a cat
 
2155
        ...         fleas = boolean(default=True)
 
2156
        ...         tail = option(long, short, default=short)
 
2157
        ...         name = string(default=pussy)
 
2158
        ...         [[[__many__]]] # spec for a kitten
 
2159
        ...             name = string(default="son of pussy")
 
2160
        ...             age = float(default=0.0)
 
2161
        ...         '''.split('\\n')
 
2162
        >>> repeated_2 = '''
 
2163
        ... [dogs]
 
2164
        ... 
 
2165
        ...     # blank dogs with puppies
 
2166
        ...     # should be filled in by the configspec
 
2167
        ...     [[dog1]]
 
2168
        ...         [[[puppy1]]]
 
2169
        ...         [[[puppy2]]]
 
2170
        ...         [[[puppy3]]]
 
2171
        ...     [[dog2]]
 
2172
        ...         [[[puppy1]]]
 
2173
        ...         [[[puppy2]]]
 
2174
        ...         [[[puppy3]]]
 
2175
        ...     [[dog3]]
 
2176
        ...         [[[puppy1]]]
 
2177
        ...         [[[puppy2]]]
 
2178
        ...         [[[puppy3]]]
 
2179
        ... [cats]
 
2180
        ... 
 
2181
        ...     # blank cats with kittens
 
2182
        ...     # should be filled in by the configspec
 
2183
        ...     [[cat1]]
 
2184
        ...         [[[kitten1]]]
 
2185
        ...         [[[kitten2]]]
 
2186
        ...         [[[kitten3]]]
 
2187
        ...     [[cat2]]
 
2188
        ...         [[[kitten1]]]
 
2189
        ...         [[[kitten2]]]
 
2190
        ...         [[[kitten3]]]
 
2191
        ...     [[cat3]]
 
2192
        ...         [[[kitten1]]]
 
2193
        ...         [[[kitten2]]]
 
2194
        ...         [[[kitten3]]]
 
2195
        ... '''.split('\\n')
 
2196
        >>> repeated_3 = '''
 
2197
        ... [dogs]
 
2198
        ... 
 
2199
        ...     [[dog1]]
 
2200
        ...     [[dog2]]
 
2201
        ...     [[dog3]]
 
2202
        ... [cats]
 
2203
        ... 
 
2204
        ...     [[cat1]]
 
2205
        ...     [[cat2]]
 
2206
        ...     [[cat3]]
 
2207
        ... '''.split('\\n')
 
2208
        >>> repeated_4 = '''
 
2209
        ... [__many__]
 
2210
        ... 
 
2211
        ...     name = string(default=Michael)
 
2212
        ...     age = float(default=0.0)
 
2213
        ...     sex = option(m, f, default=m)
 
2214
        ... '''.split('\\n')
 
2215
        >>> repeated_5 = '''
 
2216
        ... [cats]
 
2217
        ... [[__many__]]
 
2218
        ...     fleas = boolean(default=True)
 
2219
        ...     tail = option(long, short, default=short)
 
2220
        ...     name = string(default=pussy)
 
2221
        ...     [[[description]]]
 
2222
        ...         height = float(default=3.3)
 
2223
        ...         weight = float(default=6)
 
2224
        ...         [[[[coat]]]]
 
2225
        ...             fur = option(black, grey, brown, "tortoise shell", default=black)
 
2226
        ...             condition = integer(0,10, default=5)
 
2227
        ... '''.split('\\n')
 
2228
        >>> from validate import Validator
 
2229
        >>> val= Validator()
 
2230
        >>> repeater = ConfigObj(repeated_2, configspec=repeated_1)
 
2231
        >>> repeater.validate(val)
 
2232
        1
 
2233
        >>> repeater == {
 
2234
        ...     'dogs': {
 
2235
        ...         'dog1': {
 
2236
        ...             'fleas': True,
 
2237
        ...             'tail': 'long',
 
2238
        ...             'name': 'rover',
 
2239
        ...             'puppy1': {'name': 'son of rover', 'age': 0.0},
 
2240
        ...             'puppy2': {'name': 'son of rover', 'age': 0.0},
 
2241
        ...             'puppy3': {'name': 'son of rover', 'age': 0.0},
 
2242
        ...         },
 
2243
        ...         'dog2': {
 
2244
        ...             'fleas': True,
 
2245
        ...             'tail': 'long',
 
2246
        ...             'name': 'rover',
 
2247
        ...             'puppy1': {'name': 'son of rover', 'age': 0.0},
 
2248
        ...             'puppy2': {'name': 'son of rover', 'age': 0.0},
 
2249
        ...             'puppy3': {'name': 'son of rover', 'age': 0.0},
 
2250
        ...         },
 
2251
        ...         'dog3': {
 
2252
        ...             'fleas': True,
 
2253
        ...             'tail': 'long',
 
2254
        ...             'name': 'rover',
 
2255
        ...             'puppy1': {'name': 'son of rover', 'age': 0.0},
 
2256
        ...             'puppy2': {'name': 'son of rover', 'age': 0.0},
 
2257
        ...             'puppy3': {'name': 'son of rover', 'age': 0.0},
 
2258
        ...         },
 
2259
        ...     },
 
2260
        ...     'cats': {
 
2261
        ...         'cat1': {
 
2262
        ...             'fleas': True,
 
2263
        ...             'tail': 'short',
 
2264
        ...             'name': 'pussy',
 
2265
        ...             'kitten1': {'name': 'son of pussy', 'age': 0.0},
 
2266
        ...             'kitten2': {'name': 'son of pussy', 'age': 0.0},
 
2267
        ...             'kitten3': {'name': 'son of pussy', 'age': 0.0},
 
2268
        ...         },
 
2269
        ...         'cat2': {
 
2270
        ...             'fleas': True,
 
2271
        ...             'tail': 'short',
 
2272
        ...             'name': 'pussy',
 
2273
        ...             'kitten1': {'name': 'son of pussy', 'age': 0.0},
 
2274
        ...             'kitten2': {'name': 'son of pussy', 'age': 0.0},
 
2275
        ...             'kitten3': {'name': 'son of pussy', 'age': 0.0},
 
2276
        ...         },
 
2277
        ...         'cat3': {
 
2278
        ...             'fleas': True,
 
2279
        ...             'tail': 'short',
 
2280
        ...             'name': 'pussy',
 
2281
        ...             'kitten1': {'name': 'son of pussy', 'age': 0.0},
 
2282
        ...             'kitten2': {'name': 'son of pussy', 'age': 0.0},
 
2283
        ...             'kitten3': {'name': 'son of pussy', 'age': 0.0},
 
2284
        ...         },
 
2285
        ...     },
 
2286
        ... }
 
2287
        1
 
2288
        >>> repeater = ConfigObj(repeated_3, configspec=repeated_1)
 
2289
        >>> repeater.validate(val)
 
2290
        1
 
2291
        >>> repeater == {
 
2292
        ...     'cats': {
 
2293
        ...         'cat1': {'fleas': True, 'tail': 'short', 'name': 'pussy'},
 
2294
        ...         'cat2': {'fleas': True, 'tail': 'short', 'name': 'pussy'},
 
2295
        ...         'cat3': {'fleas': True, 'tail': 'short', 'name': 'pussy'},
 
2296
        ...     },
 
2297
        ...     'dogs': {
 
2298
        ...         'dog1': {'fleas': True, 'tail': 'long', 'name': 'rover'},
 
2299
        ...         'dog2': {'fleas': True, 'tail': 'long', 'name': 'rover'},
 
2300
        ...         'dog3': {'fleas': True, 'tail': 'long', 'name': 'rover'},
 
2301
        ...     },
 
2302
        ... }
 
2303
        1
 
2304
        >>> repeater = ConfigObj(configspec=repeated_4)
 
2305
        >>> repeater['Michael'] = {}
 
2306
        >>> repeater.validate(val)
 
2307
        1
 
2308
        >>> repeater == {
 
2309
        ...     'Michael': {'age': 0.0, 'name': 'Michael', 'sex': 'm'},
 
2310
        ... }
 
2311
        1
 
2312
        >>> repeater = ConfigObj(repeated_3, configspec=repeated_5)
 
2313
        >>> repeater == {
 
2314
        ...     'dogs': {'dog1': {}, 'dog2': {}, 'dog3': {}},
 
2315
        ...     'cats': {'cat1': {}, 'cat2': {}, 'cat3': {}},
 
2316
        ... }
 
2317
        1
 
2318
        >>> repeater.validate(val)
 
2319
        1
 
2320
        >>> repeater == {
 
2321
        ...     'dogs': {'dog1': {}, 'dog2': {}, 'dog3': {}},
 
2322
        ...     'cats': {
 
2323
        ...         'cat1': {
 
2324
        ...             'fleas': True,
 
2325
        ...             'tail': 'short',
 
2326
        ...             'name': 'pussy',
 
2327
        ...             'description': {
 
2328
        ...                 'weight': 6.0,
 
2329
        ...                 'height': 3.2999999999999998,
 
2330
        ...                 'coat': {'fur': 'black', 'condition': 5},
 
2331
        ...             },
 
2332
        ...         },
 
2333
        ...         'cat2': {
 
2334
        ...             'fleas': True,
 
2335
        ...             'tail': 'short',
 
2336
        ...             'name': 'pussy',
 
2337
        ...             'description': {
 
2338
        ...                 'weight': 6.0,
 
2339
        ...                 'height': 3.2999999999999998,
 
2340
        ...                 'coat': {'fur': 'black', 'condition': 5},
 
2341
        ...             },
 
2342
        ...         },
 
2343
        ...         'cat3': {
 
2344
        ...             'fleas': True,
 
2345
        ...             'tail': 'short',
 
2346
        ...             'name': 'pussy',
 
2347
        ...             'description': {
 
2348
        ...                 'weight': 6.0,
 
2349
        ...                 'height': 3.2999999999999998,
 
2350
        ...                 'coat': {'fur': 'black', 'condition': 5},
 
2351
        ...             },
 
2352
        ...         },
 
2353
        ...     },
 
2354
        ... }
 
2355
        1
 
2356
        
 
2357
        Test that interpolation is preserved for validated string values.
 
2358
        Also check that interpolation works in configspecs.
 
2359
        >>> t = ConfigObj()
 
2360
        >>> t['DEFAULT'] = {}
 
2361
        >>> t['DEFAULT']['test'] = 'a'
 
2362
        >>> t['test'] = '%(test)s'
 
2363
        >>> t['test']
 
2364
        'a'
 
2365
        >>> v = Validator()
 
2366
        >>> t.configspec = {'test': 'string'}
 
2367
        >>> t.validate(v)
 
2368
        1
 
2369
        >>> t.interpolation = False
 
2370
        >>> t
 
2371
        {'test': '%(test)s', 'DEFAULT': {'test': 'a'}}
 
2372
        >>> specs = [
 
2373
        ...    'interpolated string  = string(default="fuzzy-%(man)s")',
 
2374
        ...    '[DEFAULT]',
 
2375
        ...    'man = wuzzy',
 
2376
        ...    ]
 
2377
        >>> c = ConfigObj(configspec=specs)
 
2378
        >>> c.validate(v)
 
2379
        1
 
2380
        >>> c['interpolated string']
 
2381
        'fuzzy-wuzzy'
 
2382
        
 
2383
        FIXME: Above tests will fail if we couldn't import Validator (the ones
 
2384
        that don't raise errors will produce different output and still fail as
 
2385
        tests)
2192
2386
        """
2193
2387
        if section is None:
2194
2388
            if self.configspec is None:
2195
 
                raise ValueError('No configspec supplied.')
 
2389
                raise ValueError, 'No configspec supplied.'
2196
2390
            if preserve_errors:
2197
 
                # We do this once to remove a top level dependency on the validate module
2198
 
                # Which makes importing configobj faster
2199
 
                from validate import VdtMissingValue
2200
 
                self._vdtMissingValue = VdtMissingValue
 
2391
                if VdtMissingValue is None:
 
2392
                    raise ImportError('Missing validate module.')
2201
2393
            section = self
2202
2394
        #
2203
2395
        spec_section = section.configspec
2204
 
        if copy and getattr(section, '_configspec_initial_comment', None) is not None:
2205
 
            section.initial_comment = section._configspec_initial_comment
2206
 
            section.final_comment = section._configspec_final_comment
2207
 
            section.encoding = section._configspec_encoding
2208
 
            section.BOM = section._configspec_BOM
2209
 
            section.newlines = section._configspec_newlines
2210
 
            section.indent_type = section._configspec_indent_type
2211
 
            
2212
2396
        if '__many__' in section.configspec:
2213
2397
            many = spec_section['__many__']
2214
2398
            # dynamically assign the configspecs
2219
2403
        out = {}
2220
2404
        ret_true = True
2221
2405
        ret_false = True
2222
 
        order = [k for k in section._order if k in spec_section]
2223
 
        order += [k for k in spec_section if k not in order]
2224
 
        for entry in order:
 
2406
        for entry in spec_section:
2225
2407
            if entry == '__many__':
2226
2408
                continue
2227
2409
            if (not entry in section.scalars) or (entry in section.defaults):
2229
2411
                # or entries from defaults
2230
2412
                missing = True
2231
2413
                val = None
2232
 
                if copy and not entry in section.scalars:
2233
 
                    # copy comments
2234
 
                    section.comments[entry] = (
2235
 
                        section._configspec_comments.get(entry, []))
2236
 
                    section.inline_comments[entry] = (
2237
 
                        section._configspec_inline_comments.get(entry, ''))
2238
 
                #
2239
2414
            else:
2240
2415
                missing = False
2241
2416
                val = section[entry]
2245
2420
                                        missing=missing
2246
2421
                                        )
2247
2422
            except validator.baseErrorClass, e:
2248
 
                if not preserve_errors or isinstance(e, self._vdtMissingValue):
 
2423
                if not preserve_errors or isinstance(e, VdtMissingValue):
2249
2424
                    out[entry] = False
2250
2425
                else:
2251
2426
                    # preserve the error
2253
2428
                    ret_false = False
2254
2429
                ret_true = False
2255
2430
            else:
2256
 
                try: 
2257
 
                    section.default_values.pop(entry, None)
2258
 
                except AttributeError: 
2259
 
                    # For Python 2.2 compatibility
2260
 
                    try:
2261
 
                        del section.default_values[entry]
2262
 
                    except KeyError:
2263
 
                        pass
2264
 
                    
2265
 
                if getattr(validator, 'get_default_value', None) is not None:
2266
 
                    try: 
2267
 
                        section.default_values[entry] = validator.get_default_value(spec_section[entry])
2268
 
                    except KeyError:
2269
 
                        # No default
2270
 
                        pass
2271
 
                    
2272
2431
                ret_false = False
2273
2432
                out[entry] = True
2274
2433
                if self.stringify or missing:
2285
2444
                            check = self._str(check)
2286
2445
                    if (check != val) or missing:
2287
2446
                        section[entry] = check
2288
 
                if not copy and missing and entry not in section.defaults:
 
2447
                if missing and entry not in section.defaults:
2289
2448
                    section.defaults.append(entry)
2290
 
        # Missing sections will have been created as empty ones when the
2291
 
        # configspec was read.
 
2449
        #
 
2450
        # FIXME: Will this miss missing sections ?
2292
2451
        for entry in section.sections:
2293
 
            # FIXME: this means DEFAULT is not copied in copy mode
2294
2452
            if section is self and entry == 'DEFAULT':
2295
2453
                continue
2296
 
            if copy:
2297
 
                section.comments[entry] = section._cs_section_comments[entry]
2298
 
                section.inline_comments[entry] = (
2299
 
                    section._cs_section_inline_comments[entry])
2300
2454
            check = self.validate(validator, preserve_errors=preserve_errors,
2301
 
                copy=copy, section=section[entry])
 
2455
                section=section[entry])
2302
2456
            out[entry] = check
2303
2457
            if check == False:
2304
2458
                ret_true = False
2312
2466
            return True
2313
2467
        elif ret_false:
2314
2468
            return False
2315
 
        return out
2316
 
 
2317
 
 
2318
 
    def reset(self):
2319
 
        """Clear ConfigObj instance and restore to 'freshly created' state."""
2320
 
        self.clear()
2321
 
        self._initialise()
2322
 
        # FIXME: Should be done by '_initialise', but ConfigObj constructor (and reload)
2323
 
        #        requires an empty dictionary
2324
 
        self.configspec = None
2325
 
        # Just to be sure ;-)
2326
 
        self._original_configspec = None
2327
 
        
2328
 
        
2329
 
    def reload(self):
2330
 
        """
2331
 
        Reload a ConfigObj from file.
2332
 
        
2333
 
        This method raises a ``ReloadError`` if the ConfigObj doesn't have
2334
 
        a filename attribute pointing to a file.
2335
 
        """
2336
 
        if not isinstance(self.filename, StringTypes):
2337
 
            raise ReloadError()
2338
 
 
2339
 
        filename = self.filename
2340
 
        current_options = {}
2341
 
        for entry in OPTION_DEFAULTS:
2342
 
            if entry == 'configspec':
2343
 
                continue
2344
 
            current_options[entry] = getattr(self, entry)
2345
 
            
2346
 
        configspec = self._original_configspec
2347
 
        current_options['configspec'] = configspec
2348
 
            
2349
 
        self.clear()
2350
 
        self._initialise(current_options)
2351
 
        self._load(filename, configspec)
2352
 
        
2353
 
 
 
2469
        else:
 
2470
            return out
2354
2471
 
2355
2472
class SimpleVal(object):
2356
2473
    """
2362
2479
    method of your ``ConfigObj``. ``validate`` will return ``True`` if all
2363
2480
    members are present, or a dictionary with True/False meaning
2364
2481
    present/missing. (Whole missing sections will be replaced with ``False``)
 
2482
    
 
2483
    >>> val = SimpleVal()
 
2484
    >>> config = '''
 
2485
    ... test1=40
 
2486
    ... test2=hello
 
2487
    ... test3=3
 
2488
    ... test4=5.0
 
2489
    ... [section]
 
2490
    ... test1=40
 
2491
    ... test2=hello
 
2492
    ... test3=3
 
2493
    ... test4=5.0
 
2494
    ...     [[sub section]]
 
2495
    ...     test1=40
 
2496
    ...     test2=hello
 
2497
    ...     test3=3
 
2498
    ...     test4=5.0
 
2499
    ... '''.split('\\n')
 
2500
    >>> configspec = '''
 
2501
    ... test1=''
 
2502
    ... test2=''
 
2503
    ... test3=''
 
2504
    ... test4=''
 
2505
    ... [section]
 
2506
    ... test1=''
 
2507
    ... test2=''
 
2508
    ... test3=''
 
2509
    ... test4=''
 
2510
    ...     [[sub section]]
 
2511
    ...     test1=''
 
2512
    ...     test2=''
 
2513
    ...     test3=''
 
2514
    ...     test4=''
 
2515
    ... '''.split('\\n')
 
2516
    >>> o = ConfigObj(config, configspec=configspec)
 
2517
    >>> o.validate(val)
 
2518
    1
 
2519
    >>> o = ConfigObj(configspec=configspec)
 
2520
    >>> o.validate(val)
 
2521
    0
2365
2522
    """
2366
2523
    
2367
2524
    def __init__(self):
2370
2527
    def check(self, check, member, missing=False):
2371
2528
        """A dummy check method, always returns the value unchanged."""
2372
2529
        if missing:
2373
 
            raise self.baseErrorClass()
 
2530
            raise self.baseErrorClass
2374
2531
        return member
2375
2532
 
2376
 
 
2377
2533
# Check / processing functions for options
2378
2534
def flatten_errors(cfg, res, levels=None, results=None):
2379
2535
    """
2407
2563
    
2408
2564
    For example *The value "3" is of the wrong type*.
2409
2565
    
 
2566
    # FIXME: is the ordering of the output arbitrary ?
2410
2567
    >>> import validate
2411
2568
    >>> vtor = validate.Validator()
2412
2569
    >>> my_ini = '''
2498
2655
    return results
2499
2656
 
2500
2657
 
2501
 
"""*A programming language is a medium of expression.* - Paul Graham"""
 
2658
# FIXME: test error code for badly built multiline values
 
2659
# FIXME: test handling of StringIO
 
2660
# FIXME: test interpolation with writing
 
2661
 
 
2662
def _doctest():
 
2663
    """
 
2664
    Dummy function to hold some of the doctests.
 
2665
    
 
2666
    >>> a.depth
 
2667
    0
 
2668
    >>> a == {
 
2669
    ...     'key2': 'val',
 
2670
    ...     'key1': 'val',
 
2671
    ...     'lev1c': {
 
2672
    ...         'lev2c': {
 
2673
    ...             'lev3c': {
 
2674
    ...                 'key1': 'val',
 
2675
    ...             },
 
2676
    ...         },
 
2677
    ...     },
 
2678
    ...     'lev1b': {
 
2679
    ...         'key2': 'val',
 
2680
    ...         'key1': 'val',
 
2681
    ...         'lev2ba': {
 
2682
    ...             'key1': 'val',
 
2683
    ...         },
 
2684
    ...         'lev2bb': {
 
2685
    ...             'key1': 'val',
 
2686
    ...         },
 
2687
    ...     },
 
2688
    ...     'lev1a': {
 
2689
    ...         'key2': 'val',
 
2690
    ...         'key1': 'val',
 
2691
    ...     },
 
2692
    ... }
 
2693
    1
 
2694
    >>> b.depth
 
2695
    0
 
2696
    >>> b == {
 
2697
    ...     'key3': 'val3',
 
2698
    ...     'key2': 'val2',
 
2699
    ...     'key1': 'val1',
 
2700
    ...     'section 1': {
 
2701
    ...         'keys11': 'val1',
 
2702
    ...         'keys13': 'val3',
 
2703
    ...         'keys12': 'val2',
 
2704
    ...     },
 
2705
    ...     'section 2': {
 
2706
    ...         'section 2 sub 1': {
 
2707
    ...             'fish': '3',
 
2708
    ...     },
 
2709
    ...     'keys21': 'val1',
 
2710
    ...     'keys22': 'val2',
 
2711
    ...     'keys23': 'val3',
 
2712
    ...     },
 
2713
    ... }
 
2714
    1
 
2715
    >>> t = '''
 
2716
    ... 'a' = b # !"$%^&*(),::;'@~#= 33
 
2717
    ... "b" = b #= 6, 33
 
2718
    ... ''' .split('\\n')
 
2719
    >>> t2 = ConfigObj(t)
 
2720
    >>> assert t2 == {'a': 'b', 'b': 'b'}
 
2721
    >>> t2.inline_comments['b'] = ''
 
2722
    >>> del t2['a']
 
2723
    >>> assert t2.write() == ['','b = b', '']
 
2724
    
 
2725
    # Test ``list_values=False`` stuff
 
2726
    >>> c = '''
 
2727
    ...     key1 = no quotes
 
2728
    ...     key2 = 'single quotes'
 
2729
    ...     key3 = "double quotes"
 
2730
    ...     key4 = "list", 'with', several, "quotes"
 
2731
    ...     '''
 
2732
    >>> cfg = ConfigObj(c.splitlines(), list_values=False)
 
2733
    >>> cfg == {'key1': 'no quotes', 'key2': "'single quotes'", 
 
2734
    ... 'key3': '"double quotes"', 
 
2735
    ... 'key4': '"list", \\'with\\', several, "quotes"'
 
2736
    ... }
 
2737
    1
 
2738
    >>> cfg = ConfigObj(list_values=False)
 
2739
    >>> cfg['key1'] = 'Multiline\\nValue'
 
2740
    >>> cfg['key2'] = '''"Value" with 'quotes' !'''
 
2741
    >>> cfg.write()
 
2742
    ["key1 = '''Multiline\\nValue'''", 'key2 = "Value" with \\'quotes\\' !']
 
2743
    >>> cfg.list_values = True
 
2744
    >>> cfg.write() == ["key1 = '''Multiline\\nValue'''",
 
2745
    ... 'key2 = \\'\\'\\'"Value" with \\'quotes\\' !\\'\\'\\'']
 
2746
    1
 
2747
    
 
2748
    Test flatten_errors:
 
2749
    
 
2750
    >>> from validate import Validator, VdtValueTooSmallError
 
2751
    >>> config = '''
 
2752
    ...     test1=40
 
2753
    ...     test2=hello
 
2754
    ...     test3=3
 
2755
    ...     test4=5.0
 
2756
    ...     [section]
 
2757
    ...         test1=40
 
2758
    ...         test2=hello
 
2759
    ...         test3=3
 
2760
    ...         test4=5.0
 
2761
    ...         [[sub section]]
 
2762
    ...             test1=40
 
2763
    ...             test2=hello
 
2764
    ...             test3=3
 
2765
    ...             test4=5.0
 
2766
    ... '''.split('\\n')
 
2767
    >>> configspec = '''
 
2768
    ...     test1= integer(30,50)
 
2769
    ...     test2= string
 
2770
    ...     test3=integer
 
2771
    ...     test4=float(6.0)
 
2772
    ...     [section ]
 
2773
    ...         test1=integer(30,50)
 
2774
    ...         test2=string
 
2775
    ...         test3=integer
 
2776
    ...         test4=float(6.0)
 
2777
    ...         [[sub section]]
 
2778
    ...             test1=integer(30,50)
 
2779
    ...             test2=string
 
2780
    ...             test3=integer
 
2781
    ...             test4=float(6.0)
 
2782
    ...     '''.split('\\n')
 
2783
    >>> val = Validator()
 
2784
    >>> c1 = ConfigObj(config, configspec=configspec)
 
2785
    >>> res = c1.validate(val)
 
2786
    >>> flatten_errors(c1, res) == [([], 'test4', False), (['section', 
 
2787
    ...     'sub section'], 'test4', False), (['section'], 'test4', False)]
 
2788
    True
 
2789
    >>> res = c1.validate(val, preserve_errors=True)
 
2790
    >>> check = flatten_errors(c1, res)
 
2791
    >>> check[0][:2]
 
2792
    ([], 'test4')
 
2793
    >>> check[1][:2]
 
2794
    (['section', 'sub section'], 'test4')
 
2795
    >>> check[2][:2]
 
2796
    (['section'], 'test4')
 
2797
    >>> for entry in check:
 
2798
    ...     isinstance(entry[2], VdtValueTooSmallError)
 
2799
    ...     print str(entry[2])
 
2800
    True
 
2801
    the value "5.0" is too small.
 
2802
    True
 
2803
    the value "5.0" is too small.
 
2804
    True
 
2805
    the value "5.0" is too small.
 
2806
    
 
2807
    Test unicode handling, BOM, write witha file like object and line endings :
 
2808
    >>> u_base = '''
 
2809
    ... # initial comment
 
2810
    ...     # inital comment 2
 
2811
    ... 
 
2812
    ... test1 = some value
 
2813
    ... # comment
 
2814
    ... test2 = another value    # inline comment
 
2815
    ... # section comment
 
2816
    ... [section]    # inline comment
 
2817
    ...     test = test    # another inline comment
 
2818
    ...     test2 = test2
 
2819
    ... 
 
2820
    ... # final comment
 
2821
    ... # final comment2
 
2822
    ... '''
 
2823
    >>> u = u_base.encode('utf_8').splitlines(True)
 
2824
    >>> u[0] = BOM_UTF8 + u[0]
 
2825
    >>> uc = ConfigObj(u)
 
2826
    >>> uc.encoding = None
 
2827
    >>> uc.BOM == True
 
2828
    1
 
2829
    >>> uc == {'test1': 'some value', 'test2': 'another value',
 
2830
    ... 'section': {'test': 'test', 'test2': 'test2'}}
 
2831
    1
 
2832
    >>> uc = ConfigObj(u, encoding='utf_8', default_encoding='latin-1')
 
2833
    >>> uc.BOM
 
2834
    1
 
2835
    >>> isinstance(uc['test1'], unicode)
 
2836
    1
 
2837
    >>> uc.encoding
 
2838
    'utf_8'
 
2839
    >>> uc.newlines
 
2840
    '\\n'
 
2841
    >>> uc['latin1'] = "This costs lot's of "
 
2842
    >>> a_list = uc.write()
 
2843
    >>> len(a_list)
 
2844
    15
 
2845
    >>> isinstance(a_list[0], str)
 
2846
    1
 
2847
    >>> a_list[0].startswith(BOM_UTF8)
 
2848
    1
 
2849
    >>> u = u_base.replace('\\n', '\\r\\n').encode('utf_8').splitlines(True)
 
2850
    >>> uc = ConfigObj(u)
 
2851
    >>> uc.newlines
 
2852
    '\\r\\n'
 
2853
    >>> uc.newlines = '\\r'
 
2854
    >>> from cStringIO import StringIO
 
2855
    >>> file_like = StringIO()
 
2856
    >>> uc.write(file_like)
 
2857
    >>> file_like.seek(0)
 
2858
    >>> uc2 = ConfigObj(file_like)
 
2859
    >>> uc2 == uc
 
2860
    1
 
2861
    >>> uc2.filename is None
 
2862
    1
 
2863
    >>> uc2.newlines == '\\r'
 
2864
    1
 
2865
    """
 
2866
 
 
2867
if __name__ == '__main__':
 
2868
    # run the code tests in doctest format
 
2869
    #
 
2870
    testconfig1 = """\
 
2871
    key1= val    # comment 1
 
2872
    key2= val    # comment 2
 
2873
    # comment 3
 
2874
    [lev1a]     # comment 4
 
2875
    key1= val    # comment 5
 
2876
    key2= val    # comment 6
 
2877
    # comment 7
 
2878
    [lev1b]    # comment 8
 
2879
    key1= val    # comment 9
 
2880
    key2= val    # comment 10
 
2881
    # comment 11
 
2882
        [[lev2ba]]    # comment 12
 
2883
        key1= val    # comment 13
 
2884
        # comment 14
 
2885
        [[lev2bb]]    # comment 15
 
2886
        key1= val    # comment 16
 
2887
    # comment 17
 
2888
    [lev1c]    # comment 18
 
2889
    # comment 19
 
2890
        [[lev2c]]    # comment 20
 
2891
        # comment 21
 
2892
            [[[lev3c]]]    # comment 22
 
2893
            key1 = val    # comment 23"""
 
2894
    #
 
2895
    testconfig2 = """\
 
2896
                        key1 = 'val1'
 
2897
                        key2 =   "val2"
 
2898
                        key3 = val3
 
2899
                        ["section 1"] # comment
 
2900
                        keys11 = val1
 
2901
                        keys12 = val2
 
2902
                        keys13 = val3
 
2903
                        [section 2]
 
2904
                        keys21 = val1
 
2905
                        keys22 = val2
 
2906
                        keys23 = val3
 
2907
                        
 
2908
                            [['section 2 sub 1']]
 
2909
                            fish = 3
 
2910
    """
 
2911
    #
 
2912
    testconfig6 = '''
 
2913
    name1 = """ a single line value """ # comment
 
2914
    name2 = \''' another single line value \''' # comment
 
2915
    name3 = """ a single line value """
 
2916
    name4 = \''' another single line value \'''
 
2917
        [ "multi section" ]
 
2918
        name1 = """
 
2919
        Well, this is a
 
2920
        multiline value
 
2921
        """
 
2922
        name2 = \'''
 
2923
        Well, this is a
 
2924
        multiline value
 
2925
        \'''
 
2926
        name3 = """
 
2927
        Well, this is a
 
2928
        multiline value
 
2929
        """     # a comment
 
2930
        name4 = \'''
 
2931
        Well, this is a
 
2932
        multiline value
 
2933
        \'''  # I guess this is a comment too
 
2934
    '''
 
2935
    #
 
2936
    import doctest
 
2937
    m = sys.modules.get('__main__')
 
2938
    globs = m.__dict__.copy()
 
2939
    a = ConfigObj(testconfig1.split('\n'), raise_errors=True)
 
2940
    b = ConfigObj(testconfig2.split('\n'), raise_errors=True)
 
2941
    i = ConfigObj(testconfig6.split('\n'), raise_errors=True)
 
2942
    globs.update({
 
2943
        'INTP_VER': INTP_VER,
 
2944
        'a': a,
 
2945
        'b': b,
 
2946
        'i': i,
 
2947
    })
 
2948
    doctest.testmod(m, globs=globs)
 
2949
 
 
2950
"""
 
2951
    BUGS
 
2952
    ====
 
2953
    
 
2954
    None known.
 
2955
    
 
2956
    TODO
 
2957
    ====
 
2958
    
 
2959
    Better support for configuration from multiple files, including tracking
 
2960
    *where* the original file came from and writing changes to the correct
 
2961
    file.
 
2962
    
 
2963
    
 
2964
    Make ``newline`` an option (as well as an attribute) ?
 
2965
    
 
2966
    ``UTF16`` encoded files, when returned as a list of lines, will have the
 
2967
    BOM at the start of every line. Should this be removed from all but the
 
2968
    first line ?
 
2969
    
 
2970
    Option to set warning type for unicode decode ? (Defaults to strict).
 
2971
    
 
2972
    A method to optionally remove uniform indentation from multiline values.
 
2973
    (do as an example of using ``walk`` - along with string-escape)
 
2974
    
 
2975
    Should the results dictionary from validate be an ordered dictionary if
 
2976
    `odict <http://www.voidspace.org.uk/python/odict.html>`_ is available ?
 
2977
    
 
2978
    Implement a better ``__repr__`` ? (``ConfigObj({})``)
 
2979
    
 
2980
    Implement some of the sequence methods (which include slicing) from the
 
2981
    newer ``odict`` ?
 
2982
    
 
2983
    INCOMPATIBLE CHANGES
 
2984
    ====================
 
2985
    
 
2986
    (I have removed a lot of needless complications - this list is probably not
 
2987
    conclusive, many option/attribute/method names have changed)
 
2988
    
 
2989
    Case sensitive
 
2990
    
 
2991
    The only valid divider is '='
 
2992
    
 
2993
    We've removed line continuations with '\'
 
2994
    
 
2995
    No recursive lists in values
 
2996
    
 
2997
    No empty section
 
2998
    
 
2999
    No distinction between flatfiles and non flatfiles
 
3000
    
 
3001
    Change in list syntax - use commas to indicate list, not parentheses
 
3002
    (square brackets and parentheses are no longer recognised as lists)
 
3003
    
 
3004
    ';' is no longer valid for comments and no multiline comments
 
3005
    
 
3006
    No attribute access
 
3007
    
 
3008
    We don't allow empty values - have to use '' or ""
 
3009
    
 
3010
    In ConfigObj 3 - setting a non-flatfile member to ``None`` would
 
3011
    initialise it as an empty section.
 
3012
    
 
3013
    The escape entities '&mjf-lf;' and '&mjf-quot;' have gone
 
3014
    replaced by triple quote, multiple line values.
 
3015
    
 
3016
    The ``newline``, ``force_return``, and ``default`` options have gone
 
3017
    
 
3018
    The ``encoding`` and ``backup_encoding`` methods have gone - replaced
 
3019
    with the ``encode`` and ``decode`` methods.
 
3020
    
 
3021
    ``fileerror`` and ``createempty`` options have become ``file_error`` and
 
3022
    ``create_empty``
 
3023
    
 
3024
    Partial configspecs (for specifying the order members should be written
 
3025
    out and which should be present) have gone. The configspec is no longer
 
3026
    used to specify order for the ``write`` method.
 
3027
    
 
3028
    Exceeding the maximum depth of recursion in string interpolation now
 
3029
    raises an error ``InterpolationDepthError``.
 
3030
    
 
3031
    Specifying a value for interpolation which doesn't exist now raises an
 
3032
    error ``MissingInterpolationOption`` (instead of merely being ignored).
 
3033
    
 
3034
    The ``writein`` method has been removed.
 
3035
    
 
3036
    The comments attribute is now a list (``inline_comments`` equates to the
 
3037
    old comments attribute)
 
3038
    
 
3039
    ISSUES
 
3040
    ======
 
3041
    
 
3042
    ``validate`` doesn't report *extra* values or sections.
 
3043
    
 
3044
    You can't have a keyword with the same name as a section (in the same
 
3045
    section). They are both dictionary keys - so they would overlap.
 
3046
    
 
3047
    ConfigObj doesn't quote and unquote values if ``list_values=False``.
 
3048
    This means that leading or trailing whitespace in values will be lost when
 
3049
    writing. (Unless you manually quote).
 
3050
    
 
3051
    Interpolation checks first the 'DEFAULT' subsection of the current
 
3052
    section, next it checks the 'DEFAULT' section of the parent section,
 
3053
    last it checks the 'DEFAULT' section of the main section.
 
3054
    
 
3055
    Logically a 'DEFAULT' section should apply to all subsections of the *same
 
3056
    parent* - this means that checking the 'DEFAULT' subsection in the
 
3057
    *current section* is not necessarily logical ?
 
3058
    
 
3059
    In order to simplify unicode support (which is possibly of limited value
 
3060
    in a config file) I have removed automatic support and added the
 
3061
    ``encode`` and ``decode methods, which can be used to transform keys and
 
3062
    entries. Because the regex looks for specific values on inital parsing
 
3063
    (i.e. the quotes and the equals signs) it can only read ascii compatible
 
3064
    encodings. For unicode use ``UTF8``, which is ASCII compatible.
 
3065
    
 
3066
    Does it matter that we don't support the ':' divider, which is supported
 
3067
    by ``ConfigParser`` ?
 
3068
    
 
3069
    The regular expression correctly removes the value -
 
3070
    ``"'hello', 'goodbye'"`` and then unquote just removes the front and
 
3071
    back quotes (called from ``_handle_value``). What should we do ??
 
3072
    (*ought* to raise exception because it's an invalid value if lists are
 
3073
    off *sigh*. This is not what you want if you want to do your own list
 
3074
    processing - would be *better* in this case not to unquote.)
 
3075
    
 
3076
    String interpolation and validation don't play well together. When
 
3077
    validation changes type it sets the value. This will correctly fetch the
 
3078
    value using interpolation - but then overwrite the interpolation reference.
 
3079
    If the value is unchanged by validation (it's a string) - but other types
 
3080
    will be.
 
3081
    
 
3082
    
 
3083
    List Value Syntax
 
3084
    =================
 
3085
    
 
3086
    List values allow you to specify multiple values for a keyword. This
 
3087
    maps to a list as the resulting Python object when parsed.
 
3088
    
 
3089
    The syntax for lists is easy. A list is a comma separated set of values.
 
3090
    If these values contain quotes, the hash mark, or commas, then the values
 
3091
    can be surrounded by quotes. e.g. : ::
 
3092
    
 
3093
        keyword = value1, 'value 2', "value 3"
 
3094
    
 
3095
    If a value needs to be a list, but only has one member, then you indicate
 
3096
    this with a trailing comma. e.g. : ::
 
3097
    
 
3098
        keyword = "single value",
 
3099
    
 
3100
    If a value needs to be a list, but it has no members, then you indicate
 
3101
    this with a single comma. e.g. : ::
 
3102
    
 
3103
        keyword = ,     # an empty list
 
3104
    
 
3105
    Using triple quotes it will be possible for single values to contain
 
3106
    newlines and *both* single quotes and double quotes. Triple quotes aren't
 
3107
    allowed in list values. This means that the members of list values can't
 
3108
    contain carriage returns (or line feeds :-) or both quote values.
 
3109
      
 
3110
    CHANGELOG
 
3111
    =========
 
3112
    
 
3113
    2006/02/04
 
3114
    ----------
 
3115
    
 
3116
    Removed ``BOM_UTF8`` from ``__all__``.
 
3117
    
 
3118
    The ``BOM`` attribute has become a boolean. (Defaults to ``False``.) It is
 
3119
    *only* ``True`` for the ``UTF16`` encoding.
 
3120
    
 
3121
    File like objects no longer need a ``seek`` attribute.
 
3122
    
 
3123
    ConfigObj no longer keeps a reference to file like objects. Instead the
 
3124
    ``write`` method takes a file like object as an optional argument. (Which
 
3125
    will be used in preference of the ``filename`` attribute if htat exists as
 
3126
    well.)
 
3127
    
 
3128
    Full unicode support added. New options/attributes ``encoding``,
 
3129
    ``default_encoding``.
 
3130
    
 
3131
    utf16 files decoded to unicode.
 
3132
    
 
3133
    If ``BOM`` is ``True``, but no encoding specified, then the utf8 BOM is
 
3134
    written out at the start of the file. (It will normally only be ``True`` if
 
3135
    the utf8 BOM was found when the file was read.)
 
3136
    
 
3137
    File paths are *not* converted to absolute paths, relative paths will
 
3138
    remain relative as the ``filename`` attribute.
 
3139
    
 
3140
    Fixed bug where ``final_comment`` wasn't returned if ``write`` is returning
 
3141
    a list of lines.
 
3142
    
 
3143
    2006/01/31
 
3144
    ----------
 
3145
    
 
3146
    Added ``True``, ``False``, and ``enumerate`` if they are not defined.
 
3147
    (``True`` and ``False`` are needed for *early* versions of Python 2.2,
 
3148
    ``enumerate`` is needed for all versions ofPython 2.2)
 
3149
    
 
3150
    Deprecated ``istrue``, replaced it with ``as_bool``.
 
3151
    
 
3152
    Added ``as_int`` and ``as_float``.
 
3153
    
 
3154
    utf8 and utf16 BOM handled in an endian agnostic way.
 
3155
    
 
3156
    2005/12/14
 
3157
    ----------
 
3158
    
 
3159
    Validation no longer done on the 'DEFAULT' section (only in the root
 
3160
    level). This allows interpolation in configspecs.
 
3161
    
 
3162
    Change in validation syntax implemented in validate 0.2.1
 
3163
    
 
3164
    4.1.0
 
3165
    
 
3166
    2005/12/10
 
3167
    ----------
 
3168
    
 
3169
    Added ``merge``, a recursive update.
 
3170
    
 
3171
    Added ``preserve_errors`` to ``validate`` and the ``flatten_errors``
 
3172
    example function.
 
3173
    
 
3174
    Thanks to Matthew Brett for suggestions and helping me iron out bugs.
 
3175
    
 
3176
    Fixed bug where a config file is *all* comment, the comment will now be
 
3177
    ``initial_comment`` rather than ``final_comment``.
 
3178
    
 
3179
    2005/12/02
 
3180
    ----------
 
3181
    
 
3182
    Fixed bug in ``create_empty``. Thanks to Paul Jimenez for the report.
 
3183
    
 
3184
    2005/11/04
 
3185
    ----------
 
3186
    
 
3187
    Fixed bug in ``Section.walk`` when transforming names as well as values.
 
3188
    
 
3189
    Added the ``istrue`` method. (Fetches the boolean equivalent of a string
 
3190
    value).
 
3191
    
 
3192
    Fixed ``list_values=False`` - they are now only quoted/unquoted if they
 
3193
    are multiline values.
 
3194
    
 
3195
    List values are written as ``item, item`` rather than ``item,item``.
 
3196
    
 
3197
    4.0.1
 
3198
    
 
3199
    2005/10/09
 
3200
    ----------
 
3201
    
 
3202
    Fixed typo in ``write`` method. (Testing for the wrong value when resetting
 
3203
    ``interpolation``).
 
3204
 
 
3205
    4.0.0 Final
 
3206
    
 
3207
    2005/09/16
 
3208
    ----------
 
3209
    
 
3210
    Fixed bug in ``setdefault`` - creating a new section *wouldn't* return
 
3211
    a reference to the new section.
 
3212
    
 
3213
    2005/09/09
 
3214
    ----------
 
3215
    
 
3216
    Removed ``PositionError``.
 
3217
    
 
3218
    Allowed quotes around keys as documented.
 
3219
    
 
3220
    Fixed bug with commas in comments. (matched as a list value)
 
3221
    
 
3222
    Beta 5
 
3223
    
 
3224
    2005/09/07
 
3225
    ----------
 
3226
    
 
3227
    Fixed bug in initialising ConfigObj from a ConfigObj.
 
3228
    
 
3229
    Changed the mailing list address.
 
3230
    
 
3231
    Beta 4
 
3232
    
 
3233
    2005/09/03
 
3234
    ----------
 
3235
    
 
3236
    Fixed bug in ``Section.__delitem__`` oops.
 
3237
    
 
3238
    2005/08/28
 
3239
    ----------
 
3240
    
 
3241
    Interpolation is switched off before writing out files.
 
3242
    
 
3243
    Fixed bug in handling ``StringIO`` instances. (Thanks to report from
 
3244
    "Gustavo Niemeyer" <gustavo@niemeyer.net>)
 
3245
    
 
3246
    Moved the doctests from the ``__init__`` method to a separate function.
 
3247
    (For the sake of IDE calltips).
 
3248
    
 
3249
    Beta 3
 
3250
    
 
3251
    2005/08/26
 
3252
    ----------
 
3253
    
 
3254
    String values unchanged by validation *aren't* reset. This preserves
 
3255
    interpolation in string values.
 
3256
    
 
3257
    2005/08/18
 
3258
    ----------
 
3259
    
 
3260
    None from a default is turned to '' if stringify is off - because setting 
 
3261
    a value to None raises an error.
 
3262
    
 
3263
    Version 4.0.0-beta2
 
3264
    
 
3265
    2005/08/16
 
3266
    ----------
 
3267
    
 
3268
    By Nicola Larosa
 
3269
    
 
3270
    Actually added the RepeatSectionError class ;-)
 
3271
    
 
3272
    2005/08/15
 
3273
    ----------
 
3274
    
 
3275
    If ``stringify`` is off - list values are preserved by the ``validate``
 
3276
    method. (Bugfix)
 
3277
    
 
3278
    2005/08/14
 
3279
    ----------
 
3280
    
 
3281
    By Michael Foord
 
3282
    
 
3283
    Fixed ``simpleVal``.
 
3284
    
 
3285
    Added ``RepeatSectionError`` error if you have additional sections in a
 
3286
    section with a ``__many__`` (repeated) section.
 
3287
    
 
3288
    By Nicola Larosa
 
3289
    
 
3290
    Reworked the ConfigObj._parse, _handle_error and _multiline methods:
 
3291
    mutated the self._infile, self._index and self._maxline attributes into
 
3292
    local variables and method parameters
 
3293
    
 
3294
    Reshaped the ConfigObj._multiline method to better reflect its semantics
 
3295
    
 
3296
    Changed the "default_test" test in ConfigObj.validate to check the fix for
 
3297
    the bug in validate.Validator.check
 
3298
    
 
3299
    2005/08/13
 
3300
    ----------
 
3301
    
 
3302
    By Nicola Larosa
 
3303
    
 
3304
    Updated comments at top
 
3305
    
 
3306
    2005/08/11
 
3307
    ----------
 
3308
    
 
3309
    By Michael Foord
 
3310
    
 
3311
    Implemented repeated sections.
 
3312
    
 
3313
    By Nicola Larosa
 
3314
    
 
3315
    Added test for interpreter version: raises RuntimeError if earlier than
 
3316
    2.2
 
3317
    
 
3318
    2005/08/10
 
3319
    ----------
 
3320
   
 
3321
    By Michael Foord
 
3322
     
 
3323
    Implemented default values in configspecs.
 
3324
    
 
3325
    By Nicola Larosa
 
3326
    
 
3327
    Fixed naked except: clause in validate that was silencing the fact
 
3328
    that Python2.2 does not have dict.pop
 
3329
    
 
3330
    2005/08/08
 
3331
    ----------
 
3332
    
 
3333
    By Michael Foord
 
3334
    
 
3335
    Bug fix causing error if file didn't exist.
 
3336
    
 
3337
    2005/08/07
 
3338
    ----------
 
3339
    
 
3340
    By Nicola Larosa
 
3341
    
 
3342
    Adjusted doctests for Python 2.2.3 compatibility
 
3343
    
 
3344
    2005/08/04
 
3345
    ----------
 
3346
    
 
3347
    By Michael Foord
 
3348
    
 
3349
    Added the inline_comments attribute
 
3350
    
 
3351
    We now preserve and rewrite all comments in the config file
 
3352
    
 
3353
    configspec is now a section attribute
 
3354
    
 
3355
    The validate method changes values in place
 
3356
    
 
3357
    Added InterpolationError
 
3358
    
 
3359
    The errors now have line number, line, and message attributes. This
 
3360
    simplifies error handling
 
3361
    
 
3362
    Added __docformat__
 
3363
    
 
3364
    2005/08/03
 
3365
    ----------
 
3366
    
 
3367
    By Michael Foord
 
3368
    
 
3369
    Fixed bug in Section.pop (now doesn't raise KeyError if a default value
 
3370
    is specified)
 
3371
    
 
3372
    Replaced ``basestring`` with ``types.StringTypes``
 
3373
    
 
3374
    Removed the ``writein`` method
 
3375
    
 
3376
    Added __version__
 
3377
    
 
3378
    2005/07/29
 
3379
    ----------
 
3380
    
 
3381
    By Nicola Larosa
 
3382
    
 
3383
    Indentation in config file is not significant anymore, subsections are
 
3384
    designated by repeating square brackets
 
3385
    
 
3386
    Adapted all tests and docs to the new format
 
3387
    
 
3388
    2005/07/28
 
3389
    ----------
 
3390
    
 
3391
    By Nicola Larosa
 
3392
    
 
3393
    Added more tests
 
3394
    
 
3395
    2005/07/23
 
3396
    ----------
 
3397
    
 
3398
    By Nicola Larosa
 
3399
    
 
3400
    Reformatted final docstring in ReST format, indented it for easier folding
 
3401
    
 
3402
    Code tests converted to doctest format, and scattered them around
 
3403
    in various docstrings
 
3404
    
 
3405
    Walk method rewritten using scalars and sections attributes
 
3406
    
 
3407
    2005/07/22
 
3408
    ----------
 
3409
    
 
3410
    By Nicola Larosa
 
3411
    
 
3412
    Changed Validator and SimpleVal "test" methods to "check"
 
3413
    
 
3414
    More code cleanup
 
3415
    
 
3416
    2005/07/21
 
3417
    ----------
 
3418
    
 
3419
    Changed Section.sequence to Section.scalars and Section.sections
 
3420
    
 
3421
    Added Section.configspec
 
3422
    
 
3423
    Sections in the root section now have no extra indentation
 
3424
    
 
3425
    Comments now better supported in Section and preserved by ConfigObj
 
3426
    
 
3427
    Comments also written out
 
3428
    
 
3429
    Implemented initial_comment and final_comment
 
3430
    
 
3431
    A scalar value after a section will now raise an error
 
3432
    
 
3433
    2005/07/20
 
3434
    ----------
 
3435
    
 
3436
    Fixed a couple of bugs
 
3437
    
 
3438
    Can now pass a tuple instead of a list
 
3439
    
 
3440
    Simplified dict and walk methods
 
3441
    
 
3442
    Added __str__ to Section
 
3443
    
 
3444
    2005/07/10
 
3445
    ----------
 
3446
    
 
3447
    By Nicola Larosa
 
3448
    
 
3449
    More code cleanup
 
3450
    
 
3451
    2005/07/08
 
3452
    ----------
 
3453
    
 
3454
    The stringify option implemented. On by default.
 
3455
    
 
3456
    2005/07/07
 
3457
    ----------
 
3458
    
 
3459
    Renamed private attributes with a single underscore prefix.
 
3460
    
 
3461
    Changes to interpolation - exceeding recursion depth, or specifying a
 
3462
    missing value, now raise errors.
 
3463
    
 
3464
    Changes for Python 2.2 compatibility. (changed boolean tests - removed
 
3465
    ``is True`` and ``is False``)
 
3466
    
 
3467
    Added test for duplicate section and member (and fixed bug)
 
3468
    
 
3469
    2005/07/06
 
3470
    ----------
 
3471
    
 
3472
    By Nicola Larosa
 
3473
    
 
3474
    Code cleanup
 
3475
    
 
3476
    2005/07/02
 
3477
    ----------
 
3478
    
 
3479
    Version 0.1.0
 
3480
    
 
3481
    Now properly handles values including comments and lists.
 
3482
    
 
3483
    Better error handling.
 
3484
    
 
3485
    String interpolation.
 
3486
    
 
3487
    Some options implemented.
 
3488
    
 
3489
    You can pass a Section a dictionary to initialise it.
 
3490
    
 
3491
    Setting a Section member to a dictionary will create a Section instance.
 
3492
    
 
3493
    2005/06/26
 
3494
    ----------
 
3495
    
 
3496
    Version 0.0.1
 
3497
    
 
3498
    Experimental reader.
 
3499
    
 
3500
    A reasonably elegant implementation - a basic reader in 160 lines of code.
 
3501
    
 
3502
    *A programming language is a medium of expression.* - Paul Graham
 
3503
"""
 
3504