Exemplo n.º 1
0
 def _process_formatting_tags(self, song_xml, temporary):
     """
     Process the formatting tags from the song and either add missing tags
     temporary or permanently to the formatting tag list.
     """
     if not hasattr(song_xml, u'format'):
         return
     found_tags = []
     for tag in song_xml.format.tags.getchildren():
         name = tag.get(u'name')
         if name is None:
             continue
         start_tag = u'{%s}' % name[:5]
         # Some tags have only start tag e.g. {br}
         end_tag = u'{/' + name[:5] + u'}' if hasattr(tag, 'close') else u''
         openlp_tag = {
             u'desc': name,
             u'start tag': start_tag,
             u'end tag': end_tag,
             u'start html': tag.open.text,
             # Some tags have only start html e.g. {br}
             u'end html': tag.close.text if hasattr(tag, 'close') else u'',
             u'protected': False,
         }
         # 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[u'temporary'] = temporary
         found_tags.append(openlp_tag)
     existing_tag_ids = [tag[u'start tag'] for tag in FormattingTags.get_html_tags()]
     new_tags = [tag for tag in found_tags if tag[u'start tag'] not in existing_tag_ids]
     FormattingTags.add_html_tags(new_tags)
     FormattingTags.save_html_tags()
Exemplo n.º 2
0
    def get_html_tags_with_user_tags_test(self):
        """
        FormattingTags class - test the get_html_tags(), add_html_tags() and remove_html_tag() methods.
        """
        with patch('openlp.core.lib.translate') as mocked_translate, \
                patch('openlp.core.lib.settings') as mocked_settings, \
                patch('openlp.core.lib.formattingtags.json') as mocked_json:
            # GIVEN: Our mocked modules and functions.
            mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate
            mocked_settings.value.return_value = ''
            mocked_json.loads.side_effect = [[], [TAG]]

            # WHEN: Get the display tags.
            FormattingTags.load_tags()
            old_tags_list = copy.deepcopy(FormattingTags.get_html_tags())

            # WHEN: Add our tag and get the tags again.
            FormattingTags.load_tags()
            FormattingTags.add_html_tags([TAG])
            new_tags_list = copy.deepcopy(FormattingTags.get_html_tags())

            # THEN: Lists should not be identical.
            assert old_tags_list != new_tags_list, 'The lists should be different.'

            # THEN: Added tag and last tag should be the same.
            new_tag = new_tags_list.pop()
            assert TAG == new_tag, 'Tags should be identical.'

            # WHEN: Remove the new tag.
            FormattingTags.remove_html_tag(len(new_tags_list))

            # THEN: The lists should now be identical.
            assert old_tags_list == FormattingTags.get_html_tags(), 'The lists should be identical.'
Exemplo n.º 3
0
    def test_get_html_tags_with_user_tags(self):
        """
        FormattingTags class - test the get_html_tags(), add_html_tags() and remove_html_tag() methods.
        """
        with patch('openlp.core.lib.translate') as mocked_translate, \
                patch('openlp.core.common.settings') as mocked_settings, \
                patch('openlp.core.lib.formattingtags.json') as mocked_json:
            # GIVEN: Our mocked modules and functions.
            mocked_translate.side_effect = lambda module, string_to_translate: string_to_translate
            mocked_settings.value.return_value = ''
            mocked_json.loads.side_effect = [[], [TAG]]

            # WHEN: Get the display tags.
            FormattingTags.load_tags()
            old_tags_list = copy.deepcopy(FormattingTags.get_html_tags())

            # WHEN: Add our tag and get the tags again.
            FormattingTags.load_tags()
            FormattingTags.add_html_tags([TAG])
            new_tags_list = copy.deepcopy(FormattingTags.get_html_tags())

            # THEN: Lists should not be identical.
            assert old_tags_list != new_tags_list, 'The lists should be different.'

            # THEN: Added tag and last tag should be the same.
            new_tag = new_tags_list.pop()
            assert TAG == new_tag, 'Tags should be identical.'

            # WHEN: Remove the new tag.
            FormattingTags.remove_html_tag(len(new_tags_list))

            # THEN: The lists should now be identical.
            assert old_tags_list == FormattingTags.get_html_tags(), 'The lists should be identical.'
Exemplo n.º 4
0
    def _process_formatting_tags(self, song_xml, temporary):
        """
        Process the formatting tags from the song and either add missing tags temporary or permanently to the
        formatting tag list.

        :param song_xml: The song XML
        :param temporary: Is the song temporary?
        """
        if not hasattr(song_xml, 'format'):
            return
        found_tags = []
        for tag in song_xml.format.tags.getchildren():
            name = tag.get('name')
            if name is None:
                continue
            start_tag = '{{{name}}}'.format(name=name[:5])
            # Some tags have only start tag e.g. {br}
            end_tag = '{{/{name}}}'.format(
                name=name[:5]) if hasattr(tag, 'close') else ''
            openlp_tag = {
                'desc': name,
                'start tag': start_tag,
                'end tag': end_tag,
                'start html': tag.open.text,
                # Some tags have only start html e.g. {br}
                'end html': tag.close.text if hasattr(tag, 'close') else '',
                '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
            }
            found_tags.append(openlp_tag)
        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
        ]
        # Do not save an empty list.
        if new_tags:
            FormattingTags.add_html_tags(new_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)
Exemplo n.º 5
0
    def _process_formatting_tags(self, song_xml, temporary):
        """
        Process the formatting tags from the song and either add missing tags temporary or permanently to the
        formatting tag list.

        :param song_xml: The song XML
        :param temporary: Is the song temporary?
        """
        if not hasattr(song_xml, "format"):
            return
        found_tags = []
        for tag in song_xml.format.tags.getchildren():
            name = tag.get("name")
            if name is None:
                continue
            start_tag = "{%s}" % name[:5]
            # Some tags have only start tag e.g. {br}
            end_tag = "{/" + name[:5] + "}" if hasattr(tag, "close") else ""
            openlp_tag = {
                "desc": name,
                "start tag": start_tag,
                "end tag": end_tag,
                "start html": tag.open.text,
                # Some tags have only start html e.g. {br}
                "end html": tag.close.text if hasattr(tag, "close") else "",
                "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,
            }
            found_tags.append(openlp_tag)
        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]
        # Do not save an empty list.
        if new_tags:
            FormattingTags.add_html_tags(new_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)
Exemplo n.º 6
0
    def _process_formatting_tags(self, song_xml, temporary):
        """
        Process the formatting tags from the song and either add missing tags temporary or permanently to the
        formatting tag list.

        :param song_xml: The song XML
        :param temporary: Is the song temporary?
        """
        if not hasattr(song_xml, 'format'):
            return
        found_tags = []
        for tag in song_xml.format.tags.getchildren():
            name = tag.get('name')
            if name is None:
                continue
            start_tag = '{%s}' % name[:5]
            # Some tags have only start tag e.g. {br}
            end_tag = '{/' + name[:5] + '}' if hasattr(tag, 'close') else ''
            openlp_tag = {
                'desc': name,
                'start tag': start_tag,
                'end tag': end_tag,
                'start html': tag.open.text,
                # Some tags have only start html e.g. {br}
                'end html': tag.close.text if hasattr(tag, 'close') else '',
                '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
            }
            found_tags.append(openlp_tag)
        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]
        # Do not save an empty list.
        if new_tags:
            FormattingTags.add_html_tags(new_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)