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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. try:
  6.     from cPickle import Pickler, Unpickler
  7. except ImportError:
  8.     from pickle import Pickler, Unpickler
  9.  
  10.  
  11. try:
  12.     from cStringIO import StringIO
  13. except ImportError:
  14.     from StringIO import StringIO
  15.  
  16. import UserDict
  17. __all__ = [
  18.     'Shelf',
  19.     'BsdDbShelf',
  20.     'DbfilenameShelf',
  21.     'open']
  22.  
  23. class _ClosedDict(UserDict.DictMixin):
  24.     
  25.     def closed(self, *args):
  26.         raise ValueError('invalid operation on closed shelf')
  27.  
  28.     __getitem__ = __setitem__ = __delitem__ = keys = closed
  29.     
  30.     def __repr__(self):
  31.         return '<Closed Dictionary>'
  32.  
  33.  
  34.  
  35. class Shelf(UserDict.DictMixin):
  36.     
  37.     def __init__(self, dict, protocol = None, writeback = False):
  38.         self.dict = dict
  39.         if protocol is None:
  40.             protocol = 0
  41.         
  42.         self._protocol = protocol
  43.         self.writeback = writeback
  44.         self.cache = { }
  45.  
  46.     
  47.     def keys(self):
  48.         return self.dict.keys()
  49.  
  50.     
  51.     def __len__(self):
  52.         return len(self.dict)
  53.  
  54.     
  55.     def has_key(self, key):
  56.         return key in self.dict
  57.  
  58.     
  59.     def __contains__(self, key):
  60.         return key in self.dict
  61.  
  62.     
  63.     def get(self, key, default = None):
  64.         if key in self.dict:
  65.             return self[key]
  66.         return default
  67.  
  68.     
  69.     def __getitem__(self, key):
  70.         
  71.         try:
  72.             value = self.cache[key]
  73.         except KeyError:
  74.             f = StringIO(self.dict[key])
  75.             value = Unpickler(f).load()
  76.             if self.writeback:
  77.                 self.cache[key] = value
  78.             
  79.         except:
  80.             self.writeback
  81.  
  82.         return value
  83.  
  84.     
  85.     def __setitem__(self, key, value):
  86.         if self.writeback:
  87.             self.cache[key] = value
  88.         
  89.         f = StringIO()
  90.         p = Pickler(f, self._protocol)
  91.         p.dump(value)
  92.         self.dict[key] = f.getvalue()
  93.  
  94.     
  95.     def __delitem__(self, key):
  96.         del self.dict[key]
  97.         
  98.         try:
  99.             del self.cache[key]
  100.         except KeyError:
  101.             pass
  102.  
  103.  
  104.     
  105.     def close(self):
  106.         self.sync()
  107.         
  108.         try:
  109.             self.dict.close()
  110.         except AttributeError:
  111.             pass
  112.  
  113.         self.dict = _ClosedDict()
  114.  
  115.     
  116.     def __del__(self):
  117.         if not hasattr(self, 'writeback'):
  118.             return None
  119.         self.close()
  120.  
  121.     
  122.     def sync(self):
  123.         if self.writeback and self.cache:
  124.             self.writeback = False
  125.             for key, entry in self.cache.iteritems():
  126.                 self[key] = entry
  127.             
  128.             self.writeback = True
  129.             self.cache = { }
  130.         
  131.         if hasattr(self.dict, 'sync'):
  132.             self.dict.sync()
  133.         
  134.  
  135.  
  136.  
  137. class BsdDbShelf(Shelf):
  138.     
  139.     def __init__(self, dict, protocol = None, writeback = False):
  140.         Shelf.__init__(self, dict, protocol, writeback)
  141.  
  142.     
  143.     def set_location(self, key):
  144.         (key, value) = self.dict.set_location(key)
  145.         f = StringIO(value)
  146.         return (key, Unpickler(f).load())
  147.  
  148.     
  149.     def next(self):
  150.         (key, value) = self.dict.next()
  151.         f = StringIO(value)
  152.         return (key, Unpickler(f).load())
  153.  
  154.     
  155.     def previous(self):
  156.         (key, value) = self.dict.previous()
  157.         f = StringIO(value)
  158.         return (key, Unpickler(f).load())
  159.  
  160.     
  161.     def first(self):
  162.         (key, value) = self.dict.first()
  163.         f = StringIO(value)
  164.         return (key, Unpickler(f).load())
  165.  
  166.     
  167.     def last(self):
  168.         (key, value) = self.dict.last()
  169.         f = StringIO(value)
  170.         return (key, Unpickler(f).load())
  171.  
  172.  
  173.  
  174. class DbfilenameShelf(Shelf):
  175.     
  176.     def __init__(self, filename, flag = 'c', protocol = None, writeback = False):
  177.         import anydbm
  178.         Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  179.  
  180.  
  181.  
  182. def open(filename, flag = 'c', protocol = None, writeback = False):
  183.     return DbfilenameShelf(filename, flag, protocol, writeback)
  184.  
  185.