PageRenderTime 1ms CodeModel.GetById 300ms app.highlight 7ms RepoModel.GetById 103ms app.codeStats 0ms

/library/Location.py

http://pyjamas.googlecode.com/
Python | 93 lines | 71 code | 12 blank | 10 comment | 10 complexity | 47d9cee2fe4595b43f9982ffa6724858 MD5 | raw file
 1def makeUrlDict(s):
 2    dict = {}
 3    pairs = s.split("&")
 4    for pair in pairs:
 5        if len(pair) < 3: continue
 6        kv = pair.split("=",1)
 7        dict[kv[0]] = kv[1]
 8    return dict
 9
10def makeUrlStringFromDict(d):
11    pairs = []
12    for k,v in d.iteritems():
13        pairs.append(k+"="+v)
14    return "&".join(pairs)
15
16class Location:
17    """

18        Retrieve this class by calling Window.getLocation().

19        

20        This provides a pyjs wrapper for the current location,

21        with some utility methods for convenience.

22        

23    """
24    def __init__(self, location):
25        self.location = location
26    
27    def getHash(self):
28        return self.location.hash
29    
30    def getHashDict(self):
31        if not self.hashDict or self.hashDictHash != self.getHash():
32            self.hashDictHash = self.getHash()
33            self.hashDict = makeUrlDict(self.getHash().slice(1))
34        return self.hashDict
35    
36    def getHost(self):
37        return self.location.host
38    
39    def getHostname(self):
40        return self.location.hostname
41    
42    def getHref(self):
43        return self.location.href
44    
45    def getPageHref(self):
46        """

47        Return href with any search or hash stripped

48        """
49        href = self.location.href
50        if href.find("?"): href = href.split("?")[0]
51        if href.find("#"): href = href.split("#")[0]
52        return href
53    
54    def getPathname(self):
55        return self.location.pathname
56    
57    def getPort(self):
58        return self.location.port
59    
60    def getProtocol(self):
61        return self.location.protocol
62        
63    def getSearch(self):
64        return ""+self.location.search
65    
66    def getSearchDict(self):
67        if not self.searchDict:
68            self.searchDict = makeUrlDict(self.getSearch().slice(1))
69        return self.searchDict
70
71    def getSearchVar(self, key):
72        searchDict = self.getSearchDict()
73        return searchDict[key]
74    
75    def reload(self):
76        self.location.reload()
77        
78    def setHref(self, href):
79        self.location.href = href
80
81    def setSearch(self, search):
82        self.location.search = search
83        
84    def setSearchDict(self, searchDict):
85        self.setSearch(makeUrlStringFromDict(searchDict))
86        
87    def setHash(self, hash):
88        self.location.hash = hash
89        
90    def setHashDict(self, hashDict):
91        self.setHash(makeUrlStringFromDict(hashDict))
92        
93