PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Location.py

http://pyjamas.googlecode.com/
Python | 93 lines | 71 code | 12 blank | 10 comment | 7 complexity | 47d9cee2fe4595b43f9982ffa6724858 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. def 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. def makeUrlStringFromDict(d):
  10. pairs = []
  11. for k,v in d.iteritems():
  12. pairs.append(k+"="+v)
  13. return "&".join(pairs)
  14. class Location:
  15. """
  16. Retrieve this class by calling Window.getLocation().
  17. This provides a pyjs wrapper for the current location,
  18. with some utility methods for convenience.
  19. """
  20. def __init__(self, location):
  21. self.location = location
  22. def getHash(self):
  23. return self.location.hash
  24. def getHashDict(self):
  25. if not self.hashDict or self.hashDictHash != self.getHash():
  26. self.hashDictHash = self.getHash()
  27. self.hashDict = makeUrlDict(self.getHash().slice(1))
  28. return self.hashDict
  29. def getHost(self):
  30. return self.location.host
  31. def getHostname(self):
  32. return self.location.hostname
  33. def getHref(self):
  34. return self.location.href
  35. def getPageHref(self):
  36. """
  37. Return href with any search or hash stripped
  38. """
  39. href = self.location.href
  40. if href.find("?"): href = href.split("?")[0]
  41. if href.find("#"): href = href.split("#")[0]
  42. return href
  43. def getPathname(self):
  44. return self.location.pathname
  45. def getPort(self):
  46. return self.location.port
  47. def getProtocol(self):
  48. return self.location.protocol
  49. def getSearch(self):
  50. return ""+self.location.search
  51. def getSearchDict(self):
  52. if not self.searchDict:
  53. self.searchDict = makeUrlDict(self.getSearch().slice(1))
  54. return self.searchDict
  55. def getSearchVar(self, key):
  56. searchDict = self.getSearchDict()
  57. return searchDict[key]
  58. def reload(self):
  59. self.location.reload()
  60. def setHref(self, href):
  61. self.location.href = href
  62. def setSearch(self, search):
  63. self.location.search = search
  64. def setSearchDict(self, searchDict):
  65. self.setSearch(makeUrlStringFromDict(searchDict))
  66. def setHash(self, hash):
  67. self.location.hash = hash
  68. def setHashDict(self, hashDict):
  69. self.setHash(makeUrlStringFromDict(hashDict))