From 2443b94edaa28715670f9661a0f649538ca2071f Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Mon, 6 Jun 2016 08:16:09 -0700 Subject: [PATCH 1/6] Convert htmlbuilder strings to Template() --- openlp/core/lib/htmlbuilder.py | 181 +++++++++--------- .../openlp_core_lib/test_htmlbuilder.py | 38 ++-- 2 files changed, 116 insertions(+), 103 deletions(-) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index f0d8ddef2..28976b68c 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -390,14 +390,14 @@ is the function which has to be called from outside. The generated and returned import logging from PyQt5 import QtWebKit +from string import Template from openlp.core.common import Settings from openlp.core.lib.theme import BackgroundType, BackgroundGradientType, VerticalType, HorizontalType log = logging.getLogger(__name__) -# TODO: Verify where this is used before converting to python3 -HTMLSRC = """ +HTML_SRC = Template(""" @@ -411,14 +411,14 @@ HTMLSRC = """ -webkit-user-select: none; } body { - %s; + ${bg_css}; } .size { position: absolute; left: 0px; top: 0px; - width: 100%%; - height: 100%%; + width: 100%; + height: 100%; } #black { z-index: 8; @@ -431,14 +431,14 @@ body { #image { z-index: 2; } -%s +${css_additions} #footer { position: absolute; z-index: 6; - %s + ${footer_css} } /* lyric css */ -%s +${lyrics_css} sup { font-size: 0.6em; vertical-align: top; @@ -448,8 +448,8 @@ sup { - - -%s + + +${html_additions}
-""" +""") + +LYRICS_SRC = Template(""" +.lyricstable { + z-index: 5; + position: absolute; + display: table; + ${stable} +} +.lyricscell { + display: table-cell; + word-wrap: break-word; + -webkit-transition: opacity 0.4s ease; + ${lyrics} +} +.lyricsmain { + ${main} +} +""") + +FOOTER_SRC = Template(""" +left: ${left}px; +bottom: ${bottom}px; +width: ${width}px; +font-family: ${family}; +font-size: ${size}pt; +color: ${color}; +text-align: left; +white-space: ${space}; +""") + +LYRICS_FORMAT_SRC = Template(""" +${justify}word-wrap: break-word; +text-align: ${align}; +vertical-align: ${valign}; +font-family: ${font}; +font-size: ${size}pt; +color: ${color}; +line-height: ${line}%; +margin: 0; +padding: 0; +padding-bottom: ${bottom}; +padding-left: ${left}px; +width: ${width}px; +height: ${height}px;${font_style}${font_weight} +""") def build_html(item, screen, is_live, background, image=None, plugins=None): @@ -582,18 +627,17 @@ def build_html(item, screen, is_live, background, image=None, plugins=None): css_additions += plugin.get_display_css() js_additions += plugin.get_display_javascript() html_additions += plugin.get_display_html() - html = HTMLSRC % ( - build_background_css(item, width), - css_additions, - build_footer_css(item, height), - build_lyrics_css(item), - 'true' if theme_data and theme_data.display_slide_transition and is_live else 'false', - js_additions, - bgimage_src, - image_src, - html_additions - ) - return html + return HTML_SRC.substitute(bg_css=build_background_css(item, width), + css_additions=css_additions, + footer_css=build_footer_css(item, height), + lyrics_css=build_lyrics_css(item), + transitions='true' if (theme_data and + theme_data.display_slide_transition and + is_live) else 'false', + js_additions=js_additions, + bg_image=bgimage_src, + image=image_src, + html_additions=html_additions) def webkit_version(): @@ -650,24 +694,6 @@ def build_lyrics_css(item): :param item: Service Item containing theme and location information """ - # TODO: Verify this before converting to python3 - style = """ -.lyricstable { - z-index: 5; - position: absolute; - display: table; - %s -} -.lyricscell { - display: table-cell; - word-wrap: break-word; - -webkit-transition: opacity 0.4s ease; - %s -} -.lyricsmain { - %s -} -""" theme_data = item.theme_data lyricstable = '' lyrics = '' @@ -680,8 +706,7 @@ def build_lyrics_css(item): lyricsmain += ' text-shadow: {theme} {shadow}px ' \ '{shadow}px;'.format(theme=theme_data.font_main_shadow_color, shadow=theme_data.font_main_shadow_size) - lyrics_css = style % (lyricstable, lyrics, lyricsmain) - return lyrics_css + return LYRICS_SRC.substitute(stable=lyricstable, lyrics=lyrics, main=lyricsmain) def build_lyrics_outline_css(theme_data): @@ -710,38 +735,23 @@ def build_lyrics_format_css(theme_data, width, height): """ align = HorizontalType.Names[theme_data.display_horizontal_align] valign = VerticalType.Names[theme_data.display_vertical_align] - if theme_data.font_main_outline: - left_margin = int(theme_data.font_main_outline_size) * 2 - else: - left_margin = 0 - justify = 'white-space:pre-wrap;' + left_margin = (int(theme_data.font_main_outline_size) * 2) if theme_data.font_main_outline else 0 # fix tag incompatibilities - if theme_data.display_horizontal_align == HorizontalType.Justify: - justify = '' - if theme_data.display_vertical_align == VerticalType.Bottom: - padding_bottom = '0.5em' - else: - padding_bottom = '0' - lyrics = '{justify} word-wrap: break-word; ' \ - 'text-align: {align}; vertical-align: {valign}; font-family: {font}; ' \ - 'font-size: {size}pt; color: {color}; line-height: {line:d}%; margin: 0;' \ - 'padding: 0; padding-bottom: {bottom}; padding-left: {left}px; width: {width}px; ' \ - 'height: {height}px; '.format(justify=justify, - align=align, - valign=valign, - font=theme_data.font_main_name, - size=theme_data.font_main_size, - color=theme_data.font_main_color, - line=100 + int(theme_data.font_main_line_adjustment), - bottom=padding_bottom, - left=left_margin, - width=width, - height=height) - if theme_data.font_main_italics: - lyrics += 'font-style:italic; ' - if theme_data.font_main_bold: - lyrics += 'font-weight:bold; ' - return lyrics + justify = '' if (theme_data.display_horizontal_align == HorizontalType.Justify) else 'white-space:pre-wrap;\n' + padding_bottom = '0.5em' if (theme_data.display_vertical_align == VerticalType.Bottom) else '0' + return LYRICS_FORMAT_SRC.substitute(justify=justify, + align=align, + valign=valign, + font=theme_data.font_main_name, + size=theme_data.font_main_size, + color=theme_data.font_main_color, + line='{line:d}'.format(line=100 + int(theme_data.font_main_line_adjustment)), + bottom=padding_bottom, + left=left_margin, + width=width, + height=height, + font_style='\nfont-style:italic;' if theme_data.font_main_italics else '', + font_weight='\nfont-weight:bold;' if theme_data.font_main_bold else '') def build_footer_css(item, height): @@ -751,22 +761,11 @@ def build_footer_css(item, height): :param item: Service Item to be processed. :param height: """ - style = """ - left: {left}px; - bottom: {bottom}px; - width: {width}px; - font-family: {family}; - font-size: {size}pt; - color: {color}; - text-align: left; - white-space: {space}; - """ theme = item.theme_data if not theme or not item.footer: return '' bottom = height - int(item.footer.y()) - int(item.footer.height()) whitespace = 'normal' if Settings().value('themes/wrap footer') else 'nowrap' - lyrics_html = style.format(left=item.footer.x(), bottom=bottom, width=item.footer.width(), - family=theme.font_footer_name, size=theme.font_footer_size, - color=theme.font_footer_color, space=whitespace) - return lyrics_html + return FOOTER_SRC.substitute(left=item.footer.x(), bottom=bottom, width=item.footer.width(), + family=theme.font_footer_name, size=theme.font_footer_size, + color=theme.font_footer_color, space=whitespace) diff --git a/tests/functional/openlp_core_lib/test_htmlbuilder.py b/tests/functional/openlp_core_lib/test_htmlbuilder.py index 48c60b55f..5f385e3eb 100644 --- a/tests/functional/openlp_core_lib/test_htmlbuilder.py +++ b/tests/functional/openlp_core_lib/test_htmlbuilder.py @@ -182,19 +182,33 @@ LYRICS_CSS = """ } """ LYRICS_OUTLINE_CSS = ' -webkit-text-stroke: 0.125em #000000; -webkit-text-fill-color: #FFFFFF; ' -LYRICS_FORMAT_CSS = ' word-wrap: break-word; text-align: justify; vertical-align: bottom; ' + \ - 'font-family: Arial; font-size: 40pt; color: #FFFFFF; line-height: 108%; margin: 0;padding: 0; ' + \ - 'padding-bottom: 0.5em; padding-left: 2px; width: 1580px; height: 810px; font-style:italic; font-weight:bold; ' +LYRICS_FORMAT_CSS = """ +word-wrap: break-word; +text-align: justify; +vertical-align: bottom; +font-family: Arial; +font-size: 40pt; +color: #FFFFFF; +line-height: 108%; +margin: 0; +padding: 0; +padding-bottom: 0.5em; +padding-left: 2px; +width: 1580px; +height: 810px; +font-style:italic; +font-weight:bold; +""" FOOTER_CSS_BASE = """ - left: 10px; - bottom: 0px; - width: 1260px; - font-family: Arial; - font-size: 12pt; - color: #FFFFFF; - text-align: left; - white-space: %s; - """ +left: 10px; +bottom: 0px; +width: 1260px; +font-family: Arial; +font-size: 12pt; +color: #FFFFFF; +text-align: left; +white-space: %s; +""" FOOTER_CSS = FOOTER_CSS_BASE % ('nowrap') FOOTER_CSS_WRAP = FOOTER_CSS_BASE % ('normal') FOOTER_CSS_INVALID = '' From 5fe54b59075c2b9da658e0ee0a14cac38c2b697a Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Tue, 7 Jun 2016 06:12:22 -0700 Subject: [PATCH 2/6] Beauty spa for the htmlbuilder --- openlp/core/lib/htmlbuilder.py | 361 +++++++++--------- .../openlp_core_lib/test_htmlbuilder.py | 361 +++++++++--------- 2 files changed, 361 insertions(+), 361 deletions(-) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index 28976b68c..6f2fee68c 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -398,200 +398,199 @@ from openlp.core.lib.theme import BackgroundType, BackgroundGradientType, Vertic log = logging.getLogger(__name__) HTML_SRC = Template(""" - - - -OpenLP Display - - - - - - -${html_additions} -
- -
- - -""") + function show_text_completed(){ + return (timer == null); + } + + + + + + ${html_additions} +
+ +
+ + + """) LYRICS_SRC = Template(""" -.lyricstable { - z-index: 5; - position: absolute; - display: table; - ${stable} -} -.lyricscell { - display: table-cell; - word-wrap: break-word; - -webkit-transition: opacity 0.4s ease; - ${lyrics} -} -.lyricsmain { - ${main} -} -""") + .lyricstable { + z-index: 5; + position: absolute; + display: table; + ${stable} + } + .lyricscell { + display: table-cell; + word-wrap: break-word; + -webkit-transition: opacity 0.4s ease; + ${lyrics} + } + .lyricsmain { + ${main} + } + """) FOOTER_SRC = Template(""" -left: ${left}px; -bottom: ${bottom}px; -width: ${width}px; -font-family: ${family}; -font-size: ${size}pt; -color: ${color}; -text-align: left; -white-space: ${space}; -""") + left: ${left}px; + bottom: ${bottom}px; + width: ${width}px; + font-family: ${family}; + font-size: ${size}pt; + color: ${color}; + text-align: left; + white-space: ${space}; + """) LYRICS_FORMAT_SRC = Template(""" -${justify}word-wrap: break-word; -text-align: ${align}; -vertical-align: ${valign}; -font-family: ${font}; -font-size: ${size}pt; -color: ${color}; -line-height: ${line}%; -margin: 0; -padding: 0; -padding-bottom: ${bottom}; -padding-left: ${left}px; -width: ${width}px; -height: ${height}px;${font_style}${font_weight} -""") + ${justify}word-wrap: break-word; + text-align: ${align}; + vertical-align: ${valign}; + font-family: ${font}; + font-size: ${size}pt; + color: ${color}; + line-height: ${line}%; + margin: 0; + padding: 0; + padding-bottom: ${bottom}; + padding-left: ${left}px; + width: ${width}px; + height: ${height}px;${font_style}${font_weight} + """) def build_html(item, screen, is_live, background, image=None, plugins=None): @@ -737,7 +736,7 @@ def build_lyrics_format_css(theme_data, width, height): valign = VerticalType.Names[theme_data.display_vertical_align] left_margin = (int(theme_data.font_main_outline_size) * 2) if theme_data.font_main_outline else 0 # fix tag incompatibilities - justify = '' if (theme_data.display_horizontal_align == HorizontalType.Justify) else 'white-space:pre-wrap;\n' + justify = '' if (theme_data.display_horizontal_align == HorizontalType.Justify) else ' white-space: pre-wrap;\n' padding_bottom = '0.5em' if (theme_data.display_vertical_align == VerticalType.Bottom) else '0' return LYRICS_FORMAT_SRC.substitute(justify=justify, align=align, @@ -750,8 +749,8 @@ def build_lyrics_format_css(theme_data, width, height): left=left_margin, width=width, height=height, - font_style='\nfont-style:italic;' if theme_data.font_main_italics else '', - font_weight='\nfont-weight:bold;' if theme_data.font_main_bold else '') + font_style='\n font-style: italic;' if theme_data.font_main_italics else '', + font_weight='\n font-weight: bold;' if theme_data.font_main_bold else '') def build_footer_css(item, height): diff --git a/tests/functional/openlp_core_lib/test_htmlbuilder.py b/tests/functional/openlp_core_lib/test_htmlbuilder.py index 5f385e3eb..e76faa311 100644 --- a/tests/functional/openlp_core_lib/test_htmlbuilder.py +++ b/tests/functional/openlp_core_lib/test_htmlbuilder.py @@ -14,201 +14,200 @@ from tests.functional import MagicMock, patch from tests.helpers.testmixin import TestMixin HTML = """ - - - -OpenLP Display - - - - - - -plugin HTML -
- -
- - -""" + function show_text_completed(){ + return (timer == null); + } + + + + + + plugin HTML +
+ +
+ + + """ BACKGROUND_CSS_RADIAL = 'background: -webkit-gradient(radial, 5 50%, 100, 5 50%, 5, from(#000000), to(#FFFFFF)) fixed' LYRICS_CSS = """ -.lyricstable { - z-index: 5; - position: absolute; - display: table; - left: 10px; top: 20px; -} -.lyricscell { - display: table-cell; - word-wrap: break-word; - -webkit-transition: opacity 0.4s ease; - lyrics_format_css -} -.lyricsmain { - text-shadow: #000000 5px 5px; -} -""" + .lyricstable { + z-index: 5; + position: absolute; + display: table; + left: 10px; top: 20px; + } + .lyricscell { + display: table-cell; + word-wrap: break-word; + -webkit-transition: opacity 0.4s ease; + lyrics_format_css + } + .lyricsmain { + text-shadow: #000000 5px 5px; + } + """ LYRICS_OUTLINE_CSS = ' -webkit-text-stroke: 0.125em #000000; -webkit-text-fill-color: #FFFFFF; ' LYRICS_FORMAT_CSS = """ -word-wrap: break-word; -text-align: justify; -vertical-align: bottom; -font-family: Arial; -font-size: 40pt; -color: #FFFFFF; -line-height: 108%; -margin: 0; -padding: 0; -padding-bottom: 0.5em; -padding-left: 2px; -width: 1580px; -height: 810px; -font-style:italic; -font-weight:bold; -""" + word-wrap: break-word; + text-align: justify; + vertical-align: bottom; + font-family: Arial; + font-size: 40pt; + color: #FFFFFF; + line-height: 108%; + margin: 0; + padding: 0; + padding-bottom: 0.5em; + padding-left: 2px; + width: 1580px; + height: 810px; + font-style: italic; + font-weight: bold; + """ FOOTER_CSS_BASE = """ -left: 10px; -bottom: 0px; -width: 1260px; -font-family: Arial; -font-size: 12pt; -color: #FFFFFF; -text-align: left; -white-space: %s; -""" + left: 10px; + bottom: 0px; + width: 1260px; + font-family: Arial; + font-size: 12pt; + color: #FFFFFF; + text-align: left; + white-space: %s; + """ FOOTER_CSS = FOOTER_CSS_BASE % ('nowrap') FOOTER_CSS_WRAP = FOOTER_CSS_BASE % ('normal') FOOTER_CSS_INVALID = '' @@ -257,6 +256,8 @@ class Htmbuilder(TestCase, TestMixin): # WHEN: Create the html. html = build_html(item, screen, is_live, background, plugins=plugins) + self.maxDiff = None + # THEN: The returned html should match. self.assertEqual(html, HTML, 'The returned html should match') From 828741ec84f9d2a175990295722088043e4846d7 Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Tue, 7 Jun 2016 06:21:07 -0700 Subject: [PATCH 3/6] Remove testing verbosity flag --- tests/functional/openlp_core_lib/test_htmlbuilder.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_htmlbuilder.py b/tests/functional/openlp_core_lib/test_htmlbuilder.py index e76faa311..31885c2e2 100644 --- a/tests/functional/openlp_core_lib/test_htmlbuilder.py +++ b/tests/functional/openlp_core_lib/test_htmlbuilder.py @@ -256,8 +256,6 @@ class Htmbuilder(TestCase, TestMixin): # WHEN: Create the html. html = build_html(item, screen, is_live, background, plugins=plugins) - self.maxDiff = None - # THEN: The returned html should match. self.assertEqual(html, HTML, 'The returned html should match') From b84dbb15a2f077642f641eb775e81095c9a6e2ad Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Wed, 8 Jun 2016 13:26:01 -0700 Subject: [PATCH 4/6] Oops in string format --- openlp/plugins/bibles/forms/editbibledialog.py | 2 +- openlp/plugins/songs/lib/mediaitem.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openlp/plugins/bibles/forms/editbibledialog.py b/openlp/plugins/bibles/forms/editbibledialog.py index ca3bea4ed..f1e833637 100644 --- a/openlp/plugins/bibles/forms/editbibledialog.py +++ b/openlp/plugins/bibles/forms/editbibledialog.py @@ -104,7 +104,7 @@ class Ui_EditBibleDialog(object): for book in BiblesResourcesDB.get_books(): self.book_name_label[book['abbreviation']] = QtWidgets.QLabel(self.book_name_widget) self.book_name_label[book['abbreviation']].setObjectName( - 'book_name_label[{name}]'.format(book=book['abbreviation'])) + 'book_name_label[{book}]'.format(book=book['abbreviation'])) self.book_name_edit[book['abbreviation']] = QtWidgets.QLineEdit(self.book_name_widget) self.book_name_edit[book['abbreviation']].setObjectName( 'book_name_edit[{name}]'.format(name=book['abbreviation'])) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index f52abb86f..b89858019 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -603,7 +603,7 @@ class SongMediaItem(MediaManagerItem): else: verse_index = VerseType.from_tag(verse[0]['type']) verse_tag = VerseType.translated_tags[verse_index] - verse_def = '{tag}{label}'.format(tzg=verse_tag, text=verse[0]['label']) + verse_def = '{tag}{text}'.format(tag=verse_tag, text=verse[0]['label']) service_item.add_from_text(verse[1], verse_def) service_item.title = song.title author_list = self.generate_footer(service_item, song) From e222a11390c16f2486a82b9d52f9138f3a04d254 Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Wed, 8 Jun 2016 13:41:21 -0700 Subject: [PATCH 5/6] String format oops --- openlp/core/ui/firsttimeform.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlp/core/ui/firsttimeform.py b/openlp/core/ui/firsttimeform.py index cadb4814f..183c577ef 100644 --- a/openlp/core/ui/firsttimeform.py +++ b/openlp/core/ui/firsttimeform.py @@ -571,7 +571,7 @@ class FirstTimeForm(QtWidgets.QWizard, UiFirstTimeWizard, RegistryProperties): text = translate('OpenLP.FirstTimeWizard', 'Download complete. Click the {button} button to start OpenLP.' ).format(button=clean_button_text(self.buttonText(QtWidgets.QWizard.FinishButton))) - self.progress_label.setText() + self.progress_label.setText(text) else: if self.has_run_wizard: text = translate('OpenLP.FirstTimeWizard', @@ -582,7 +582,7 @@ class FirstTimeForm(QtWidgets.QWizard, UiFirstTimeWizard, RegistryProperties): text = translate('OpenLP.FirstTimeWizard', 'Click the {button} button to start OpenLP.' ).format(button=clean_button_text(self.buttonText(QtWidgets.QWizard.FinishButton))) - self.progress_label.setText() + self.progress_label.setText(text) self.finish_button.setVisible(True) self.finish_button.setEnabled(True) self.cancel_button.setVisible(False) From aedcecc7ffa37b390b4cb3077f719f91c581e9be Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Wed, 8 Jun 2016 19:57:21 -0700 Subject: [PATCH 6/6] String format oops --- openlp/core/ui/firsttimeform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/core/ui/firsttimeform.py b/openlp/core/ui/firsttimeform.py index 183c577ef..9ae2e0898 100644 --- a/openlp/core/ui/firsttimeform.py +++ b/openlp/core/ui/firsttimeform.py @@ -565,7 +565,7 @@ class FirstTimeForm(QtWidgets.QWizard, UiFirstTimeWizard, RegistryProperties): if self.has_run_wizard: text = translate('OpenLP.FirstTimeWizard', 'Download complete. Click the {button} button to return to OpenLP.' - ).format(text=clean_button_text(self.buttonText(QtWidgets.QWizard.FinishButton))) + ).format(button=clean_button_text(self.buttonText(QtWidgets.QWizard.FinishButton))) self.progress_label.setText(text) else: text = translate('OpenLP.FirstTimeWizard',