Skip to content

Commit

Permalink
DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 11, 2021
1 parent 7ac7932 commit add6a6e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 131 deletions.
39 changes: 27 additions & 12 deletions src/calibre/gui2/css_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,25 @@ class Tester(Dialog): # {{{

DIALOG_TITLE = _('Test style transform rules')
PREFS_NAME = 'test-style-transform-rules'
LABEL = _('Enter a CSS stylesheet below to test')
LABEL = _('Enter a CSS stylesheet below and click the "Test" button')
SYNTAX = 'css'
RESULTS = '/* %s */\n\n' % _('Resulting stylesheet')

def __init__(self, rules, parent=None):
self.rules = compile_rules(rules)
self.rules = self.compile_rules(rules)
Dialog.__init__(self, self.DIALOG_TITLE, self.PREFS_NAME, parent=parent)

def compile_rules(self, rules):
return compile_rules(rules)

def setup_ui(self):
from calibre.gui2.tweak_book.editor.text import TextEdit
self.l = l = QVBoxLayout(self)
self.bb.setStandardButtons(QDialogButtonBox.StandardButton.Close)
self.la = la = QLabel(self.LABEL)
l.addWidget(la)
self.css = t = TextEdit(self)
t.load_text('/* %s */\n' % _('Enter CSS rules below and click the "Test" button'), 'css')
t.load_text('', self.SYNTAX)
la.setBuddy(t)
c = t.textCursor()
c.movePosition(QTextCursor.MoveOperation.End)
Expand All @@ -234,7 +239,10 @@ def do_test(self):
css = decl.cssText
if isinstance(css, bytes):
css = css.decode('utf-8')
self.result.load_text('/* %s */\n\n%s' % (_('Resulting stylesheet'), css), 'css')
self.set_result(css)

def set_result(self, css):
self.result.load_text(self.RESULTS + css, self.SYNTAX)

def sizeHint(self):
return QSize(800, 600)
Expand All @@ -259,13 +267,20 @@ def __init__(self, *args, **kw):
class RulesWidget(QWidget, SaveLoadMixin): # {{{

changed = pyqtSignal()
PREFS_NAME = 'style-transform-rules'
INITIAL_FILE_NAME = 'css-rules.txt'
DIR_SAVE_NAME = 'export-style-transform-rules'
export_func = export_rules
import_func = import_rules
TesterClass = Tester
RulesClass = Rules

def __init__(self, parent=None):
self.loaded_ruleset = None
QWidget.__init__(self, parent)
self.PREFS_OBJECT = JSONConfig('style-transform-rules')
self.PREFS_OBJECT = JSONConfig(self.PREFS_NAME)
l = QVBoxLayout(self)
self.rules_widget = w = Rules(self)
self.rules_widget = w = self.RulesClass(self)
w.changed.connect(self.changed.emit)
l.addWidget(w)
self.h = h = QHBoxLayout()
Expand All @@ -286,7 +301,7 @@ def __init__(self, parent=None):
b.setToolTip(_('Save this ruleset for later re-use'))
b.clicked.connect(self.save_ruleset)
h.addWidget(b)
self.export_button = b = QPushButton(_('&Load'), self)
self.load_button = b = QPushButton(_('&Load'), self)
self.load_menu = QMenu(self)
b.setMenu(self.load_menu)
b.setToolTip(_('Load a previously saved ruleset'))
Expand All @@ -299,17 +314,17 @@ def export_rules(self):
if not rules:
return error_dialog(self, _('No rules'), _(
'There are no rules to export'), show=True)
path = choose_save_file(self, 'export-style-transform-rules', _('Choose file for exported rules'), initial_filename='rules.txt')
path = choose_save_file(self, self.DIR_SAVE_NAME, _('Choose file for exported rules'), initial_filename=self.INITIAL_FILE_NAME)
if path:
raw = export_rules(rules)
raw = self.export_func(rules)
with open(path, 'wb') as f:
f.write(raw)

def import_rules(self):
paths = choose_files(self, 'export-style-transform-rules', _('Choose file to import rules from'), select_only_single_file=True)
paths = choose_files(self, self.DIR_SAVE_NAME, _('Choose file to import rules from'), select_only_single_file=True)
if paths:
with open(paths[0], 'rb') as f:
rules = import_rules(f.read())
rules = self.import_func(f.read())
self.rules_widget.rules = list(rules) + list(self.rules_widget.rules)
self.changed.emit()

Expand All @@ -318,7 +333,7 @@ def load_ruleset(self, name):
self.changed.emit()

def test_rules(self):
Tester(self.rules_widget.rules, self).exec_()
self.TesterClass(self.rules_widget.rules, self).exec_()

@property
def rules(self):
Expand Down
142 changes: 23 additions & 119 deletions src/calibre/gui2/html_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@


from qt.core import (
QComboBox, QDialogButtonBox, QFrame, QHBoxLayout, QIcon, QLabel, QLineEdit,
QMenu, QPushButton, QScrollArea, QSize, Qt, QTextCursor, QToolButton,
QVBoxLayout, QWidget, pyqtSignal
QComboBox, QFrame, QHBoxLayout, QIcon, QLabel, QLineEdit, QPushButton,
QScrollArea, Qt, QToolButton, QVBoxLayout, QWidget, pyqtSignal
)

