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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         self._protocol = protocol
  42.         self.writeback = writeback
  43.         self.cache = { }
  44.  
  45.     
  46.     def keys(self):
  47.         return self.dict.keys()
  48.  
  49.     
  50.     def __len__(self):
  51.         return len(self.dict)
  52.  
  53.     
  54.     def has_key(self, key):
  55.         return key in self.dict
  56.  
  57.     
  58.     def __contains__(self, key):
  59.         return key in self.dict
  60.  
  61.     
  62.     def get(self, key, default = None):
  63.         if key in self.dict:
  64.             return self[key]
  65.  
  66.     
  67.     def __getitem__(self, key):
  68.         
  69.         try:
  70.             value = self.cache[key]
  71.         except KeyError:
  72.             f = StringIO(self.dict[key])
  73.             value = Unpickler(f).load()
  74.             if self.writeback:
  75.                 self.cache[key] = value
  76.             
  77.  
  78.         return value
  79.  
  80.     
  81.     def __setitem__(self, key, value):
  82.         if self.writeback:
  83.             self.cache[key] = value
  84.         f = StringIO()
  85.         p = Pickler(f, self._protocol)
  86.         p.dump(value)
  87.         self.dict[key] = f.getvalue()
  88.  
  89.     
  90.     def __delitem__(self, key):
  91.         del self.dict[key]
  92.         
  93.         try:
  94.             del self.cache[key]
  95.         except KeyError:
  96.             pass
  97.  
  98.  
  99.     
  100.     def close(self):
  101.         self.sync()
  102.         
  103.         try:
  104.             self.dict.close()
  105.         except AttributeError:
  106.             pass
  107.  
  108.         
  109.         try:
  110.             self.dict = _ClosedDict()
  111.         except (NameError, TypeError):
  112.             self.dict = None
  113.  
  114.  
  115.     
  116.     def __del__(self):
  117.         if not hasattr(self, 'writeback'):
  118.             return None
  119.         None.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.         if hasattr(self.dict, 'sync'):
  131.             self.dict.sync()
  132.  
  133.  
  134.  
  135. class BsdDbShelf(Shelf):
  136.     
  137.     def __init__(self, dict, protocol = None, writeback = False):
  138.         Shelf.__init__(self, dict, protocol, writeback)
  139.  
  140.     
  141.     def set_location(self, key):
  142.         (key, value) = self.dict.set_location(key)
  143.         f = StringIO(value)
  144.         return (key, Unpickler(f).load())
  145.  
  146.     
  147.     def next(self):
  148.         (key, value) = self.dict.next()
  149.         f = StringIO(value)
  150.         return (key, Unpickler(f).load())
  151.  
  152.     
  153.     def previous(self):
  154.         (key, value) = self.dict.previous()
  155.         f = StringIO(value)
  156.         return (key, Unpickler(f).load())
  157.  
  158.     
  159.     def first(self):
  160.         (key, value) = self.dict.first()
  161.         f = StringIO(value)
  162.         return (key, Unpickler(f).load())
  163.  
  164.     
  165.     def last(self):
  166.         (key, value) = self.dict.last()
  167.         f = StringIO(value)
  168.         return (key, Unpickler(f).load())
  169.  
  170.  
  171.  
  172. class DbfilenameShelf(Shelf):
  173.     
  174.     def __init__(self, filename, flag = 'c', protocol = None, writeback = False):
  175.         import anydbm
  176.         Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  177.  
  178.  
  179.  
  180. def open(filename, flag = 'c', protocol = None, writeback = False):
  181.     return DbfilenameShelf(filename, flag, protocol, writeback)
  182.  
  183.