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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import datetime
  5. import threading
  6. import time
  7. import cherrypy
  8. from cherrypy.lib import cptools, http
  9.  
  10. class MemoryCache:
  11.     maxobjects = 1000
  12.     maxobj_size = 100000
  13.     maxsize = 10000000
  14.     delay = 600
  15.     
  16.     def __init__(self):
  17.         self.clear()
  18.         t = threading.Thread(target = self.expire_cache, name = 'expire_cache')
  19.         self.expiration_thread = t
  20.         if hasattr(threading.Thread, 'daemon'):
  21.             t.daemon = True
  22.         else:
  23.             t.setDaemon(True)
  24.         t.start()
  25.  
  26.     
  27.     def clear(self):
  28.         self.cache = { }
  29.         self.expirations = { }
  30.         self.tot_puts = 0
  31.         self.tot_gets = 0
  32.         self.tot_hist = 0
  33.         self.tot_expires = 0
  34.         self.tot_non_modified = 0
  35.         self.cursize = 0
  36.  
  37.     
  38.     def key(self):
  39.         return cherrypy.url(qs = cherrypy.request.query_string)
  40.  
  41.     
  42.     def expire_cache(self):
  43.         while time:
  44.             now = time.time()
  45.             for expiration_time, objects in self.expirations.items():
  46.                 if expiration_time <= now:
  47.                     for obj_size, obj_key in objects:
  48.                         
  49.                         try:
  50.                             del self.cache[obj_key]
  51.                             self.tot_expires += 1
  52.                             self.cursize -= obj_size
  53.                         continue
  54.                         except KeyError:
  55.                             continue
  56.                         
  57.  
  58.                     
  59.                     del self.expirations[expiration_time]
  60.                     continue
  61.                 None<EXCEPTION MATCH>KeyError
  62.             
  63.             time.sleep(0.1)
  64.  
  65.     
  66.     def get(self):
  67.         self.tot_gets += 1
  68.         cache_item = self.cache.get(self.key(), None)
  69.         if cache_item:
  70.             self.tot_hist += 1
  71.             return cache_item
  72.         return None
  73.  
  74.     
  75.     def put(self, obj):
  76.         if len(self.cache) < self.maxobjects:
  77.             obj_size = len(obj[2])
  78.             total_size = self.cursize + obj_size
  79.             if obj_size < self.maxobj_size and total_size < self.maxsize:
  80.                 expiration_time = cherrypy.response.time + self.delay
  81.                 obj_key = self.key()
  82.                 bucket = self.expirations.setdefault(expiration_time, [])
  83.                 bucket.append((obj_size, obj_key))
  84.                 self.cache[obj_key] = obj
  85.                 self.tot_puts += 1
  86.                 self.cursize = total_size
  87.             
  88.         
  89.  
  90.     
  91.     def delete(self):
  92.         self.cache.pop(self.key(), None)
  93.  
  94.  
  95.  
  96. def get(invalid_methods = ('POST', 'PUT', 'DELETE'), **kwargs):
  97.     request = cherrypy.request
  98.     if request.method in invalid_methods:
  99.         cherrypy._cache.delete()
  100.         request.cached = False
  101.         request.cacheable = False
  102.         return False
  103.     cache_data = cherrypy._cache.get()
  104.     request.cacheable = not c
  105.     if c:
  106.         response = cherrypy.response
  107.         (s, h, b, create_time, original_req_headers) = cache_data
  108.         for header_element in h.elements('Vary'):
  109.             key = header_element.value
  110.             if original_req_headers[key] != request.headers.get(key, 'missing'):
  111.                 request.cached = False
  112.                 request.cacheable = True
  113.                 return False
  114.         
  115.         for k in h:
  116.             dict.__setitem__(rh, k, dict.__getitem__(h, k))
  117.         
  118.         response.headers['Age'] = str(int(response.time - create_time))
  119.         
  120.         try:
  121.             cptools.validate_since()
  122.         except cherrypy.HTTPRedirect:
  123.             original_req_headers[key] != request.headers.get(key, 'missing')
  124.             x = original_req_headers[key] != request.headers.get(key, 'missing')
  125.             request.cached = c = bool(cache_data)
  126.             raise 
  127.         except:
  128.             None if x.status == 304 else request.method in invalid_methods
  129.  
  130.         response.status = s
  131.         response.body = b
  132.     
  133.     return c
  134.  
  135.  
  136. def tee_output():
  137.     
  138.     def tee(body):
  139.         output = []
  140.         for chunk in body:
  141.             output.append(chunk)
  142.             yield chunk
  143.         
  144.  
  145.     response = cherrypy.response
  146.     response.body = tee(response.body)
  147.  
  148.  
  149. def expires(secs = 0, force = False):
  150.     response = cherrypy.response
  151.     headers = response.headers
  152.     cacheable = False
  153.     if not force:
  154.         for indicator in ('Etag', 'Last-Modified', 'Age', 'Expires'):
  155.             if indicator in headers:
  156.                 cacheable = True
  157.                 break
  158.                 continue
  159.         
  160.     
  161.     if not cacheable:
  162.         if isinstance(secs, datetime.timedelta):
  163.             secs = 86400 * secs.days + secs.seconds
  164.         
  165.         if secs == 0:
  166.             if force or 'Pragma' not in headers:
  167.                 headers['Pragma'] = 'no-cache'
  168.             
  169.             if cherrypy.request.protocol >= (1, 1):
  170.                 if force or 'Cache-Control' not in headers:
  171.                     headers['Cache-Control'] = 'no-cache, must-revalidate'
  172.                 
  173.             
  174.             expiry = http.HTTPDate(1.16994e+09)
  175.         else:
  176.             expiry = http.HTTPDate(response.time + secs)
  177.         if force or 'Expires' not in headers:
  178.             headers['Expires'] = expiry
  179.         
  180.     
  181.  
  182.