from calibre import prepare_string_for_xml
from calibre.ebooks.html_transform_rules import (
ACTION_MAP, MATCH_TYPE_MAP, export_rules, import_rules, transform_html,
validate_rule
)
from calibre.gui2 import choose_files, choose_save_file, elided_text, error_dialog
from calibre.gui2 import elided_text, error_dialog
from calibre.gui2.convert.xpath_wizard import XPathEdit
from calibre.gui2.css_transform_rules import (
RulesWidget as RulesWidgetBase, Tester as TesterBase
)
from calibre.gui2.tag_mapper import (
RuleEditDialog as RuleEditDialogBase, RuleItem as RuleItemBase,
Rules as RulesBase, RulesDialog as RulesDialogBase, SaveLoadMixin
Rules as RulesBase, RulesDialog as RulesDialogBase
)
from calibre.gui2.widgets2 import Dialog
from calibre.utils.config import JSONConfig


# Classes for rule edit widget {{{


class TagAction(QWidget):

remove_action = pyqtSignal(object)
Expand Down Expand Up @@ -312,49 +313,20 @@ class Rules(RulesBase): # {{{
# }}}


class Tester(Dialog): # {{{
class Tester(TesterBase): # {{{

DIALOG_TITLE = _('Test HTML transform rules')
PREFS_NAME = 'test-html-transform-rules'
LABEL = _('Enter an HTML document below to test')

def __init__(self, rules, parent=None):
self.rules = rules
Dialog.__init__(self, self.DIALOG_TITLE, self.PREFS_NAME, parent=parent)

def setup_ui(self):
from calibre.gui2.tweak_book.editor.text import TextEdit
self.l = l = QVBoxLayout(self)
self.bb.setStandardButtons(QDialogButtonBox.StandardButton.Close)
self.la = la = QLabel(self.LABEL)
l.addWidget(la)
self.html = t = TextEdit(self)
t.load_text('<!-- %s -->\n' % _('Enter the HTML below and click the "Test" button'), 'html')
la.setBuddy(t)
c = t.textCursor()
c.movePosition(QTextCursor.MoveOperation.End)
t.setTextCursor(c)
self.h = h = QHBoxLayout()
l.addLayout(h)
h.addWidget(t)
self.test_button = b = QPushButton(_('&Test'), self)
b.clicked.connect(self.do_test)
h.addWidget(b)
self.result = la = TextEdit(self)
la.setReadOnly(True)
l.addWidget(la)
l.addWidget(self.bb)
LABEL = _('Enter an HTML document below and click the "Test" button')
SYNTAX = 'html'
RESULTS = '<!-- %s -->\n\n' % _('Resulting HTML')

@property
def value(self):
return self.html.toPlainText()
def compile_rules(self, rules):
return rules

def do_test(self):
changed, html = transform_html('\n' + self.value + '\n', self.rules)
self.result.load_text('<!-- %s -->\n\n%s' % (_('Resulting HTML'), html), 'html')

def sizeHint(self):
return QSize(800, 600)
self.set_result(html)
# }}}


Expand All @@ -373,82 +345,14 @@ def __init__(self, *args, **kw):
# }}}


class RulesWidget(QWidget, SaveLoadMixin): # {{{

changed = pyqtSignal()

def __init__(self, parent=None):
self.loaded_ruleset = None
QWidget.__init__(self, parent)
self.PREFS_OBJECT = JSONConfig('html-transform-rules')
l = QVBoxLayout(self)
self.rules_widget = w = Rules(self)
w.changed.connect(self.changed.emit)
l.addWidget(w)
self.h = h = QHBoxLayout()
l.addLayout(h)
self.export_button = b = QPushButton(_('E&xport'), self)
b.setToolTip(_('Export these rules to a file'))
b.clicked.connect(self.export_rules)
h.addWidget(b)
self.import_button = b = QPushButton(_('&Import'), self)
b.setToolTip(_('Import previously exported rules'))
b.clicked.connect(self.import_rules)
h.addWidget(b)
self.test_button = b = QPushButton(_('&Test rules'), self)
b.clicked.connect(self.test_rules)
h.addWidget(b)
h.addStretch(10)
self.save_button = b = QPushButton(_('&Save'), self)
b.setToolTip(_('Save this ruleset for later re-use'))
b.clicked.connect(self.save_ruleset)
h.addWidget(b)
self.export_button = b = QPushButton(_('&Load'), self)
self.load_menu = QMenu(self)
b.setMenu(self.load_menu)
b.setToolTip(_('Load a previously saved ruleset'))
b.clicked.connect(self.load_ruleset)
h.addWidget(b)
self.build_load_menu()

def export_rules(self):
rules = self.rules_widget.rules
if not rules:
return error_dialog(self, _('No rules'), _(
'There are no rules to export'), show=True)
path = choose_save_file(self, 'export-html-transform-rules', _('Choose file for exported rules'), initial_filename='html-rules.json')
if path:
raw = export_rules(rules)
with open(path, 'wb') as f:
f.write(raw)

def import_rules(self):
paths = choose_files(self, 'export-html-transform-rules', _('Choose file to import rules from'), select_only_single_file=True)
if paths:
with open(paths[0], 'rb') as f:
rules = import_rules(f.read())
self.rules_widget.rules = list(rules) + list(self.rules_widget.rules)
self.changed.emit()

def load_ruleset(self, name):
SaveLoadMixin.load_ruleset(self, name)
self.changed.emit()

def test_rules(self):
Tester(self.rules_widget.rules, self).exec_()

@property
def rules(self):
return self.rules_widget.rules

@rules.setter
def rules(self, val):
try:
self.rules_widget.rules = val or []
except Exception:
import traceback
traceback.print_exc()
self.rules_widget.rules = []
class RulesWidget(RulesWidgetBase): # {{{
PREFS_NAME = 'html-transform-rules'
INITIAL_FILE_NAME = 'html-rules.txt'
DIR_SAVE_NAME = 'export-html-transform-rules'
export_func = export_rules
import_func = import_rules
TesterClass = Tester
RulesClass = Rules
# }}}


Expand Down

0 comments on commit add6a6e

Please sign in to comment.