def cd(self, resource): """ Change our working dir to RESOURCE. Can be used as a contextmanager that returns us to whatever state we were previously in on exit. Arguments: - `resource`: str or Path Return: None Exceptions: None """ oldwd = self.wd self.wd = urlhelp.protocolise(resource) class HTTPCd(object): """ Define this class in a closure to implement the contextmanager protocol while remaining able to operate on SELF. """ def __enter__(zelf): return def __exit__(zelf, msg, val, tb): self.wd = oldwd return return HTTPCd()
def abspath(self, resource): """ Return an absolute path for RESOURCE Arguments: - `resource`: str or Path Return: str Exceptions: None """ return urlhelp.protocolise(resource)
def exists(self, resource): """ Predicate method to determine whether RESOURCE exists. Arguments: - `resource`: str or Path Return: bool Exceptions: None """ resp = requests.head(urlhelp.protocolise(resource)) return resp.status_code == 200
def open(self, resource): """ Return a file-like object that represents the contents of a HTTP resource Arguments: - `resource`: str or Path Return: File-Like object Exceptions: None """ url = urlhelp.protocolise(resource) resp = requests.get(url) flike = HTTPFlike(resp.content, url=url, headers=resp.headers) return flike