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 handle(self): try: fs_info = facets.FileSystemInfoFacet(modified_time=self._new_mtime) new_item = self.drive.update_item(item_path=self.remote_path, new_file_system_info=fs_info) self.items_store.update_item(new_item, ItemRecordStatuses.OK) except errors.OneDriveError as e: self.logger.error('Error occurred updating server mtime for entry "%s": %s', self.local_path, e)
def __init__(self, drive, data): """ :param onedrived.api.drives.DriveObject drive: The parent drive object. :param dict[str, str | int | dict[str, str | int | dict]] data: JSON response for an Item resource. """ self.drive = drive self._data = data if 'fileSystemInfo' in data: self._fs_info = facets.FileSystemInfoFacet(data['fileSystemInfo']) else: self._fs_info = None
def test_update_item(self): new_params = { 'item_id': '123', 'new_name': 'whatever.doc', 'new_description': 'This is a dummy description.', 'new_parent_reference': resources.ItemReference.build(drive_id='aaa', id='012'), 'new_file_system_info': facets.FileSystemInfoFacet( created_time=str_to_datetime('1971-01-01T02:03:04Z'), modified_time=str_to_datetime('2008-01-02T03:04:05.06Z')) } with requests_mock.Mocker() as mock: def callback(request, context): json = request.json() self.assertEqual(json['name'], new_params['new_name']) self.assertEqual(json['description'], new_params['new_description']) self.assertDictEqual(json['parentReference'], new_params['new_parent_reference'].data) self.assertDictEqual(json['fileSystemInfo'], new_params['new_file_system_info'].data) return get_data('image_item.json') mock.patch(self.drive.get_item_uri(new_params['item_id'], None), json=callback) self.drive.update_item(**new_params)
def assert_timestamp(self, attr_name, dict_key, time_str): obj = facets.FileSystemInfoFacet( **{attr_name: str_to_datetime(time_str)}) self.assertEqual(str_to_datetime(time_str), getattr(obj, attr_name, None)) self.assertEqual(time_str, obj.data[dict_key])
def setUp(self): self.data = get_data('facets/filesysteminfo_facet.json') self.facet = facets.FileSystemInfoFacet(self.data)