diff options
Diffstat (limited to 'module/plugins/hooks/MergeFiles.py')
-rw-r--r-- | module/plugins/hooks/MergeFiles.py | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/module/plugins/hooks/MergeFiles.py b/module/plugins/hooks/MergeFiles.py index e6f8bb26f..4de45f958 100644 --- a/module/plugins/hooks/MergeFiles.py +++ b/module/plugins/hooks/MergeFiles.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from __future__ import with_statement + import os import re @@ -12,7 +14,7 @@ from module.utils import save_join, fs_encode class MergeFiles(Hook): __name__ = "MergeFiles" __type__ = "hook" - __version__ = "0.12" + __version__ = "0.13" __config__ = [("activated", "bool", "Activated", True)] @@ -24,6 +26,11 @@ class MergeFiles(Hook): BUFFER_SIZE = 4096 + #@TODO: Remove in 0.4.10 + def initPeriodical(self): + pass + + def setup(self): # nothing to do pass @@ -48,32 +55,35 @@ class MergeFiles(Hook): for name, file_list in files.iteritems(): self.logInfo(_("Starting merging of"), name) - final_file = open(save_join(download_folder, name), "wb") - - for splitted_file in file_list: - self.logDebug("Merging part", splitted_file) - pyfile = self.core.files.getFile(fid_dict[splitted_file]) - pyfile.setStatus("processing") - try: - s_file = open(os.path.join(download_folder, splitted_file), "rb") - size_written = 0 - s_file_size = int(os.path.getsize(os.path.join(download_folder, splitted_file))) - while True: - f_buffer = s_file.read(self.BUFFER_SIZE) - if f_buffer: - final_file.write(f_buffer) - size_written += self.BUFFER_SIZE - pyfile.setProgress((size_written * 100) / s_file_size) - else: - break - s_file.close() - self.logDebug("Finished merging part", splitted_file) - except Exception, e: - print_exc() - finally: - pyfile.setProgress(100) - pyfile.setStatus("finished") - pyfile.release() - - final_file.close() + + with open(save_join(download_folder, name), "wb") as final_file: + for splitted_file in file_list: + self.logDebug("Merging part", splitted_file) + + pyfile = self.core.files.getFile(fid_dict[splitted_file]) + + pyfile.setStatus("processing") + + try: + with open(os.path.join(download_folder, splitted_file), "rb") as s_file: + size_written = 0 + s_file_size = int(os.path.getsize(os.path.join(download_folder, splitted_file))) + while True: + f_buffer = s_file.read(self.BUFFER_SIZE) + if f_buffer: + final_file.write(f_buffer) + size_written += self.BUFFER_SIZE + pyfile.setProgress((size_written * 100) / s_file_size) + else: + break + self.logDebug("Finished merging part", splitted_file) + + except Exception, e: + print_exc() + + finally: + pyfile.setProgress(100) + pyfile.setStatus("finished") + pyfile.release() + self.logInfo(_("Finished merging of"), name) |