* derive LocalText from dict, better interface
* autoxml write: check object before trying to encode
This commit is contained in:
+18
-13
@@ -70,13 +70,14 @@ Integer = types.IntType
|
|||||||
# # standard initialization
|
# # standard initialization
|
||||||
# super(autoxml, cls).__init__(name, bases, dict)
|
# super(autoxml, cls).__init__(name, bases, dict)
|
||||||
|
|
||||||
class LocalText(object):
|
class LocalText(dict):
|
||||||
"""Handles XML tags with localized text"""
|
"""Handles XML tags with localized text"""
|
||||||
|
|
||||||
def __init__(self, tag, spec):
|
def __init__(self, tag, spec):
|
||||||
self.tag = tag
|
self.tag = tag
|
||||||
self.req = spec[1]
|
self.req = spec[1]
|
||||||
self.locs = {}
|
dict.__init__(self)
|
||||||
|
#self.locs = {}
|
||||||
|
|
||||||
def decode(self, node, errs, where = ""):
|
def decode(self, node, errs, where = ""):
|
||||||
# flags, tag name, instance attribute
|
# flags, tag name, instance attribute
|
||||||
@@ -95,33 +96,33 @@ class LocalText(object):
|
|||||||
# FIXME: check for dups and 'en'
|
# FIXME: check for dups and 'en'
|
||||||
if not lang:
|
if not lang:
|
||||||
lang = 'en'
|
lang = 'en'
|
||||||
self.locs[lang] = c
|
self[lang] = c
|
||||||
|
|
||||||
def encode(self, xml, node, errs):
|
def encode(self, xml, node, errs):
|
||||||
for key in self.locs.iterkeys():
|
for key in self.iterkeys():
|
||||||
newnode = newNode(node, self.tag)
|
newnode = newNode(node, self.tag)
|
||||||
newnode.setAttribute('xml:lang', key)
|
newnode.setAttribute('xml:lang', key)
|
||||||
newtext = newTextNode(node, self.locs[key])
|
newtext = newTextNode(node, self[key])
|
||||||
newnode.appendChild(newtext)
|
newnode.appendChild(newtext)
|
||||||
node.appendChild(newnode)
|
node.appendChild(newnode)
|
||||||
|
|
||||||
def check(self, where = unicode()):
|
def check(self, where = unicode()):
|
||||||
errs = []
|
errs = []
|
||||||
langs = [ locale.getlocale()[0][0:2], 'tr', 'en' ]
|
langs = [ locale.getlocale()[0][0:2], 'tr', 'en' ]
|
||||||
if not util.any(lambda x : self.locs.has_key(x), langs):
|
if not util.any(lambda x : self.has_key(x), langs):
|
||||||
errs.append( where + _("Tag should have at least an English or Turkish version"))
|
errs.append( where + _("Tag should have at least an English or Turkish version"))
|
||||||
return errs
|
return errs
|
||||||
|
|
||||||
def format(self, f, errs):
|
def format(self, f, errs):
|
||||||
L = locale.getlocale()[0][0:2] # try to read language, pathetic isn't it?
|
L = locale.getlocale()[0][0:2] # try to read language, pathetic isn't it?
|
||||||
if self.locs.has_key(L):
|
if self.has_key(L):
|
||||||
f.add_flowing_data(self.locs[L])
|
f.add_flowing_data(self[L])
|
||||||
elif self.locs.has_key('en'):
|
elif self.has_key('en'):
|
||||||
# fallback to English, blah
|
# fallback to English, blah
|
||||||
f.add_flowing_data(self.locs['en'])
|
f.add_flowing_data(self['en'])
|
||||||
elif self.locs.has_key('tr'):
|
elif self.has_key('tr'):
|
||||||
# fallback to Turkish
|
# fallback to Turkish
|
||||||
f.add_flowing_data(self.locs['tr'])
|
f.add_flowing_data(self['tr'])
|
||||||
else:
|
else:
|
||||||
errs.append(_("Tag should have at least an English or Turkish version"))
|
errs.append(_("Tag should have at least an English or Turkish version"))
|
||||||
|
|
||||||
@@ -328,13 +329,17 @@ class autoxml(oo.autosuper):
|
|||||||
raise Error(*errs)
|
raise Error(*errs)
|
||||||
|
|
||||||
def write(self, filename):
|
def write(self, filename):
|
||||||
|
errs = self.check()
|
||||||
|
if errs:
|
||||||
|
errs.append(_("autoxml.write: object validation has failed"))
|
||||||
|
raise Error(*errs)
|
||||||
errs = []
|
errs = []
|
||||||
self.newDOM()
|
self.newDOM()
|
||||||
self.encode(self, self.rootNode(), errs)
|
self.encode(self, self.rootNode(), errs)
|
||||||
if hasattr(self, 'write_hook'):
|
if hasattr(self, 'write_hook'):
|
||||||
self.write_hook(errs)
|
self.write_hook(errs)
|
||||||
if errs:
|
if errs:
|
||||||
errs.append(_("autoxml.write: File '%s' has errors") % filename)
|
errs.append(_("autoxml.write: File encoding '%s' has errors") % filename)
|
||||||
raise Error(*errs)
|
raise Error(*errs)
|
||||||
self.writexml(filename)
|
self.writexml(filename)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user