-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathredirect.py
54 lines (41 loc) · 1.46 KB
/
redirect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import htmlunit
from lazyproperty import lazyproperty
from ignore_urls import filterIgnoreUrlParts
from vectors import urlvector
from link import Link, AbstractLink, Links
class Redirect(Link):
@lazyproperty
def location(self):
location = self.internal
location = filterIgnoreUrlParts(location)
return location
def __str__(self):
return "Redirect(%s)" % (self.location)
@property
def linkvector(self):
return urlvector(htmlunit.URL(self.reqresp.request.webrequest.getUrl(), self.location))
@lazyproperty
def dompath(self):
return "REDIRECT"
class AbstractRedirect(AbstractLink):
def __init__(self, redirects):
if not isinstance(redirects, list):
redirects = list(redirects)
AbstractLink.__init__(self, redirects)
self.locations = set(i.location for i in redirects)
self.type = Links.Type.REDIRECT
def update(self, redirects):
self.locations = set(i.location for i in redirects)
@property
def _str(self):
return "AbstractRedirect(%s, targets=%s)" % (self.locations, self.targets)
def equals(self, a):
return self.locations == a.locations
@lazyproperty
def hasquery(self):
return any(i.find('?') != -1 for i in self.locations)
@lazyproperty
def location(self):
# XXX multiple hrefs not supported yet
assert len(self.locations) == 1
return iter(self.locations).next()