def handle(self): try: size = os.path.getsize(self.local_path) with open(self.local_path, 'rb') as f: item = self.drive.upload_file( filename=self.item_name, data=f, size=size, parent_path=self.remote_parent_path, conflict_behavior=self._conflict_behavior) modified_time = timestamp_to_datetime( os.path.getmtime(self.local_path)) fs_info = facets.FileSystemInfoFacet( modified_time=modified_time) item = self.drive.update_item(item_id=item.id, new_file_system_info=fs_info) self.items_store.update_item(item, ItemRecordStatuses.OK) self.logger.info('Uploaded file "%s".', self.local_path) except (IOError, OSError) as e: self.logger.error('IO error when uploading "%s": %s.', self.local_path, e) except errors.OneDriveError as e: self.logger.error('API error when uploading "%s": %s.', self.local_path, e) self.task_pool.clear_hold(self)
def test_handle(self): with mock.patch( 'onedrived.api.facets.FileSystemInfoFacet') as mock_class: self.task.handle() mock_class.assert_called_once_with( modified_time=timestamp_to_datetime(self.new_mtime)) self.assertEqual( 1, len(self.task.items_store.get_items_by_id(item_id=self.item.id)))
def handle(self): try: size = os.path.getsize(self.local_path) with open(self.local_path, 'rb') as f: item = self.drive.upload_file( filename=self.item_name, data=f, size=size, parent_path=self.remote_parent_path, conflict_behavior=self._conflict_behavior) modified_time = timestamp_to_datetime(os.path.getmtime(self.local_path)) fs_info = facets.FileSystemInfoFacet(modified_time=modified_time) item = self.drive.update_item(item_id=item.id, new_file_system_info=fs_info) self.items_store.update_item(item, ItemRecordStatuses.OK) self.logger.info('Uploaded file "%s".', self.local_path) except (IOError, OSError) as e: self.logger.error('IO error when uploading "%s": %s.', self.local_path, e) except errors.OneDriveError as e: self.logger.error('API error when uploading "%s": %s.', self.local_path, e)
def __init__(self, parent_task, rel_parent_path, item_name, new_mtime): super().__init__(parent_task, rel_parent_path, item_name, None) if isinstance(new_mtime, int): new_mtime = timestamp_to_datetime(new_mtime) self._new_mtime = new_mtime
def test_handle(self): with mock.patch("onedrived.api.facets.FileSystemInfoFacet") as mock_class: self.task.handle() mock_class.assert_called_once_with(modified_time=timestamp_to_datetime(self.new_mtime)) self.assertEqual(1, len(self.task.items_store.get_items_by_id(item_id=self.item.id)))