home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / Cookie.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  14.0 KB  |  508 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import string
  5.  
  6. try:
  7.     from cPickle import dumps, loads
  8. except ImportError:
  9.     from pickle import dumps, loads
  10.  
  11. import re
  12. import warnings
  13. __all__ = [
  14.     'CookieError',
  15.     'BaseCookie',
  16.     'SimpleCookie',
  17.     'SerialCookie',
  18.     'SmartCookie',
  19.     'Cookie']
  20. _nulljoin = ''.join
  21. _semispacejoin = '; '.join
  22. _spacejoin = ' '.join
  23.  
  24. class CookieError(Exception):
  25.     pass
  26.  
  27. _LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~"
  28. _Translator = {
  29.     '\x00': '\\000',
  30.     '\x01': '\\001',
  31.     '\x02': '\\002',
  32.     '\x03': '\\003',
  33.     '\x04': '\\004',
  34.     '\x05': '\\005',
  35.     '\x06': '\\006',
  36.     '\x07': '\\007',
  37.     '\x08': '\\010',
  38.     '\t': '\\011',
  39.     '\n': '\\012',
  40.     '\x0b': '\\013',
  41.     '\x0c': '\\014',
  42.     '\r': '\\015',
  43.     '\x0e': '\\016',
  44.     '\x0f': '\\017',
  45.     '\x10': '\\020',
  46.     '\x11': '\\021',
  47.     '\x12': '\\022',
  48.     '\x13': '\\023',
  49.     '\x14': '\\024',
  50.     '\x15': '\\025',
  51.     '\x16': '\\026',
  52.     '\x17': '\\027',
  53.     '\x18': '\\030',
  54.     '\x19': '\\031',
  55.     '\x1a': '\\032',
  56.     '\x1b': '\\033',
  57.     '\x1c': '\\034',
  58.     '\x1d': '\\035',
  59.     '\x1e': '\\036',
  60.     '\x1f': '\\037',
  61.     '"': '\\"',
  62.     '\\': '\\\\',
  63.     '\x7f': '\\177',
  64.     '\x80': '\\200',
  65.     '\x81': '\\201',
  66.     '\x82': '\\202',
  67.     '\x83': '\\203',
  68.     '\x84': '\\204',
  69.     '\x85': '\\205',
  70.     '\x86': '\\206',
  71.     '\x87': '\\207',
  72.     '\x88': '\\210',
  73.     '\x89': '\\211',
  74.     '\x8a': '\\212',
  75.     '\x8b': '\\213',
  76.     '\x8c': '\\214',
  77.     '\x8d': '\\215',
  78.     '\x8e': '\\216',
  79.     '\x8f': '\\217',
  80.     '\x90': '\\220',
  81.     '\x91': '\\221',
  82.     '\x92': '\\222',
  83.     '\x93': '\\223',
  84.     '\x94': '\\224',
  85.     '\x95': '\\225',
  86.     '\x96': '\\226',
  87.     '\x97': '\\227',
  88.     '\x98': '\\230',
  89.     '\x99': '\\231',
  90.     '\x9a': '\\232',
  91.     '\x9b': '\\233',
  92.     '\x9c': '\\234',
  93.     '\x9d': '\\235',
  94.     '\x9e': '\\236',
  95.     '\x9f': '\\237',
  96.     '\xa0': '\\240',
  97.     '\xa1': '\\241',
  98.     '\xa2': '\\242',
  99.     '\xa3': '\\243',
  100.     '\xa4': '\\244',
  101.     '\xa5': '\\245',
  102.     '\xa6': '\\246',
  103.     '\xa7': '\\247',
  104.     '\xa8': '\\250',
  105.     '\xa9': '\\251',
  106.     '\xaa': '\\252',
  107.     '\xab': '\\253',
  108.     '\xac': '\\254',
  109.     '\xad': '\\255',
  110.     '\xae': '\\256',
  111.     '\xaf': '\\257',
  112.     '\xb0': '\\260',
  113.     '\xb1': '\\261',
  114.     '\xb2': '\\262',
  115.     '\xb3': '\\263',
  116.     '\xb4': '\\264',
  117.     '\xb5': '\\265',
  118.     '\xb6': '\\266',
  119.     '\xb7': '\\267',
  120.     '\xb8': '\\270',
  121.     '\xb9': '\\271',
  122.     '\xba': '\\272',
  123.     '\xbb': '\\273',
  124.     '\xbc': '\\274',
  125.     '\xbd': '\\275',
  126.     '\xbe': '\\276',
  127.     '\xbf': '\\277',
  128.     '\xc0': '\\300',
  129.     '\xc1': '\\301',
  130.     '\xc2': '\\302',
  131.     '\xc3': '\\303',
  132.     '\xc4': '\\304',
  133.     '\xc5': '\\305',
  134.     '\xc6': '\\306',
  135.     '\xc7': '\\307',
  136.     '\xc8': '\\310',
  137.     '\xc9': '\\311',
  138.     '\xca': '\\312',
  139.     '\xcb': '\\313',
  140.     '\xcc': '\\314',
  141.     '\xcd': '\\315',
  142.     '\xce': '\\316',
  143.     '\xcf': '\\317',
  144.     '\xd0': '\\320',
  145.     '\xd1': '\\321',
  146.     '\xd2': '\\322',
  147.     '\xd3': '\\323',
  148.     '\xd4': '\\324',
  149.     '\xd5': '\\325',
  150.     '\xd6': '\\326',
  151.     '\xd7': '\\327',
  152.     '\xd8': '\\330',
  153.     '\xd9': '\\331',
  154.     '\xda': '\\332',
  155.     '\xdb': '\\333',
  156.     '\xdc': '\\334',
  157.     '\xdd': '\\335',
  158.     '\xde': '\\336',
  159.     '\xdf': '\\337',
  160.     '\xe0': '\\340',
  161.     '\xe1': '\\341',
  162.     '\xe2': '\\342',
  163.     '\xe3': '\\343',
  164.     '\xe4': '\\344',
  165.     '\xe5': '\\345',
  166.     '\xe6': '\\346',
  167.     '\xe7': '\\347',
  168.     '\xe8': '\\350',
  169.     '\xe9': '\\351',
  170.     '\xea': '\\352',
  171.     '\xeb': '\\353',
  172.     '\xec': '\\354',
  173.     '\xed': '\\355',
  174.     '\xee': '\\356',
  175.     '\xef': '\\357',
  176.     '\xf0': '\\360',
  177.     '\xf1': '\\361',
  178.     '\xf2': '\\362',
  179.     '\xf3': '\\363',
  180.     '\xf4': '\\364',
  181.     '\xf5': '\\365',
  182.     '\xf6': '\\366',
  183.     '\xf7': '\\367',
  184.     '\xf8': '\\370',
  185.     '\xf9': '\\371',
  186.     '\xfa': '\\372',
  187.     '\xfb': '\\373',
  188.     '\xfc': '\\374',
  189.     '\xfd': '\\375',
  190.     '\xfe': '\\376',
  191.     '\xff': '\\377' }
  192. _idmap = ''.join((lambda .0: pass)(xrange(256)))
  193.  
  194. def _quote(str, LegalChars = _LegalChars, idmap = _idmap, translate = string.translate):
  195.     if '' == translate(str, idmap, LegalChars):
  196.         return str
  197.     return None + _nulljoin(map(_Translator.get, str, str)) + '"'
  198.  
  199. _OctalPatt = re.compile('\\\\[0-3][0-7][0-7]')
  200. _QuotePatt = re.compile('[\\\\].')
  201.  
  202. def _unquote(str):
  203.     if len(str) < 2:
  204.         return str
  205.     if None[0] != '"' or str[-1] != '"':
  206.         return str
  207.     str = None[1:-1]
  208.     i = 0
  209.     n = len(str)
  210.     res = []
  211.     while i <= i:
  212.         pass
  213.     0
  214.     if i < n:
  215.         Omatch = _OctalPatt.search(str, i)
  216.         Qmatch = _QuotePatt.search(str, i)
  217.         if not Omatch and not Qmatch:
  218.             res.append(str[i:])
  219.             break
  220.         j = k = -1
  221.         if Omatch:
  222.             j = Omatch.start(0)
  223.         if Qmatch:
  224.             k = Qmatch.start(0)
  225.         if Qmatch:
  226.             if not Omatch or k < j:
  227.                 res.append(str[i:k])
  228.                 res.append(str[k + 1])
  229.                 i = k + 2
  230.                 continue
  231.         res.append(str[i:j])
  232.         res.append(chr(int(str[j + 1:j + 4], 8)))
  233.         i = j + 4
  234.     return _nulljoin(res)
  235.  
  236. _weekdayname = [
  237.     'Mon',
  238.     'Tue',
  239.     'Wed',
  240.     'Thu',
  241.     'Fri',
  242.     'Sat',
  243.     'Sun']
  244. _monthname = [
  245.     None,
  246.     'Jan',
  247.     'Feb',
  248.     'Mar',
  249.     'Apr',
  250.     'May',
  251.     'Jun',
  252.     'Jul',
  253.     'Aug',
  254.     'Sep',
  255.     'Oct',
  256.     'Nov',
  257.     'Dec']
  258.  
  259. def _getdate(future = 0, weekdayname = _weekdayname, monthname = _monthname):
  260.     gmtime = gmtime
  261.     time = time
  262.     import time
  263.     now = time()
  264.     (year, month, day, hh, mm, ss, wd, y, z) = gmtime(now + future)
  265.     return '%s, %02d-%3s-%4d %02d:%02d:%02d GMT' % (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
  266.  
  267.  
  268. class Morsel(dict):
  269.     _reserved = {
  270.         'expires': 'expires',
  271.         'path': 'Path',
  272.         'comment': 'Comment',
  273.         'domain': 'Domain',
  274.         'max-age': 'Max-Age',
  275.         'secure': 'secure',
  276.         'httponly': 'httponly',
  277.         'version': 'Version' }
  278.     
  279.     def __init__(self):
  280.         self.key = None
  281.         self.value = None
  282.         self.coded_value = None
  283.         for K in self._reserved:
  284.             dict.__setitem__(self, K, '')
  285.         
  286.  
  287.     
  288.     def __setitem__(self, K, V):
  289.         K = K.lower()
  290.         if K not in self._reserved:
  291.             raise CookieError('Invalid Attribute %s' % K)
  292.         dict.__setitem__(self, K, V)
  293.  
  294.     
  295.     def isReservedKey(self, K):
  296.         return K.lower() in self._reserved
  297.  
  298.     
  299.     def set(self, key, val, coded_val, LegalChars = _LegalChars, idmap = _idmap, translate = string.translate):
  300.         if key.lower() in self._reserved:
  301.             raise CookieError('Attempt to set a reserved key: %s' % key)
  302.         if '' != translate(key, idmap, LegalChars):
  303.             raise CookieError('Illegal key value: %s' % key)
  304.         self.key = key
  305.         self.value = val
  306.         self.coded_value = coded_val
  307.  
  308.     
  309.     def output(self, attrs = None, header = 'Set-Cookie:'):
  310.         return '%s %s' % (header, self.OutputString(attrs))
  311.  
  312.     __str__ = output
  313.     
  314.     def __repr__(self):
  315.         return '<%s: %s=%s>' % (self.__class__.__name__, self.key, repr(self.value))
  316.  
  317.     
  318.     def js_output(self, attrs = None):
  319.         return '\n        <script type="text/javascript">\n        <!-- begin hiding\n        document.cookie = "%s";\n        // end hiding -->\n        </script>\n        ' % (self.OutputString(attrs).replace('"', '\\"'),)
  320.  
  321.     
  322.     def OutputString(self, attrs = None):
  323.         result = []
  324.         RA = result.append
  325.         RA('%s=%s' % (self.key, self.coded_value))
  326.         if attrs is None:
  327.             attrs = self._reserved
  328.         items = self.items()
  329.         items.sort()
  330.         for K, V in items:
  331.             if V == '':
  332.                 continue
  333.             if K not in attrs:
  334.                 continue
  335.             if K == 'expires' and type(V) == type(1):
  336.                 RA('%s=%s' % (self._reserved[K], _getdate(V)))
  337.                 continue
  338.             if K == 'max-age' and type(V) == type(1):
  339.                 RA('%s=%d' % (self._reserved[K], V))
  340.                 continue
  341.             if K == 'secure':
  342.                 RA(str(self._reserved[K]))
  343.                 continue
  344.             if K == 'httponly':
  345.                 RA(str(self._reserved[K]))
  346.                 continue
  347.             RA('%s=%s' % (self._reserved[K], V))
  348.         
  349.         return _semispacejoin(result)
  350.  
  351.  
  352. _LegalCharsPatt = "[\\w\\d!#%&'~_`><@,:/\\$\\*\\+\\-\\.\\^\\|\\)\\(\\?\\}\\{\\=]"
  353. _CookiePattern = re.compile('(?x)(?P<key>' + _LegalCharsPatt + '+?)\\s*=\\s*(?P<val>"(?:[^\\\\"]|\\\\.)*"|\\w{3},\\s[\\w\\d-]{9,11}\\s[\\d:]{8}\\sGMT|' + _LegalCharsPatt + '*)\\s*;?')
  354.  
  355. class BaseCookie(dict):
  356.     
  357.     def value_decode(self, val):
  358.         return (val, val)
  359.  
  360.     
  361.     def value_encode(self, val):
  362.         strval = str(val)
  363.         return (strval, strval)
  364.  
  365.     
  366.     def __init__(self, input = None):
  367.         if input:
  368.             self.load(input)
  369.  
  370.     
  371.     def __set(self, key, real_value, coded_value):
  372.         M = self.get(key, Morsel())
  373.         M.set(key, real_value, coded_value)
  374.         dict.__setitem__(self, key, M)
  375.  
  376.     
  377.     def __setitem__(self, key, value):
  378.         (rval, cval) = self.value_encode(value)
  379.         self._BaseCookie__set(key, rval, cval)
  380.  
  381.     
  382.     def output(self, attrs = None, header = 'Set-Cookie:', sep = '\r\n'):
  383.         result = []
  384.         items = self.items()
  385.         items.sort()
  386.         for K, V in items:
  387.             result.append(V.output(attrs, header))
  388.         
  389.         return sep.join(result)
  390.  
  391.     __str__ = output
  392.     
  393.     def __repr__(self):
  394.         L = []
  395.         items = self.items()
  396.         items.sort()
  397.         for K, V in items:
  398.             L.append('%s=%s' % (K, repr(V.value)))
  399.         
  400.         return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L))
  401.  
  402.     
  403.     def js_output(self, attrs = None):
  404.         result = []
  405.         items = self.items()
  406.         items.sort()
  407.         for K, V in items:
  408.             result.append(V.js_output(attrs))
  409.         
  410.         return _nulljoin(result)
  411.  
  412.     
  413.     def load(self, rawdata):
  414.         if type(rawdata) == type(''):
  415.             self._BaseCookie__ParseString(rawdata)
  416.         else:
  417.             for k, v in rawdata.items():
  418.                 self[k] = v
  419.             
  420.  
  421.     
  422.     def __ParseString(self, str, patt = _CookiePattern):
  423.         i = 0
  424.         n = len(str)
  425.         M = None
  426.         while i <= i:
  427.             pass
  428.         0
  429.         if i < n:
  430.             match = patt.search(str, i)
  431.             if not match:
  432.                 break
  433.             K = match.group('key')
  434.             V = match.group('val')
  435.             i = match.end(0)
  436.             if K[0] == '$' or M:
  437.                 M[K[1:]] = V
  438.             
  439.             if K.lower() in Morsel._reserved or M:
  440.                 M[K] = _unquote(V)
  441.             
  442.             (rval, cval) = self.value_decode(V)
  443.             self._BaseCookie__set(K, rval, cval)
  444.             M = self[K]
  445.  
  446.  
  447.  
  448. class SimpleCookie(BaseCookie):
  449.     
  450.     def value_decode(self, val):
  451.         return (_unquote(val), val)
  452.  
  453.     
  454.     def value_encode(self, val):
  455.         strval = str(val)
  456.         return (strval, _quote(strval))
  457.  
  458.  
  459.  
  460. class SerialCookie(BaseCookie):
  461.     
  462.     def __init__(self, input = None):
  463.         warnings.warn('SerialCookie class is insecure; do not use it', DeprecationWarning)
  464.         BaseCookie.__init__(self, input)
  465.  
  466.     
  467.     def value_decode(self, val):
  468.         return (loads(_unquote(val)), val)
  469.  
  470.     
  471.     def value_encode(self, val):
  472.         return (val, _quote(dumps(val)))
  473.  
  474.  
  475.  
  476. class SmartCookie(BaseCookie):
  477.     
  478.     def __init__(self, input = None):
  479.         warnings.warn('Cookie/SmartCookie class is insecure; do not use it', DeprecationWarning)
  480.         BaseCookie.__init__(self, input)
  481.  
  482.     
  483.     def value_decode(self, val):
  484.         strval = _unquote(val)
  485.         
  486.         try:
  487.             return (loads(strval), val)
  488.         except:
  489.             return (strval, val)
  490.  
  491.  
  492.     
  493.     def value_encode(self, val):
  494.         if type(val) == type(''):
  495.             return (val, _quote(val))
  496.         return (None, _quote(dumps(val)))
  497.  
  498.  
  499. Cookie = SmartCookie
  500.  
  501. def _test():
  502.     import doctest
  503.     import Cookie
  504.     return doctest.testmod(Cookie)
  505.  
  506. if __name__ == '__main__':
  507.     _test()
  508.