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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '1.0'
  5. import re
  6.  
  7. def detect(aBuf):
  8.     import calibre.ebooks.chardet.universaldetector as universaldetector
  9.     u = universaldetector.UniversalDetector()
  10.     u.reset()
  11.     u.feed(aBuf)
  12.     u.close()
  13.     return u.result
  14.  
  15. ENCODING_PATS = [
  16.     re.compile('<\\?[^<>]+encoding=[\\\'"](.*?)[\\\'"][^<>]*>', re.IGNORECASE),
  17.     re.compile('<meta\\s+?[^<>]+?content=[\'"][^\'"]*?charset=([-a-z0-9]+)[^\'"]*?[\'"][^<>]*>', re.IGNORECASE)]
  18. ENTITY_PATTERN = re.compile('&(\\S+?);')
  19.  
  20. def strip_encoding_declarations(raw):
  21.     for pat in ENCODING_PATS:
  22.         raw = pat.sub('', raw)
  23.     
  24.     return raw
  25.  
  26.  
  27. def substitute_entites(raw):
  28.     xml_entity_to_unicode = xml_entity_to_unicode
  29.     import calibre
  30.     return ENTITY_PATTERN.sub(xml_entity_to_unicode, raw)
  31.  
  32. _CHARSET_ALIASES = {
  33.     'macintosh': 'mac-roman',
  34.     'x-sjis': 'shift-jis' }
  35.  
  36. def force_encoding(raw, verbose, assume_utf8 = False):
  37.     preferred_encoding = preferred_encoding
  38.     import calibre.constants
  39.     
  40.     try:
  41.         chardet = detect(raw)
  42.     except:
  43.         chardet = {
  44.             'encoding': preferred_encoding,
  45.             'confidence': 0 }
  46.  
  47.     encoding = chardet['encoding']
  48.     if chardet['confidence'] < 1 and assume_utf8:
  49.         encoding = 'utf-8'
  50.     
  51.     if chardet['confidence'] < 1 and verbose:
  52.         print 'WARNING: Encoding detection confidence %d%%' % chardet['confidence'] * 100
  53.     
  54.     if not encoding:
  55.         encoding = preferred_encoding
  56.     
  57.     encoding = encoding.lower()
  58.     if _CHARSET_ALIASES.has_key(encoding):
  59.         encoding = _CHARSET_ALIASES[encoding]
  60.     
  61.     if encoding == 'ascii':
  62.         encoding = 'utf-8'
  63.     
  64.     return encoding
  65.  
  66.  
  67. def xml_to_unicode(raw, verbose = False, strip_encoding_pats = False, resolve_entities = False, assume_utf8 = False):
  68.     encoding = None
  69.     if not raw:
  70.         return (u'', encoding)
  71.     if not isinstance(raw, unicode):
  72.         if raw.startswith('\xff\xfe'):
  73.             raw = raw.decode('utf-16-le')[1:]
  74.             encoding = 'utf-16-le'
  75.         elif raw.startswith('\xfe\xff'):
  76.             raw = raw.decode('utf-16-be')[1:]
  77.             encoding = 'utf-16-be'
  78.         
  79.     
  80.     if not isinstance(raw, unicode):
  81.         for pat in ENCODING_PATS:
  82.             match = pat.search(raw)
  83.             if match:
  84.                 encoding = match.group(1)
  85.                 break
  86.                 continue
  87.         
  88.         if encoding is None:
  89.             encoding = force_encoding(raw, verbose, assume_utf8 = assume_utf8)
  90.         
  91.         
  92.         try:
  93.             if encoding.lower().strip() == 'macintosh':
  94.                 encoding = 'mac-roman'
  95.             
  96.             raw = raw.decode(encoding, 'replace')
  97.         except LookupError:
  98.             encoding = 'utf-8'
  99.             raw = raw.decode(encoding, 'replace')
  100.         except:
  101.             None<EXCEPTION MATCH>LookupError
  102.         
  103.  
  104.     None<EXCEPTION MATCH>LookupError
  105.     if strip_encoding_pats:
  106.         raw = strip_encoding_declarations(raw)
  107.     
  108.     if resolve_entities:
  109.         raw = substitute_entites(raw)
  110.     
  111.     return (raw, encoding)
  112.  
  113.