Gentoo Archives: gentoo-commits

From: "Anthony G. Basile" <blueness@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/webapp-config:master commit in: WebappConfig/
Date: Fri, 29 Jun 2012 13:00:57
Message-Id: 1340974225.70b36b1615bf83b30d164a674b7ae8c3968d45f6.blueness@gentoo
1 commit: 70b36b1615bf83b30d164a674b7ae8c3968d45f6
2 Author: Arfrever Frehtes Taifersar Arahesis <Arfrever <AT> Apache <DOT> Org>
3 AuthorDate: Thu Jun 28 23:53:47 2012 +0000
4 Commit: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
5 CommitDate: Fri Jun 29 12:50:25 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/webapp-config.git;a=commit;h=70b36b16
7
8 Update configparser imports for compatibility with Python 3.
9
10 Signed-off-by: Anthony G. Basile <blueness <AT> gentoo.org>
11
12 ---
13 WebappConfig/config.py | 39 +++++++++++++++++++++++----------------
14 1 files changed, 23 insertions(+), 16 deletions(-)
15
16 diff --git a/WebappConfig/config.py b/WebappConfig/config.py
17 index 371602e..37fb0e2 100644
18 --- a/WebappConfig/config.py
19 +++ b/WebappConfig/config.py
20 @@ -19,12 +19,19 @@
21 # Dependencies
22 # ------------------------------------------------------------------------
23
24 -import sys, os, os.path, ConfigParser, re, socket, time
25 -
26 -from ConfigParser import MAX_INTERPOLATION_DEPTH, \
27 - ParsingError, InterpolationMissingOptionError, \
28 - InterpolationSyntaxError, \
29 - InterpolationDepthError
30 +import sys, os, os.path, re, socket, time
31 +
32 +try:
33 + # Python 3
34 + import configparser
35 + if sys.version_info >= (3, 2):
36 + from configparser import ConfigParser as configparser_ConfigParser
37 + else:
38 + from configparser import SafeConfigParser as configparser_ConfigParser
39 +except ImportError:
40 + # Python 2
41 + import ConfigParser as configparser
42 + from ConfigParser import SafeConfigParser as configparser_ConfigParser
43
44 import WebappConfig.server
45 import WebappConfig.permissions as Perm
46 @@ -40,20 +47,20 @@ from WebappConfig.permissions import PermissionMap
47 # BashParser class
48 # ------------------------------------------------------------------------
49
50 -class BashConfigParser(ConfigParser.SafeConfigParser):
51 +class BashConfigParser(configparser_ConfigParser):
52
53 _interpvar_match = re.compile(r"(%\(([^)]+)\)s|\$\{([^}]+)\})").match
54
55 def __init__(self, defaults=None):
56 self.error_action = 1
57 - ConfigParser.SafeConfigParser.__init__(self, defaults)
58 + configparser_ConfigParser.__init__(self, defaults)
59
60 def on_error(self, action = 0):
61 self.error_action = action
62
63 def get(self, section, option):
64 try:
65 - return ConfigParser.SafeConfigParser.get(self, section, option)
66 + return configparser_ConfigParser.get(self, section, option)
67 except Exception as e:
68 error = '\nThere is a problem with your configuration file or' \
69 ' an environment variable.\n' \
70 @@ -69,8 +76,8 @@ class BashConfigParser(ConfigParser.SafeConfigParser):
71 return ''
72
73 def _interpolate_some(self, option, accum, rest, section, map, depth):
74 - if depth > MAX_INTERPOLATION_DEPTH:
75 - raise InterpolationDepthError(option, section, rest)
76 + if depth > configparser.MAX_INTERPOLATION_DEPTH:
77 + raise configparser.InterpolationDepthError(option, section, rest)
78 while rest:
79 p = rest.find("%")
80 if p < 0:
81 @@ -92,7 +99,7 @@ class BashConfigParser(ConfigParser.SafeConfigParser):
82 elif c == "(" or c == "{":
83 m = self._interpvar_match(rest)
84 if m is None:
85 - raise InterpolationSyntaxError(option, section,
86 + raise configparser.InterpolationSyntaxError(option, section,
87 "bad interpolation variable reference %r" % rest)
88 var = m.group(2)
89 if not var:
90 @@ -102,7 +109,7 @@ class BashConfigParser(ConfigParser.SafeConfigParser):
91 try:
92 v = map[var]
93 except KeyError:
94 - raise InterpolationMissingOptionError(
95 + raise configparser.InterpolationMissingOptionError(
96 option, section, rest, var)
97 if "%" in v or "$" in v:
98 self._interpolate_some(option, accum, v,
99 @@ -110,7 +117,7 @@ class BashConfigParser(ConfigParser.SafeConfigParser):
100 else:
101 accum.append(v)
102 else:
103 - raise InterpolationSyntaxError(
104 + raise configparser.InterpolationSyntaxError(
105 option, section,
106 "'" + c + "' must be followed by '" + c + "', '{', or '(', found: %r" % (rest,))
107
108 @@ -187,7 +194,7 @@ class BashConfigParser(ConfigParser.SafeConfigParser):
109 # raised at the end of the file and will contain a
110 # list of all bogus lines
111 if not e:
112 - e = ParsingError(fpname)
113 + e = configparser.ParsingError(fpname)
114 e.append(lineno, repr(line))
115 # if any parsing errors occurred, raise an exception
116 if e:
117 @@ -1056,7 +1063,7 @@ class Config:
118 if not i in ['pn', 'pvr']:
119 try:
120 print(i.upper() + '="' + self.config.get('USER', i) + '"')
121 - except InterpolationSyntaxError:
122 + except configparser.InterpolationSyntaxError:
123 print('# Failed to evaluate: ' + i.upper())
124
125 sys.exit(0)