home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2092 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  15.5 KB  |  470 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import xml.dom as xml
  5. from xml.dom.minicompat import *
  6. from namespaces import nsdict
  7. import grammar
  8. from attrconverters import AttrConverters
  9.  
  10. def _escape(data, entities = { }):
  11.     data = data.replace('&', '&')
  12.     data = data.replace('<', '<')
  13.     data = data.replace('>', '>')
  14.     for chars, entity in entities.items():
  15.         data = data.replace(chars, entity)
  16.     
  17.     return data
  18.  
  19.  
  20. def _quoteattr(data, entities = { }):
  21.     entities['\n'] = ' '
  22.     entities['\r'] = ' '
  23.     data = _escape(data, entities)
  24.     if '"' in data:
  25.         if "'" in data:
  26.             data = '"%s"' % data.replace('"', '"')
  27.         else:
  28.             data = "'%s'" % data
  29.     else:
  30.         data = '"%s"' % data
  31.     return data
  32.  
  33.  
  34. def _nssplit(qualifiedName):
  35.     fields = qualifiedName.split(':', 1)
  36.     if len(fields) == 2:
  37.         return fields
  38.     return (None, fields[0])
  39.  
  40.  
  41. def _nsassign(namespace):
  42.     return nsdict.setdefault(namespace, 'ns' + str(len(nsdict)))
  43.  
  44.  
  45. class IllegalChild(StandardError):
  46.     pass
  47.  
  48.  
  49. class IllegalText(StandardError):
  50.     pass
  51.  
  52.  
  53. class Node(xml.dom.Node):
  54.     parentNode = None
  55.     nextSibling = None
  56.     previousSibling = None
  57.     
  58.     def hasChildNodes(self):
  59.         if self.childNodes:
  60.             return True
  61.         return False
  62.  
  63.     
  64.     def _get_childNodes(self):
  65.         return self.childNodes
  66.  
  67.     
  68.     def _get_firstChild(self):
  69.         if self.childNodes:
  70.             return self.childNodes[0]
  71.  
  72.     
  73.     def _get_lastChild(self):
  74.         if self.childNodes:
  75.             return self.childNodes[-1]
  76.  
  77.     
  78.     def insertBefore(self, newChild, refChild):
  79.         if newChild.nodeType not in self._child_node_types:
  80.             raise IllegalChild, '%s cannot be child of %s' % (newChild.tagName, self.tagName)
  81.         newChild.nodeType not in self._child_node_types
  82.         if newChild.parentNode is not None:
  83.             newChild.parentNode.removeChild(newChild)
  84.         
  85.         if refChild is None:
  86.             self.appendChild(newChild)
  87.         else:
  88.             
  89.             try:
  90.                 index = self.childNodes.index(refChild)
  91.             except ValueError:
  92.                 raise xml.dom.NotFoundErr()
  93.  
  94.             self.childNodes.insert(index, newChild)
  95.             newChild.nextSibling = refChild
  96.             refChild.previousSibling = newChild
  97.             if index:
  98.                 node = self.childNodes[index - 1]
  99.                 node.nextSibling = newChild
  100.                 newChild.previousSibling = node
  101.             else:
  102.                 newChild.previousSibling = None
  103.             newChild.parentNode = self
  104.         return newChild
  105.  
  106.     
  107.     def appendChild(self, node):
  108.         if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
  109.             for c in tuple(node.childNodes):
  110.                 self.appendChild(c)
  111.             
  112.             return node
  113.         if node.nodeType not in self._child_node_types:
  114.             raise IllegalChild, '<%s> is not allowed in %s' % (node.tagName, self.tagName)
  115.         node.nodeType not in self._child_node_types
  116.         if node.parentNode is not None:
  117.             node.parentNode.removeChild(node)
  118.         
  119.         _append_child(self, node)
  120.         node.nextSibling = None
  121.         return node
  122.  
  123.     
  124.     def removeChild(self, oldChild):
  125.         
  126.         try:
  127.             self.childNodes.remove(oldChild)
  128.         except ValueError:
  129.             raise xml.dom.NotFoundErr()
  130.  
  131.         if oldChild.nextSibling is not None:
  132.             oldChild.nextSibling.previousSibling = oldChild.previousSibling
  133.         
  134.         if oldChild.previousSibling is not None:
  135.             oldChild.previousSibling.nextSibling = oldChild.nextSibling
  136.         
  137.         oldChild.nextSibling = None
  138.         oldChild.previousSibling = None
  139.         if self.ownerDocument:
  140.             self.ownerDocument.clear_caches()
  141.         
  142.         oldChild.parentNode = None
  143.         return oldChild
  144.  
  145.     
  146.     def __str__(self):
  147.         val = []
  148.         for c in self.childNodes:
  149.             val.append(str(c))
  150.         
  151.         return ''.join(val)
  152.  
  153.     
  154.     def __unicode__(self):
  155.         val = []
  156.         for c in self.childNodes:
  157.             val.append(unicode(c))
  158.         
  159.         return u''.join(val)
  160.  
  161.  
  162. defproperty(Node, 'firstChild', doc = 'First child node, or None.')
  163. defproperty(Node, 'lastChild', doc = 'Last child node, or None.')
  164.  
  165. def _append_child(self, node):
  166.     childNodes = self.childNodes
  167.     if childNodes:
  168.         last = childNodes[-1]
  169.         node.__dict__['previousSibling'] = last
  170.         last.__dict__['nextSibling'] = node
  171.     
  172.     childNodes.append(node)
  173.     node.__dict__['parentNode'] = self
  174.  
  175.  
  176. class Childless:
  177.     attributes = None
  178.     childNodes = EmptyNodeList()
  179.     firstChild = None
  180.     lastChild = None
  181.     
  182.     def _get_firstChild(self):
  183.         pass
  184.  
  185.     
  186.     def _get_lastChild(self):
  187.         pass
  188.  
  189.     
  190.     def appendChild(self, node):
  191.         raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes cannot have children')
  192.  
  193.     
  194.     def hasChildNodes(self):
  195.         return False
  196.  
  197.     
  198.     def insertBefore(self, newChild, refChild):
  199.         raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes do not have children')
  200.  
  201.     
  202.     def removeChild(self, oldChild):
  203.         raise xml.dom.NotFoundErr(self.tagName + ' nodes do not have children')
  204.  
  205.     
  206.     def replaceChild(self, newChild, oldChild):
  207.         raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes do not have children')
  208.  
  209.  
  210.  
  211. class Text(Childless, Node):
  212.     nodeType = Node.TEXT_NODE
  213.     tagName = 'Text'
  214.     
  215.     def __init__(self, data):
  216.         self.data = data
  217.  
  218.     
  219.     def __str__(self):
  220.         return self.data.encode()
  221.  
  222.     
  223.     def __unicode__(self):
  224.         return self.data
  225.  
  226.     
  227.     def toXml(self, level, f):
  228.         if self.data:
  229.             f.write(_escape(unicode(self.data).encode('utf-8')))
  230.         
  231.  
  232.  
  233.  
  234. class CDATASection(Childless, Text):
  235.     nodeType = Node.CDATA_SECTION_NODE
  236.     
  237.     def toXml(self, level, f):
  238.         if self.data:
  239.             f.write('<![CDATA[%s]]>' % self.data)
  240.         
  241.  
  242.  
  243.  
  244. class Element(Node):
  245.     nodeType = Node.ELEMENT_NODE
  246.     namespaces = { }
  247.     _child_node_types = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, Node.COMMENT_NODE, Node.TEXT_NODE, Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE)
  248.     
  249.     def __init__(self, attributes = None, text = None, cdata = None, qname = None, qattributes = None, check_grammar = True, **args):
  250.         if qname is not None:
  251.             self.qname = qname
  252.         
  253.         self.ownerDocument = None
  254.         self.childNodes = []
  255.         self.allowed_children = grammar.allowed_children.get(self.qname)
  256.         prefix = self.get_nsprefix(self.qname[0])
  257.         self.tagName = prefix + ':' + self.qname[1]
  258.         if text is not None:
  259.             self.addText(text)
  260.         
  261.         if cdata is not None:
  262.             self.addCDATA(cdata)
  263.         
  264.         allowed_attrs = self.allowed_attributes()
  265.         self.attributes = { }
  266.         if attributes:
  267.             for attr, value in attributes.items():
  268.                 self.setAttribute(attr, value)
  269.             
  270.         
  271.         if qattributes:
  272.             for attr, value in qattributes.items():
  273.                 self.setAttrNS(attr[0], attr[1], value)
  274.             
  275.         
  276.         if allowed_attrs is not None:
  277.             for arg in args.keys():
  278.                 self.setAttribute(arg, args[arg])
  279.             
  280.         else:
  281.             for arg in args.keys():
  282.                 self.attributes[arg] = args[arg]
  283.             
  284.         if not check_grammar:
  285.             return None
  286.         required = grammar.required_attributes.get(self.qname)
  287.         if required:
  288.             for r in required:
  289.                 if self.getAttrNS(r[0], r[1]) is None:
  290.                     raise AttributeError, 'Required attribute missing: %s in <%s>' % (r[1].lower().replace('-', ''), self.tagName)
  291.                 self.getAttrNS(r[0], r[1]) is None
  292.             
  293.         
  294.  
  295.     
  296.     def get_nsprefix(self, namespace):
  297.         if namespace is None:
  298.             namespace = ''
  299.         
  300.         prefix = _nsassign(namespace)
  301.         if not self.namespaces.has_key(namespace):
  302.             self.namespaces[namespace] = prefix
  303.         
  304.         return prefix
  305.  
  306.     
  307.     def allowed_attributes(self):
  308.         return grammar.allowed_attributes.get(self.qname)
  309.  
  310.     
  311.     def _setOwnerDoc(self, element):
  312.         element.ownerDocument = self.ownerDocument
  313.         for child in element.childNodes:
  314.             self._setOwnerDoc(child)
  315.         
  316.  
  317.     
  318.     def addElement(self, element, check_grammar = True):
  319.         if check_grammar and self.allowed_children is not None:
  320.             if element.qname not in self.allowed_children:
  321.                 raise IllegalChild, '<%s> is not allowed in <%s>' % (element.tagName, self.tagName)
  322.             element.qname not in self.allowed_children
  323.         
  324.         self.appendChild(element)
  325.         self._setOwnerDoc(element)
  326.         if self.ownerDocument:
  327.             self.ownerDocument.rebuild_caches(element)
  328.         
  329.  
  330.     
  331.     def addText(self, text, check_grammar = True):
  332.         if check_grammar and self.qname not in grammar.allows_text:
  333.             raise IllegalText, 'The <%s> element does not allow text' % self.tagName
  334.         self.qname not in grammar.allows_text
  335.         if text != '':
  336.             self.appendChild(Text(text))
  337.         
  338.  
  339.     
  340.     def addCDATA(self, cdata, check_grammar = True):
  341.         if check_grammar and self.qname not in grammar.allows_text:
  342.             raise IllegalText, 'The <%s> element does not allow text' % self.tagName
  343.         self.qname not in grammar.allows_text
  344.         self.appendChild(CDATASection(cdata))
  345.  
  346.     
  347.     def removeAttribute(self, attr, check_grammar = True):
  348.         allowed_attrs = self.allowed_attributes()
  349.         if allowed_attrs is None:
  350.             if type(attr) == type(()):
  351.                 (prefix, localname) = attr
  352.                 self.removeAttrNS(prefix, localname)
  353.             else:
  354.                 raise AttributeError, 'Unable to add simple attribute - use (namespace, localpart)'
  355.         type(attr) == type(())
  356.         allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
  357.         if check_grammar and attr not in allowed_args:
  358.             raise AttributeError, 'Attribute %s is not allowed in <%s>' % (attr, self.tagName)
  359.         attr not in allowed_args
  360.         i = allowed_args.index(attr)
  361.         self.removeAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
  362.  
  363.     
  364.     def setAttribute(self, attr, value, check_grammar = True):
  365.         allowed_attrs = self.allowed_attributes()
  366.         if allowed_attrs is None:
  367.             if type(attr) == type(()):
  368.                 (prefix, localname) = attr
  369.                 self.setAttrNS(prefix, localname, value)
  370.             else:
  371.                 raise AttributeError, 'Unable to add simple attribute - use (namespace, localpart)'
  372.         type(attr) == type(())
  373.         allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
  374.         if check_grammar and attr not in allowed_args:
  375.             raise AttributeError, 'Attribute %s is not allowed in <%s>' % (attr, self.tagName)
  376.         attr not in allowed_args
  377.         i = allowed_args.index(attr)
  378.         self.setAttrNS(allowed_attrs[i][0], allowed_attrs[i][1], value)
  379.  
  380.     
  381.     def setAttrNS(self, namespace, localpart, value):
  382.         allowed_attrs = self.allowed_attributes()
  383.         prefix = self.get_nsprefix(namespace)
  384.         c = AttrConverters()
  385.         self.attributes[prefix + ':' + localpart] = c.convert((namespace, localpart), value, self.qname)
  386.  
  387.     
  388.     def getAttrNS(self, namespace, localpart):
  389.         prefix = self.get_nsprefix(namespace)
  390.         return self.attributes.get(prefix + ':' + localpart)
  391.  
  392.     
  393.     def removeAttrNS(self, namespace, localpart):
  394.         prefix = self.get_nsprefix(namespace)
  395.         del self.attributes[prefix + ':' + localpart]
  396.  
  397.     
  398.     def getAttribute(self, attr):
  399.         allowed_attrs = self.allowed_attributes()
  400.         if allowed_attrs is None:
  401.             if type(attr) == type(()):
  402.                 (prefix, localname) = attr
  403.                 return self.getAttrNS(prefix, localname)
  404.             raise AttributeError, 'Unable to get simple attribute - use (namespace, localpart)'
  405.         allowed_attrs is None
  406.         allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
  407.         i = allowed_args.index(attr)
  408.         return self.getAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
  409.  
  410.     
  411.     def write_open_tag(self, level, f):
  412.         f.write('<' + self.tagName)
  413.         if level == 0:
  414.             for namespace, prefix in self.namespaces.items():
  415.                 f.write(' xmlns:' + prefix + '="' + _escape(str(namespace)) + '"')
  416.             
  417.         
  418.         for attkey in self.attributes.keys():
  419.             f.write(' ' + _escape(str(attkey)) + '=' + _quoteattr(unicode(self.attributes[attkey]).encode('utf-8')))
  420.         
  421.         f.write('>')
  422.  
  423.     
  424.     def write_close_tag(self, level, f):
  425.         f.write('</' + self.tagName + '>')
  426.  
  427.     
  428.     def toXml(self, level, f):
  429.         f.write('<' + self.tagName)
  430.         if level == 0:
  431.             for namespace, prefix in self.namespaces.items():
  432.                 f.write(' xmlns:' + prefix + '="' + _escape(str(namespace)) + '"')
  433.             
  434.         
  435.         for attkey in self.attributes.keys():
  436.             f.write(' ' + _escape(str(attkey)) + '=' + _quoteattr(unicode(self.attributes[attkey]).encode('utf-8')))
  437.         
  438.         if self.childNodes:
  439.             f.write('>')
  440.             for element in self.childNodes:
  441.                 element.toXml(level + 1, f)
  442.             
  443.             f.write('</' + self.tagName + '>')
  444.         else:
  445.             f.write('/>')
  446.  
  447.     
  448.     def _getElementsByObj(self, obj, accumulator):
  449.         if self.qname == obj.qname:
  450.             accumulator.append(self)
  451.         
  452.         for e in self.childNodes:
  453.             if e.nodeType == Node.ELEMENT_NODE:
  454.                 accumulator = e._getElementsByObj(obj, accumulator)
  455.                 continue
  456.         
  457.         return accumulator
  458.  
  459.     
  460.     def getElementsByType(self, element):
  461.         obj = element(check_grammar = False)
  462.         return self._getElementsByObj(obj, [])
  463.  
  464.     
  465.     def isInstanceOf(self, element):
  466.         obj = element(check_grammar = False)
  467.         return self.qname == obj.qname
  468.  
  469.  
  470.