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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import re
  8. from PyQt4.QtCore import SIGNAL, Qt
  9. from PyQt4.QtGui import QDialog, QWidget, QDialogButtonBox, QBrush, QTextCursor, QTextEdit
  10. from calibre.gui2.convert.regex_builder_ui import Ui_RegexBuilder
  11. from calibre.gui2.convert.xexp_edit_ui import Ui_Form as Ui_Edit
  12. from calibre.gui2 import error_dialog, choose_files
  13. from calibre.ebooks.oeb.iterator import EbookIterator
  14. from calibre.ebooks.conversion.preprocess import convert_entities
  15. from calibre.gui2.dialogs.choose_format import ChooseFormatDialog
  16.  
  17. class RegexBuilder(QDialog, Ui_RegexBuilder):
  18.     
  19.     def __init__(self, db, book_id, regex, *args):
  20.         QDialog.__init__(self, *args)
  21.         self.setupUi(self)
  22.         self.regex.setText(regex)
  23.         self.regex_valid()
  24.         if not db or not book_id:
  25.             self.button_box.addButton(QDialogButtonBox.Open)
  26.         elif not self.select_format(db, book_id):
  27.             self.cancelled = True
  28.             return None
  29.         self.cancelled = False
  30.         self.connect(self.button_box, SIGNAL('clicked(QAbstractButton*)'), self.button_clicked)
  31.         self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
  32.         self.connect(self.test, SIGNAL('clicked()'), self.do_test)
  33.  
  34.     
  35.     def regex_valid(self):
  36.         regex = unicode(self.regex.text())
  37.         if regex:
  38.             
  39.             try:
  40.                 re.compile(regex)
  41.                 self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }')
  42.             self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }')
  43.             return False
  44.  
  45.         else:
  46.             self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }')
  47.         return True
  48.  
  49.     
  50.     def do_test(self):
  51.         selections = []
  52.         if self.regex_valid():
  53.             text = unicode(self.preview.toPlainText())
  54.             regex = unicode(self.regex.text())
  55.             cursor = QTextCursor(self.preview.document())
  56.             extsel = QTextEdit.ExtraSelection()
  57.             extsel.cursor = cursor
  58.             extsel.format.setBackground(QBrush(Qt.yellow))
  59.             
  60.             try:
  61.                 for match in re.finditer(regex, text):
  62.                     es = QTextEdit.ExtraSelection(extsel)
  63.                     es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor)
  64.                     es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor)
  65.                     selections.append(es)
  66.  
  67.         
  68.         self.preview.setExtraSelections(selections)
  69.  
  70.     
  71.     def select_format(self, db, book_id):
  72.         format = None
  73.         formats = db.formats(book_id, index_is_id = True).upper().split(',')
  74.         if len(formats) == 1:
  75.             format = formats[0]
  76.         elif len(formats) > 1:
  77.             d = ChooseFormatDialog(self, _('Choose the format to view'), formats)
  78.             d.exec_()
  79.             if d.result() == QDialog.Accepted:
  80.                 format = d.format()
  81.             
  82.         
  83.         if not format:
  84.             error_dialog(self, _('No formats available'), _('Cannot build regex using the GUI builder without a book.'), show = True)
  85.             return False
  86.         self.open_book(db.format_abspath(book_id, format, index_is_id = True))
  87.         return True
  88.  
  89.     
  90.     def open_book(self, pathtoebook):
  91.         self.iterator = EbookIterator(pathtoebook)
  92.         self.iterator.__enter__(only_input_plugin = True)
  93.         text = [
  94.             u'']
  95.         ent_pat = re.compile('&(\\S+?);')
  96.         for path in self.iterator.spine:
  97.             html = open(path, 'rb').read().decode('utf-8', 'replace')
  98.             html = ent_pat.sub(convert_entities, html)
  99.             text.append(html)
  100.         
  101.         self.preview.setPlainText('\n---\n'.join(text))
  102.  
  103.     
  104.     def button_clicked(self, button):
  105.         if button == self.button_box.button(QDialogButtonBox.Open):
  106.             files = choose_files(self, 'regexp tester dialog', _('Open book'), select_only_single_file = True)
  107.             if files:
  108.                 self.open_book(files[0])
  109.             
  110.         
  111.         if button == self.button_box.button(QDialogButtonBox.Ok):
  112.             self.accept()
  113.         
  114.  
  115.  
  116.  
  117. class RegexEdit(QWidget, Ui_Edit):
  118.     
  119.     def __init__(self, parent = None):
  120.         QWidget.__init__(self, parent)
  121.         self.setupUi(self)
  122.         self.book_id = None
  123.         self.db = None
  124.         self.connect(self.button, SIGNAL('clicked()'), self.builder)
  125.  
  126.     
  127.     def builder(self):
  128.         bld = RegexBuilder(self.db, self.book_id, self.edit.text(), self)
  129.         if bld.cancelled:
  130.             return None
  131.         if bld.exec_() == bld.Accepted:
  132.             self.edit.setText(bld.regex.text())
  133.         
  134.  
  135.     
  136.     def set_msg(self, msg):
  137.         self.msg.setText(msg)
  138.  
  139.     
  140.     def set_book_id(self, book_id):
  141.         self.book_id = book_id
  142.  
  143.     
  144.     def set_db(self, db):
  145.         self.db = db
  146.  
  147.     
  148.     def break_cycles(self):
  149.         self.db = None
  150.  
  151.     
  152.     def text(self):
  153.         return unicode(self.edit.text())
  154.  
  155.     text = property(text)
  156.     
  157.     def regex(self):
  158.         return self.text
  159.  
  160.     regex = property(regex)
  161.     
  162.     def check(self):
  163.         return True
  164.  
  165.  
  166.