Пример #1
0
 def _setelement(self, tree):
     # set internal element.
     self.__name__ = _checkname(tree.attrib[self.mark])
     # set internal element and backup tree
     self._tree, self._btree = tree, _copytree(tree)
     # make tree attribute dictionary a field attribute
     self._attrib, tattrib = tree.attrib, dict()
     # if inline text template in tree, assign it to template
     try:
         self.template = tree.text
     except TypeError:
         pass
     checkname_ = _checkname
     auto = self._auto
     mark = self.mark
     setattr_ = setattr
     cls = self.__class__
     for attr, atext in items(self._attrib):
         # if attribute template text in template source, add to _tattrib
         if '$' in atext or '%' in atext:
             tattrib[attr] = atext
         # make XML attrs attributes of self if automagic on & not a mark
         if auto and attr != mark:
             name = checkname_(attr)
             # set new class as attribute of self's superclass
             setattr_(cls, name, _Attribute(attr, name))
     # assign any attribute templates
     if tattrib:
         self.atemplates(tattrib)
Пример #2
0
 def _addfield(self, child, parent):
     # if element has variable delimiter, make field
     name = _checkname(child.attrib[self._mark])
     # check if processed already
     if name not in self._filter:
         # add to filter list if unprocessed
         self._filter.add(name)
         # add child as field
         self._setfield(
             name, self._field(child, parent, self._auto, self._max)
         )
Пример #3
0
 def __delitem__(self, key):
     '''Deletes an XML attribute.'''
     # delete the property if automagic on
     try:
         delattr(self, _checkname(key))
     except AttributeError:
         del self._attrib[key]
     # remove any template for the attribute
     try:
         self._tattrib.pop(key)
     except (AttributeError, KeyError):
         pass
Пример #4
0
 def _setelement(self, tree):
     # sets the internal element.
     self.__name__ = _checkname(tree.attrib[self.groupmark])
     # set internal element and backup tree attributes
     self._tree, self._btree = tree, _copytree(tree)
     addfield = self._addfield
     # find fields in group
     for parent in tree.getiterator():
         # get child, parent pairs
         for child in parent:
             try:
                 addfield(child, parent)
             except KeyError:
                 pass
Пример #5
0
 def _setattr(self, key, value):
     # sets attribute of the internal element to a new value.
     if isinstance(value, basestring):
         self._tree.set(key, value)
     elif isinstance(value, dict):
         # try string.template substitution
         try:
             self._tree.set(key, self._tattrib[key].substitute(value))
         # try string interpolation
         except AttributeError:
             self._tree.set(key, self._tattrib[key] % value)
     if self._auto:
         name = _checkname(key)
         # make attribute attribute of self's superclass if automagic on
         setattr(self.__class__, name, _Attribute(key, name))
Пример #6
0
 def _addgroup(self, group):
     # create group templates
     realname, template = self._groupbr.match(group).groups()
     # separate group name and template
     name = _checkname(realname)
     # check if group already processed
     if name not in self._filter:
         # mark as processed
         self._filter.add(name)
         # make new group template without passing child
         node = _TextGroup(template, self._auto, self._max)
         # name group
         node.__name__ = name
         # set field
         self._setfield(name, node)
Пример #7
0
 def _addgroup(self, child, parent):
     # verify element is a group element
     realname = child.attrib[self._groupmark]
     name = _checkname(realname)
     # check if group already processed
     if name not in self._filter:
         # mark as processed
         self._filter.add(name)
         # extract any groups under the group's element
         addgroup = self._addgroup
         for element in child:
             try:
                 addgroup(element, child)
             except KeyError:
                 pass
         # make new group template w/o passing group's element to __init__
         node = self._group(None, parent, self._auto, self._max)
         # add self's filter to avoid duplicate children
         node._filter = self._filter
         # process group's element
         node._setelement(child)
         # only attach groups with children to root template
         if len(node):
             self._setfield(name, node)