home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1303 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.2 KB  |  76 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. import shutil
  10. from contextlib import closing
  11. from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED
  12. from PyQt4.Qt import QDialog
  13. from calibre.gui2 import open_local_file
  14. from calibre.gui2.dialogs.tweak_epub_ui import Ui_Dialog
  15. from calibre.libunzip import extract as zipextract
  16. from calibre.ptempfile import PersistentTemporaryDirectory
  17.  
  18. class TweakEpub(QDialog, Ui_Dialog):
  19.     
  20.     def __init__(self, parent, epub):
  21.         QDialog.__init__(self, parent)
  22.         self._epub = epub
  23.         self._exploded = None
  24.         self._output = None
  25.         self.setupUi(self)
  26.         self.cancel_button.clicked.connect(self.reject)
  27.         self.explode_button.clicked.connect(self.explode)
  28.         self.rebuild_button.clicked.connect(self.rebuild)
  29.         parent_loc = parent.pos()
  30.         self.move(parent_loc.x(), parent_loc.y())
  31.  
  32.     
  33.     def cleanup(self):
  34.         if self._exploded is not None:
  35.             shutil.rmtree(self._exploded, ignore_errors = True)
  36.         
  37.  
  38.     
  39.     def display_exploded(self):
  40.         open_local_file(self._exploded)
  41.  
  42.     
  43.     def explode(self, *args):
  44.         if self._exploded is None:
  45.             self._exploded = PersistentTemporaryDirectory('_exploded', prefix = '')
  46.             zipextract(self._epub, self._exploded)
  47.             self.display_exploded()
  48.             self.rebuild_button.setEnabled(True)
  49.             self.explode_button.setEnabled(False)
  50.         
  51.  
  52.     
  53.     def rebuild(self, *args):
  54.         self._output = os.path.join(self._exploded, 'rebuilt.epub')
  55.         
  56.         try:
  57.             zf = _[1]
  58.             zf.write(os.path.join(self._exploded, 'mimetype'), 'mimetype', compress_type = ZIP_STORED)
  59.             exclude_files = [
  60.                 '.DS_Store',
  61.                 'mimetype',
  62.                 'iTunesMetadata.plist',
  63.                 'rebuilt.epub']
  64.             for root, dirs, files in os.walk(self._exploded):
  65.                 for fn in files:
  66.                     absfn = os.path.join(root, fn)
  67.                     zfn = os.path.relpath(absfn, self._exploded).replace(os.sep, '/')
  68.                     zf.write(absfn, zfn)
  69.                 
  70.         finally:
  71.             pass
  72.  
  73.         return QDialog.accept(self)
  74.  
  75.  
  76.