def retranslateUi(self): """ Song wizard localisation. """ self.setWindowTitle(translate('SongsPlugin.ImportWizardForm', 'Song Import Wizard')) self.title_label.setText(WizardStrings.HeaderStyle % translate('OpenLP.Ui', 'Welcome to the Song Import Wizard')) self.information_label.setText( translate('SongsPlugin.ImportWizardForm', 'This wizard will help you to import songs from a variety of formats. Click the next button ' 'below to start the process by selecting a format to import from.')) self.source_page.setTitle(WizardStrings.ImportSelect) self.source_page.setSubTitle(WizardStrings.ImportSelectLong) self.format_label.setText(WizardStrings.FormatLabel) for format_list in SongFormat.get_format_list(): format_name, custom_combo_text, description_text, select_mode = \ SongFormat.get(format_list, 'name', 'comboBoxText', 'descriptionText', 'selectMode') combo_box_text = (custom_combo_text if custom_combo_text else format_name) self.format_combo_box.setItemText(format_list, combo_box_text) if description_text is not None: self.format_widgets[format_list]['description_label'].setText(description_text) if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[format_list]['addButton'].setText( translate('SongsPlugin.ImportWizardForm', 'Add Files...')) self.format_widgets[format_list]['removeButton'].setText( translate('SongsPlugin.ImportWizardForm', 'Remove File(s)')) else: self.format_widgets[format_list]['browseButton'].setText(UiStrings().Browse) f_label = 'Filename:' if select_mode == SongFormatSelect.SingleFolder: f_label = 'Folder:' self.format_widgets[format_list]['filepathLabel'].setText( translate('SongsPlugin.ImportWizardForm', f_label)) for format_list in self.disablable_formats: self.format_widgets[format_list]['disabled_label'].setText(SongFormat.get(format_list, 'disabledLabelText')) self.progress_page.setTitle(WizardStrings.Importing) self.progress_page.setSubTitle( translate('SongsPlugin.ImportWizardForm', 'Please wait while your songs are imported.')) self.progress_label.setText(WizardStrings.Ready) self.progress_bar.setFormat(WizardStrings.PercentSymbolFormat) self.error_copy_to_button.setText(translate('SongsPlugin.ImportWizardForm', 'Copy')) self.error_save_to_button.setText(translate('SongsPlugin.ImportWizardForm', 'Save to File')) # Align all QFormLayouts towards each other. formats = [f for f in SongFormat.get_format_list() if 'filepathLabel' in self.format_widgets[f]] labels = [self.format_widgets[f]['filepathLabel'] for f in formats] # Get max width of all labels max_label_width = max(self.format_label.minimumSizeHint().width(), max([label.minimumSizeHint().width() for label in labels])) self.format_spacer.changeSize(max_label_width, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) spacers = [self.format_widgets[f]['filepathSpacer'] for f in formats] for index, spacer in enumerate(spacers): spacer.changeSize( max_label_width - labels[index].minimumSizeHint().width(), 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) # Align descriptionLabels with rest of layout for format_list in SongFormat.get_format_list(): if SongFormat.get(format_list, 'descriptionText') is not None: self.format_widgets[format_list]['descriptionSpacer'].changeSize( max_label_width + self.format_h_spacing, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
def validateCurrentPage(self): """ Re-implement the validateCurrentPage() method. Validate the current page before moving on to the next page. Provide each song format class with a chance to validate its input by overriding is_valid_source(). """ if self.currentPage() == self.welcome_page: return True elif self.currentPage() == self.source_page: this_format = self.current_format Settings().setValue('songs/last import type', this_format) select_mode, class_, error_msg = SongFormat.get(this_format, 'selectMode', 'class', 'invalidSourceMsg') if select_mode == SongFormatSelect.MultipleFiles: import_source = self.get_list_of_files(self.format_widgets[this_format]['file_list_widget']) error_title = UiStrings().IFSp focus_button = self.format_widgets[this_format]['addButton'] else: import_source = self.format_widgets[this_format]['file_path_edit'].text() error_title = (UiStrings().IFSs if select_mode == SongFormatSelect.SingleFile else UiStrings().IFdSs) focus_button = self.format_widgets[this_format]['browseButton'] if not class_.is_valid_source(import_source): critical_error_message_box(error_title, error_msg) focus_button.setFocus() return False return True elif self.currentPage() == self.progress_page: return True
def isComplete(self): """ Return True if: * an available format is selected, and * if MultipleFiles mode, at least one file is selected * or if SingleFile mode, the specified file exists * or if SingleFolder mode, the specified folder exists When this method returns True, the wizard's Next button is enabled. """ wizard = self.wizard() this_format = wizard.current_format select_mode, format_available = SongFormat.get(this_format, 'selectMode', 'availability') if format_available: if select_mode == SongFormatSelect.MultipleFiles: if wizard.format_widgets[this_format]['file_list_widget'].count() > 0: return True else: file_path = str(wizard.format_widgets[this_format]['file_path_edit'].text()) if file_path: if select_mode == SongFormatSelect.SingleFile and os.path.isfile(file_path): return True elif select_mode == SongFormatSelect.SingleFolder and os.path.isdir(file_path): return True return False
def custom_init(self): """ Song wizard specific initialisation. """ for song_format in SongFormat.get_format_list(): if not SongFormat.get(song_format, 'availability'): self.format_widgets[song_format]['disabled_widget'].setVisible(True) self.format_widgets[song_format]['import_widget'].setVisible(False)
def on_add_button_clicked(self): """ Add a file or directory. """ this_format = self.current_format select_mode, format_name, ext_filter, custom_title = \ SongFormat.get(this_format, 'selectMode', 'name', 'filter', 'getFilesTitle') title = custom_title if custom_title else WizardStrings.OpenTypeFile % format_name if select_mode == SongFormatSelect.MultipleFiles: self.get_files(title, self.format_widgets[this_format]['file_list_widget'], ext_filter) self.source_page.emit(QtCore.SIGNAL('completeChanged()'))
def custom_signals(self): """ Song wizard specific signals. """ for song_format in SongFormat.get_format_list(): select_mode = SongFormat.get(song_format, 'selectMode') if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[song_format]['addButton'].clicked.connect(self.on_add_button_clicked) self.format_widgets[song_format]['removeButton'].clicked.connect(self.onRemoveButtonClicked) else: self.format_widgets[song_format]['browseButton'].clicked.connect(self.on_browse_button_clicked) self.format_widgets[song_format]['file_path_edit'].textChanged.connect(self.onFilepathEditTextChanged)
def import_songs(self, import_format, **kwargs): """ Add the correct importer class :param import_format: The import_format to be used :param kwargs: The arguments :return: the correct importer """ class_ = SongFormat.get(import_format, 'class') importer = class_(self.manager, **kwargs) importer.register(self.media_item.import_wizard) return importer
def on_browse_button_clicked(self): """ Browse for files or a directory. """ this_format = self.current_format select_mode, format_name, ext_filter = SongFormat.get(this_format, 'selectMode', 'name', 'filter') file_path_edit = self.format_widgets[this_format]['file_path_edit'] if select_mode == SongFormatSelect.SingleFile: self.get_file_name( WizardStrings.OpenTypeFile % format_name, file_path_edit, 'last directory import', ext_filter) elif select_mode == SongFormatSelect.SingleFolder: self.get_folder(WizardStrings.OpenTypeFolder % format_name, file_path_edit, 'last directory import')
def test_get_attributed_single_attribute(self): """ Test that SongFormat.get(song_format, attribute) returns only one -and the correct- attribute """ # GIVEN: A SongFormat for song_format in SongFormat.get_format_list(): # WHEN: Retrieving an attribute that overrides the default values for attribute in SongFormat.get(song_format).keys(): # THEN: Return the attribute self.assertEquals( SongFormat.get(song_format, attribute), SongFormat.get(song_format)[attribute], "The returned attribute doesn't match the stored one") # WHEN: Retrieving an attribute that was not overridden for attribute in SongFormat.__defaults__.keys(): if attribute not in SongFormat.get(song_format).keys(): # THEN: Return the default value self.assertEquals( SongFormat.get(song_format, attribute), SongFormat.__defaults__[attribute], "The returned attribute does not match the default values stored" )
def on_add_button_clicked(self): """ Add a file or directory. """ this_format = self.current_format select_mode, format_name, ext_filter, custom_title = \ SongFormat.get(this_format, 'selectMode', 'name', 'filter', 'getFilesTitle') title = custom_title if custom_title else WizardStrings.OpenTypeFile % format_name if select_mode == SongFormatSelect.MultipleFiles: self.get_files( title, self.format_widgets[this_format]['file_list_widget'], ext_filter) self.source_page.completeChanged.emit()
def on_browse_button_clicked(self): """ Browse for files or a directory. """ this_format = self.current_format select_mode, format_name, ext_filter = SongFormat.get( this_format, 'selectMode', 'name', 'filter') file_path_edit = self.format_widgets[this_format]['file_path_edit'] if select_mode == SongFormatSelect.SingleFile: self.get_file_name(WizardStrings.OpenTypeFile % format_name, file_path_edit, 'last directory import', ext_filter) elif select_mode == SongFormatSelect.SingleFolder: self.get_folder(WizardStrings.OpenTypeFolder % format_name, file_path_edit, 'last directory import')
def custom_signals(self): """ Song wizard specific signals. """ for song_format in SongFormat.get_format_list(): select_mode = SongFormat.get(song_format, 'selectMode') if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[song_format]['addButton'].clicked.connect( self.on_add_button_clicked) self.format_widgets[song_format][ 'removeButton'].clicked.connect( self.on_remove_button_clicked) else: self.format_widgets[song_format][ 'browseButton'].clicked.connect( self.on_browse_button_clicked) self.format_widgets[song_format]['file_path_edit'].textChanged.\ connect(self.on_filepath_edit_text_changed)
def perform_wizard(self): """ Perform the actual import. This method pulls in the correct importer class, and then runs the ``do_import`` method of the importer to do the actual importing. """ source_format = self.current_format select_mode = SongFormat.get(source_format, 'selectMode') if select_mode == SongFormatSelect.SingleFile: importer = self.plugin.import_songs(source_format, filename=self.format_widgets[source_format]['file_path_edit'].text()) elif select_mode == SongFormatSelect.SingleFolder: importer = self.plugin.import_songs(source_format, folder=self.format_widgets[source_format]['file_path_edit'].text()) else: importer = self.plugin.import_songs( source_format, filenames=self.get_list_of_files(self.format_widgets[source_format]['file_list_widget'])) importer.do_import() self.progress_label.setText(WizardStrings.FinishedImport)
def perform_wizard(self): """ Perform the actual import. This method pulls in the correct importer class, and then runs the ``do_import`` method of the importer to do the actual importing. """ source_format = self.current_format select_mode = SongFormat.get(source_format, 'selectMode') if select_mode == SongFormatSelect.SingleFile: importer = self.plugin.import_songs(source_format, file_path=self.format_widgets[source_format]['path_edit'].path) elif select_mode == SongFormatSelect.SingleFolder: importer = self.plugin.import_songs(source_format, folder_path=self.format_widgets[source_format]['path_edit'].path) else: importer = self.plugin.import_songs( source_format, file_paths=self.get_list_of_paths(self.format_widgets[source_format]['file_list_widget'])) importer.do_import() self.progress_label.setText(WizardStrings.FinishedImport)
def set_defaults(self): """ Set default form values for the song import wizard. """ self.restart() self.finish_button.setVisible(False) self.cancel_button.setVisible(True) last_import_type = self.settings.value('songs/last import type') if last_import_type < 0 or last_import_type >= self.format_combo_box.count(): last_import_type = 0 self.format_combo_box.setCurrentIndex(last_import_type) for format_list in SongFormat.get_format_list(): select_mode = SongFormat.get(format_list, 'selectMode') if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[format_list]['file_list_widget'].clear() self.error_report_text_edit.clear() self.error_report_text_edit.setHidden(True) self.error_copy_to_button.setHidden(True) self.error_save_to_button.setHidden(True)
def set_defaults(self): """ Set default form values for the song import wizard. """ self.restart() self.finish_button.setVisible(False) self.cancel_button.setVisible(True) last_import_type = Settings().value('songs/last import type') if last_import_type < 0 or last_import_type >= self.format_combo_box.count(): last_import_type = 0 self.format_combo_box.setCurrentIndex(last_import_type) for format_list in SongFormat.get_format_list(): select_mode = SongFormat.get(format_list, 'selectMode') if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[format_list]['file_list_widget'].clear() else: self.format_widgets[format_list]['file_path_edit'].setText('') self.error_report_text_edit.clear() self.error_report_text_edit.setHidden(True) self.error_copy_to_button.setHidden(True) self.error_save_to_button.setHidden(True)
def perform_wizard(self): """ Perform the actual import. This method pulls in the correct importer class, and then runs the ``do_import`` method of the importer to do the actual importing. """ source_format = self.current_format select_mode = SongFormat.get(source_format, 'selectMode') if select_mode == SongFormatSelect.SingleFile: importer = self.plugin.import_songs(source_format, file_path=self.format_widgets[source_format]['path_edit'].path) elif select_mode == SongFormatSelect.SingleFolder: importer = self.plugin.import_songs(source_format, folder_path=self.format_widgets[source_format]['path_edit'].path) else: importer = self.plugin.import_songs( source_format, file_paths=self.get_list_of_paths(self.format_widgets[source_format]['file_list_widget'])) try: importer.do_import() self.progress_label.setText(WizardStrings.FinishedImport) except OSError as e: log.exception('Importing songs failed') self.progress_label.setText(translate('SongsPlugin.ImportWizardForm', 'Your Song import failed. {error}').format(error=e))
def do_import(self): """ Receive either a list of files or a folder (unicode) to import. """ from openlp.plugins.songs.lib.importer import SongFormat ps_string = SongFormat.get(SongFormat.PowerSong, 'name') if isinstance(self.import_source, str): if os.path.isdir(self.import_source): dir = self.import_source self.import_source = [] for file in os.listdir(dir): if fnmatch.fnmatch(file, '*.song'): self.import_source.append(os.path.join(dir, file)) else: self.import_source = '' if not self.import_source or not isinstance(self.import_source, list): self.log_error( translate('SongsPlugin.PowerSongImport', 'No songs to import.'), translate('SongsPlugin.PowerSongImport', 'No {text} files found.').format(text=ps_string)) return self.import_wizard.progress_bar.setMaximum(len(self.import_source)) for file in self.import_source: if self.stop_import_flag: return self.set_defaults() parse_error = False with open(file, 'rb') as song_data: while True: try: label = self._read_string(song_data) if not label: break field = self._read_string(song_data) except ValueError: parse_error = True self.log_error( os.path.basename(file), translate( 'SongsPlugin.PowerSongImport', 'Invalid {text} file. Unexpected byte value.'). format(text=ps_string)) break else: if label == 'TITLE': self.title = field.replace('\n', ' ') elif label == 'AUTHOR': self.parse_author(field) elif label == 'COPYRIGHTLINE': found_copyright = True self._parse_copyright_cCCLI(field) elif label == 'PART': self.add_verse(field) if parse_error: continue # Check that file had TITLE field if not self.title: self.log_error( os.path.basename(file), translate( 'SongsPlugin.PowerSongImport', 'Invalid {text} file. Missing "TITLE" header.').format( text=ps_string)) continue # Check that file had COPYRIGHTLINE label if not found_copyright: self.log_error( self.title, translate( 'SongsPlugin.PowerSongImport', 'Invalid {text} file. Missing "COPYRIGHTLINE" header.' ).format(text=ps_string)) continue # Check that file had at least one verse if not self.verses: self.log_error( self.title, translate('SongsPlugin.PowerSongImport', 'Verses not found. Missing "PART" header.')) continue if not self.finish(): self.log_error(self.title)
def retranslateUi(self): """ Song wizard localisation. """ self.setWindowTitle( translate('SongsPlugin.ImportWizardForm', 'Song Import Wizard')) self.title_label.setText( WizardStrings.HeaderStyle % translate('OpenLP.Ui', 'Welcome to the Song Import Wizard')) self.information_label.setText( translate( 'SongsPlugin.ImportWizardForm', 'This wizard will help you to import songs from a variety of formats. Click the next button ' 'below to start the process by selecting a format to import from.' )) self.source_page.setTitle(WizardStrings.ImportSelect) self.source_page.setSubTitle(WizardStrings.ImportSelectLong) self.format_label.setText(WizardStrings.FormatLabel) for format_list in SongFormat.get_format_list(): format_name, custom_combo_text, description_text, select_mode = \ SongFormat.get(format_list, 'name', 'comboBoxText', 'descriptionText', 'selectMode') combo_box_text = (custom_combo_text if custom_combo_text else format_name) self.format_combo_box.setItemText(format_list, combo_box_text) if description_text is not None: self.format_widgets[format_list]['description_label'].setText( description_text) if select_mode == SongFormatSelect.MultipleFiles: self.format_widgets[format_list]['addButton'].setText( translate('SongsPlugin.ImportWizardForm', 'Add Files...')) self.format_widgets[format_list]['removeButton'].setText( translate('SongsPlugin.ImportWizardForm', 'Remove File(s)')) else: self.format_widgets[format_list]['browseButton'].setText( UiStrings().Browse) f_label = 'Filename:' if select_mode == SongFormatSelect.SingleFolder: f_label = 'Folder:' self.format_widgets[format_list]['filepathLabel'].setText( translate('SongsPlugin.ImportWizardForm', f_label)) for format_list in self.disablable_formats: self.format_widgets[format_list]['disabled_label'].setText( SongFormat.get(format_list, 'disabledLabelText')) self.progress_page.setTitle(WizardStrings.Importing) self.progress_page.setSubTitle( translate('SongsPlugin.ImportWizardForm', 'Please wait while your songs are imported.')) self.progress_label.setText(WizardStrings.Ready) self.progress_bar.setFormat(WizardStrings.PercentSymbolFormat) self.error_copy_to_button.setText( translate('SongsPlugin.ImportWizardForm', 'Copy')) self.error_save_to_button.setText( translate('SongsPlugin.ImportWizardForm', 'Save to File')) # Align all QFormLayouts towards each other. formats = [ f for f in SongFormat.get_format_list() if 'filepathLabel' in self.format_widgets[f] ] labels = [self.format_widgets[f]['filepathLabel'] for f in formats] # Get max width of all labels max_label_width = max( self.format_label.minimumSizeHint().width(), max([label.minimumSizeHint().width() for label in labels])) self.format_spacer.changeSize(max_label_width, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) spacers = [self.format_widgets[f]['filepathSpacer'] for f in formats] for index, spacer in enumerate(spacers): spacer.changeSize( max_label_width - labels[index].minimumSizeHint().width(), 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) # Align descriptionLabels with rest of layout for format_list in SongFormat.get_format_list(): if SongFormat.get(format_list, 'descriptionText') is not None: self.format_widgets[format_list][ 'descriptionSpacer'].changeSize( max_label_width + self.format_h_spacing, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
def add_file_select_item(self): """ Add a file selection page. """ this_format = self.current_format prefix, can_disable, description_text, select_mode = \ SongFormat.get(this_format, 'prefix', 'canDisable', 'descriptionText', 'selectMode') page = QtGui.QWidget() page.setObjectName(prefix + 'Page') if can_disable: import_widget = self.disablable_widget(page, prefix) else: import_widget = page import_layout = QtGui.QVBoxLayout(import_widget) import_layout.setMargin(0) import_layout.setObjectName(prefix + 'ImportLayout') if description_text is not None: description_layout = QtGui.QHBoxLayout() description_layout.setObjectName(prefix + 'DescriptionLayout') description_spacer = QtGui.QSpacerItem(0, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) description_layout.addSpacerItem(description_spacer) description_label = QtGui.QLabel(import_widget) description_label.setWordWrap(True) description_label.setOpenExternalLinks(True) description_label.setObjectName(prefix + '_description_label') description_layout.addWidget(description_label) import_layout.addLayout(description_layout) self.format_widgets[this_format]['description_label'] = description_label self.format_widgets[this_format]['descriptionSpacer'] = description_spacer if select_mode == SongFormatSelect.SingleFile or select_mode == SongFormatSelect.SingleFolder: file_path_layout = QtGui.QHBoxLayout() file_path_layout.setObjectName(prefix + '_file_path_layout') file_path_layout.setContentsMargins(0, self.format_v_spacing, 0, 0) file_path_label = QtGui.QLabel(import_widget) file_path_label.setObjectName(prefix + 'FilepathLabel') file_path_layout.addWidget(file_path_label) file_path_spacer = QtGui.QSpacerItem(0, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) file_path_layout.addSpacerItem(file_path_spacer) file_path_edit = QtGui.QLineEdit(import_widget) file_path_edit.setObjectName(prefix + '_file_path_edit') file_path_layout.addWidget(file_path_edit) browse_button = QtGui.QToolButton(import_widget) browse_button.setIcon(self.open_icon) browse_button.setObjectName(prefix + 'BrowseButton') file_path_layout.addWidget(browse_button) import_layout.addLayout(file_path_layout) import_layout.addSpacerItem(self.stack_spacer) self.format_widgets[this_format]['filepathLabel'] = file_path_label self.format_widgets[this_format]['filepathSpacer'] = file_path_spacer self.format_widgets[this_format]['file_path_layout'] = file_path_layout self.format_widgets[this_format]['file_path_edit'] = file_path_edit self.format_widgets[this_format]['browseButton'] = browse_button elif select_mode == SongFormatSelect.MultipleFiles: file_list_widget = QtGui.QListWidget(import_widget) file_list_widget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) file_list_widget.setObjectName(prefix + 'FileListWidget') import_layout.addWidget(file_list_widget) button_layout = QtGui.QHBoxLayout() button_layout.setObjectName(prefix + '_button_layout') add_button = QtGui.QPushButton(import_widget) add_button.setIcon(self.open_icon) add_button.setObjectName(prefix + 'AddButton') button_layout.addWidget(add_button) button_layout.addStretch() remove_button = QtGui.QPushButton(import_widget) remove_button.setIcon(self.delete_icon) remove_button.setObjectName(prefix + 'RemoveButton') button_layout.addWidget(remove_button) import_layout.addLayout(button_layout) self.format_widgets[this_format]['file_list_widget'] = file_list_widget self.format_widgets[this_format]['button_layout'] = button_layout self.format_widgets[this_format]['addButton'] = add_button self.format_widgets[this_format]['removeButton'] = remove_button self.format_stack.addWidget(page) self.format_widgets[this_format]['page'] = page self.format_widgets[this_format]['importLayout'] = import_layout self.format_combo_box.addItem('')
def add_file_select_item(self): """ Add a file selection page. """ this_format = self.current_format prefix, can_disable, description_text, select_mode = \ SongFormat.get(this_format, 'prefix', 'canDisable', 'descriptionText', 'selectMode') page = QtWidgets.QWidget() page.setObjectName(prefix + 'Page') if can_disable: import_widget = self.disablable_widget(page, prefix) else: import_widget = page import_layout = QtWidgets.QVBoxLayout(import_widget) import_layout.setContentsMargins(0, 0, 0, 0) import_layout.setObjectName(prefix + 'ImportLayout') if description_text is not None: description_layout = QtWidgets.QHBoxLayout() description_layout.setObjectName(prefix + 'DescriptionLayout') description_spacer = QtWidgets.QSpacerItem( 0, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) description_layout.addSpacerItem(description_spacer) description_label = QtWidgets.QLabel(import_widget) description_label.setWordWrap(True) description_label.setOpenExternalLinks(True) description_label.setObjectName(prefix + '_description_label') description_layout.addWidget(description_label) import_layout.addLayout(description_layout) self.format_widgets[this_format][ 'description_label'] = description_label self.format_widgets[this_format][ 'descriptionSpacer'] = description_spacer if select_mode == SongFormatSelect.SingleFile or select_mode == SongFormatSelect.SingleFolder: file_path_layout = QtWidgets.QHBoxLayout() file_path_layout.setObjectName(prefix + '_file_path_layout') file_path_layout.setContentsMargins(0, self.format_v_spacing, 0, 0) file_path_label = QtWidgets.QLabel(import_widget) file_path_label.setObjectName(prefix + 'FilepathLabel') file_path_layout.addWidget(file_path_label) file_path_spacer = QtWidgets.QSpacerItem( 0, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) file_path_layout.addSpacerItem(file_path_spacer) file_path_edit = QtWidgets.QLineEdit(import_widget) file_path_edit.setObjectName(prefix + '_file_path_edit') file_path_layout.addWidget(file_path_edit) browse_button = QtWidgets.QToolButton(import_widget) browse_button.setIcon(self.open_icon) browse_button.setObjectName(prefix + 'BrowseButton') file_path_layout.addWidget(browse_button) import_layout.addLayout(file_path_layout) import_layout.addSpacerItem(self.stack_spacer) self.format_widgets[this_format]['filepathLabel'] = file_path_label self.format_widgets[this_format][ 'filepathSpacer'] = file_path_spacer self.format_widgets[this_format][ 'file_path_layout'] = file_path_layout self.format_widgets[this_format]['file_path_edit'] = file_path_edit self.format_widgets[this_format]['browseButton'] = browse_button elif select_mode == SongFormatSelect.MultipleFiles: file_list_widget = QtWidgets.QListWidget(import_widget) file_list_widget.setSelectionMode( QtWidgets.QAbstractItemView.ExtendedSelection) file_list_widget.setObjectName(prefix + 'FileListWidget') import_layout.addWidget(file_list_widget) button_layout = QtWidgets.QHBoxLayout() button_layout.setObjectName(prefix + '_button_layout') add_button = QtWidgets.QPushButton(import_widget) add_button.setIcon(self.open_icon) add_button.setObjectName(prefix + 'AddButton') button_layout.addWidget(add_button) button_layout.addStretch() remove_button = QtWidgets.QPushButton(import_widget) remove_button.setIcon(self.delete_icon) remove_button.setObjectName(prefix + 'RemoveButton') button_layout.addWidget(remove_button) import_layout.addLayout(button_layout) self.format_widgets[this_format][ 'file_list_widget'] = file_list_widget self.format_widgets[this_format]['button_layout'] = button_layout self.format_widgets[this_format]['addButton'] = add_button self.format_widgets[this_format]['removeButton'] = remove_button self.format_stack.addWidget(page) self.format_widgets[this_format]['page'] = page self.format_widgets[this_format]['importLayout'] = import_layout self.format_combo_box.addItem('')
def importSongs(self, format, **kwargs): class_ = SongFormat.get(format, 'class') importer = class_(self.manager, **kwargs) importer.register(self.media_item.import_wizard) return importer
def add_file_select_item(self): """ Add a file selection page. """ this_format = self.current_format format_name, prefix, can_disable, description_text, select_mode, filters = \ SongFormat.get(this_format, 'name', 'prefix', 'canDisable', 'descriptionText', 'selectMode', 'filter') page = QtWidgets.QWidget() page.setObjectName(prefix + 'Page') if can_disable: import_widget = self.disablable_widget(page, prefix) else: import_widget = page import_layout = QtWidgets.QVBoxLayout(import_widget) import_layout.setContentsMargins(0, 0, 0, 0) import_layout.setObjectName(prefix + 'ImportLayout') if description_text is not None: description_layout = QtWidgets.QHBoxLayout() description_layout.setObjectName(prefix + 'DescriptionLayout') description_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) description_layout.addSpacerItem(description_spacer) description_label = QtWidgets.QLabel(import_widget) description_label.setWordWrap(True) description_label.setOpenExternalLinks(True) description_label.setObjectName(prefix + '_description_label') description_layout.addWidget(description_label) import_layout.addLayout(description_layout) self.format_widgets[this_format]['description_label'] = description_label self.format_widgets[this_format]['descriptionSpacer'] = description_spacer if select_mode == SongFormatSelect.SingleFile or select_mode == SongFormatSelect.SingleFolder: file_path_layout = QtWidgets.QHBoxLayout() file_path_layout.setObjectName(prefix + '_file_path_layout') file_path_label = QtWidgets.QLabel(import_widget) file_path_layout.addWidget(file_path_label) if select_mode == SongFormatSelect.SingleFile: path_type = PathEditType.Files dialog_caption = WizardStrings.OpenTypeFile.format(file_type=format_name) else: path_type = PathEditType.Directories dialog_caption = WizardStrings.OpenTypeFolder.format(folder_name=format_name) path_edit = PathEdit( parent=import_widget, path_type=path_type, dialog_caption=dialog_caption, show_revert=False) if path_edit.filters: path_edit.filters = filters + ';;' + path_edit.filters else: path_edit.filters = filters path_edit.path = self.settings.value(self.plugin.settings_section + '/last directory import') file_path_layout.addWidget(path_edit) import_layout.addLayout(file_path_layout) import_layout.addSpacerItem(self.stack_spacer) self.format_widgets[this_format]['filepathLabel'] = file_path_label self.format_widgets[this_format]['path_edit'] = path_edit elif select_mode == SongFormatSelect.MultipleFiles: file_list_widget = QtWidgets.QListWidget(import_widget) file_list_widget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) file_list_widget.setObjectName(prefix + 'FileListWidget') import_layout.addWidget(file_list_widget) button_layout = QtWidgets.QHBoxLayout() button_layout.setObjectName(prefix + '_button_layout') add_button = QtWidgets.QPushButton(import_widget) add_button.setIcon(self.open_icon) add_button.setObjectName(prefix + 'AddButton') button_layout.addWidget(add_button) button_layout.addStretch() remove_button = QtWidgets.QPushButton(import_widget) remove_button.setIcon(self.delete_icon) remove_button.setObjectName(prefix + 'RemoveButton') button_layout.addWidget(remove_button) import_layout.addLayout(button_layout) self.format_widgets[this_format]['file_list_widget'] = file_list_widget self.format_widgets[this_format]['button_layout'] = button_layout self.format_widgets[this_format]['addButton'] = add_button self.format_widgets[this_format]['removeButton'] = remove_button self.format_stack.addWidget(page) self.format_widgets[this_format]['page'] = page self.format_widgets[this_format]['importLayout'] = import_layout self.format_combo_box.addItem('')
def do_import(self): """ Receive either a list of files or a folder (unicode) to import. """ from openlp.plugins.songs.lib.importer import SongFormat ps_string = SongFormat.get(SongFormat.PowerSong, 'name') if isinstance(self.import_source, str): if os.path.isdir(self.import_source): dir = self.import_source self.import_source = [] for file in os.listdir(dir): if fnmatch.fnmatch(file, '*.song'): self.import_source.append(os.path.join(dir, file)) else: self.import_source = '' if not self.import_source or not isinstance(self.import_source, list): self.log_error(translate('SongsPlugin.PowerSongImport', 'No songs to import.'), translate('SongsPlugin.PowerSongImport', 'No {text} files found.').format(text=ps_string)) return self.import_wizard.progress_bar.setMaximum(len(self.import_source)) for file in self.import_source: if self.stop_import_flag: return self.set_defaults() parse_error = False with open(file, 'rb') as song_data: while True: try: label = self._read_string(song_data) if not label: break field = self._read_string(song_data) except ValueError: parse_error = True self.log_error(os.path.basename(file), translate('SongsPlugin.PowerSongImport', 'Invalid {text} file. Unexpected byte value.').format(text=ps_string)) break else: if label == 'TITLE': self.title = field.replace('\n', ' ') elif label == 'AUTHOR': self.parse_author(field) elif label == 'COPYRIGHTLINE': found_copyright = True self._parse_copyright_cCCLI(field) elif label == 'PART': self.add_verse(field) if parse_error: continue # Check that file had TITLE field if not self.title: self.log_error(os.path.basename(file), translate('SongsPlugin.PowerSongImport', 'Invalid {text} file. Missing "TITLE" header.').format(text=ps_string)) continue # Check that file had COPYRIGHTLINE label if not found_copyright: self.log_error(self.title, translate('SongsPlugin.PowerSongImport', 'Invalid {text} file. Missing "COPYRIGHTLINE" header.').format(text=ps_string)) continue # Check that file had at least one verse if not self.verses: self.log_error(self.title, translate('SongsPlugin.PowerSongImport', 'Verses not found. Missing "PART" header.')) continue if not self.finish(): self.log_error(self.title)
def importSongs(self, format, **kwargs): class_ = SongFormat.get(format, u'class') kwargs[u'plugin'] = self importer = class_(self.manager, **kwargs) importer.register(self.mediaItem.importWizard) return importer