From 571b50f016e4bb7df450ec89cd51fc1618461b13 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Sat, 15 Nov 2014 22:38:36 +0000 Subject: [PATCH] Fix for overwriting files on song export. Fixes bug 1216232 Fixes: https://launchpad.net/bugs/1216232 --- openlp/plugins/songs/lib/openlyricsexport.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/openlp/plugins/songs/lib/openlyricsexport.py b/openlp/plugins/songs/lib/openlyricsexport.py index 0458b893b..84647dbee 100644 --- a/openlp/plugins/songs/lib/openlyricsexport.py +++ b/openlp/plugins/songs/lib/openlyricsexport.py @@ -75,9 +75,14 @@ class OpenLyricsExport(RegistryProperties): 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 = '%s.xml' % filename[0:250 - len(self.save_path)] + 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), 'wb'), encoding='utf-8', xml_declaration=True, - pretty_print=True) + tree.write(open(os.path.join(self.save_path, filename_with_ext), 'wb'), encoding='utf-8', + xml_declaration=True, pretty_print=True) return True