def check_if_forbidden(path, raise_error=True): """Raise an HTTPForbidden if there's any exception accessing file/dir""" # check if there are any exception try: if vfs.isdir(path): vfs.listdir(path) else: vfs.open(path).close() except FSError, e: if raise_error: ##FIXME: explanation too verbose? raise exc.HTTPForbidden(explanation=str(e)) else: return True
def isdir_alone(path): """ Returns true when path is a directory without subdirectories """ if check_if_forbidden(path, raise_error=False): return True child_dirs = [elem for elem in vfs.listdir(path) if vfs.isdir(path_join(path, elem))] if len(child_dirs) > 0: return False else: return True
def gen_prop_tree(self, props, only_names=False): XMLroot = ET.Element("{DAV:}multistatus") XMLresp = ET.SubElement(XMLroot, "{DAV:}response") # depth==0: only this resource resources = [self.path] # depth==1: add resources of the collection if self.depth == "1": ##FIXME: fix unicode (most errors here!) resources.extend([path_join(self.path, res) for res in vfs.listdir(self.path)]) for res in resources: XMLres_href = ET.SubElement(XMLresp, "{DAV:}href") XMLres_href.text = res XMLres_propstat = ET.SubElement(XMLresp, "{DAV:}propstat") XMLres_propstat_prop = ET.SubElement(XMLres_propstat, "{DAV:}prop") res_info = vfs.getinfo(res) if "{DAV:}creationdate" in props: prop = ET.SubElement(XMLres_propstat_prop, "{DAV:}creationdate") if not only_names: prop.text = res_info["created_time"].strftime("%Y-%m-%dT%H:%M:%SZ") if "{DAV:}getcontentlength" in props: prop = ET.SubElement(XMLres_propstat_prop, "{DAV:}getcontentlength") if not only_names: prop.text = str(res_info["size"]) if "{DAV:}getlastmodified" in props: prop = ET.SubElement(XMLres_propstat_prop, "{DAV:}getlastmodified") if not only_names: prop.text = res_info["modified_time"].strftime("%Y-%m-%dT%H:%M:%SZ") if "{DAV:}resourcetype" in props: restype = ET.SubElement(XMLres_propstat_prop, "{DAV:}resourcetype") if not only_names and vfs.isdir(res): ET.SubElement(restype, "{DAV:}collection") ##FIXME: add DAV:supportedlock and DAV:lockdiscovery XMLres_propstat_status = ET.SubElement(XMLres_propstat, "{DAV:}status") XMLres_propstat_status.text = "HTTP/1.1 200 OK" # XMLres_propstat_status = ET.SubElement(XMLres_propstat, '{DAV:}status') # XMLres_propstat_status.text = 'HTTP/1.1 403 Forbidden' # XMLres_propstat_responsedescription = ET.SubElement(XMLres_propstat, '{DAV:}responsedescription') # XMLres_propstat_responsedescription.text = 'There has been an access violation error.' return XMLroot