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

View File

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