Merge branch 'fix_loading_saved_services' into 'master'

Fix loading saved services

Closes #622 and #611

See merge request openlp/openlp!212
This commit is contained in:
Tim Bentley 2020-07-01 16:09:44 +00:00
commit f8cfda1ed3
3 changed files with 60 additions and 5 deletions

View File

@ -493,6 +493,7 @@ class ServiceItem(RegistryProperties):
if path: if path:
self.has_original_file_path = False self.has_original_file_path = False
for text_image in service_item['serviceitem']['data']: for text_image in service_item['serviceitem']['data']:
text = None
file_hash = None file_hash = None
thumbnail = None thumbnail = None
if version >= 3: if version >= 3:
@ -504,17 +505,18 @@ class ServiceItem(RegistryProperties):
copy(path / 'thumbnails' / os.path.basename(text_image['image']), copy(path / 'thumbnails' / os.path.basename(text_image['image']),
AppLocation.get_section_data_path(self.name) / 'thumbnails') AppLocation.get_section_data_path(self.name) / 'thumbnails')
else: 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 # 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) 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 file_path = path / new_file
move(org_file_path, file_path) move(org_file_path, file_path)
# Check if (by chance) the thumbnails for this image is available on this machine # 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 test_thumb = AppLocation.get_section_data_path(self.name) / 'thumbnails' / new_file
if test_thumb.exists(): if test_thumb.exists():
thumbnail = test_thumb 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: else:
for text_image in service_item['serviceitem']['data']: for text_image in service_item['serviceitem']['data']:
file_hash = None file_hash = None

View File

@ -221,6 +221,7 @@ class OpenLyrics(object):
IMPLEMENTED_VERSION = '0.8' IMPLEMENTED_VERSION = '0.8'
START_TAGS_REGEX = re.compile(r'\{(\w+)\}') START_TAGS_REGEX = re.compile(r'\{(\w+)\}')
END_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]?)') VERSE_TAG_SPLITTER = re.compile('([a-zA-Z]+)([0-9]*)([a-zA-Z]?)')
def __init__(self, manager): def __init__(self, manager):
@ -319,13 +320,15 @@ class OpenLyrics(object):
optional_verse = start_tags + optional_verse optional_verse = start_tags + optional_verse
start_tags, end_tags = self._get_missing_tags(optional_verse) start_tags, end_tags = self._get_missing_tags(optional_verse)
optional_verse += end_tags optional_verse += end_tags
# convert chords
optional_verse = self._chordpro_to_openlyrics(optional_verse)
# Add formatting tags to text # Add formatting tags to text
lines_element = self._add_text_with_tags_to_lines(verse_element, optional_verse, tags_element) 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. # Do not add the break attribute to the last lines element.
if index < len(optional_verses) - 1: if index < len(optional_verses) - 1:
lines_element.set('break', 'optional') lines_element.set('break', 'optional')
xml_text = self._extract_xml(song_xml).decode() xml_text = self._extract_xml(song_xml).decode()
return self._chordpro_to_openlyrics(xml_text) return xml_text
def _chordpro_to_openlyrics(self, text): def _chordpro_to_openlyrics(self, text):
""" """
@ -335,7 +338,7 @@ class OpenLyrics(object):
:return: the lyrics with the converted chords :return: the lyrics with the converted chords
""" """
# Process chords. # Process chords.
new_text = re.sub(r'\[(\w.*?)\]', r'<chord name="\1"/>', text) new_text = re.sub(OpenLyrics.CHORD_TAGS_REGEX, r'<chord name="\1"/>', text)
return new_text return new_text
def _get_missing_tags(self, text): def _get_missing_tags(self, text):

View File

@ -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): def test_service_item_load_image_from_service(state_media, settings):
""" """
Test the Service Item - adding an image from a saved service 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 # GIVEN: A new service item and a mocked add icon function
image_name = 'image_1.jpg' image_name = 'image_1.jpg'