Esempio n. 1
0
    def get_filesystem_encoding_test(self):
        """
        Test the get_filesystem_encoding() function
        """
        with patch('openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
             patch('openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding:
            # GIVEN: sys.getfilesystemencoding returns "cp1252"
            mocked_getfilesystemencoding.return_value = 'cp1252'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            assert not mocked_getdefaultencoding.called
            assert result == 'cp1252', 'The result should be "cp1252"'

            # GIVEN: sys.getfilesystemencoding returns None and sys.getdefaultencoding returns "utf-8"
            mocked_getfilesystemencoding.return_value = None
            mocked_getdefaultencoding.return_value = 'utf-8'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            mocked_getdefaultencoding.assert_called_with()
            assert result == 'utf-8', 'The result should be "utf-8"'
Esempio n. 2
0
 def _performWizard(self):
     """
     Run the tasks in the wizard.
     """
     # Set plugin states
     self._incrementProgressBar(translate('OpenLP.FirstTimeWizard', 'Enabling selected plugins...'))
     self._setPluginStatus(self.songsCheckBox, u'songs/status')
     self._setPluginStatus(self.bibleCheckBox, u'bibles/status')
     # TODO Presentation plugin is not yet working on Mac OS X.
     # For now just ignore it.
     if sys.platform != 'darwin':
         self._setPluginStatus(self.presentationCheckBox, u'presentations/status')
     self._setPluginStatus(self.imageCheckBox, u'images/status')
     self._setPluginStatus(self.mediaCheckBox, u'media/status')
     self._setPluginStatus(self.remoteCheckBox, u'remotes/status')
     self._setPluginStatus(self.customCheckBox, u'custom/status')
     self._setPluginStatus(self.songUsageCheckBox, u'songusage/status')
     self._setPluginStatus(self.alertCheckBox, u'alerts/status')
     if self.webAccess:
         # Build directories for downloads
         songs_destination = os.path.join(
             unicode(gettempdir(), get_filesystem_encoding()), u'openlp')
         bibles_destination = AppLocation.get_section_data_path(u'bibles')
         themes_destination = AppLocation.get_section_data_path(u'themes')
         # Download songs
         for i in xrange(self.songsListWidget.count()):
             item = self.songsListWidget.item(i)
             if item.checkState() == QtCore.Qt.Checked:
                 filename = item.data(QtCore.Qt.UserRole)
                 self._incrementProgressBar(self.downloading % filename, 0)
                 self.previous_size = 0
                 destination = os.path.join(songs_destination, unicode(filename))
                 self.urlGetFile(u'%s%s' % (self.web, filename), destination)
         # Download Bibles
         bibles_iterator = QtGui.QTreeWidgetItemIterator(
             self.biblesTreeWidget)
         while bibles_iterator.value():
             item = bibles_iterator.value()
             if item.parent() and item.checkState(0) == QtCore.Qt.Checked:
                 bible = item.data(0, QtCore.Qt.UserRole)
                 self._incrementProgressBar(self.downloading % bible, 0)
                 self.previous_size = 0
                 self.urlGetFile(u'%s%s' % (self.web, bible), os.path.join(bibles_destination, bible))
             bibles_iterator += 1
         # Download themes
         for i in xrange(self.themesListWidget.count()):
             item = self.themesListWidget.item(i)
             if item.checkState() == QtCore.Qt.Checked:
                 theme = item.data(QtCore.Qt.UserRole)
                 self._incrementProgressBar(self.downloading % theme, 0)
                 self.previous_size = 0
                 self.urlGetFile(u'%s%s' % (self.web, theme), os.path.join(themes_destination, theme))
     # Set Default Display
     if self.displayComboBox.currentIndex() != -1:
         Settings().setValue(u'General/monitor', self.displayComboBox.currentIndex())
         self.screens.set_current_display(self.displayComboBox.currentIndex())
     # Set Global Theme
     if self.themeComboBox.currentIndex() != -1:
         Settings().setValue(u'themes/global theme', self.themeComboBox.currentText())
Esempio n. 3
0
    def get_filesystem_encoding_sys_function_not_called_test(self):
        """
        Test the get_filesystem_encoding() function does not call the sys.getdefaultencoding() function
        """
        # GIVEN: sys.getfilesystemencoding returns "cp1252"
        with patch('openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
                patch('openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding:
            mocked_getfilesystemencoding.return_value = 'cp1252'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            self.assertEqual(0, mocked_getdefaultencoding.called, 'getdefaultencoding should not have been called')
            self.assertEqual('cp1252', result, 'The result should be "cp1252"')
Esempio n. 4
0
 def _buildThemeScreenshots(self):
     """
     This method builds the theme screenshots' icons for all items in the
     ``self.themesListWidget``.
     """
     themes = self.config.get(u'themes', u'files')
     themes = themes.split(u',')
     for theme in themes:
         filename = self.config.get(u'theme_%s' % theme, u'filename')
         screenshot = self.config.get(u'theme_%s' % theme, u'screenshot')
         for index in xrange(self.themesListWidget.count()):
             item = self.themesListWidget.item(index)
             if item.data(QtCore.Qt.UserRole) == filename:
                 break
         item.setIcon(build_icon(os.path.join(unicode(gettempdir(), get_filesystem_encoding()), u'openlp',
             screenshot)))
Esempio n. 5
0
    def get_filesystem_encoding_sys_function_is_called_test(self):
        """
        Test the get_filesystem_encoding() function calls the sys.getdefaultencoding() function
        """
        # GIVEN: sys.getfilesystemencoding returns None and sys.getdefaultencoding returns "utf-8"
        with patch('openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
                patch('openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding:
            mocked_getfilesystemencoding.return_value = None
            mocked_getdefaultencoding.return_value = 'utf-8'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            mocked_getdefaultencoding.assert_called_with()
            self.assertEqual('utf-8', result, 'The result should be "utf-8"')
Esempio n. 6
0
    def get_filesystem_encoding_sys_function_is_called_test(self):
        """
        Test the get_filesystem_encoding() function calls the sys.getdefaultencoding() function
        """
        # GIVEN: sys.getfilesystemencoding returns None and sys.getdefaultencoding returns "utf-8"
        with patch('openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
                patch('openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding:
            mocked_getfilesystemencoding.return_value = None
            mocked_getdefaultencoding.return_value = 'utf-8'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            mocked_getdefaultencoding.assert_called_with()
            self.assertEqual('utf-8', result, 'The result should be "utf-8"')
Esempio n. 7
0
    def get_filesystem_encoding_sys_function_not_called_test(self):
        """
        Test the get_filesystem_encoding() function does not call the sys.getdefaultencoding() function
        """
        # GIVEN: sys.getfilesystemencoding returns "cp1252"
        with patch('openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
                patch('openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding:
            mocked_getfilesystemencoding.return_value = 'cp1252'

            # WHEN: get_filesystem_encoding() is called
            result = get_filesystem_encoding()

            # THEN: getdefaultencoding should have been called
            mocked_getfilesystemencoding.assert_called_with()
            self.assertEqual(0, mocked_getdefaultencoding.called,
                             'getdefaultencoding should not have been called')
            self.assertEqual('cp1252', result, 'The result should be "cp1252"')
Esempio n. 8
0
    def delete_theme(self, theme):
        """
        Delete a theme.

        :param theme: The theme to delete.
        """
        self.theme_list.remove(theme)
        thumb = '%s.png' % theme
        delete_file(os.path.join(self.path, thumb))
        delete_file(os.path.join(self.thumb_path, thumb))
        try:
            # Windows is always unicode, so no need to encode filenames
            if is_win():
                shutil.rmtree(os.path.join(self.path, theme))
            else:
                encoding = get_filesystem_encoding()
                shutil.rmtree(os.path.join(self.path, theme).encode(encoding))
        except OSError as os_error:
            shutil.Error = os_error
            self.log_exception('Error deleting theme %s' % theme)
Esempio n. 9
0
 def run(self):
     """
     Overridden method to run the thread.
     """
     themes = self.parent().config.get(u'themes', u'files')
     themes = themes.split(u',')
     config = self.parent().config
     for theme in themes:
         # Stop if the wizard has been cancelled.
         if self.parent().downloadCancelled:
             return
         title = config.get(u'theme_%s' % theme, u'title')
         filename = config.get(u'theme_%s' % theme, u'filename')
         screenshot = config.get(u'theme_%s' % theme, u'screenshot')
         urllib.urlretrieve(u'%s%s' % (self.parent().web, screenshot),
             os.path.join(unicode(gettempdir(), get_filesystem_encoding()), u'openlp', screenshot))
         item = QtGui.QListWidgetItem(title, self.parent().themesListWidget)
         item.setData(QtCore.Qt.UserRole, filename)
         item.setCheckState(QtCore.Qt.Unchecked)
         item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
Esempio n. 10
0
    def delete_theme(self, theme):
        """
        Delete a theme.

        :param theme: The theme to delete.
        """
        self.theme_list.remove(theme)
        thumb = '%s.png' % theme
        delete_file(os.path.join(self.path, thumb))
        delete_file(os.path.join(self.thumb_path, thumb))
        try:
            # Windows is always unicode, so no need to encode filenames
            if is_win():
                shutil.rmtree(os.path.join(self.path, theme))
            else:
                encoding = get_filesystem_encoding()
                shutil.rmtree(os.path.join(self.path, theme).encode(encoding))
        except OSError as os_error:
            shutil.Error = os_error
            self.log_exception('Error deleting theme %s' % theme)
Esempio n. 11
0
 def setDefaults(self):
     """
     Set up display at start of theme edit.
     """
     self.restart()
     check_directory_exists(os.path.join(
         unicode(gettempdir(), get_filesystem_encoding()), u'openlp'))
     self.noInternetFinishButton.setVisible(False)
     # Check if this is a re-run of the wizard.
     self.hasRunWizard = Settings().value(u'general/has run wizard')
     # Sort out internet access for downloads
     if self.webAccess:
         songs = self.config.get(u'songs', u'languages')
         songs = songs.split(u',')
         for song in songs:
             title = unicode(self.config.get(u'songs_%s' % song, u'title'), u'utf8')
             filename = unicode(self.config.get(u'songs_%s' % song, u'filename'), u'utf8')
             item = QtGui.QListWidgetItem(title, self.songsListWidget)
             item.setData(QtCore.Qt.UserRole, filename)
             item.setCheckState(QtCore.Qt.Unchecked)
             item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
         bible_languages = self.config.get(u'bibles', u'languages')
         bible_languages = bible_languages.split(u',')
         for lang in bible_languages:
             language = unicode(self.config.get(u'bibles_%s' % lang, u'title'), u'utf8')
             langItem = QtGui.QTreeWidgetItem(self.biblesTreeWidget, [language])
             bibles = self.config.get(u'bibles_%s' % lang, u'translations')
             bibles = bibles.split(u',')
             for bible in bibles:
                 title = unicode(self.config.get(u'bible_%s' % bible, u'title'), u'utf8')
                 filename = unicode(self.config.get(u'bible_%s' % bible, u'filename'))
                 item = QtGui.QTreeWidgetItem(langItem, [title])
                 item.setData(0, QtCore.Qt.UserRole, filename)
                 item.setCheckState(0, QtCore.Qt.Unchecked)
                 item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
         self.biblesTreeWidget.expandAll()
         # Download the theme screenshots.
         self.themeScreenshotThread = ThemeScreenshotThread(self)
         self.themeScreenshotThread.start()
     self.application.set_normal_cursor()
Esempio n. 12
0
    def _write_theme(self, theme, image_from, image_to):
        """
        Writes the theme to the disk and handles the background image if necessary

        :param theme: The theme data object.
        :param image_from: Where the theme image is currently located.
        :param image_to: Where the Theme Image is to be saved to
        """
        name = theme.theme_name
        theme_pretty_xml = theme.extract_formatted_xml()
        theme_dir = os.path.join(self.path, name)
        check_directory_exists(theme_dir)
        theme_file = os.path.join(theme_dir, name + '.xml')
        if self.old_background_image and image_to != self.old_background_image:
            delete_file(self.old_background_image)
        out_file = None
        try:
            out_file = open(theme_file, 'w', encoding='utf-8')
            out_file.write(theme_pretty_xml.decode('utf-8'))
        except IOError:
            self.log_exception('Saving theme to file failed')
        finally:
            if out_file:
                out_file.close()
        if image_from and os.path.abspath(image_from) != os.path.abspath(
                image_to):
            try:
                # Windows is always unicode, so no need to encode filenames
                if is_win():
                    shutil.copyfile(image_from, image_to)
                else:
                    encoding = get_filesystem_encoding()
                    shutil.copyfile(image_from.encode(encoding),
                                    image_to.encode(encoding))
            except IOError as xxx_todo_changeme:
                shutil.Error = xxx_todo_changeme
                self.log_exception('Failed to save theme image')
        self.generate_and_save_image(name, theme)
Esempio n. 13
0
    def __init__(self, parent, manager, bibleplugin):
        """
        Instantiate the wizard, and run any extra setup we need to.

        ``parent``
            The QWidget-derived parent of the wizard.

        ``manager``
            The Bible manager.

        ``bibleplugin``
            The Bible plugin.
        """
        self.manager = manager
        self.mediaItem = bibleplugin.mediaItem
        self.suffix = u'.sqlite'
        self.settingsSection = u'bibles'
        self.path = AppLocation.get_section_data_path(self.settingsSection)
        self.temp_dir = os.path.join(unicode(gettempdir(), get_filesystem_encoding()), u'openlp')
        self.files = self.manager.old_bible_databases
        self.success = {}
        self.newbibles = {}
        OpenLPWizard.__init__(self, parent, bibleplugin, u'bibleUpgradeWizard', u':/wizards/wizard_importbible.bmp')
Esempio n. 14
0
 def first_time(self):
     """
     If the first time wizard has run, this function is run to import all the
     new songs into the database.
     """
     self.application.process_events()
     self.onToolsReindexItemTriggered()
     self.application.process_events()
     db_dir = unicode(os.path.join(unicode(gettempdir(), get_filesystem_encoding()), u'openlp'))
     if not os.path.exists(db_dir):
         return
     song_dbs = []
     song_count = 0
     for sfile in os.listdir(db_dir):
         if sfile.startswith(u'songs_') and sfile.endswith(u'.sqlite'):
             self.application.process_events()
             song_dbs.append(os.path.join(db_dir, sfile))
             song_count += self._countSongs(os.path.join(db_dir, sfile))
     if not song_dbs:
         return
     self.application.process_events()
     progress = QtGui.QProgressDialog(self.main_window)
     progress.setWindowModality(QtCore.Qt.WindowModal)
     progress.setWindowTitle(translate('OpenLP.Ui', 'Importing Songs'))
     progress.setLabelText(translate('OpenLP.Ui', 'Starting import...'))
     progress.setCancelButton(None)
     progress.setRange(0, song_count)
     progress.setMinimumDuration(0)
     progress.forceShow()
     self.application.process_events()
     for db in song_dbs:
         importer = OpenLPSongImport(self.manager, filename=db)
         importer.doImport(progress)
         self.application.process_events()
     progress.setValue(song_count)
     self.mediaItem.onSearchTextButtonClicked()
Esempio n. 15
0
    def _write_theme(self, theme, image_from, image_to):
        """
        Writes the theme to the disk and handles the background image if necessary

        :param theme: The theme data object.
        :param image_from: Where the theme image is currently located.
        :param image_to: Where the Theme Image is to be saved to
        """
        name = theme.theme_name
        theme_pretty_xml = theme.extract_formatted_xml()
        theme_dir = os.path.join(self.path, name)
        check_directory_exists(theme_dir)
        theme_file = os.path.join(theme_dir, name + '.xml')
        if self.old_background_image and image_to != self.old_background_image:
            delete_file(self.old_background_image)
        out_file = None
        try:
            out_file = open(theme_file, 'w', encoding='utf-8')
            out_file.write(theme_pretty_xml.decode('utf-8'))
        except IOError:
            self.log_exception('Saving theme to file failed')
        finally:
            if out_file:
                out_file.close()
        if image_from and os.path.abspath(image_from) != os.path.abspath(image_to):
            try:
                # Windows is always unicode, so no need to encode filenames
                if is_win():
                    shutil.copyfile(image_from, image_to)
                else:
                    encoding = get_filesystem_encoding()
                    shutil.copyfile(image_from.encode(encoding), image_to.encode(encoding))
            except IOError as xxx_todo_changeme:
                shutil.Error = xxx_todo_changeme
                self.log_exception('Failed to save theme image')
        self.generate_and_save_image(name, theme)