我正在按照http://packages.python.org/dexml/api/dexml.fields.html或https://gist.github.com/485977的方式为 XML 数据开发一个轻量级的类包装器。这些类包含一个 elementTree 元素并具有提供属性访问的描述符(对包含的代码量表示歉意,我认为保留大部分注释会更容易理解)
class XPropBase(object):
DEFAULT_TO = (lambda s, x: str(x))
DEFAULT_FROM = (lambda s, p: p)
def __init__(self, path, convert_from=None, convert_to = None, read_only = False, allow_missing=False):
'''
important bits here are convert_to and convert_from, which do the translation in and out of XML strings in derived classes...
'''
self.path = path
self.convert_from = convert_from or self.DEFAULT_FROM
self.convert_to = convert_to or self.DEFAULT_TO
self.read_only = read_only
self.allow_missing = allow_missing
def _get_xml(self, instance):
#only a an instance method for convenience...
#intended to be overridden for different target instances
return instance.get_element()
class XElement(XPropBase):
'''
Wraps an xml item whose content is contained in the text of
an XML tag, ie: <tag>Content</tag>. The convert_to and convert_from methods
will be applied to the text property of the corresponding Element
@note this will use the first instance of a given node path that it finds,
so it is not guaranteed if the supplied path leads to more than one xml tag.
'''
def __get__(self, instance, owner=None):
the_xml = self._get_xml(instance)
if not self.path:
return self.convert_from(the_xml.text)
try:
underlying = the_xml.find(self.path)
return self.convert_from(underlying.text)
except AttributeError:
if self.allow_missing:
return None
else:
raise XMLWrapperError, "%s has no element named %s" % (instance, self.path)
def __set__(self, instance, value, owner =None):
if self.read_only:
raise XMLWrapperError('%s is a read-only property' % self.path)
the_xml= self._get_xml(instance)
if not self.path:
the_xml.text = self.convert_to(value)
return
try:
underlying = self._get_xml(instance).find(self.path)
underlying.text = self.convert_to(value)
except AttributeError:
if self.allow_missing:
SubElement(self._get_xml(instance), self.path, text=self.convert_to(value))
else:
raise XMLWrapperError, "%s has no element named %s" % (instance, self.path)
class XAttrib(XPropBase):
'''
Wraps a property in an attribute on the containing xml tag specified by path
if the supplied attribute is not present, will raise an XMLWrapperError unless the allow_missing flag is set to True
'''
def __get__(self, instance, owner=None):
try:
res = self._get_xml(instance).attrib[self.path]
return self.convert_from(res)
except KeyError:
if self.allow_missing:
return None
raise XMLWrapperError, "%s has no attribute named %s" % (instance, self.path)
def __set__(self, instance, value, owner =None):
xml = self._get_xml(instance)
has_element = xml.get(self.path, 'NOT_FOUND')
if has_element == 'NOT_FOUND' and not self.allow_missing:
raise XMLWrapperError, "%s has no attribute named %s" % (instance, self.path)
xml.set(self.path, self.convert_to(value))
def _get_element(self):
return None
def _get_attribute(self):
return self.path
class XInstance(XPropBase):
'''
Represents an attribute which is mapped onto a class. The supplied class is specified in the constructor
@note: As with XElement, this works on the first appropriately named tag it
finds. If there are multiple path values with the same tag, it will cause
errors.
'''
def __init__(self, path, cls, convert_from=None, convert_to = None, read_only = False, allow_missing=False):
self.cls = cls
XPropBase.__init__(self, path, convert_from = convert_from , convert_to = convert_to , read_only = read_only, allow_missing=allow_missing)
def __get__(self, instance, owner=None):
sub_elem = self._get_xml(instance).find(self.path)
if not sub_elem and not self.allow_missing:
XMLWrapperError, "%s has no child named %s" % (instance, self.path)
return self.cls(sub_elem)
def __set__(self, instance, value):
my_element = self._get_xml(instance)
original_child = my_element.find(self.path)
if original_child:
my_element.remove(original_child)
my_element.append(self._get_xml(value))
class XInstanceGroup(XInstance):
'''
Represents a collection of XInstances contained below a particular tag
'''
def __get__(self, instance, owner=None):
return [self.cls(item) for item in self._get_xml(instance).findall(self.path)]
def __set__(self, instance, value):
my_element = self._get_xml(instance)
for item in my_element.findall(self.path):
my_element.remove(item)
for each_element in map(self._get_xml, value):
my_element.append(each_element)
似乎有效(尽管要进行彻底的测试),但有一点令人讨厌。XInstanceGroup 描述符处理如下情况:
<Object name="dummy">
<Child name="kid2" blah="dee blah"/>
<Child name="kid2" blah="zey"/>
</Object>
class Kid(XMLData):
Name = XAttribute("name")
Blah = XAttribute("blah")
class DummyWrapper(XMLData):
Name = XAttribute("name")
Kids = XInstanceGroup('Child', Kid)
因此,如果您将 DummyWrapper 用于它的孩子,您将获得 Kid 对象的列表。但是我对更新该列表的过程不满意:
#this works
kids = Dummy_example.Kids
kids.append(Kid (name = 'marky mark', blah='funky_fresh'))
Dummy_example.Kids = kids
#this doesn't
Dummy_example.Kids.append(Kid(name = 'joey fatone', blah = 'indeed!'))
这是因为 Dummy.Kids 实际上是一个返回组的函数,而不是存储为成员字段的持久列表对象。
现在的问题是:有没有办法使用描述符来做到这一点?似乎障碍在于描述符实例无法持久化数据 - 它只在调用实例时才知道实例。我不喜欢以某种方式将存储从描述符注入实例的想法(如果没有别的,它会增加令人不快的耦合)。到目前为止,明显的谷歌搜索没有帮助。