comparison configmix/ini.py @ 149:614a0a648f48

Work around the deprecation of SafeConfigParser in Python 3.2+
author Franz Glasner <hg@dom66.de>
date Sat, 14 Apr 2018 12:52:15 +0200
parents be352645871c
children 0ac6ffae969f
comparison
equal deleted inserted replaced
148:be352645871c 149:614a0a648f48
12 import sys 12 import sys
13 import os 13 import os
14 import io 14 import io
15 import locale 15 import locale
16 try: 16 try:
17 from configparser import SafeConfigParser, NoSectionError, NoOptionError 17 from configparser import ConfigParser, NoSectionError, NoOptionError
18 # SafeConfigParser is deprecated in Python 3.2+ (together with "readfp()")
19 if hasattr(ConfigParser, "read_file"):
20 _ConfigParserBase = ConfigParser
21 else:
22 from configparser import SafeConfigParser
23 _ConfigParserBase = SafeConfigParser
18 except ImportError: 24 except ImportError:
19 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError 25 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
26 _ConfigParserBase = SafeConfigParser
20 try: 27 try:
21 from collections import OrderedDict as DictImpl 28 from collections import OrderedDict as DictImpl
22 except ImportError: 29 except ImportError:
23 try: 30 try:
24 from ordereddict import OrderedDict as DictImpl 31 from ordereddict import OrderedDict as DictImpl
30 37
31 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError", 38 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError",
32 "load"] 39 "load"]
33 40
34 41
35 class INIConfigParser(SafeConfigParser): 42 class INIConfigParser(_ConfigParserBase):
36 43
37 """A case sensitive config parser that returns all-unicode string 44 """A case sensitive config parser that returns all-unicode string
38 values. 45 values.
39 46
40 """ 47 """
41 48
42 def __init__(self, filename, executable=None, encoding=None): 49 def __init__(self, filename, executable=None, encoding=None):
43 SafeConfigParser.__init__(self) 50 _ConfigParserBase.__init__(self)
44 if executable is None: 51 if executable is None:
45 executable = sys.argv[0] 52 executable = sys.argv[0]
46 if PY2: 53 if PY2:
47 if isinstance(filename, str): 54 if isinstance(filename, str):
48 filename = filename.decode(locale.getpreferredencoding()) 55 filename = filename.decode(locale.getpreferredencoding())
79 if isinstance(filename, str): 86 if isinstance(filename, str):
80 filename = filename.decode(locale.getpreferredencoding()) 87 filename = filename.decode(locale.getpreferredencoding())
81 self.set(None, u("self"), filename) 88 self.set(None, u("self"), filename)
82 self.set(None, u("here"), os.path.dirname(filename)) 89 self.set(None, u("here"), os.path.dirname(filename))
83 self.set(None, u("root"), os.path.dirname(self.executable)) 90 self.set(None, u("root"), os.path.dirname(self.executable))
84 if hasattr(SafeConfigParser, "read_file"): 91 if hasattr(_ConfigParserBase, "read_file"):
85 SafeConfigParser.read_file(self, fp, source=filename) 92 _ConfigParserBase.read_file(self, fp, source=filename)
86 else: 93 else:
87 SafeConfigParser.readfp(self, fp, filename=filename) 94 _ConfigParserBase.readfp(self, fp, filename=filename)
88 self.filename = filename 95 self.filename = filename
89 self.root = os.path.dirname(self.executable) 96 self.root = os.path.dirname(self.executable)
90 97
91 def getx(self, section, option): 98 def getx(self, section, option):
92 """Extended `get()` with some automatic type conversion support. 99 """Extended `get()` with some automatic type conversion support.