def chdir(self, path): '''Change the current working directory to path raises DocException if the path doesn't exist ''' path = self._norm_path(path) while path.startswith('..'): self._cwd = self._cwd.rsplit('/', 1)[0] if not self._cwd: # trying to traverse past root self._cwd = '/' head, path = os.path.split(path) #print 'head "%s" path "%s' % (head, path) if not head: break #print 'path after: "%s", cwd: "%s"' % (path, self._cwd) if not path or '..' == path: return norm_path = self._norm_path(path) L.debug("chdir: %s, normalized: %s", path, norm_path) try: if self._access.exists(norm_path): self._cwd = norm_path else: raise DocException('directory does not exist') except KeyError: raise DocException('directory does not exist')
def set_metadata(self, path, metadata_dict): '''set the metadata on path''' entity = self._get_entity_by_path(path) if entity is None: raise DocException('Path does not exist: %s' % path) return self._set_metadata(entity, metadata_dict)
def set_standard_attr(self, path, attr_dict): '''set the standard attributes on path''' entity = self._get_entity_by_path(path) if entity is None: raise DocException('Path does not exist: %s' % path) return self._set_standard_attr(entity, attr_dict)
def get_standard_attr(self, path): '''get the standard attributes for a path''' entity = self._get_entity_by_path(path) if entity is None: raise DocException('Path does not exist: %s' % path) return dict( (n, getattr(entity, n)) for n in entity.swaggerTypes.keys())
def _upload_content(self, content, dst, mimetype, st_attr): '''upload the content Args: content(string or open file): follows the same conventions as HTTPConnection.request dst(string path): on the server, including the /project/folder/...folders/file mimetype(str): set the _contentType property to this mimetype st_attr(dict): standard attributes Returns: uuid of created entity ''' parent, entity = self._create_placeholder(dst, mimetype, st_attr) content_url = joinp(self.host, 'file', entity._uuid, 'content/upload') headers = copy.copy(self._get_headers()) resp = requests.post(content_url, headers=headers, data=content) if 201 != resp.status_code: raise DocException('Could not upload file (%s): %s' % (resp.status_code, resp.text)) response_obj = json.loads(resp.text) entity = self._api.deserialize(response_obj, EntityReturn.EntityReturn) #add entity to cache, since everything was succesful self._add_to_cache(parent, (entity, )) return entity._uuid
def download_file_by_id(self, _id, dst_path=None): '''download a file from the server Args: id(string): the id of the file entity dst_path: the path to store the downloaded contents Returns: path to the file if dst_path was provided contents of the file as a string otherwise ''' content_url = joinp(self.host, 'file', _id, 'content/download') resp = requests.get(content_url, headers=self._get_headers()) if 200 != resp.status_code: raise DocException('Could not download file (%s): %s' % (resp.status_code, resp.text)) if dst_path: CHUNK_SIZE = 10 * 1024 with open(dst_path, 'wb') as f: for chunk in resp.iter_content(CHUNK_SIZE): f.write(chunk) return dst_path else: return resp.text
def get_project_by_collab_id(self, collab_id): '''return project managed by collab''' projects = self._access.filter_projects('managed_by_collab=%s' % collab_id) if len(projects) > 1: raise DocException('More than one project exists for collab %s' % collab_id) if projects: return dict((n, getattr(projects[0], n)) for n in projects[0].swaggerTypes.keys()) return None
def _get_children(self, entity): '''get the contents of a folder/project from the server''' if self.isroot(entity): children = DocAccess._get_full_list(self._project.get_all_projects, {}) elif self.isproject(entity): children = DocAccess._get_full_list( self._project.get_entity_children, {'uuid': entity._uuid}) elif self.isfolder(entity): children = DocAccess._get_full_list( self._folder.get_entity_children, {'uuid': entity._uuid}) elif self.isfile(entity): raise DocException('file has no children') else: raise DocException('Received unknown type from server: %s' % entity._entityType) self._add_to_cache(entity, children) return children
def rename(self, src, dst): '''rename project/dir/file, analagous to os.rename''' if src.rsplit('/', 1)[0] != dst.rsplit('/', 1)[0]: raise DocException('can only rename the last level of the path') entity = self._get_entity_by_path(src) if entity is None: raise OSError('Path does not exist: %s' % src) _uuid = entity._uuid body = copy.copy(entity) name = dst.rsplit('/', 1)[1] body._name = name parent = body._parent del body._createdOn del body._uuid del body._entityType del body._createdBy del body._contentUri del body._contentType del body._description if self.isproject(entity): del body._parent body._name = body._name.lstrip('/') self._project.update_project(_uuid, body) elif self.isfolder(entity): self._folder.update_folder(_uuid, body) elif self.isfile(entity): self._file.update_file(_uuid, body) else: raise DocException('Cannot rename %s' % entity._entityType) entity._name = name self._remove_from_cache(entity) self._add_to_cache(parent, (entity, ))
def get_metadata(self, path): '''get the metadata for a path''' entity = self._get_entity_by_path(path) if entity is None: raise DocException('Path does not exist: %s' % path) return self._get_metadata(entity)