def copy_from_camera(self, rpd_file: RPDFile) -> bool: try: src_bytes = self.camera.save_file_by_chunks( dir_name=rpd_file.path, file_name=rpd_file.name, size=rpd_file.size, dest_full_filename=rpd_file.temp_full_file_name, progress_callback=self.update_progress, check_for_command=self.check_for_controller_directive, return_file_bytes=self.verify_file) except CameraProblemEx as e: name = rpd_file.name uri = rpd_file.get_uri() if e.gp_code in (gp.GP_ERROR_IO_USB_FIND, gp.GP_ERROR_BAD_PARAMETERS): self.terminate_camera_removed() elif e.code == CameraErrorCode.read: self.problems.append( CameraFileReadProblem(name=name, uri=uri, gp_code=e.gp_code)) else: assert e.code == CameraErrorCode.write self.problems.append( FileWriteProblem(name=name, uri=uri, exception=e.py_exception)) return False if self.verify_file: rpd_file.md5 = hashlib.md5(src_bytes).hexdigest() return True
def copy_from_filesystem(self, source: str, destination: str, rpd_file: RPDFile) -> bool: src_chunks = [] try: self.dest = io.open(destination, 'wb', self.io_buffer) self.src = io.open(source, 'rb', self.io_buffer) total = rpd_file.size amount_downloaded = 0 while True: # first check if process is being stopped or paused self.check_for_controller_directive() chunk = self.src.read(self.io_buffer) if chunk: self.dest.write(chunk) if self.verify_file: src_chunks.append(chunk) amount_downloaded += len(chunk) self.update_progress(amount_downloaded, total) else: break self.dest.close() self.src.close() if self.verify_file: src_bytes = b''.join(src_chunks) rpd_file.md5 = hashlib.md5(src_bytes).hexdigest() return True except (OSError, FileNotFoundError, PermissionError) as e: self.problems.append( FileCopyProblem(name=os.path.basename(source), uri=get_uri(full_file_name=source), exception=e)) try: msg = '%s: %s' % (e.errno, e.strerror) except AttributeError: msg = str(e) logging.error("%s. Failed to copy %s to %s", msg, source, destination) return False except Exception as e: self.problems.append( FileCopyProblem(name=os.path.basename(source), uri=get_uri(full_file_name=source), exception=e)) try: msg = '%s: %s' % (e.errno, e.strerror) except AttributeError: msg = str(e) logging.error("Unexpected error: %s. Failed to copy %s to %s", msg, source, destination) return False