Merge branch 'fix-openlyrics-chord-import' into 'master'

OpenLyrics importer: Fix whitespaces being 'eaten' when importing a song with chord tags

See merge request openlp/openlp!520
This commit is contained in:
Tim Bentley 2022-12-18 07:50:31 +00:00
commit 8feaa63561
4 changed files with 50 additions and 4 deletions

View File

@ -67,8 +67,8 @@ class OpenLyricsImport(SongImport):
for elem in root.iter('{*}lines'):
self._strip_whitespace(elem)
for subelem in elem.iter('{*}br'):
self._strip_whitespace(subelem)
next_subelem = subelem.getnext()
self._strip_whitespace(subelem, next_subelem)
xml = etree.tostring(root).decode()
self.open_lyrics.xml_to_song(xml)
except etree.XMLSyntaxError:
@ -80,11 +80,15 @@ class OpenLyricsImport(SongImport):
text=exception.log_message))
self.log_error(file_path, exception.display_message)
def _strip_whitespace(self, elem):
def _strip_whitespace(self, elem, next_subelem=None):
"""
Remove leading and trailing whitespace from the 'text' and 'tail' attributes of an etree._Element object
"""
is_chord_after_tail = False
if next_subelem is not None:
if next_subelem.tag.endswith('chord'):
is_chord_after_tail = True
if elem.text is not None:
elem.text = elem.text.strip()
if elem.tail is not None:
if elem.tail is not None and not is_chord_after_tail:
elem.tail = elem.tail.strip()

View File

@ -179,3 +179,25 @@ class TestOpenLyricsImport(TestCase, TestMixin):
# THEN: The last call of the xml_to_song() method should have got the same XML content as its first call
importer.open_lyrics.xml_to_song.assert_called_with(no_whitespaces_xml)
def test_chord_leading_space_is_not_removed(self):
"""
Test if chords' leading space aren't removed when importing music.
"""
# GIVEN: One OpenLyrics XML with the <lines> tag (Amazing_Grace_3_chords.xml)
mocked_manager = MagicMock()
mocked_import_wizard = MagicMock()
importer = OpenLyricsImport(mocked_manager, file_paths=[])
importer.import_wizard = mocked_import_wizard
expected_content_file = TEST_PATH / 'Amazing_Grace_3_chords_result.xml'
expected_content = expected_content_file.read_text()
# WHEN: Importing the file not having those whitespaces...
importer.import_source = [TEST_PATH / 'Amazing_Grace_3_chords.xml']
importer.open_lyrics = MagicMock()
importer.open_lyrics.xml_to_song = MagicMock()
importer.do_import()
# THEN: The song should preserve spaces before chords
import_content = importer.open_lyrics.xml_to_song.call_args[0][0]
assert import_content == expected_content

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<song xmlns="http://openlyrics.info/namespace/2009/song"
version="0.8"
createdIn="OpenLP 2.9.5"
modifiedIn="MyApp 0.0.1"
modifiedDate="2022-12-12T00:01:00+10:00">
<properties>
<titles>
<title>Amazing Grace</title>
</titles>
</properties>
<lyrics>
<verse name="v1">
<lines>
<chord name="C"/>Amazing <chord name="C7"/>grace, how s<chord name="F"/>weet the s<chord name="Fm"/>ound<br/>That <chord name="C"/>saved a <chord name="Am7"/>wretch like<chord name="D"/> me!<br/>
</lines>
</verse>
</lyrics>
</song>

View File

@ -0,0 +1 @@
<song xmlns="http://openlyrics.info/namespace/2009/song" version="0.8" createdIn="OpenLP 2.9.5" modifiedIn="MyApp 0.0.1" modifiedDate="2022-12-12T00:01:00+10:00"><properties><titles><title>Amazing Grace</title></titles></properties><lyrics><verse name="v1"><lines><chord name="C"/>Amazing <chord name="C7"/>grace, how s<chord name="F"/>weet the s<chord name="Fm"/>ound<br/>That <chord name="C"/>saved a <chord name="Am7"/>wretch like<chord name="D"/> me!<br/></lines></verse></lyrics></song>