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 "{title}"...').format(title=song.title)) xml = open_lyrics.song_to_xml(song) tree = etree.ElementTree(etree.fromstring(xml.encode())) filename = '{title} ({author})'.format(title=song.title, author=', '.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 = '{name}.xml'.format(name=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 = '{name}-{extra}.xml'.format(name=filename[0:247 - len(self.save_path)], extra=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
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 "{title}"...').format(title=song.title)) xml = open_lyrics.song_to_xml(song) tree = etree.ElementTree(etree.fromstring(xml.encode())) filename = '{title} ({author})'.format(title=song.title, author=', '.join([author.display_name for author in song.authors])) filename = clean_filename(filename) # Ensure the filename isn't too long for some filesystems path_length = len(str(self.save_path)) filename_with_ext = '{name}.xml'.format(name=filename[0:250 - path_length]) # Make sure we're not overwriting an existing file conflicts = 0 while (self.save_path / filename_with_ext).exists(): conflicts += 1 filename_with_ext = '{name}-{extra}.xml'.format(name=filename[0:247 - path_length], extra=conflicts) # Pass a file object, because lxml does not cope with some special # characters in the path (see lp:757673 and lp:744337). with (self.save_path / filename_with_ext).open('wb') as out_file: tree.write(out_file, encoding='utf-8', xml_declaration=True, pretty_print=True) return True
def test_clean_filename(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 wanted_name == result, 'The file name should not contain any special characters.'
def test_clean_filename(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.')
def _setup(self, parent, **kwargs): """ Run some initial setup. This method is separate from __init__ in order to mock it out in tests. """ self.bible_plugin = parent self.session = None if 'path' not in kwargs: raise KeyError('Missing keyword argument "path".') self.path = kwargs['path'] if 'name' not in kwargs and 'file' not in kwargs: raise KeyError('Missing keyword argument "name" or "file".') if 'name' in kwargs: self.name = kwargs['name'] if not isinstance(self.name, str): self.name = str(self.name, 'utf-8') self.file_path = Path(clean_filename(self.name) + '.sqlite') if 'file' in kwargs: self.file_path = kwargs['file'] Manager.__init__(self, 'bibles', init_schema, self.file_path, upgrade) if self.session and 'file' in kwargs: self.get_name() self._is_web_bible = None
def _setup(self, parent, **kwargs): """ Run some initial setup. This method is separate from __init__ in order to mock it out in tests. """ 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".') 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']