home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / pyxmpp / xmppstringprep.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  6.0 KB  |  221 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: xmppstringprep.py,v 1.16 2004/10/07 22:28:04 jajcus Exp $'
  5. __docformat__ = 'restructuredtext en'
  6. import stringprep
  7. import unicodedata
  8. from pyxmpp.exceptions import StringprepError
  9.  
  10. class LookupFunction:
  11.     
  12.     def __init__(self, function):
  13.         self.lookup = function
  14.  
  15.  
  16.  
  17. class LookupTable:
  18.     
  19.     def __init__(self, singles, ranges):
  20.         self.singles = singles
  21.         self.ranges = ranges
  22.  
  23.     
  24.     def lookup(self, c):
  25.         if self.singles.has_key(c):
  26.             return self.singles[c]
  27.         c = ord(c)
  28.         for start, end in self.ranges:
  29.             value = None
  30.             if c < start:
  31.                 return None
  32.             if c <= end:
  33.                 return value
  34.         
  35.  
  36.  
  37. A_1 = LookupFunction(stringprep.in_table_a1)
  38.  
  39. def b1_mapping(uc):
  40.     if stringprep.in_table_b1(uc):
  41.         return u''
  42.     return None
  43.  
  44. B_1 = LookupFunction(b1_mapping)
  45. B_2 = LookupFunction(stringprep.map_table_b2)
  46. B_3 = LookupFunction(stringprep.map_table_b3)
  47. C_1_1 = LookupFunction(stringprep.in_table_c11)
  48. C_1_2 = LookupFunction(stringprep.in_table_c12)
  49. C_2_1 = LookupFunction(stringprep.in_table_c21)
  50. C_2_2 = LookupFunction(stringprep.in_table_c22)
  51. C_3 = LookupFunction(stringprep.in_table_c3)
  52. C_4 = LookupFunction(stringprep.in_table_c4)
  53. C_5 = LookupFunction(stringprep.in_table_c5)
  54. C_6 = LookupFunction(stringprep.in_table_c6)
  55. C_7 = LookupFunction(stringprep.in_table_c7)
  56. C_8 = LookupFunction(stringprep.in_table_c8)
  57. C_9 = LookupFunction(stringprep.in_table_c9)
  58. D_1 = LookupFunction(stringprep.in_table_d1)
  59. D_2 = LookupFunction(stringprep.in_table_d2)
  60.  
  61. def nfkc(data):
  62.     if type(data) is list:
  63.         data = u''.join(data)
  64.     
  65.     return unicodedata.normalize('NFKC', data)
  66.  
  67.  
  68. class Profile:
  69.     cache_items = []
  70.     
  71.     def __init__(self, unassigned, mapping, normalization, prohibited, bidi = 1):
  72.         self.unassigned = unassigned
  73.         self.mapping = mapping
  74.         self.normalization = normalization
  75.         self.prohibited = prohibited
  76.         self.bidi = bidi
  77.         self.cache = { }
  78.  
  79.     
  80.     def prepare(self, data):
  81.         r = self.cache.get(data)
  82.         if r is not None:
  83.             return r
  84.         s = self.map(data)
  85.         if self.normalization:
  86.             s = self.normalization(s)
  87.         
  88.         s = self.prohibit(s)
  89.         s = self.check_unassigned(s)
  90.         if self.bidi:
  91.             s = self.check_bidi(s)
  92.         
  93.         if type(s) is list:
  94.             s = u''.string.join()
  95.         
  96.         if len(self.cache_items) >= stringprep_cache_size:
  97.             remove = self.cache_items[:-stringprep_cache_size / 2]
  98.             for profile, key in remove:
  99.                 
  100.                 try:
  101.                     del profile.cache[key]
  102.                 continue
  103.                 except KeyError:
  104.                     continue
  105.                 
  106.  
  107.             
  108.             self.cache_items[:] = self.cache_items[-stringprep_cache_size / 2:]
  109.         
  110.         self.cache_items.append((self, data))
  111.         self.cache[data] = s
  112.         return s
  113.  
  114.     
  115.     def prepare_query(self, s):
  116.         s = self.map(s)
  117.         if self.normalization:
  118.             s = self.normalization(s)
  119.         
  120.         s = self.prohibit(s)
  121.         if self.bidi:
  122.             s = self.check_bidi(s)
  123.         
  124.         if type(s) is list:
  125.             s = u''.string.join(s)
  126.         
  127.         return s
  128.  
  129.     
  130.     def map(self, s):
  131.         r = []
  132.         for c in s:
  133.             rc = None
  134.             for t in self.mapping:
  135.                 rc = t.lookup(c)
  136.                 if rc is not None:
  137.                     break
  138.                     continue
  139.             
  140.             if rc is not None:
  141.                 r.append(rc)
  142.                 continue
  143.             r.append(c)
  144.         
  145.         return r
  146.  
  147.     
  148.     def prohibit(self, s):
  149.         for c in s:
  150.             for t in self.prohibited:
  151.                 if t.lookup(c):
  152.                     raise StringprepError, 'Prohibited character: %r' % (c,)
  153.                 t.lookup(c)
  154.             
  155.         
  156.         return s
  157.  
  158.     
  159.     def check_unassigned(self, s):
  160.         for c in s:
  161.             for t in self.unassigned:
  162.                 if t.lookup(c):
  163.                     raise StringprepError, 'Unassigned character: %r' % (c,)
  164.                 t.lookup(c)
  165.             
  166.         
  167.         return s
  168.  
  169.     
  170.     def check_bidi(self, s):
  171.         has_l = 0
  172.         has_ral = 0
  173.         for c in s:
  174.             if D_1.lookup(c):
  175.                 has_ral = 1
  176.                 continue
  177.             if D_2.lookup(c):
  178.                 has_l = 1
  179.                 continue
  180.         
  181.         if has_l and has_ral:
  182.             raise StringprepError, 'Both RandALCat and LCat characters present'
  183.         has_ral
  184.         if has_l:
  185.             if D_1.lookup(s[0]) is None or D_1.lookup(s[-1]) is None:
  186.                 raise StringprepError, 'The first and the last character must be RandALCat'
  187.         D_1.lookup(s[-1]) is None
  188.         return s
  189.  
  190.  
  191. nodeprep = Profile(unassigned = (A_1,), mapping = (B_1, B_2), normalization = nfkc, prohibited = (C_1_1, C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9, LookupTable({
  192.     u'"': True,
  193.     u'&': True,
  194.     u"'": True,
  195.     u'/': True,
  196.     u':': True,
  197.     u'<': True,
  198.     u'>': True,
  199.     u'@': True }, ())), bidi = 1)
  200. resourceprep = Profile(unassigned = (A_1,), mapping = (B_1,), normalization = nfkc, prohibited = (C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9), bidi = 1)
  201. stringprep_cache_size = 1000
  202.  
  203. def set_stringprep_cache_size(size):
  204.     global stringprep_cache_size
  205.     stringprep_cache_size = size
  206.     if len(Profile.cache_items) > size:
  207.         remove = Profile.cache_items[:-size]
  208.         for profile, key in remove:
  209.             
  210.             try:
  211.                 del profile.cache[key]
  212.             continue
  213.             except KeyError:
  214.                 continue
  215.             
  216.  
  217.         
  218.         Profile.cache_items = Profile.cache_items[-size:]
  219.     
  220.  
  221.