PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Cookies.py

http://pyjamas.googlecode.com/
Python | 56 lines | 56 code | 0 blank | 0 comment | 0 complexity | 86a8d9f0f67aabc240150254cd19a889 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. def getCookie(key):
  2. JS("""
  3. var cookies = Cookies_loadCookies();
  4. var value = cookies[key];
  5. return (value == null) ? null : value;
  6. """)
  7. # expires can be int or Date
  8. def setCookie(name, value, expires, domain=None, path=None, secure=False):
  9. JS("""
  10. if (expires instanceof Date) expires = expires.getTime();
  11. if (pyjslib_isUndefined(domain)) domain = null;
  12. if (pyjslib_isUndefined(path)) path = null;
  13. if (pyjslib_isUndefined(secure)) secure = false;
  14. var date = new Date(expires);
  15. var c = encodeURIComponent(name) + '=' + encodeURIComponent(value);
  16. c += ';expires=' + date.toGMTString();
  17. if (domain)
  18. c += ';domain=' + domain;
  19. if (path)
  20. c += ';path=' + path;
  21. if (secure)
  22. c += ';secure';
  23. $doc.cookie = c;
  24. """)
  25. def loadCookies():
  26. JS("""
  27. var cookies = {};
  28. var docCookie = $doc.cookie;
  29. if (docCookie && docCookie != '') {
  30. var crumbs = docCookie.split('; ');
  31. for (var i = 0; i < crumbs.length; ++i) {
  32. var name, value;
  33. var eqIdx = crumbs[i].indexOf('=');
  34. if (eqIdx == -1) {
  35. name = crumbs[i];
  36. value = '';
  37. } else {
  38. name = crumbs[i].substring(0, eqIdx);
  39. value = crumbs[i].substring(eqIdx + 1);
  40. }
  41. cookies[decodeURIComponent(name)] = decodeURIComponent(value);
  42. }
  43. }
  44. return cookies;
  45. """)