From 357fe20c91d0f8886a1437c58f31c95eba37dfc6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 1 Jul 2020 16:09:44 +0000 Subject: [PATCH] Fix incorrect image title from loading service This fixes the crash when you select a image loaded from a service file --- openlp/core/lib/serviceitem.py | 8 +-- openlp/plugins/songs/lib/openlyricsxml.py | 7 ++- .../openlp_core/lib/test_serviceitem.py | 50 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/openlp/core/lib/serviceitem.py b/openlp/core/lib/serviceitem.py index af0a7f135..88c1d9309 100644 --- a/openlp/core/lib/serviceitem.py +++ b/openlp/core/lib/serviceitem.py @@ -493,6 +493,7 @@ class ServiceItem(RegistryProperties): if path: self.has_original_file_path = False for text_image in service_item['serviceitem']['data']: + text = None file_hash = None thumbnail = None if version >= 3: @@ -504,17 +505,18 @@ class ServiceItem(RegistryProperties): copy(path / 'thumbnails' / os.path.basename(text_image['image']), AppLocation.get_section_data_path(self.name) / 'thumbnails') else: - org_file_path = path / text_image + text = text_image + org_file_path = path / text # rename the extracted file so that it follows the sha256 based approach of openlp 3 self.sha256_file_hash = sha256_file_hash(org_file_path) - new_file = '{hash}{ext}'.format(hash=self.sha256_file_hash, ext=os.path.splitext(text_image)[1]) + new_file = '{hash}{ext}'.format(hash=self.sha256_file_hash, ext=os.path.splitext(text)[1]) file_path = path / new_file move(org_file_path, file_path) # Check if (by chance) the thumbnails for this image is available on this machine test_thumb = AppLocation.get_section_data_path(self.name) / 'thumbnails' / new_file if test_thumb.exists(): thumbnail = test_thumb - self.add_from_image(file_path, text_image, thumbnail=thumbnail, file_hash=file_hash) + self.add_from_image(file_path, text, thumbnail=thumbnail, file_hash=file_hash) else: for text_image in service_item['serviceitem']['data']: file_hash = None diff --git a/openlp/plugins/songs/lib/openlyricsxml.py b/openlp/plugins/songs/lib/openlyricsxml.py index c20dbd8e7..20a75773e 100644 --- a/openlp/plugins/songs/lib/openlyricsxml.py +++ b/openlp/plugins/songs/lib/openlyricsxml.py @@ -221,6 +221,7 @@ class OpenLyrics(object): IMPLEMENTED_VERSION = '0.8' START_TAGS_REGEX = re.compile(r'\{(\w+)\}') END_TAGS_REGEX = re.compile(r'\{/(\w+)\}') + CHORD_TAGS_REGEX = re.compile(r'\[(\w.*?)\]') VERSE_TAG_SPLITTER = re.compile('([a-zA-Z]+)([0-9]*)([a-zA-Z]?)') def __init__(self, manager): @@ -319,13 +320,15 @@ class OpenLyrics(object): optional_verse = start_tags + optional_verse start_tags, end_tags = self._get_missing_tags(optional_verse) optional_verse += end_tags + # convert chords + optional_verse = self._chordpro_to_openlyrics(optional_verse) # Add formatting tags to text lines_element = self._add_text_with_tags_to_lines(verse_element, optional_verse, tags_element) # Do not add the break attribute to the last lines element. if index < len(optional_verses) - 1: lines_element.set('break', 'optional') xml_text = self._extract_xml(song_xml).decode() - return self._chordpro_to_openlyrics(xml_text) + return xml_text def _chordpro_to_openlyrics(self, text): """ @@ -335,7 +338,7 @@ class OpenLyrics(object): :return: the lyrics with the converted chords """ # Process chords. - new_text = re.sub(r'\[(\w.*?)\]', r'', text) + new_text = re.sub(OpenLyrics.CHORD_TAGS_REGEX, r'', text) return new_text def _get_missing_tags(self, text): diff --git a/tests/functional/openlp_core/lib/test_serviceitem.py b/tests/functional/openlp_core/lib/test_serviceitem.py index b71097e52..faa7ef369 100644 --- a/tests/functional/openlp_core/lib/test_serviceitem.py +++ b/tests/functional/openlp_core/lib/test_serviceitem.py @@ -118,6 +118,56 @@ def test_service_item_load_custom_from_service(state_media, settings, service_it def test_service_item_load_image_from_service(state_media, settings): """ Test the Service Item - adding an image from a saved service + tests a version 3 service file serviceitem (openlp-servicefile-version == 3) + """ + # GIVEN: A new service item (pre encoded from the json format) and a mocked add icon function + image_name = 'BrightDots.png' + fake_hash = 'abcd' + extracted_file = Path(TEST_PATH) / '{base}{ext}'.format(base=fake_hash, ext=os.path.splitext(image_name)[1]) + frame_array = {'path': extracted_file, 'title': image_name, 'file_hash': fake_hash, + 'thumbnail': Path("/path/images/thumbnails/abcd.png")} + service_item = ServiceItem(None) + service_item.add_icon = MagicMock() + item = {'serviceitem': {'header': {'name': 'images', 'plugin': 'images', 'theme': -1, 'title': 'BrightDots.png', + 'footer': [], 'type': 2, 'audit': '', 'notes': '', 'from_plugin': False, + 'capabilities': [3, 1, 5, 6, 17, 21], 'search': '', 'data': '', + 'xml_version': None, 'auto_play_slides_once': False, + 'auto_play_slides_loop': False, 'timed_slide_interval': 0, 'start_time': 0, + 'end_time': 0, 'media_length': 0, 'background_audio': [], + 'theme_overwritten': False, 'will_auto_start': False, 'processor': None, + 'metadata': [], 'sha256_file_hash': None, 'stored_filename': None}, + 'data': [{'title': 'BrightDots.png', + 'image': Path('images/thumbnails/{}.png'.format(fake_hash)), + 'file_hash': fake_hash}]}} + + # WHEN: adding an image from a saved Service and mocked exists + with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists,\ + patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as mocked_get_section_data_path,\ + patch('openlp.core.lib.serviceitem.AppLocation.get_data_path') as mocked_get_data_path,\ + patch('openlp.core.lib.serviceitem.sha256_file_hash') as mocked_sha256_file_hash,\ + patch('openlp.core.lib.serviceitem.copy'),\ + patch('openlp.core.lib.serviceitem.move'): + mocked_sha256_file_hash.return_value = fake_hash + mocked_exists.return_value = True + mocked_get_section_data_path.return_value = Path('/path/') + mocked_get_data_path.return_value = Path('/path/') + service_item.set_from_service(item, TEST_PATH, 3) + + # THEN: We should get back a valid service item + assert service_item.is_valid is True, 'The new service item should be valid' + assert extracted_file == service_item.get_rendered_frame(0), 'The first frame should match the path to the image' + assert frame_array == service_item.get_frames()[0], 'The return should match frame array1' + assert extracted_file == service_item.get_frame_path(0), \ + 'The frame path should match the full path to the image' + assert image_name == service_item.get_frame_title(0), 'The frame title should match the image name' + assert image_name == service_item.get_display_title(), 'The display title should match the first image name' + assert service_item.is_image() is True, 'This service item should be of an "image" type' + + +def test_old_service_item_load_image_from_service(state_media, settings): + """ + Test the Service Item - adding an image from a saved service + tests a old service file serviceitem (openlp-servicefile-version < 3) """ # GIVEN: A new service item and a mocked add icon function image_name = 'image_1.jpg'