PageRenderTime 323ms CodeModel.GetById 161ms app.highlight 2ms RepoModel.GetById 159ms 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
 1def getCookie(key):
 2    JS("""

 3    var cookies = Cookies_loadCookies();

 4    var value = cookies[key];

 5    return (value == null) ? null : value;

 6    """)
 7
 8# expires can be int or Date

 9def setCookie(name, value, expires, domain=None, path=None, secure=False):
10    JS("""

11    if (expires instanceof Date) expires = expires.getTime();

12    if (pyjslib_isUndefined(domain)) domain = null;

13    if (pyjslib_isUndefined(path)) path = null;

14    if (pyjslib_isUndefined(secure)) secure = false;

15    

16    var date = new Date(expires);

17

18    var c = encodeURIComponent(name) + '=' + encodeURIComponent(value);

19    c += ';expires=' + date.toGMTString();

20

21    if (domain)

22        c += ';domain=' + domain;

23    if (path)

24        c += ';path=' + path;

25    if (secure)

26        c += ';secure';

27

28    $doc.cookie = c;

29    """)
30
31def loadCookies():
32    JS("""

33    var cookies = {};

34

35    var docCookie = $doc.cookie;

36    if (docCookie && docCookie != '') {

37        var crumbs = docCookie.split('; ');

38        for (var i = 0; i < crumbs.length; ++i) {

39            var name, value;

40

41            var eqIdx = crumbs[i].indexOf('=');

42            if (eqIdx == -1) {

43                name = crumbs[i];

44                value = '';

45            } else {

46                name = crumbs[i].substring(0, eqIdx);

47                value = crumbs[i].substring(eqIdx + 1);

48            }

49

50        cookies[decodeURIComponent(name)] = decodeURIComponent(value);

51        }

52    }

53

54    return cookies;

55    """)
56