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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os as _os
  5. import __builtin__
  6. import UserDict
  7. _open = __builtin__.open
  8. _BLOCKSIZE = 512
  9. error = IOError
  10.  
  11. class _Database(UserDict.DictMixin):
  12.     _os = _os
  13.     _open = _open
  14.     
  15.     def __init__(self, filebasename, mode):
  16.         self._mode = mode
  17.         self._dirfile = filebasename + _os.extsep + 'dir'
  18.         self._datfile = filebasename + _os.extsep + 'dat'
  19.         self._bakfile = filebasename + _os.extsep + 'bak'
  20.         self._index = None
  21.         
  22.         try:
  23.             f = _open(self._datfile, 'r')
  24.         except IOError:
  25.             f = _open(self._datfile, 'w')
  26.             self._chmod(self._datfile)
  27.  
  28.         f.close()
  29.         self._update()
  30.  
  31.     
  32.     def _update(self):
  33.         self._index = { }
  34.         
  35.         try:
  36.             f = _open(self._dirfile)
  37.         except IOError:
  38.             pass
  39.  
  40.         for line in f:
  41.             line = line.rstrip()
  42.             (key, pos_and_siz_pair) = eval(line)
  43.             self._index[key] = pos_and_siz_pair
  44.         
  45.         f.close()
  46.  
  47.     
  48.     def _commit(self):
  49.         if self._index is None:
  50.             return None
  51.         
  52.         try:
  53.             self._os.unlink(self._bakfile)
  54.         except self._os.error:
  55.             self._index is None
  56.             self._index is None
  57.         except:
  58.             self._index is None
  59.  
  60.         
  61.         try:
  62.             self._os.rename(self._dirfile, self._bakfile)
  63.         except self._os.error:
  64.             self._index is None
  65.             self._index is None
  66.         except:
  67.             self._index is None
  68.  
  69.         f = self._open(self._dirfile, 'w')
  70.         self._chmod(self._dirfile)
  71.         for key, pos_and_siz_pair in self._index.iteritems():
  72.             f.write('%r, %r\n' % (key, pos_and_siz_pair))
  73.         
  74.         f.close()
  75.  
  76.     sync = _commit
  77.     
  78.     def __getitem__(self, key):
  79.         (pos, siz) = self._index[key]
  80.         f = _open(self._datfile, 'rb')
  81.         f.seek(pos)
  82.         dat = f.read(siz)
  83.         f.close()
  84.         return dat
  85.  
  86.     
  87.     def _addval(self, val):
  88.         f = _open(self._datfile, 'rb+')
  89.         f.seek(0, 2)
  90.         pos = int(f.tell())
  91.         npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
  92.         f.write('\x00' * (npos - pos))
  93.         pos = npos
  94.         f.write(val)
  95.         f.close()
  96.         return (pos, len(val))
  97.  
  98.     
  99.     def _setval(self, pos, val):
  100.         f = _open(self._datfile, 'rb+')
  101.         f.seek(pos)
  102.         f.write(val)
  103.         f.close()
  104.         return (pos, len(val))
  105.  
  106.     
  107.     def _addkey(self, key, pos_and_siz_pair):
  108.         self._index[key] = pos_and_siz_pair
  109.         f = _open(self._dirfile, 'a')
  110.         self._chmod(self._dirfile)
  111.         f.write('%r, %r\n' % (key, pos_and_siz_pair))
  112.         f.close()
  113.  
  114.     
  115.     def __setitem__(self, key, val):
  116.         if type('') == type(''):
  117.             pass
  118.         elif not type('') == type(val):
  119.             raise TypeError, 'keys and values must be strings'
  120.         
  121.         if key not in self._index:
  122.             self._addkey(key, self._addval(val))
  123.         else:
  124.             (pos, siz) = self._index[key]
  125.             oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE
  126.             newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE
  127.             if newblocks <= oldblocks:
  128.                 self._index[key] = self._setval(pos, val)
  129.             else:
  130.                 self._index[key] = self._addval(val)
  131.  
  132.     
  133.     def __delitem__(self, key):
  134.         del self._index[key]
  135.         self._commit()
  136.  
  137.     
  138.     def keys(self):
  139.         return self._index.keys()
  140.  
  141.     
  142.     def has_key(self, key):
  143.         return key in self._index
  144.  
  145.     
  146.     def __contains__(self, key):
  147.         return key in self._index
  148.  
  149.     
  150.     def iterkeys(self):
  151.         return self._index.iterkeys()
  152.  
  153.     __iter__ = iterkeys
  154.     
  155.     def __len__(self):
  156.         return len(self._index)
  157.  
  158.     
  159.     def close(self):
  160.         self._commit()
  161.         self._index = None
  162.         self._datfile = None
  163.         self._dirfile = None
  164.         self._bakfile = None
  165.  
  166.     __del__ = close
  167.     
  168.     def _chmod(self, file):
  169.         if hasattr(self._os, 'chmod'):
  170.             self._os.chmod(file, self._mode)
  171.         
  172.  
  173.  
  174.  
  175. def open(file, flag = None, mode = 438):
  176.     
  177.     try:
  178.         um = _os.umask(0)
  179.         _os.umask(um)
  180.     except AttributeError:
  181.         pass
  182.  
  183.     mode = mode & ~um
  184.     return _Database(file, mode)
  185.  
  186.