From 018e05ba3b9ce421ab31d0b624cd8581b3e323e7 Mon Sep 17 00:00:00 2001 From: Sebastien Pierre Date: Mon, 16 Jun 2014 17:56:48 -0400 Subject: [PATCH] Improved URL functions --- Sources/wwwclient/browse.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Sources/wwwclient/browse.py b/Sources/wwwclient/browse.py index bb68377..a18cd19 100644 --- a/Sources/wwwclient/browse.py +++ b/Sources/wwwclient/browse.py @@ -604,11 +604,28 @@ def status( self ): assert self.last(), "No transaction available." return self.last().status() - def url( self ): + def url( self, suffix=None ): """Returns the URL of the last page. This is an alias for `self.last().url()`""" - assert self.last(), "No transaction available." - return self.last().url() + if not suffix: + assert self.last(), "No transaction available." + return self.last().url() + else: + if "://" in suffix: + return suffix + elif suffix[0] == "/": + return "{0}{1}".format(self.rootUrl(), suffix) + else: + return "{0}/{1}".format(self.baseUrl(), suffix) + + def baseUrl( self ): + url = self.url() + return url.rsplit("/", 1)[0] + + def rootUrl( self ): + url = self.url() + proto, path = url.rsplit("://", 1) + return "{0}://{1}".format(proto, path.split("/",1)[0]) def form( self, name=None ): """Returns the first form declared in the last transaction response data."""