def trash(self, system, file_path, trash_path): name = os.path.basename(file_path) f = BaseFileResource(self._ag, system, file_path) # first ensure trash_path exists BaseFileResource.ensure_path(self._ag, system, trash_path) # check if file with same name exists in trash; expect 404 try: check = os.path.join(trash_path, name) BaseFileResource.listing(self._ag, system, check) # if we did not 404, then we have a conflict; append date to name name_ext = os.path.splitext(name) timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') name = '{0} {1}{2}'.format(name_ext[0], timestamp, name_ext[1]) except HTTPError as e: if e.response.status_code != 404: raise resp = f.move(trash_path, name) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) parent_path = parent_path.strip('/') or '/' reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, trash_path), 'levels': 1}) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) return resp
def rename(self, system, file_path, rename_to): f = BaseFileResource.listing(self._ag, system, file_path) resp = f.rename(rename_to) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) return resp
def project_directory(self): """ Queries for the File object that represents the root of this Project's files. :return: The project's root dir :rtype: :class:`BaseFileResource` """ if self._project_directory is None: self._project_directory = BaseFileResource.listing( system=self.project_system_id, path='/', agave_client=self._agave) return self._project_directory
def import_data(self, system, file_path, from_system, from_file_path): file_path = file_path or '/' if file_path != '/': file_path = file_path.strip('/') from_file_path = from_file_path.strip('/') f = BaseFileResource.listing(self._ag, system, file_path) res = f.import_data(from_system, from_file_path) file_name = from_file_path.split('/')[-1] reindex_agave.apply_async(kwargs={'username': '******', 'file_id': '{}/{}'.format(system, os.path.join(file_path, file_name))}) return res
def get(self, request, project_id, file_path=''): """ :return: The root directory for the Project's data :rtype: JsonResponse """ ag = request.user.agave_oauth.client p = Project(ag, uuid=project_id) list_path = '/'.join([project_id, file_path]) listing = BaseFileResource.listing(ag, p.project_system_id, list_path) return JsonResponse(listing, encoder=AgaveJSONEncoder, safe=False)
def move(self, system, file_path, dest_path, dest_name=None): f = BaseFileResource.listing(self._ag, system, file_path) resp = f.move(dest_path, dest_name) parent_path = '/'.join(file_path.strip('/').split('/')[:-1]) parent_path = parent_path.strip('/') or '/' reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, parent_path), 'levels': 1}) reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}'.format(system, os.path.join(dest_path, resp.name)), 'levels': 1}) return resp
def copy(self, system, file_path, dest_path=None, dest_name=None): f = BaseFileResource.listing(self._ag, system, file_path) # default to same path if dest_path is None: dest_path = f.path # default to same name if dest_name is None: dest_name = f.name # if same path and name, add suffix to file name if dest_name == f.name and dest_path == f.path: dest_name = '{0}_copy{1}'.format(*os.path.splitext(dest_name)) copied_file = f.copy(dest_path, dest_name) # schedule celery task to index new copy reindex_agave.apply_async(kwargs = {'username': '******', 'file_id': '{}/{}/{}'.format(system, dest_path.strip('/'), dest_name)}) return copied_file
def project_data_listing(self, path='/'): return BaseFileResource.listing(system=self.project_system_id, path=path, agave_client=self._agave)
def listing(self, system, file_path, offset=0, limit=100): return BaseFileResource.listing(self._ag, system, file_path, offset, limit)
def download(self, system, path): file_obj = BaseFileResource.listing(self._ag, system, path) postit = file_obj.download_postit() return postit