home *** CD-ROM | disk | FTP | other *** search
/ Freelog 116 / FreelogNo116-JuilletSeptembre2013.iso / Bureautique / gImageReader / gimagereader_0.9-1_win32.exe / bin / encodings / idna.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2011-03-24  |  6KB  |  227 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import stringprep
  5. import re
  6. import codecs
  7. from unicodedata import ucd_3_2_0 as unicodedata
  8. dots = re.compile(u'[.\xe3\x80\x82\xef\xbc\x8e\xef\xbd\xa1]')
  9. ace_prefix = 'xn--'
  10. uace_prefix = unicode(ace_prefix, 'ascii')
  11.  
  12. def nameprep(label):
  13.     newlabel = []
  14.     for c in label:
  15.         if stringprep.in_table_b1(c):
  16.             continue
  17.         newlabel.append(stringprep.map_table_b2(c))
  18.     
  19.     label = u''.join(newlabel)
  20.     label = unicodedata.normalize('NFKC', label)
  21.     for c in label:
  22.         if not stringprep.in_table_c12(c) and stringprep.in_table_c22(c) and stringprep.in_table_c3(c) and stringprep.in_table_c4(c) and stringprep.in_table_c5(c) and stringprep.in_table_c6(c) and stringprep.in_table_c7(c) and stringprep.in_table_c8(c):
  23.             if stringprep.in_table_c9(c):
  24.                 raise UnicodeError('Invalid character %r' % c)
  25.     RandAL = map(stringprep.in_table_d1, label)
  26.     for c in RandAL:
  27.         if c or filter(stringprep.in_table_d2, label):
  28.             raise UnicodeError('Violation of BIDI requirement 2')
  29.         if not RandAL[0] or not RandAL[-1]:
  30.             raise UnicodeError('Violation of BIDI requirement 3')
  31.         continue
  32.     
  33.     return label
  34.  
  35.  
  36. def ToASCII(label):
  37.     
  38.     try:
  39.         label = label.encode('ascii')
  40.     except UnicodeError:
  41.         pass
  42.  
  43.     if len(label) < len(label):
  44.         pass
  45.     elif len(label) < 64:
  46.         return label
  47.     raise UnicodeError('label empty or too long')
  48.     label = nameprep(label)
  49.     
  50.     try:
  51.         label = label.encode('ascii')
  52.     except UnicodeError:
  53.         pass
  54.  
  55.     if len(label) < len(label):
  56.         pass
  57.     elif len(label) < 64:
  58.         return label
  59.     raise UnicodeError('label empty or too long')
  60.     if label.startswith(uace_prefix):
  61.         raise UnicodeError('Label starts with ACE prefix')
  62.     label = label.encode('punycode')
  63.     label = ace_prefix + label
  64.     if len(label) < len(label):
  65.         pass
  66.     elif len(label) < 64:
  67.         return label
  68.     raise UnicodeError('label empty or too long')
  69.  
  70.  
  71. def ToUnicode(label):
  72.     if isinstance(label, str):
  73.         pure_ascii = True
  74.     else:
  75.         
  76.         try:
  77.             label = label.encode('ascii')
  78.             pure_ascii = True
  79.         except UnicodeError:
  80.             pure_ascii = False
  81.  
  82.     if not pure_ascii:
  83.         label = nameprep(label)
  84.         
  85.         try:
  86.             label = label.encode('ascii')
  87.         except UnicodeError:
  88.             raise UnicodeError('Invalid character in IDN label')
  89.         
  90.  
  91.     if not label.startswith(ace_prefix):
  92.         return unicode(label, 'ascii')
  93.     label1 = None[len(ace_prefix):]
  94.     result = label1.decode('punycode')
  95.     label2 = ToASCII(result)
  96.     if label.lower() != label2:
  97.         raise UnicodeError('IDNA does not round-trip', label, label2)
  98.     return result
  99.  
  100.  
  101. class Codec(codecs.Codec):
  102.     
  103.     def encode(self, input, errors = 'strict'):
  104.         if errors != 'strict':
  105.             raise UnicodeError('unsupported error handling ' + errors)
  106.         if not input:
  107.             return ('', 0)
  108.         result = None
  109.         labels = dots.split(input)
  110.         if labels and len(labels[-1]) == 0:
  111.             trailing_dot = '.'
  112.             del labels[-1]
  113.         else:
  114.             trailing_dot = ''
  115.         for label in labels:
  116.             result.append(ToASCII(label))
  117.         
  118.         return ('.'.join(result) + trailing_dot, len(input))
  119.  
  120.     
  121.     def decode(self, input, errors = 'strict'):
  122.         if errors != 'strict':
  123.             raise UnicodeError('Unsupported error handling ' + errors)
  124.         if not input:
  125.             return (u'', 0)
  126.         if None(input, unicode):
  127.             labels = dots.split(input)
  128.         else:
  129.             input = str(input)
  130.             unicode(input, 'ascii')
  131.             labels = input.split('.')
  132.         if labels and len(labels[-1]) == 0:
  133.             trailing_dot = u'.'
  134.             del labels[-1]
  135.         else:
  136.             trailing_dot = u''
  137.         result = []
  138.         for label in labels:
  139.             result.append(ToUnicode(label))
  140.         
  141.         return (u'.'.join(result) + trailing_dot, len(input))
  142.  
  143.  
  144.  
  145. class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
  146.     
  147.     def _buffer_encode(self, input, errors, final):
  148.         if errors != 'strict':
  149.             raise UnicodeError('unsupported error handling ' + errors)
  150.         if not input:
  151.             return ('', 0)
  152.         labels = None.split(input)
  153.         trailing_dot = u''
  154.         if labels:
  155.             if not labels[-1]:
  156.                 trailing_dot = '.'
  157.                 del labels[-1]
  158.             elif not final:
  159.                 del labels[-1]
  160.                 if labels:
  161.                     trailing_dot = '.'
  162.                 
  163.             
  164.         result = []
  165.         size = 0
  166.         for label in labels:
  167.             result.append(ToASCII(label))
  168.             if size:
  169.                 size += 1
  170.             size += len(label)
  171.         
  172.         result = '.'.join(result) + trailing_dot
  173.         size += len(trailing_dot)
  174.         return (result, size)
  175.  
  176.  
  177.  
  178. class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
  179.     
  180.     def _buffer_decode(self, input, errors, final):
  181.         if errors != 'strict':
  182.             raise UnicodeError('Unsupported error handling ' + errors)
  183.         if not input:
  184.             return (u'', 0)
  185.         if None(input, unicode):
  186.             labels = dots.split(input)
  187.         else:
  188.             input = str(input)
  189.             unicode(input, 'ascii')
  190.             labels = input.split('.')
  191.         trailing_dot = u''
  192.         if labels:
  193.             if not labels[-1]:
  194.                 trailing_dot = u'.'
  195.                 del labels[-1]
  196.             elif not final:
  197.                 del labels[-1]
  198.                 if labels:
  199.                     trailing_dot = u'.'
  200.                 
  201.             
  202.         result = []
  203.         size = 0
  204.         for label in labels:
  205.             result.append(ToUnicode(label))
  206.             if size:
  207.                 size += 1
  208.             size += len(label)
  209.         
  210.         result = u'.'.join(result) + trailing_dot
  211.         size += len(trailing_dot)
  212.         return (result, size)
  213.  
  214.  
  215.  
  216. class StreamWriter(Codec, codecs.StreamWriter):
  217.     pass
  218.  
  219.  
  220. class StreamReader(Codec, codecs.StreamReader):
  221.     pass
  222.  
  223.  
  224. def getregentry():
  225.     return codecs.CodecInfo(name = 'idna', encode = Codec().encode, decode = Codec().decode, incrementalencoder = IncrementalEncoder, incrementaldecoder = IncrementalDecoder, streamwriter = StreamWriter, streamreader = StreamReader)
  226.  
  227.