public
Description: Google's Webkit, ported to Python, ported to Desktops.
Homepage: http://lkcl.net/pyjamas-desktop
Clone URL: git://github.com/lkcl/pyjamas-desktop.git
100644 99 lines (73 sloc) 2.704 kb
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pyjslib
 
def makeUrlDict(s):
    dict = {}
    pairs = s.split("&")
    for pair in pairs:
        if len(pair) < 3: continue
        kv = pair.split("=",1)
        dict[kv[0]] = kv[1]
    return dict
 
def makeUrlStringFromDict(d):
    pairs = []
    for k,v in d.iteritems():
        pairs.append(k+"="+v)
    return "&".join(pairs)
 
class Location:
    """
Retrieve this class by calling Window.getLocation().
This provides a pyjs wrapper for the current location,
with some utility methods for convenience.
"""
    def __init__(self, location):
        self.location = location
        self.searchDict = None
    
    def getHash(self):
        return self.location.props.hash
    
    def getHashDict(self):
        if not self.hashDict or self.hashDictHash != self.getHash():
            self.hashDictHash = self.getHash()
            self.hashDict = makeUrlDict(self.getHash()[1:])
        return self.hashDict
    
    def getHost(self):
        return self.location.props.host
    
    def getHostname(self):
        return self.location.props.hostname
    
    def getHref(self):
        return self.location.props.href
    
    def getPageHref(self):
        """
Return href with any search or hash stripped
"""
        href = self.location.props.href
        if href.find("?"): href = href.split("?")[0]
        if href.find("#"): href = href.split("#")[0]
        return href
    
    def getPathname(self):
        return self.location.props.pathname
    
    def getPort(self):
        return self.location.props.port
    
    def getProtocol(self):
        return self.location.props.protocol
        
    def getSearch(self):
        return ""+self.location.props.search
    
    def getSearchDict(self):
        if pyjslib.isString(self.location):
            return {}
        if not self.searchDict:
            self.searchDict = makeUrlDict(self.getSearch()[1:])
        return self.searchDict
 
    def getSearchVar(self, key):
        searchDict = self.getSearchDict()
        return searchDict[key]
    
    def reload(self):
        self.location.props.reload()
        
    def setHref(self, href):
        self.location.props.href = href
 
    def setSearch(self, search):
        self.location.props.search = search
        
    def setSearchDict(self, searchDict):
        self.setSearch(makeUrlStringFromDict(searchDict))
        
    def setHash(self, hash):
        self.location.props.hash = hash
        
    def setHashDict(self, hashDict):
        self.setHash(makeUrlStringFromDict(hashDict))