Package pyjamas :: Module Cookies
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.Cookies

 1  # This is the gtk-dependent Cookies module.
 
 2  # For the pyjamas/javascript version, see platform/CookiesPyJS.py
 
 3  
 
 4  import sys 
 5  from __pyjamas__ import JS, doc 
 6  if sys.platform not in ['mozilla', 'ie6', 'opera', 'oldmoz', 'safari']: 
 7      from Cookie import SimpleCookie 
 8      import urllib 
 9      import datetime 
10      from string import strip 
11  
 
12 -def getCookie(key):
13 return getCookie2(key)
14
15 -def getCookie2(cookie_name):
16 cookiestr = doc().cookie 17 c = SimpleCookie(cookiestr) 18 cs = c.get(cookie_name, None) 19 print "getCookie2", cookiestr, "name", cookie_name, "val", cs 20 if cs: 21 return cs.value 22 return None
23 24 # expires can be int or Date
25 -def setCookie(name, value, expires, domain=None, path=None, secure=False):
26 cookiestr = doc().cookie 27 c = SimpleCookie(cookiestr) 28 c[name] = value 29 m = c[name] 30 d = datetime.datetime.now() + datetime.timedelta(0, expires/1000) 31 d = d.strftime("%a, %d %b %Y %H:%M:%S GMT") 32 m['expires'] = '"%s"' % d 33 if domain: 34 m['domain'] = domain 35 if path: 36 m['path'] = path 37 if secure: 38 m['secure'] = '' 39 40 c = c.output(header='').strip() 41 print "set cookies", c 42 _doc = doc() 43 _doc.cookie = c 44 45 return 46 JS(""" 47 if (expires instanceof Date) expires = expires.getTime(); 48 if (pyjslib_isUndefined(domain)) domain = null; 49 if (pyjslib_isUndefined(path)) path = null; 50 if (pyjslib_isUndefined(secure)) secure = false; 51 52 var today = new Date(); 53 var expiration = new Date(); 54 expiration.setTime(today.getTime() + expires) 55 56 var c = encodeURIComponent(name) + '=' + encodeURIComponent(value); 57 c += ';expires=' + expiration.toGMTString(); 58 59 if (domain) 60 c += ';domain=' + domain; 61 if (path) 62 c += ';path=' + path; 63 if (secure) 64 c += ';secure'; 65 66 $doc.cookie = c; 67 """)
68
69 -def get_crumbs():
70 docCookie = doc().cookie 71 c = SimpleCookie(docCookie) 72 c = c.output(header='') 73 return map(strip, c.split('\n'))
74
75 -def loadCookies():
76 pass
77