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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Charset',
  6.     'add_alias',
  7.     'add_charset',
  8.     'add_codec']
  9. import email.base64mime as email
  10. import email.quoprimime as email
  11. from email import errors
  12. from email.encoders import encode_7or8bit
  13. QP = 1
  14. BASE64 = 2
  15. SHORTEST = 3
  16. MISC_LEN = 7
  17. DEFAULT_CHARSET = 'us-ascii'
  18. CHARSETS = {
  19.     'iso-8859-1': (QP, QP, None),
  20.     'iso-8859-2': (QP, QP, None),
  21.     'iso-8859-3': (QP, QP, None),
  22.     'iso-8859-4': (QP, QP, None),
  23.     'iso-8859-9': (QP, QP, None),
  24.     'iso-8859-10': (QP, QP, None),
  25.     'iso-8859-13': (QP, QP, None),
  26.     'iso-8859-14': (QP, QP, None),
  27.     'iso-8859-15': (QP, QP, None),
  28.     'iso-8859-16': (QP, QP, None),
  29.     'windows-1252': (QP, QP, None),
  30.     'viscii': (QP, QP, None),
  31.     'us-ascii': (None, None, None),
  32.     'big5': (BASE64, BASE64, None),
  33.     'gb2312': (BASE64, BASE64, None),
  34.     'euc-jp': (BASE64, None, 'iso-2022-jp'),
  35.     'shift_jis': (BASE64, None, 'iso-2022-jp'),
  36.     'iso-2022-jp': (BASE64, None, None),
  37.     'koi8-r': (BASE64, BASE64, None),
  38.     'utf-8': (SHORTEST, BASE64, 'utf-8'),
  39.     '8bit': (None, BASE64, 'utf-8') }
  40. ALIASES = {
  41.     'latin_1': 'iso-8859-1',
  42.     'latin-1': 'iso-8859-1',
  43.     'latin_2': 'iso-8859-2',
  44.     'latin-2': 'iso-8859-2',
  45.     'latin_3': 'iso-8859-3',
  46.     'latin-3': 'iso-8859-3',
  47.     'latin_4': 'iso-8859-4',
  48.     'latin-4': 'iso-8859-4',
  49.     'latin_5': 'iso-8859-9',
  50.     'latin-5': 'iso-8859-9',
  51.     'latin_6': 'iso-8859-10',
  52.     'latin-6': 'iso-8859-10',
  53.     'latin_7': 'iso-8859-13',
  54.     'latin-7': 'iso-8859-13',
  55.     'latin_8': 'iso-8859-14',
  56.     'latin-8': 'iso-8859-14',
  57.     'latin_9': 'iso-8859-15',
  58.     'latin-9': 'iso-8859-15',
  59.     'latin_10': 'iso-8859-16',
  60.     'latin-10': 'iso-8859-16',
  61.     'cp949': 'ks_c_5601-1987',
  62.     'euc_jp': 'euc-jp',
  63.     'euc_kr': 'euc-kr',
  64.     'ascii': 'us-ascii' }
  65. CODEC_MAP = {
  66.     'gb2312': 'eucgb2312_cn',
  67.     'big5': 'big5_tw',
  68.     'us-ascii': None }
  69.  
  70. def add_charset(charset, header_enc = None, body_enc = None, output_charset = None):
  71.     if body_enc == SHORTEST:
  72.         raise ValueError('SHORTEST not allowed for body_enc')
  73.     body_enc == SHORTEST
  74.     CHARSETS[charset] = (header_enc, body_enc, output_charset)
  75.  
  76.  
  77. def add_alias(alias, canonical):
  78.     ALIASES[alias] = canonical
  79.  
  80.  
  81. def add_codec(charset, codecname):
  82.     CODEC_MAP[charset] = codecname
  83.  
  84.  
  85. class Charset:
  86.     
  87.     def __init__(self, input_charset = DEFAULT_CHARSET):
  88.         
  89.         try:
  90.             if isinstance(input_charset, unicode):
  91.                 input_charset.encode('ascii')
  92.             else:
  93.                 input_charset = unicode(input_charset, 'ascii')
  94.         except UnicodeError:
  95.             raise errors.CharsetError(input_charset)
  96.  
  97.         input_charset = input_charset.lower()
  98.         self.input_charset = ALIASES.get(input_charset, input_charset)
  99.         (henc, benc, conv) = CHARSETS.get(self.input_charset, (SHORTEST, BASE64, None))
  100.         if not conv:
  101.             conv = self.input_charset
  102.         
  103.         self.header_encoding = henc
  104.         self.body_encoding = benc
  105.         self.output_charset = ALIASES.get(conv, conv)
  106.         self.input_codec = CODEC_MAP.get(self.input_charset, self.input_charset)
  107.         self.output_codec = CODEC_MAP.get(self.output_charset, self.output_charset)
  108.  
  109.     
  110.     def __str__(self):
  111.         return self.input_charset.lower()
  112.  
  113.     __repr__ = __str__
  114.     
  115.     def __eq__(self, other):
  116.         return str(self) == str(other).lower()
  117.  
  118.     
  119.     def __ne__(self, other):
  120.         return not self.__eq__(other)
  121.  
  122.     
  123.     def get_body_encoding(self):
  124.         if self.body_encoding == QP:
  125.             return 'quoted-printable'
  126.         if self.body_encoding == BASE64:
  127.             return 'base64'
  128.         return encode_7or8bit
  129.  
  130.     
  131.     def convert(self, s):
  132.         if self.input_codec != self.output_codec:
  133.             return unicode(s, self.input_codec).encode(self.output_codec)
  134.         return s
  135.  
  136.     
  137.     def to_splittable(self, s):
  138.         if isinstance(s, unicode) or self.input_codec is None:
  139.             return s
  140.         
  141.         try:
  142.             return unicode(s, self.input_codec, 'replace')
  143.         except LookupError:
  144.             self.input_codec is None
  145.             self.input_codec is None
  146.             return s
  147.  
  148.  
  149.     
  150.     def from_splittable(self, ustr, to_output = True):
  151.         if to_output:
  152.             codec = self.output_codec
  153.         else:
  154.             codec = self.input_codec
  155.         if not isinstance(ustr, unicode) or codec is None:
  156.             return ustr
  157.         
  158.         try:
  159.             return ustr.encode(codec, 'replace')
  160.         except LookupError:
  161.             codec is None
  162.             codec is None
  163.             return ustr
  164.  
  165.  
  166.     
  167.     def get_output_charset(self):
  168.         if not self.output_charset:
  169.             pass
  170.         return self.input_charset
  171.  
  172.     
  173.     def encoded_header_len(self, s):
  174.         cset = self.get_output_charset()
  175.         if self.header_encoding == BASE64:
  176.             return email.base64mime.base64_len(s) + len(cset) + MISC_LEN
  177.         if self.header_encoding == QP:
  178.             return email.quoprimime.header_quopri_len(s) + len(cset) + MISC_LEN
  179.         if self.header_encoding == SHORTEST:
  180.             lenb64 = email.base64mime.base64_len(s)
  181.             lenqp = email.quoprimime.header_quopri_len(s)
  182.             return min(lenb64, lenqp) + len(cset) + MISC_LEN
  183.         return len(s)
  184.  
  185.     
  186.     def header_encode(self, s, convert = False):
  187.         cset = self.get_output_charset()
  188.         if convert:
  189.             s = self.convert(s)
  190.         
  191.         if self.header_encoding == BASE64:
  192.             return email.base64mime.header_encode(s, cset)
  193.         if self.header_encoding == QP:
  194.             return email.quoprimime.header_encode(s, cset, maxlinelen = None)
  195.         if self.header_encoding == SHORTEST:
  196.             lenb64 = email.base64mime.base64_len(s)
  197.             lenqp = email.quoprimime.header_quopri_len(s)
  198.             if lenb64 < lenqp:
  199.                 return email.base64mime.header_encode(s, cset)
  200.             return email.quoprimime.header_encode(s, cset, maxlinelen = None)
  201.         self.header_encoding == SHORTEST
  202.         return s
  203.  
  204.     
  205.     def body_encode(self, s, convert = True):
  206.         if convert:
  207.             s = self.convert(s)
  208.         
  209.         if self.body_encoding is BASE64:
  210.             return email.base64mime.body_encode(s)
  211.         if self.body_encoding is QP:
  212.             return email.quoprimime.body_encode(s)
  213.         return s
  214.  
  215.  
  216.