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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import urllib
  5. from html5lib import HTMLParser as _HTMLParser, XHTMLParser as _XHTMLParser
  6. from lxml import etree
  7. from lxml.html import _contains_block_level_tag, XHTML_NAMESPACE
  8. from lxml.html._html5builder import TreeBuilder
  9.  
  10. try:
  11.     _strings = basestring
  12. except NameError:
  13.     _strings = (bytes, str)
  14.  
  15.  
  16. class HTMLParser(_HTMLParser):
  17.     
  18.     def __init__(self, strict = False):
  19.         _HTMLParser.__init__(self, strict = strict, tree = TreeBuilder)
  20.  
  21.  
  22.  
  23. class XHTMLParser(_XHTMLParser):
  24.     
  25.     def __init__(self, strict = False):
  26.         _XHTMLParser.__init__(self, strict = strict, tree = TreeBuilder)
  27.  
  28.  
  29.  
  30. def _find_tag(tree, tag):
  31.     elem = tree.find(tag)
  32.     if elem is not None:
  33.         return elem
  34.     return tree.find('{%s}%s' % (XHTML_NAMESPACE, tag))
  35.  
  36.  
  37. def document_fromstring(html, guess_charset = True, parser = None):
  38.     if not isinstance(html, _strings):
  39.         raise TypeError('string required')
  40.     isinstance(html, _strings)
  41.     if parser is None:
  42.         parser = html_parser
  43.     
  44.     return parser.parse(html, useChardet = guess_charset).getroot()
  45.  
  46.  
  47. def fragments_fromstring(html, no_leading_text = False, guess_charset = False, parser = None):
  48.     if not isinstance(html, _strings):
  49.         raise TypeError('string required')
  50.     isinstance(html, _strings)
  51.     if parser is None:
  52.         parser = html_parser
  53.     
  54.     children = parser.parseFragment(html, 'div', useChardet = guess_charset)
  55.     if children and isinstance(children[0], _strings):
  56.         if no_leading_text:
  57.             if children[0].strip():
  58.                 raise etree.ParserError('There is leading text: %r' % children[0])
  59.             children[0].strip()
  60.             del children[0]
  61.         
  62.     
  63.     return children
  64.  
  65.  
  66. def fragment_fromstring(html, create_parent = False, guess_charset = False, parser = None):
  67.     if not isinstance(html, _strings):
  68.         raise TypeError('string required')
  69.     isinstance(html, _strings)
  70.     if create_parent:
  71.         if not create_parent:
  72.             pass
  73.         container = 'div'
  74.         html = '<%s>%s</%s>' % (container, html, container)
  75.     
  76.     children = fragments_fromstring(html, True, guess_charset, parser)
  77.     if not children:
  78.         raise etree.ParserError('No elements found')
  79.     children
  80.     if len(children) > 1:
  81.         raise etree.ParserError('Multiple elements found')
  82.     len(children) > 1
  83.     result = children[0]
  84.     if result.tail and result.tail.strip():
  85.         raise etree.ParserError('Element followed by text: %r' % result.tail)
  86.     result.tail.strip()
  87.     result.tail = None
  88.     return result
  89.  
  90.  
  91. def fromstring(html, guess_charset = True, parser = None):
  92.     if not isinstance(html, _strings):
  93.         raise TypeError('string required')
  94.     isinstance(html, _strings)
  95.     doc = document_fromstring(html, parser = parser, guess_charset = guess_charset)
  96.     start = html[:50].lstrip().lower()
  97.     if start.startswith('<html') or start.startswith('<!doctype'):
  98.         return doc
  99.     head = _find_tag(doc, 'head')
  100.     if len(head):
  101.         return doc
  102.     body = _find_tag(doc, 'body')
  103.     if len(body) == 1:
  104.         if not (body.text) or not body.text.strip():
  105.             if not (body[-1].tail) or not body[-1].tail.strip():
  106.                 return body[0]
  107.     return body
  108.  
  109.  
  110. def parse(filename_url_or_file, guess_charset = True, parser = None):
  111.     if parser is None:
  112.         parser = html_parser
  113.     
  114.     if isinstance(filename_url_or_file, basestring):
  115.         fp = urllib.urlopen(filename_url_or_file)
  116.     else:
  117.         fp = filename_url_or_file
  118.     return parser.parse(fp, useChardet = guess_charset)
  119.  
  120. html_parser = HTMLParser()
  121. xhtml_parser = XHTMLParser()
  122.