Better code, as suggested and a few suggested fixes as well.

This commit is contained in:
Mattias Põldaru 2011-03-09 09:17:58 +02:00
parent 25dbf271f6
commit c7a435951d
1 changed files with 66 additions and 76 deletions

View File

@ -415,84 +415,74 @@ class ServiceManager(QtGui.QWidget):
""" """
if not self.fileName(): if not self.fileName():
return self.saveFileAs() return self.saveFileAs()
else: path_file_name = unicode(self.fileName())
path_file_name = unicode(self.fileName()) (path, file_name) = os.path.split(path_file_name)
(path, file_name) = os.path.split(path_file_name) basename = file_name[0:file_name.rfind(u'.')-1]
basename = file_name[0:file_name.rfind(u'.')-1] service_file_name = basename + '.osd'
service_file_name = basename + '.osd' log.debug(u'ServiceManager.saveFile - %s' % path_file_name)
log.debug(u'ServiceManager.saveFile - %s' % path_file_name) SettingsManager.set_last_dir(self.mainwindow.serviceSettingsSection,
SettingsManager.set_last_dir(self.mainwindow.serviceSettingsSection, path)
path) service = []
service = [] write_list = []
zip = None total_size = 0
try: for item in self.serviceItems:
write_list = [] service.append({u'serviceitem':
total_size = 0 item[u'service_item'].get_service_repr()})
for item in self.serviceItems: if not item[u'service_item'].uses_file():
service.append({u'serviceitem': continue
item[u'service_item'].get_service_repr()}) for frame in item[u'service_item'].get_frames():
if item[u'service_item'].uses_file(): if item[u'service_item'].is_image():
for frame in item[u'service_item'].get_frames(): path_from = frame[u'path']
if item[u'service_item'].is_image(): else:
path_from = frame[u'path'] path_from = os.path.join(frame[u'path'], frame[u'title'])
else: # Only write a file once
path_from = os.path.join(frame[u'path'], if path_from in write_list:
frame[u'title']) continue
# Only write a file once file_size = os.path.getsize(path_from)
if not path_from in write_list: size_limit = 52428800 # 50MiB
file_size = os.path.getsize(path_from) if file_size > size_limit:
size_limit = 52428800 # 50MiB # File exeeds size_limit bytes, ask user
if file_size > size_limit: message = unicode(translate('OpenLP.ServiceManager',
# File exeeds size_limit bytes, ask user 'Do you want to include \n%.1f MB file "%s"\n'
message = unicode(translate( 'into the service file?\nThis may take some time.\n\n'
'OpenLP.ServiceManager', 'Do you want' 'Please note that you need to\ntake care of that file '
' to include \n%.1f MB file "%s"\n' 'yourself,\nif you leave it out.')) % \
'into the service file?\n' (file_size/1048576, os.path.split(path_from)[1])
'This may take some time.\n\n' ans = QtGui.QMessageBox.question(self.mainwindow,
'Please note that you need to\n' translate('OpenLP.ServiceManager', 'Including Large '
'take care of that file yourself,\n' 'File'), message, QtGui.QMessageBox.StandardButtons(
'if you leave it out.')) %\ QtGui.QMessageBox.Ok|QtGui.QMessageBox.Cancel),
(file_size/1048576, QtGui.QMessageBox.Ok)
os.path.split(path_from)[1]) if ans == QtGui.QMessageBox.Cancel:
ans = QtGui.QMessageBox.question( continue
self.mainwindow, write_list.append(path_from)
translate('OpenLP.ServiceManager', total_size += file_size
'Including Large File'), log.debug(u'ServiceManager.saveFile - ZIP contents size is %i bytes' %
message, total_size)
QtGui.QMessageBox.StandardButtons( service_content = cPickle.dumps(service)
QtGui.QMessageBox.Ok|\ # Usual Zip file cannot exceed 2GiB, file with Zip64 cannot be
QtGui.QMessageBox.Cancel), # extracted using unzip in UNIX.
QtGui.QMessageBox.Ok) allow_zip_64 = (total_size > 2147483648 + len(service_content))
if ans == QtGui.QMessageBox.Ok: log.debug(u'ServiceManager.saveFile - allowZip64 is %s' %
write_list.append(path_from) allow_zip_64)
total_size += file_size try:
else: zip = zipfile.ZipFile(path_file_name, 'w', zipfile.ZIP_STORED,
write_list.append(path_from) allow_zip_64)
total_size += file_size # First we add service contents.
log.debug(u'ServiceManager.saveFile - ZIP contents size is %i' # We save ALL filenames into ZIP using UTF-8.
' bytes' % total_size) zip.writestr(service_file_name.encode(u'utf-8'),
service_content = cPickle.dumps(service) service_content)
# Usual Zip file cannot exceed 2GiB, file with Zip64 cannot be # Finally add all the listed media files.
# extracted using unzip in UNIX. for path_from in write_list:
allow_zip_64 = (total_size > 2147483648 + len(service_content)) zip.write(path_from, path_from.encode(u'utf-8'))
log.debug(u'ServiceManager.saveFile - allowZip64 is %s' % except IOError:
allow_zip_64) log.exception(u'Failed to save service to disk')
zip = zipfile.ZipFile(path_file_name, 'w', zipfile.ZIP_STORED, finally:
allow_zip_64) zip.close()
# We first add service contents.
# We save ALL filenames into ZIP using UTF-8.
zip.writestr(service_file_name.encode(u'utf-8'),
service_content)
# Finally add all the listed media files.
for path_from in write_list:
zip.write(path_from, path_from.encode(u'utf-8'))
zip.close()
except IOError:
log.exception(u'Failed to save service to disk')
return False
self.mainwindow.addRecentFile(path_file_name) self.mainwindow.addRecentFile(path_file_name)
self.setModified(False) self.setModified(False)
return True return True
return False
def saveFileAs(self): def saveFileAs(self):
""" """