comparison configmix/ini.py @ 21:ce290b10dac5

Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
author Franz Glasner <f.glasner@feldmann-mg.com>
date Thu, 10 Mar 2016 13:28:09 +0100
parents 9bdc4e421415
children 7c7955da42ab
comparison
equal deleted inserted replaced
20:9bdc4e421415 21:ce290b10dac5
19 try: 19 try:
20 from ordereddict import OrderedDict as DictImpl 20 from ordereddict import OrderedDict as DictImpl
21 except ImportError: 21 except ImportError:
22 DictImpl = dict 22 DictImpl = dict
23 23
24 from .compat import PY2 24 from .compat import PY2, u
25 25
26 26
27 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError", 27 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError",
28 "load"] 28 "load"]
29 29
66 raise RuntimeError("already initialized") 66 raise RuntimeError("already initialized")
67 filename = os.path.normpath(os.path.abspath(filename)) 67 filename = os.path.normpath(os.path.abspath(filename))
68 if PY2: 68 if PY2:
69 if isinstance(filename, str): 69 if isinstance(filename, str):
70 filename = filename.decode(locale.getpreferredencoding()) 70 filename = filename.decode(locale.getpreferredencoding())
71 self.set(None, "self", filename) 71 self.set(None, u("self"), filename)
72 self.set(None, "here", os.path.dirname(filename)) 72 self.set(None, u("here"), os.path.dirname(filename))
73 self.set(None, "root", os.path.dirname(self.executable)) 73 self.set(None, u("root"), os.path.dirname(self.executable))
74 SafeConfigParser.readfp(self, fp, filename=filename) 74 SafeConfigParser.readfp(self, fp, filename=filename)
75 self.filename = filename 75 self.filename = filename
76 self.root = os.path.dirname(self.executable) 76 self.root = os.path.dirname(self.executable)
77 77
78 def getx(self, section, option): 78 def getx(self, section, option):
84 ``:int:`` fetch as int, if annotated with ``:float:`` fetch as 84 ``:int:`` fetch as int, if annotated with ``:float:`` fetch as
85 float. 85 float.
86 86
87 """ 87 """
88 v = self.get(section, option) 88 v = self.get(section, option)
89 if v.startswith(":bool:"): 89 if v.startswith(u(":bool:")):
90 v = v[6:].lower() 90 v = v[6:].lower()
91 if v not in self._BOOL_CVT: 91 if v not in self._BOOL_CVT:
92 raise ValueError("Not a boolean: %s" % (v, )) 92 raise ValueError("Not a boolean: %r" % v)
93 return self._BOOL_CVT[v] 93 return self._BOOL_CVT[v]
94 elif v.startswith(":int:"): 94 elif v.startswith(u(":int:")):
95 return int(v[5:], 0) 95 return int(v[5:], 0)
96 elif v.startswith(":float:"): 96 elif v.startswith(u(":float:")):
97 return float(v[7:]) 97 return float(v[7:])
98 else: 98 else:
99 return v 99 return v
100 100
101 _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True, 101 _BOOL_CVT = {u('1'): True,
102 '0': False, 'no': False, 'false': False, 'off': False} 102 u('yes'): True,
103 u('true'): True,
104 u('on'): True,
105 u('0'): False,
106 u('no'): False,
107 u('false'): False,
108 u('off'): False}
103 109
104 110
105 def load(filename, extract=["config"]): 111 def load(filename, extract=["config"]):
106 """Load a single INI file and read/interpolate the sections given in 112 """Load a single INI file and read/interpolate the sections given in
107 `extract`. 113 `extract`.