python3 functionality confirmed
This commit is contained in:
parent
86cb0a5ef5
commit
2b90657d15
2 changed files with 25 additions and 14 deletions
|
@ -7,15 +7,20 @@ __all__ = ["objproxy", "config"]
|
||||||
|
|
||||||
from . import objproxy, config
|
from . import objproxy, config
|
||||||
|
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
def ascii(s):
|
def ascii(s):
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, unicode):
|
||||||
return s.encode(b'ascii')
|
return s.encode(u'ascii')
|
||||||
else:
|
else:
|
||||||
return str(s)
|
return str(s)
|
||||||
|
|
||||||
def utf8(s):
|
def utf8(s):
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, unicode):
|
||||||
return s.encode(b'utf-8')
|
return s.encode(u'utf-8')
|
||||||
else:
|
else:
|
||||||
return str(s)
|
return str(s)
|
||||||
|
|
||||||
|
@ -23,17 +28,17 @@ def from_ascii(s):
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, unicode):
|
||||||
return s
|
return s
|
||||||
else:
|
else:
|
||||||
return s.decode(b'ascii')
|
return s.decode(u'ascii')
|
||||||
|
|
||||||
def from_latin1(s):
|
def from_latin1(s):
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, unicode):
|
||||||
return s
|
return s
|
||||||
else:
|
else:
|
||||||
return s.decode(b'latin-1')
|
return s.decode(u'latin-1')
|
||||||
|
|
||||||
def from_utf8(s):
|
def from_utf8(s):
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, unicode):
|
||||||
return s
|
return s
|
||||||
else:
|
else:
|
||||||
return s.decode(b'utf-8')
|
return s.decode(u'utf-8')
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
basestring
|
||||||
|
except NameError:
|
||||||
|
basestring = str
|
||||||
|
|
||||||
def sanitize_for_subst(key):
|
def sanitize_for_subst(key):
|
||||||
"""
|
"""
|
||||||
|
@ -66,7 +70,7 @@ class inifilesec(dict):
|
||||||
raise(KeyError, "Section '%s' has no key '%s'" % (self.name, key))
|
raise(KeyError, "Section '%s' has no key '%s'" % (self.name, key))
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
if not self.has_key(key.lower()):
|
if not key.lower() in self:
|
||||||
if self.initializing:
|
if self.initializing:
|
||||||
self.verbatim_fields += [key]
|
self.verbatim_fields += [key]
|
||||||
else:
|
else:
|
||||||
|
@ -99,7 +103,8 @@ class inifile(dict):
|
||||||
return
|
return
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
fd = file(self.fname, 'r')
|
fd = open(self.fname, 'r')
|
||||||
|
|
||||||
|
|
||||||
act_section = 'default'
|
act_section = 'default'
|
||||||
section_init = False
|
section_init = False
|
||||||
|
@ -119,7 +124,7 @@ class inifile(dict):
|
||||||
if line[-1] == ']':
|
if line[-1] == ']':
|
||||||
act_section = line[1:-1]
|
act_section = line[1:-1]
|
||||||
section_init = True
|
section_init = True
|
||||||
if not self.has_key(act_section.lower()):
|
if not act_section.lower() in self:
|
||||||
"add specified section"
|
"add specified section"
|
||||||
actsec = inifilesec(act_section, self)
|
actsec = inifilesec(act_section, self)
|
||||||
dict.__setitem__(self, act_section.lower(), actsec)
|
dict.__setitem__(self, act_section.lower(), actsec)
|
||||||
|
@ -139,7 +144,7 @@ class inifile(dict):
|
||||||
else:
|
else:
|
||||||
k = line[:peq]
|
k = line[:peq]
|
||||||
v = line[peq+1:].rstrip('\n\r')
|
v = line[peq+1:].rstrip('\n\r')
|
||||||
if not self.has_key(act_section.lower()):
|
if not act_section.lower() in self:
|
||||||
"add default section"
|
"add default section"
|
||||||
assert not section_init
|
assert not section_init
|
||||||
|
|
||||||
|
@ -166,7 +171,7 @@ class inifile(dict):
|
||||||
else:
|
else:
|
||||||
self.verbatim_hdr += ["\n"]
|
self.verbatim_hdr += ["\n"]
|
||||||
|
|
||||||
if self.has_key('default'):
|
if 'default' in self:
|
||||||
"has a default section. make sure it's added to the section_order"
|
"has a default section. make sure it's added to the section_order"
|
||||||
try:
|
try:
|
||||||
self.section_order.index('default')
|
self.section_order.index('default')
|
||||||
|
@ -177,7 +182,8 @@ class inifile(dict):
|
||||||
secdict.initializing = False
|
secdict.initializing = False
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
fd = file(self.fname, 'w')
|
fd = open(self.fname, 'w')
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
|
|
||||||
fd.write("".join(self.verbatim_hdr))
|
fd.write("".join(self.verbatim_hdr))
|
||||||
|
@ -198,7 +204,7 @@ class inifile(dict):
|
||||||
fd.write(secdict.verbatim_ftr)
|
fd.write(secdict.verbatim_ftr)
|
||||||
|
|
||||||
def add_section(self, key):
|
def add_section(self, key):
|
||||||
if not self.has_key(key):
|
if not key in self:
|
||||||
self[key.lower()] = inifilesec(key, self)
|
self[key.lower()] = inifilesec(key, self)
|
||||||
self.section_order += [key]
|
self.section_order += [key]
|
||||||
|
|
||||||
|
@ -208,7 +214,7 @@ class inifile(dict):
|
||||||
raise(TypeError, "Ini files can only contain string keys")
|
raise(TypeError, "Ini files can only contain string keys")
|
||||||
|
|
||||||
if self.cur_section != None:
|
if self.cur_section != None:
|
||||||
if dict.__getitem__(self, self.cur_section).has_key(key.lower()):
|
if key.lower() in dict.__getitem__(self, self.cur_section):
|
||||||
return dict.__getitem__(self, self.cur_section)[key.lower()]
|
return dict.__getitem__(self, self.cur_section)[key.lower()]
|
||||||
else:
|
else:
|
||||||
raise(KeyError, "Section '%s' does not contain a key named '%s'" % (self.cur_section, key))
|
raise(KeyError, "Section '%s' does not contain a key named '%s'" % (self.cur_section, key))
|
||||||
|
@ -218,7 +224,7 @@ class inifile(dict):
|
||||||
return dict.__getitem__(self, key.lower())
|
return dict.__getitem__(self, key.lower())
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
if self.cur_section != None:
|
if self.cur_section != None:
|
||||||
if not self.has_key(self.cur_section):
|
if not self.cur_section in self:
|
||||||
raise(KeyError, "Section '%s' does not exist" % (self.cur_section))
|
raise(KeyError, "Section '%s' does not exist" % (self.cur_section))
|
||||||
self[self.cur_section][key.lower()] = value
|
self[self.cur_section][key.lower()] = value
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Reference in a new issue