comparison configmix/ini.py @ 148:be352645871c

Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
author Franz Glasner <hg@dom66.de>
date Sat, 14 Apr 2018 12:44:42 +0200
parents ba5970a2dcef
children 614a0a648f48
comparison
equal deleted inserted replaced
147:8b7333caba25 148:be352645871c
51 self.executable = os.path.normpath(os.path.abspath(executable)) 51 self.executable = os.path.normpath(os.path.abspath(executable))
52 if encoding is None: 52 if encoding is None:
53 encoding = locale.getpreferredencoding() 53 encoding = locale.getpreferredencoding()
54 self.encoding = encoding 54 self.encoding = encoding
55 with io.open(filename, mode="rt", encoding=self.encoding) as cf: 55 with io.open(filename, mode="rt", encoding=self.encoding) as cf:
56 self.readfp(cf, filename) 56 self.read_file(cf, filename)
57 57
58 def optionxform(self, option): 58 def optionxform(self, option):
59 return option 59 return option
60 60
61 def get_path_list(self, section, option): 61 def get_path_list(self, section, option):
62 v = self.get(section, option) 62 v = self.get(section, option)
63 return v.split(os.pathsep) 63 return v.split(os.pathsep)
64 64
65 def read(self, filenames): 65 def read(self, filenames):
66 """Not implemented. Use :meth:`readfp` instead""" 66 """Not implemented. Use :meth:`read_file` instead"""
67 raise NotImplementedError("use `readfp()' instead") 67 raise NotImplementedError("use `read_file()' instead")
68 68
69 def readfp(self, fp, filename): 69 def read_file(self, fp, filename):
70 """Read from a file-like object `fp`. 70 """Read from a file-like object `fp`.
71 71
72 The `fp` argument must have a `readline()` method. 72 The `fp` argument must have a `readline()` method.
73 73
74 """ 74 """
79 if isinstance(filename, str): 79 if isinstance(filename, str):
80 filename = filename.decode(locale.getpreferredencoding()) 80 filename = filename.decode(locale.getpreferredencoding())
81 self.set(None, u("self"), filename) 81 self.set(None, u("self"), filename)
82 self.set(None, u("here"), os.path.dirname(filename)) 82 self.set(None, u("here"), os.path.dirname(filename))
83 self.set(None, u("root"), os.path.dirname(self.executable)) 83 self.set(None, u("root"), os.path.dirname(self.executable))
84 SafeConfigParser.readfp(self, fp, filename=filename) 84 if hasattr(SafeConfigParser, "read_file"):
85 SafeConfigParser.read_file(self, fp, source=filename)
86 else:
87 SafeConfigParser.readfp(self, fp, filename=filename)
85 self.filename = filename 88 self.filename = filename
86 self.root = os.path.dirname(self.executable) 89 self.root = os.path.dirname(self.executable)
87 90
88 def getx(self, section, option): 91 def getx(self, section, option):
89 """Extended `get()` with some automatic type conversion support. 92 """Extended `get()` with some automatic type conversion support.