Example #1
0
 def do_export(self):
     """
     Export the songs.
     """
     log.debug('started OpenLyricsExport')
     open_lyrics = OpenLyrics(self.manager)
     self.parent.progress_bar.setMaximum(len(self.songs))
     for song in self.songs:
         self.application.process_events()
         if self.parent.stop_export_flag:
             return False
         self.parent.increment_progress_bar(
             translate('SongsPlugin.OpenLyricsExport', 'Exporting "%s"...') % song.title)
         xml = open_lyrics.song_to_xml(song)
         tree = etree.ElementTree(etree.fromstring(xml.encode()))
         filename = '%s (%s)' % (song.title, ', '.join([author.display_name for author in song.authors]))
         filename = clean_filename(filename)
         # Ensure the filename isn't too long for some filesystems
         filename_with_ext = '%s.xml' % filename[0:250 - len(self.save_path)]
         # Make sure we're not overwriting an existing file
         conflicts = 0
         while os.path.exists(os.path.join(self.save_path, filename_with_ext)):
             conflicts += 1
             filename_with_ext = '%s-%d.xml' % (filename[0:247 - len(self.save_path)], conflicts)
         # Pass a file object, because lxml does not cope with some special
         # characters in the path (see lp:757673 and lp:744337).
         tree.write(open(os.path.join(self.save_path, filename_with_ext), 'wb'), encoding='utf-8',
                    xml_declaration=True, pretty_print=True)
     return True
Example #2
0
 def do_export(self):
     """
     Export the songs.
     """
     log.debug('started OpenLyricsExport')
     open_lyrics = OpenLyrics(self.manager)
     self.parent.progress_bar.setMaximum(len(self.songs))
     for song in self.songs:
         self.application.process_events()
         if self.parent.stop_export_flag:
             return False
         self.parent.increment_progress_bar(
             translate('SongsPlugin.OpenLyricsExport', 'Exporting "%s"...') % song.title)
         xml = open_lyrics.song_to_xml(song)
         tree = etree.ElementTree(etree.fromstring(xml.encode()))
         filename = '%s (%s)' % (song.title, ', '.join([author.display_name for author in song.authors]))
         filename = clean_filename(filename)
         # Ensure the filename isn't too long for some filesystems
         filename_with_ext = '%s.xml' % filename[0:250 - len(self.save_path)]
         # Make sure we're not overwriting an existing file
         conflicts = 0
         while os.path.exists(os.path.join(self.save_path, filename_with_ext)):
             conflicts += 1
             filename_with_ext = '%s-%d.xml' % (filename[0:247 - len(self.save_path)], conflicts)
         # Pass a file object, because lxml does not cope with some special
         # characters in the path (see lp:757673 and lp:744337).
         tree.write(open(os.path.join(self.save_path, filename_with_ext), 'wb'), encoding='utf-8',
                    xml_declaration=True, pretty_print=True)
     return True
Example #3
0
    def __init__(self, parent, **kwargs):
        """
        The constructor loads up the database and creates and initialises the
        tables if the database doesn't exist.

        :param parent:
        :param kwargs:
            ``path``
                The path to the bible database file.

            ``name``
                The name of the database. This is also used as the file name for SQLite databases.
        """
        log.info('BibleDB loaded')
        self.bible_plugin = parent
        self.session = None
        if 'path' not in kwargs:
            raise KeyError('Missing keyword argument "path".')
        if 'name' not in kwargs and 'file' not in kwargs:
            raise KeyError('Missing keyword argument "name" or "file".')
        self.stop_import_flag = False
        if 'name' in kwargs:
            self.name = kwargs['name']
            if not isinstance(self.name, str):
                self.name = str(self.name, 'utf-8')
            self.file = clean_filename(self.name) + '.sqlite'
        if 'file' in kwargs:
            self.file = kwargs['file']
        Manager.__init__(self, 'bibles', init_schema, self.file, upgrade)
        if self.session and 'file' in kwargs:
                self.get_name()
        if 'path' in kwargs:
            self.path = kwargs['path']
        self.wizard = None
        Registry().register_function('openlp_stop_wizard', self.stop_import)
Example #4
0
    def clean_filename_test(self):
        """
        Test the clean_filename() function
        """
        # GIVEN: A invalid file name and the valid file name.
        invalid_name = 'A_file_with_invalid_characters_[\\/:\*\?"<>\|\+\[\]%].py'
        wanted_name = 'A_file_with_invalid_characters______________________.py'

        # WHEN: Clean the name.
        result = clean_filename(invalid_name)

        # THEN: The file name should be cleaned.
        assert result == wanted_name, 'The file name should not contain any special characters.'
Example #5
0
    def clean_filename_test(self):
        """
        Test the clean_filename() function
        """
        # GIVEN: A invalid file name and the valid file name.
        invalid_name = 'A_file_with_invalid_characters_[\\/:\*\?"<>\|\+\[\]%].py'
        wanted_name = 'A_file_with_invalid_characters______________________.py'

        # WHEN: Clean the name.
        result = clean_filename(invalid_name)

        # THEN: The file name should be cleaned.
        self.assertEqual(
            wanted_name, result,
            'The file name should not contain any special characters.')
Example #6
0
    def __init__(self, parent, **kwargs):
        """
        The constructor loads up the database and creates and initialises the
        tables if the database doesn't exist.

        **Required keyword arguments:**

        ``path``
            The path to the bible database file.

        ``name``
            The name of the database. This is also used as the file name for
            SQLite databases.
        """
        log.info(u'BibleDB loaded')
        QtCore.QObject.__init__(self)
        self.bible_plugin = parent
        if u'path' not in kwargs:
            raise KeyError(u'Missing keyword argument "path".')
        if u'name' not in kwargs and u'file' not in kwargs:
            raise KeyError(u'Missing keyword argument "name" or "file".')
        self.stop_import_flag = False
        if u'name' in kwargs:
            self.name = kwargs[u'name']
            if not isinstance(self.name, unicode):
                self.name = unicode(self.name, u'utf-8')
            self.file = clean_filename(self.name) + u'.sqlite'
        if u'file' in kwargs:
            self.file = kwargs[u'file']
        Manager.__init__(self, u'bibles', init_schema, self.file, upgrade)
        if u'file' in kwargs:
            self.get_name()
        if u'path' in kwargs:
            self.path = kwargs[u'path']
        self.wizard = None
        QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import)
Example #7
0
    def __init__(self, parent, **kwargs):
        """
        The constructor loads up the database and creates and initialises the
        tables if the database doesn't exist.

        :param parent:
        :param kwargs:
            ``path``
                The path to the bible database file.

            ``name``
                The name of the database. This is also used as the file name for SQLite databases.
        """
        log.info('BibleDB loaded')
        QtCore.QObject.__init__(self)
        self.bible_plugin = parent
        self.session = None
        if 'path' not in kwargs:
            raise KeyError('Missing keyword argument "path".')
        if 'name' not in kwargs and 'file' not in kwargs:
            raise KeyError('Missing keyword argument "name" or "file".')
        self.stop_import_flag = False
        if 'name' in kwargs:
            self.name = kwargs['name']
            if not isinstance(self.name, str):
                self.name = str(self.name, 'utf-8')
            self.file = clean_filename(self.name) + '.sqlite'
        if 'file' in kwargs:
            self.file = kwargs['file']
        Manager.__init__(self, 'bibles', init_schema, self.file, upgrade)
        if self.session and 'file' in kwargs:
                self.get_name()
        if 'path' in kwargs:
            self.path = kwargs['path']
        self.wizard = None
        Registry().register_function('openlp_stop_wizard', self.stop_import)