Created
November 7, 2012 18:21
-
-
Save ojii/4033383 to your computer and use it in GitHub Desktop.
persist state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import requests | |
__doc__ = """bitmask.py | |
Usage: | |
bitmask.py <varname> <cookiename> [--default=<default>] [--html] [--compress] <mask>... | |
Options: | |
-h --help Show this message. | |
--default=<default> Default mask [default: 0]. | |
--compress Compress using Google Closure Compiler. | |
--html Output as HTML for testing. | |
""" | |
HTML_TEMPLATE = r"""<html> | |
<head> | |
<title>Bitmask.js</title> | |
<head> | |
<body> | |
%(_controls)s | |
<script>%(_bitmasks)s;</script> | |
<script> | |
%(_load)s | |
function toggle(name){ | |
%(varname)s[name] = !%(varname)s[name]; | |
%(varname)s.save(); | |
document.getElementById(name).innerText = %(varname)s[name] ? 'true' : 'false'; | |
} | |
</script> | |
</body> | |
</html>""" | |
HTML_LOAD_TEMPLATE = r"document.getElementById('%(name)s').innerText = %(varname)s.%(name)s ? 'true' : 'false';" | |
CONTROLS_TEMPLATE = r"""<p> | |
%(name)s: <span id='%(name)s'>Loading...</span> <button onclick="toggle('%(name)s');">Toggle</button> | |
<p>""" | |
JS_TEMPLATE = r"""var %(varname)s = (function(){ | |
var output = {}; | |
var mask = parseInt(unescape(document.cookie.replace(RegExp("(?:^|.*;\\s*)" + escape("%(cookiename)s").replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")) || "%(default)s", 10); | |
%(_load)s | |
output['save'] = function(){ | |
var tmp = 0; | |
%(_save)s | |
document.cookie = escape("%(cookiename)s") + "=" + escape(tmp + ""); | |
}; | |
return output; | |
})();""" | |
LOAD_TEMPLATE = r"output['%(name)s'] = mask & %(value)s;" | |
SAVE_TEMPLATE = r"""if (output['%(name)s']){ | |
tmp |= %(value)s; | |
}""" | |
def main(varname, cookiename, masks, html=False, compress=False, default=0): | |
_load = '\n '.join([ | |
LOAD_TEMPLATE % {'name': name, 'value': 2**i} for i, name in enumerate(masks) | |
]) | |
_save = '\n '.join( | |
SAVE_TEMPLATE % {'name': name, 'value': 2**i } for i, name in enumerate(masks) | |
) | |
if isinstance(default, int): | |
default = default | |
elif default.isdigit(): | |
default = int(default) | |
else: | |
default = 2 ** masks.index(default) | |
result = JS_TEMPLATE % { | |
'varname': varname, | |
'cookiename': cookiename, | |
'default': default, | |
'_save': _save, | |
'_load': _load, | |
} | |
if compress: | |
data = { | |
'js_code': result, | |
'compilation_level': 'SIMPLE_OPTIMIZATIONS', | |
'output_format': 'text', | |
'output_info': 'compiled_code', | |
'js_externs': varname, | |
} | |
response = requests.post('http://closure-compiler.appspot.com/compile', data=data) | |
if not response.ok: | |
print >> sys.stderr, response.content | |
response.raise_for_status() | |
result = response.content | |
if html: | |
_controls = '\n '.join([ | |
CONTROLS_TEMPLATE % {'name': name} for name in masks | |
]) | |
_load_html = '\n '.join([ | |
HTML_LOAD_TEMPLATE % { | |
'name': name, | |
'varname': varname, | |
} for name in masks | |
]) | |
result = HTML_TEMPLATE % { | |
'_controls': _controls, | |
'varname': varname, | |
'_bitmasks': result, | |
'_load': _load_html, | |
} | |
print result | |
if __name__ == '__main__': | |
import docopt | |
args = docopt.docopt(__doc__) | |
main(args['<varname>'], args['<cookiename>'], args['<mask>'], args['--html'], args['--compress'], args['--default']) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var State = (function(){ | |
var output = {}; | |
var mask = parseInt(unescape(document.cookie.replace(RegExp("(?:^|.*;\\s*)" + escape("_cms_toolbar_state").replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")) || "0", 10); | |
output['edit'] = mask & 1; | |
output['layout'] = mask & 2; | |
output['live'] = mask & 4; | |
output['collapsed'] = mask & 8; | |
output['save'] = function(){ | |
var tmp = 0; | |
if (output['edit']){ | |
tmp |= 1; | |
} | |
if (output['layout']){ | |
tmp |= 2; | |
} | |
if (output['live']){ | |
tmp |= 4; | |
} | |
if (output['collapsed']){ | |
tmp |= 8; | |
} | |
document.cookie = escape("_cms_toolbar_state") + "=" + escape(tmp + ""); | |
}; | |
return output; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var State=function(){var a={},c=parseInt(unescape(document.cookie.replace(RegExp("(?:^|.*;\\s*)"+escape("_cms_toolbar_state").replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"),"$1"))||"0",10);a.edit=c&1;a.layout=c&2;a.live=c&4;a.collapsed=c&8;a.save=function(){var b=0;a.edit&&(b|=1);a.layout&&(b|=2);a.live&&(b|=4);a.collapsed&&(b|=8);document.cookie=escape("_cms_toolbar_state")+"="+escape(b+"")};return a}(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Bitmask.js</title> | |
<head> | |
<body> | |
<p> | |
edit: <span id='edit'>Loading...</span> <button onclick="toggle('edit');">Toggle</button> | |
<p> | |
<p> | |
layout: <span id='layout'>Loading...</span> <button onclick="toggle('layout');">Toggle</button> | |
<p> | |
<p> | |
live: <span id='live'>Loading...</span> <button onclick="toggle('live');">Toggle</button> | |
<p> | |
<p> | |
collapsed: <span id='collapsed'>Loading...</span> <button onclick="toggle('collapsed');">Toggle</button> | |
<p> | |
<script>var State=function(){var a={},c=parseInt(unescape(document.cookie.replace(RegExp("(?:^|.*;\\s*)"+escape("_cms_toolbar_state").replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"),"$1"))||"0",10);a.edit=c&1;a.layout=c&2;a.live=c&4;a.collapsed=c&8;a.save=function(){var b=0;a.edit&&(b|=1);a.layout&&(b|=2);a.live&&(b|=4);a.collapsed&&(b|=8);document.cookie=escape("_cms_toolbar_state")+"="+escape(b+"")};return a}(); | |
;</script> | |
<script> | |
document.getElementById('edit').innerText = State.edit ? 'true' : 'false'; | |
document.getElementById('layout').innerText = State.layout ? 'true' : 'false'; | |
document.getElementById('live').innerText = State.live ? 'true' : 'false'; | |
document.getElementById('collapsed').innerText = State.collapsed ? 'true' : 'false'; | |
function toggle(name){ | |
State[name] = !State[name]; | |
State.save(); | |
document.getElementById(name).innerText = State[name] ? 'true' : 'false'; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment