4
4
# This code is licensed under the GPL 2.0 license, available at the root
5
5
# application directory.
6
6
7
+ import re
8
+ import UserDict as _UserDict
9
+
7
10
__author__ = "Alessio Fabiani"
8
11
__copyright__ = "Copyright 2016 Open Source Geospatial Foundation - all rights reserved"
9
12
__license__ = "GPL"
103
106
# fallback for setup.py which hasn't yet built _collections
104
107
_default_dict = dict
105
108
106
- import re
107
-
108
109
__all__ = ["NoSectionError" , "DuplicateSectionError" , "NoOptionError" ,
109
110
"InterpolationError" , "InterpolationDepthError" ,
110
111
"InterpolationSyntaxError" , "ParsingError" ,
117
118
MAX_INTERPOLATION_DEPTH = 10
118
119
119
120
120
-
121
121
# exception classes
122
122
class Error (Exception ):
123
123
"""Base class for ConfigParser exceptions."""
@@ -146,6 +146,7 @@ def __repr__(self):
146
146
147
147
__str__ = __repr__
148
148
149
+
149
150
class NoSectionError (Error ):
150
151
"""Raised when no section matches a requested option."""
151
152
@@ -154,6 +155,7 @@ def __init__(self, section):
154
155
self .section = section
155
156
self .args = (section , )
156
157
158
+
157
159
class DuplicateSectionError (Error ):
158
160
"""Raised when a section is multiply-created."""
159
161
@@ -162,6 +164,7 @@ def __init__(self, section):
162
164
self .section = section
163
165
self .args = (section , )
164
166
167
+
165
168
class NoOptionError (Error ):
166
169
"""A requested option was not found."""
167
170
@@ -172,6 +175,7 @@ def __init__(self, option, section):
172
175
self .section = section
173
176
self .args = (option , section )
174
177
178
+
175
179
class InterpolationError (Error ):
176
180
"""Base class for interpolation-related exceptions."""
177
181
@@ -181,6 +185,7 @@ def __init__(self, option, section, msg):
181
185
self .section = section
182
186
self .args = (option , section , msg )
183
187
188
+
184
189
class InterpolationMissingOptionError (InterpolationError ):
185
190
"""A string substitution required a setting which was not available."""
186
191
@@ -195,10 +200,12 @@ def __init__(self, option, section, rawval, reference):
195
200
self .reference = reference
196
201
self .args = (option , section , rawval , reference )
197
202
203
+
198
204
class InterpolationSyntaxError (InterpolationError ):
199
205
"""Raised when the source text into which substitutions are made
200
206
does not conform to the required syntax."""
201
207
208
+
202
209
class InterpolationDepthError (InterpolationError ):
203
210
"""Raised when substitutions are nested too deeply."""
204
211
@@ -211,6 +218,7 @@ def __init__(self, option, section, rawval):
211
218
InterpolationError .__init__ (self , option , section , msg )
212
219
self .args = (option , section , rawval )
213
220
221
+
214
222
class ParsingError (Error ):
215
223
"""Raised when a configuration file does not follow legal syntax."""
216
224
@@ -224,6 +232,7 @@ def append(self, lineno, line):
224
232
self .errors .append ((lineno , line ))
225
233
self .message += '\n \t [line %2d]: %s' % (lineno , line )
226
234
235
+
227
236
class MissingSectionHeaderError (ParsingError ):
228
237
"""Raised when a key-value pair is found before any section header."""
229
238
@@ -268,7 +277,7 @@ def add_section(self, section):
268
277
case-insensitive variants.
269
278
"""
270
279
if section .lower () == "default" :
271
- raise ValueError , 'Invalid section name: %s' % section
280
+ raise ValueError ( 'Invalid section name: %s' % section )
272
281
273
282
if section in self ._sections :
274
283
raise DuplicateSectionError (section )
@@ -377,7 +386,7 @@ def getfloat(self, section, option):
377
386
def getboolean (self , section , option ):
378
387
v = self .get (section , option )
379
388
if v .lower () not in self ._boolean_states :
380
- raise ValueError , 'Not a boolean: %s' % v
389
+ raise ValueError ( 'Not a boolean: %s' % v )
381
390
return self ._boolean_states [v .lower ()]
382
391
383
392
def optionxform (self , optionstr ):
@@ -392,8 +401,8 @@ def has_option(self, section, option):
392
401
return False
393
402
else :
394
403
option = self .optionxform (option )
395
- return (option in self ._sections [section ]
396
- or option in self ._defaults )
404
+ return (option in self ._sections [section ] or
405
+ option in self ._defaults )
397
406
398
407
def set (self , section , option , value = None ):
399
408
"""Set an option."""
@@ -452,15 +461,15 @@ def remove_section(self, section):
452
461
r'\[' # [
453
462
r'(?P<header>[^]]+)' # very permissive!
454
463
r'\]' # ]
455
- )
464
+ )
456
465
OPTCRE = re .compile (
457
466
r'(?P<option>[^:=\s][^:=]*)' # very permissive!
458
467
r'\s*(?P<vi>[:=])\s*' # any number of space/tab,
459
468
# followed by separator
460
469
# (either : or =), followed
461
470
# by any # space/tab
462
471
r'(?P<value>.*)$' # everything up to eol
463
- )
472
+ )
464
473
OPTCRE_NV = re .compile (
465
474
r'(?P<option>[^:=\s][^:=]*)' # very permissive!
466
475
r'\s*(?:' # any number of space/tab,
@@ -469,7 +478,7 @@ def remove_section(self, section):
469
478
# =), followed by any #
470
479
# space/tab
471
480
r'(?P<value>.*))?$' # everything up to eol
472
- )
481
+ )
473
482
474
483
def _read (self , fp , fpname ):
475
484
"""Parse a sectioned setup file.
@@ -563,7 +572,6 @@ def _read(self, fp, fpname):
563
572
if isinstance (val , list ):
564
573
options [name ] = '\n ' .join (val )
565
574
566
- import UserDict as _UserDict
567
575
568
576
class _Chainmap (_UserDict .DictMixin ):
569
577
"""Combine multiple mappings for successive lookups.
@@ -595,6 +603,7 @@ def keys(self):
595
603
seen .add (key )
596
604
return result
597
605
606
+
598
607
class ConfigParser (RawConfigParser ):
599
608
600
609
def get (self , section , option , raw = False , vars = None ):
@@ -674,7 +683,7 @@ def _interpolate(self, section, option, rawval, vars):
674
683
value = self ._KEYCRE .sub (self ._interpolation_replace , value )
675
684
try :
676
685
value = value % vars
677
- except KeyError , e :
686
+ except KeyError as e :
678
687
raise InterpolationMissingOptionError (
679
688
option , section , rawval , e .args [0 ])
680
689
else :
@@ -723,7 +732,7 @@ def _interpolate_some(self, option, accum, rest, section, map, depth):
723
732
m = self ._interpvar_re .match (rest )
724
733
if m is None :
725
734
raise InterpolationSyntaxError (option , section ,
726
- "bad interpolation variable reference %r" % rest )
735
+ "bad interpolation variable reference %r" % rest )
727
736
var = self .optionxform (m .group (1 ))
728
737
rest = rest [m .end ():]
729
738
try :
@@ -759,5 +768,5 @@ def set(self, section, option, value=None):
759
768
# then, check if there's a lone percent sign left
760
769
if '%' in tmp_value :
761
770
raise ValueError ("invalid interpolation syntax in %r at "
762
- "position %d" % (value , tmp_value .find ('%' )))
771
+ "position %d" % (value , tmp_value .find ('%' )))
763
772
ConfigParser .set (self , section , option , value )
0 commit comments