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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import time
  7. import struct
  8. import logging
  9. if os.name == 'nt':
  10.     import _winreg
  11.  
  12. from _clientcookie import FileCookieJar, CookieJar, Cookie, MISSING_FILENAME_TEXT, LoadError
  13. debug = logging.getLogger('mechanize').debug
  14.  
  15. def regload(path, leaf):
  16.     key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, path, 0, _winreg.KEY_ALL_ACCESS)
  17.     
  18.     try:
  19.         value = _winreg.QueryValueEx(key, leaf)[0]
  20.     except WindowsError:
  21.         value = None
  22.  
  23.     return value
  24.  
  25. WIN32_EPOCH = 0x19DB1DED53E8000L
  26.  
  27. def epoch_time_offset_from_win32_filetime(filetime):
  28.     if filetime < WIN32_EPOCH:
  29.         raise ValueError('filetime (%d) is before epoch (%d)' % (filetime, WIN32_EPOCH))
  30.     filetime < WIN32_EPOCH
  31.     return divmod(filetime - WIN32_EPOCH, 0x989680L)[0]
  32.  
  33.  
  34. def binary_to_char(c):
  35.     return '%02X' % ord(c)
  36.  
  37.  
  38. def binary_to_str(d):
  39.     return ''.join(map(binary_to_char, list(d)))
  40.  
  41.  
  42. class MSIEBase:
  43.     magic_re = re.compile('Client UrlCache MMF Ver \\d\\.\\d.*')
  44.     padding = '\r\xf0\xad\x0b'
  45.     msie_domain_re = re.compile('^([^/]+)(/.*)$')
  46.     cookie_re = re.compile('Cookie\\:.+\\@([!-\xff]+).*?(.+\\@[!-\xff]+\\.txt)')
  47.     reg_path = 'software\\microsoft\\windows\\currentversion\\explorer\\shell folders'
  48.     reg_key = 'Cookies'
  49.     
  50.     def __init__(self):
  51.         self._delayload_domains = { }
  52.  
  53.     
  54.     def _delayload_domain(self, domain):
  55.         delayload_info = self._delayload_domains.get(domain)
  56.         if delayload_info is not None:
  57.             (cookie_file, ignore_discard, ignore_expires) = delayload_info
  58.             
  59.             try:
  60.                 self.load_cookie_data(cookie_file, ignore_discard, ignore_expires)
  61.             except (LoadError, IOError):
  62.                 debug('error reading cookie file, skipping: %s', cookie_file)
  63.  
  64.             del self._delayload_domains[domain]
  65.         
  66.  
  67.     
  68.     def _load_cookies_from_file(self, filename):
  69.         debug('Loading MSIE cookies file: %s', filename)
  70.         cookies = []
  71.         cookies_fh = open(filename)
  72.         
  73.         try:
  74.             while None:
  75.                 key = cookies_fh.readline()
  76.                 if key == '':
  77.                     break
  78.                 
  79.                 rl = cookies_fh.readline
  80.                 
  81.                 def getlong(rl = rl):
  82.                     return long(rl().rstrip())
  83.  
  84.                 
  85.                 def getstr(rl = rl):
  86.                     return rl().rstrip()
  87.  
  88.                 key = key.rstrip()
  89.                 value = getstr()
  90.                 domain_path = getstr()
  91.                 flags = getlong()
  92.                 lo_expire = getlong()
  93.                 hi_expire = getlong()
  94.                 lo_create = getlong()
  95.                 hi_create = getlong()
  96.                 sep = getstr()
  97.                 if '' in (key, value, domain_path, flags, hi_expire, lo_expire, hi_create, lo_create, sep) or sep != '*':
  98.                     break
  99.                 
  100.                 m = self.msie_domain_re.search(domain_path)
  101.                 if m:
  102.                     domain = m.group(1)
  103.                     path = m.group(2)
  104.                     cookies.append({
  105.                         'KEY': key,
  106.                         'VALUE': value,
  107.                         'DOMAIN': domain,
  108.                         'PATH': path,
  109.                         'FLAGS': flags,
  110.                         'HIXP': hi_expire,
  111.                         'LOXP': lo_expire,
  112.                         'HICREATE': hi_create,
  113.                         'LOCREATE': lo_create })
  114.                     continue
  115.             cookies_fh.close()
  116.             return cookies
  117.  
  118.  
  119.     
  120.     def load_cookie_data(self, filename, ignore_discard = False, ignore_expires = False):
  121.         now = int(time.time())
  122.         cookie_data = self._load_cookies_from_file(filename)
  123.         for cookie in cookie_data:
  124.             flags = cookie['FLAGS']
  125.             secure = flags & 8192 != 0
  126.             filetime = (cookie['HIXP'] << 32) + cookie['LOXP']
  127.             expires = epoch_time_offset_from_win32_filetime(filetime)
  128.             if expires < now:
  129.                 discard = True
  130.             else:
  131.                 discard = False
  132.             domain = cookie['DOMAIN']
  133.             initial_dot = domain.startswith('.')
  134.             if initial_dot:
  135.                 domain_specified = True
  136.             else:
  137.                 domain_specified = False
  138.             c = Cookie(0, cookie['KEY'], cookie['VALUE'], None, False, domain, domain_specified, initial_dot, cookie['PATH'], False, secure, expires, discard, None, None, {
  139.                 'flags': flags })
  140.             if not ignore_discard and c.discard:
  141.                 continue
  142.             
  143.             if not ignore_expires and c.is_expired(now):
  144.                 continue
  145.             
  146.             CookieJar.set_cookie(self, c)
  147.         
  148.  
  149.     
  150.     def load_from_registry(self, ignore_discard = False, ignore_expires = False, username = None):
  151.         cookies_dir = regload(self.reg_path, self.reg_key)
  152.         filename = os.path.normpath(os.path.join(cookies_dir, 'INDEX.DAT'))
  153.         self.load(filename, ignore_discard, ignore_expires, username)
  154.  
  155.     
  156.     def _really_load(self, index, filename, ignore_discard, ignore_expires, username):
  157.         now = int(time.time())
  158.         if username is None:
  159.             username = os.environ['USERNAME'].lower()
  160.         
  161.         cookie_dir = os.path.dirname(filename)
  162.         data = index.read(256)
  163.         if len(data) != 256:
  164.             raise LoadError('%s file is too short' % filename)
  165.         len(data) != 256
  166.         sig = data[:32]
  167.         size = data[32:36]
  168.         data = data[36:]
  169.         size = struct.unpack('<L', size)[0]
  170.         if not self.magic_re.match(sig) or size != 16384:
  171.             raise LoadError("%s ['%s' %s] does not seem to contain cookies" % (str(filename), sig, size))
  172.         size != 16384
  173.         index.seek(size, 0)
  174.         sector = 128
  175.         while None:
  176.             data = ''
  177.             to_read = 2 * sector
  178.             d = index.read(to_read)
  179.             if len(d) != to_read:
  180.                 break
  181.             
  182.             data = data + d
  183.             sig = data[:4]
  184.             size = data[4:8]
  185.             data = data[8:]
  186.             size = struct.unpack('<L', size)[0]
  187.             to_read = (size - 2) * sector
  188.             if sig != 'URL ':
  189.                 if sig == '\x00\x00\x00\x00':
  190.                     break
  191.                 
  192.                 if sig == self.padding:
  193.                     continue
  194.                 
  195.                 if size != 2:
  196.                     index.seek(to_read, 1)
  197.                     continue
  198.                 continue
  199.             
  200.             if size > 2:
  201.                 more_data = index.read(to_read)
  202.                 if len(more_data) != to_read:
  203.                     break
  204.                 
  205.                 data = data + more_data
  206.             
  207.             cookie_re = 'Cookie\\:%s\\@([!-\xff]+).*?' % username + '(%s\\@[!-\xff]+\\.txt)' % username
  208.             m = re.search(cookie_re, data, re.I)
  209.             if m:
  210.                 cookie_file = os.path.join(cookie_dir, m.group(2))
  211.                 if not self.delayload:
  212.                     
  213.                     try:
  214.                         self.load_cookie_data(cookie_file, ignore_discard, ignore_expires)
  215.                     except (LoadError, IOError):
  216.                         debug('error reading cookie file, skipping: %s', cookie_file)
  217.                     except:
  218.                         None<EXCEPTION MATCH>(LoadError, IOError)
  219.                     
  220.  
  221.                 None<EXCEPTION MATCH>(LoadError, IOError)
  222.                 domain = m.group(1)
  223.                 i = domain.find('/')
  224.                 if i != -1:
  225.                     domain = domain[:i]
  226.                 
  227.                 self._delayload_domains[domain] = (cookie_file, ignore_discard, ignore_expires)
  228.                 continue
  229.             continue
  230.             return None
  231.  
  232.  
  233.  
  234. class MSIECookieJar(MSIEBase, FileCookieJar):
  235.     
  236.     def __init__(self, filename = None, delayload = False, policy = None):
  237.         MSIEBase.__init__(self)
  238.         FileCookieJar.__init__(self, filename, delayload, policy)
  239.  
  240.     
  241.     def set_cookie(self, cookie):
  242.         if self.delayload:
  243.             self._delayload_domain(cookie.domain)
  244.         
  245.         CookieJar.set_cookie(self, cookie)
  246.  
  247.     
  248.     def _cookies_for_request(self, request):
  249.         domains = self._cookies.copy()
  250.         domains.update(self._delayload_domains)
  251.         domains = domains.keys()
  252.         cookies = []
  253.         for domain in domains:
  254.             cookies.extend(self._cookies_for_domain(domain, request))
  255.         
  256.         return cookies
  257.  
  258.     
  259.     def _cookies_for_domain(self, domain, request):
  260.         if not self._policy.domain_return_ok(domain, request):
  261.             return []
  262.         debug('Checking %s for cookies to return', domain)
  263.         if self.delayload:
  264.             self._delayload_domain(domain)
  265.         
  266.         return CookieJar._cookies_for_domain(self, domain, request)
  267.  
  268.     
  269.     def read_all_cookies(self):
  270.         if self.delayload:
  271.             for domain in self._delayload_domains.keys():
  272.                 self._delayload_domain(domain)
  273.             
  274.         
  275.  
  276.     
  277.     def load(self, filename, ignore_discard = False, ignore_expires = False, username = None):
  278.         if filename is None:
  279.             if self.filename is not None:
  280.                 filename = self.filename
  281.             else:
  282.                 raise ValueError(MISSING_FILENAME_TEXT)
  283.         self.filename is not None
  284.         index = open(filename, 'rb')
  285.         
  286.         try:
  287.             self._really_load(index, filename, ignore_discard, ignore_expires, username)
  288.         finally:
  289.             index.close()
  290.  
  291.  
  292.  
  293.