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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. import os
  7. import sys
  8. import urlparse
  9. from BeautifulSoup import BeautifulSoup, Tag
  10. from PyQt4 import QtCore
  11. from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, Qt, QPrinter, QPrintPreviewDialog, QPrintDialog, QDialog, QMetaObject, Q_ARG
  12. from PyQt4.QtWebKit import QWebView
  13. PRINTCSS = 'body{width:100%;margin:0;padding:0;font-family:Arial;color:#000;background:none;font-size:12pt;text-align:left;}h1,h2,h3,h4,h5,h6{font-family:Helvetica;}h1{font-size:19pt;}h2{font-size:17pt;}h3{font-size:15pt;}h4,h5,h6{font-size:12pt;}pre,code,samp{font:10ptCourier,monospace;white-space:pre-wrap;page-break-inside:avoid;}blockquote{margin:1.3em;padding:1em;font-size:10pt;}hr{background-color:#ccc;}aimg{border:none;}a:link,a:visited{background:transparent;font-weight:700;text-decoration:underline;color:#333;}a:link:after,a{color:#000;}table{margin:1px;text-align:left;}th{border-bottom:1pxsolid#333;font-weight:bold;}td{border-bottom:1pxsolid#333;}th,td{padding:4px10px4px0;}tfoot{font-style:italic;}caption{background:#fff;margin-bottom:2em;text-align:left;}thead{display:table-header-group;}tr{page-break-inside:avoid;}#header,.header,#footer,.footer,#navbar,.navbar,#navigation,.navigation,#rightSideBar,.rightSideBar,#leftSideBar,.leftSideBar{display:none;}'
  14.  
  15. class Printing(QObject):
  16.     
  17.     def __init__(self, spine, preview):
  18.         is_ok_to_use_qt = is_ok_to_use_qt
  19.         import calibre.gui2
  20.         if not is_ok_to_use_qt():
  21.             raise Exception('Not OK to use Qt')
  22.         is_ok_to_use_qt()
  23.         QObject.__init__(self)
  24.         self.loop = QEventLoop()
  25.         self.view = QWebView()
  26.         if preview:
  27.             self.connect(self.view, SIGNAL('loadFinished(bool)'), self.print_preview)
  28.         else:
  29.             self.connect(self.view, SIGNAL('loadFinished(bool)'), self.print_book)
  30.         self.process_content(spine)
  31.  
  32.     
  33.     def process_content(self, spine):
  34.         content = ''
  35.         for path in spine:
  36.             raw = self.raw_content(path)
  37.             content += self.parsed_content(raw, path)
  38.         
  39.         refined_content = self.refine_content(content)
  40.         base = os.path.splitdrive(spine[0])[0]
  41.         base = None if base != '' else '/'
  42.         QMetaObject.invokeMethod(self, 'load_content', Qt.QueuedConnection, Q_ARG('QString', refined_content), Q_ARG('QString', base))
  43.         self.loop.exec_()
  44.  
  45.     
  46.     def load_content(self, content, base):
  47.         self.view.setHtml(content, QUrl(base))
  48.  
  49.     load_content = QtCore.pyqtSignature('load_content(QString, QString)')(load_content)
  50.     
  51.     def raw_content(self, path):
  52.         return open(path, 'rb').read().decode(path.encoding)
  53.  
  54.     
  55.     def parsed_content(self, raw_content, path):
  56.         dom_tree = BeautifulSoup(raw_content).body
  57.         styles = dom_tree.findAll('style')
  58.         for s in styles:
  59.             s.extract()
  60.         
  61.         scripts = dom_tree.findAll('script')
  62.         for s in scripts:
  63.             s.extract()
  64.         
  65.         links = dom_tree.findAll(src = True)
  66.         for s in links:
  67.             if QUrl(s['src']).isRelative():
  68.                 s['src'] = urlparse.urljoin(path, s['src'])
  69.                 continue
  70.         
  71.         links = dom_tree.findAll(href = True)
  72.         for s in links:
  73.             if QUrl(s['href']).isRelative():
  74.                 s['href'] = urlparse.urljoin(path, s['href'])
  75.                 continue
  76.         
  77.         return unicode(dom_tree)
  78.  
  79.     
  80.     def refine_content(self, content):
  81.         dom_tree = BeautifulSoup('<html><head></head><body>%s</body></html>' % content)
  82.         css = dom_tree.findAll('link')
  83.         for c in css:
  84.             c.extract()
  85.         
  86.         print_css = Tag(BeautifulSoup(), 'style', [
  87.             ('type', 'text/css'),
  88.             ('title', 'override_css')])
  89.         print_css.insert(0, PRINTCSS)
  90.         dom_tree.findAll('head')[0].insert(0, print_css)
  91.         return unicode(dom_tree)
  92.  
  93.     
  94.     def print_preview(self, ok):
  95.         printer = QPrinter(QPrinter.HighResolution)
  96.         printer.setPageMargins(1, 1, 1, 1, QPrinter.Inch)
  97.         previewDialog = QPrintPreviewDialog(printer)
  98.         self.connect(previewDialog, SIGNAL('paintRequested(QPrinter *)'), self.view.print_)
  99.         previewDialog.exec_()
  100.         self.disconnect(previewDialog, SIGNAL('paintRequested(QPrinter *)'), self.view.print_)
  101.         self.loop.quit()
  102.  
  103.     
  104.     def print_book(self, ok):
  105.         printer = QPrinter(QPrinter.HighResolution)
  106.         printer.setPageMargins(1, 1, 1, 1, QPrinter.Inch)
  107.         printDialog = QPrintDialog(printer)
  108.         printDialog.setWindowTitle(_('Print eBook'))
  109.         printDialog.exec_()
  110.         if printDialog.result() == QDialog.Accepted:
  111.             self.view.print_(printer)
  112.         
  113.         self.loop.quit()
  114.  
  115.  
  116.  
  117. def main():
  118.     return 0
  119.  
  120. if __name__ == '__main__':
  121.     sys.exit(main())
  122.  
  123.