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
|
||||
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
def ascii(s):
|
||||
if isinstance(s, unicode):
|
||||
return s.encode(b'ascii')
|
||||
return s.encode(u'ascii')
|
||||
else:
|
||||
return str(s)
|
||||
|
||||
def utf8(s):
|
||||
if isinstance(s, unicode):
|
||||
return s.encode(b'utf-8')
|
||||
return s.encode(u'utf-8')
|
||||
else:
|
||||
return str(s)
|
||||
|
||||
|
@ -23,17 +28,17 @@ def from_ascii(s):
|
|||
if isinstance(s, unicode):
|
||||
return s
|
||||
else:
|
||||
return s.decode(b'ascii')
|
||||
return s.decode(u'ascii')
|
||||
|
||||
def from_latin1(s):
|
||||
if isinstance(s, unicode):
|
||||
return s
|
||||
else:
|
||||
return s.decode(b'latin-1')
|
||||
return s.decode(u'latin-1')
|
||||
|
||||
def from_utf8(s):
|
||||
if isinstance(s, unicode):
|
||||
return s
|
||||
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.
|
||||
"""
|
||||
|
||||
try:
|
||||
basestring
|
||||
except NameError:
|
||||
basestring = str
|
||||
|
||||
def sanitize_for_subst(key):
|
||||
"""
|
||||
|
@ -66,7 +70,7 @@ class inifilesec(dict):
|
|||
raise(KeyError, "Section '%s' has no key '%s'" % (self.name, key))
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if not self.has_key(key.lower()):
|
||||
if not key.lower() in self:
|
||||
if self.initializing:
|
||||
self.verbatim_fields += [key]
|
||||
else:
|
||||
|
@ -99,7 +103,8 @@ class inifile(dict):
|
|||
return
|
||||
|
||||
def read(self):
|
||||
fd = file(self.fname, 'r')
|
||||
fd = open(self.fname, 'r')
|
||||
|
||||
|
||||
act_section = 'default'
|
||||
section_init = False
|
||||
|
@ -119,7 +124,7 @@ class inifile(dict):
|
|||
if line[-1] == ']':
|
||||
act_section = line[1:-1]
|
||||
section_init = True
|
||||
if not self.has_key(act_section.lower()):
|
||||
if not act_section.lower() in self:
|
||||
"add specified section"
|
||||
actsec = inifilesec(act_section, self)
|
||||
dict.__setitem__(self, act_section.lower(), actsec)
|
||||
|
@ -139,7 +144,7 @@ class inifile(dict):
|
|||
else:
|
||||
k = line[:peq]
|
||||
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"
|
||||
assert not section_init
|
||||
|
||||
|
@ -166,7 +171,7 @@ class inifile(dict):
|
|||
else:
|
||||
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"
|
||||
try:
|
||||
self.section_order.index('default')
|
||||
|
@ -177,7 +182,8 @@ class inifile(dict):
|
|||
secdict.initializing = False
|
||||
|
||||
def write(self):
|
||||
fd = file(self.fname, 'w')
|
||||
fd = open(self.fname, 'w')
|
||||
|
||||
first = True
|
||||
|
||||
fd.write("".join(self.verbatim_hdr))
|
||||
|
@ -198,7 +204,7 @@ class inifile(dict):
|
|||
fd.write(secdict.verbatim_ftr)
|
||||
|
||||
def add_section(self, key):
|
||||
if not self.has_key(key):
|
||||
if not key in self:
|
||||
self[key.lower()] = inifilesec(key, self)
|
||||
self.section_order += [key]
|
||||
|
||||
|
@ -208,7 +214,7 @@ class inifile(dict):
|
|||
raise(TypeError, "Ini files can only contain string keys")
|
||||
|
||||
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()]
|
||||
else:
|
||||
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())
|
||||
def __setitem__(self, key, value):
|
||||
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))
|
||||
self[self.cur_section][key.lower()] = value
|
||||
else:
|
||||
|
|
Loading…
Add table
Reference in a new issue