MCPcopy Index your code
hub / github.com/rawpython/remi / CookieInterface

Class CookieInterface

examples/session_app.py:20–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18import threading
19
20class CookieInterface(gui.Tag, gui.EventSource):
21 def __init__(self, remi_app_instance, **kwargs):
22 """
23 This class uses javascript code from cookie.js framework ( https://developer.mozilla.org/en-US/docs/Web/API/document.cookie )
24 /*\
25 |*|
26 |*| :: cookies.js ::
27 |*|
28 |*| A complete cookies reader/writer framework with full unicode support.
29 |*|
30 |*| Revision #2 - June 13th, 2017
31 |*|
32 |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
33 |*| https://developer.mozilla.org/User:fusionchess
34 |*| https://github.com/madmurphy/cookies.js
35 |*|
36 |*| This framework is released under the GNU Public License, version 3 or later.
37 |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
38 |*|
39 \*/
40 """
41 super(CookieInterface, self).__init__(**kwargs)
42 gui.EventSource.__init__(self)
43 self.app_instance = remi_app_instance
44 self.EVENT_ONCOOKIES = "on_cookies"
45 self.cookies = {}
46
47 def request_cookies(self):
48 self.app_instance.execute_javascript("""
49 var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
50 var result = {};
51 for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
52 var key = decodeURIComponent(aKeys[nIdx]);
53 result[key] = decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
54 }
55 remi.sendCallbackParam('%s','%s', result);
56 """%(self.identifier, self.EVENT_ONCOOKIES))
57
58 @gui.decorate_event
59 def on_cookies(self, **value):
60 self.cookies = value
61 return (value,)
62
63 def remove_cookie(self, key, path='/', domain=''):
64 if not key in self.cookies.keys():
65 return
66 self.app_instance.execute_javascript( """
67 var sKey = "%(sKey)s";
68 var sPath = "%(sPath)s";
69 var sDomain = "%(sDomain)s";
70 document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
71 """%{'sKey': key, 'sPath': path, 'sDomain': domain} )
72
73 def set_cookie(self, key, value, expiration='Infinity', path='/', domain='', secure=False):
74 """
75 expiration (int): seconds after with the cookie automatically gets deleted
76 """
77

Callers 1

mainMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected