Fix for importing custom tags from openlyricsxml + test in progress

This commit is contained in:
Tomas Groth 2014-09-25 17:01:11 +02:00
parent 977fd1d187
commit 3e66b4aa98
2 changed files with 53 additions and 6 deletions

View File

@ -239,6 +239,7 @@ class OpenLyrics(object):
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
FormattingTags.load_tags()
def song_to_xml(self, song): def song_to_xml(self, song):
""" """
@ -582,18 +583,19 @@ class OpenLyrics(object):
# Some tags have only start html e.g. {br} # Some tags have only start html e.g. {br}
'end html': tag.close.text if hasattr(tag, 'close') else '', 'end html': tag.close.text if hasattr(tag, 'close') else '',
'protected': False, 'protected': False,
# Add 'temporary' key in case the formatting tag should not be saved otherwise it is supposed that
# formatting tag is permanent.
'temporary': temporary
} }
# Add 'temporary' key in case the formatting tag should not be saved otherwise it is supposed that
# formatting tag is permanent.
if temporary:
openlp_tag['temporary'] = temporary
found_tags.append(openlp_tag) found_tags.append(openlp_tag)
existing_tag_ids = [tag['start tag'] for tag in FormattingTags.get_html_tags()] existing_tag_ids = [tag['start tag'] for tag in FormattingTags.get_html_tags()]
new_tags = [tag for tag in found_tags if tag['start tag'] not in existing_tag_ids] new_tags = [tag for tag in found_tags if tag['start tag'] not in existing_tag_ids]
# Do not save an empty list. # Do not save an empty list.
if new_tags: if new_tags:
FormattingTags.add_html_tags(new_tags) FormattingTags.add_html_tags(new_tags)
FormattingTags.save_html_tags() if not temporary:
custom_tags = [tag for tag in FormattingTags.get_html_tags() if not tag['protected'] and not tag['temporary']]
FormattingTags.save_html_tags(custom_tags)
def _process_lines_mixed_content(self, element, newlines=True): def _process_lines_mixed_content(self, element, newlines=True):
""" """

View File

@ -31,11 +31,18 @@ This module contains tests for the OpenLyrics song importer.
""" """
import os import os
import json
from unittest import TestCase from unittest import TestCase
from lxml import etree, objectify
from tests.functional import MagicMock, patch from tests.functional import MagicMock, patch
from tests.helpers.testmixin import TestMixin
from openlp.plugins.songs.lib.importers.openlyrics import OpenLyricsImport from openlp.plugins.songs.lib.importers.openlyrics import OpenLyricsImport
from openlp.plugins.songs.lib.importers.songimport import SongImport from openlp.plugins.songs.lib.importers.songimport import SongImport
from openlp.plugins.songs.lib.openlyricsxml import OpenLyrics
from openlp.core.common import Registry, Settings
from openlp.core.lib import FormattingTags
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', '..', 'resources', 'openlyricssongs')) '..', '..', '..', 'resources', 'openlyricssongs'))
@ -59,11 +66,28 @@ SONG_TEST_DATA = {
} }
} }
tags_str ='[{"protected": false, "desc": "z", "start tag": "{z}", "end html": "</strong>", "temporary": false, "end tag": "{/z}", "start html": "strong>"}]'
class TestOpenLyricsImport(TestCase):
class TestOpenLyricsImport(TestCase, TestMixin):
""" """
Test the functions in the :mod:`openlyricsimport` module. Test the functions in the :mod:`openlyricsimport` module.
""" """
def setUp(self):
"""
Create the registry
"""
self.get_application()
Registry.create()
self.build_settings()
#Settings().extend_default_settings(__default_settings__)
def tearDown(self):
"""
Cleanup
"""
self.destroy_settings()
def create_importer_test(self): def create_importer_test(self):
""" """
Test creating an instance of the OpenLyrics file importer Test creating an instance of the OpenLyrics file importer
@ -97,3 +121,24 @@ class TestOpenLyricsImport(TestCase):
# THEN: The xml_to_song() method should have been called # THEN: The xml_to_song() method should have been called
self.assertTrue(importer.open_lyrics.xml_to_song.called) self.assertTrue(importer.open_lyrics.xml_to_song.called)
def process_formatting_tags_test(self):
"""
Test that _process_formatting_tags works
"""
# GIVEN: A OpenLyric XML with formatting and a mocked out formattingtag-setting
mocked_manager = MagicMock()
Settings().setValue('formattingTags/html_tags', json.loads(tags_str))
ol = OpenLyrics(mocked_manager)
parser = etree.XMLParser(remove_blank_text=True)
parsed_file = etree.parse(open(os.path.join(TEST_PATH, 'duchu-tags.xml'), 'rb'), parser)
xml = etree.tostring(parsed_file).decode()
song_xml = objectify.fromstring(xml)
# WHEN: processing the formatting tags
print(Settings().value('formattingTags/html_tags'))
ol._process_formatting_tags(song_xml, False)
# THEN: New tags should have been saved
print(str(Settings().value('formattingTags/html_tags')))
self.assertTrue(False)