Skip to content

Instantly share code, notes, and snippets.

@bgnori
Last active December 18, 2015 03:29
Show Gist options
  • Save bgnori/5718984 to your computer and use it in GitHub Desktop.
Save bgnori/5718984 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from selenium import webdriver
PAGE = "file:///home/nori/Desktop/study/yamljs/hoge.html"
class Executor:
def __init__(self):
self.driver = driver = webdriver.PhantomJS('phantomjs')
self.driver.get(PAGE)
def execute(self, script):
self.driver.execute_script(r'''$(document).ready(function(){
$("#output").replaceWith('<div id="output">' + %s + '</div>');
});'''%(script,))
return self.output()
def output(self):
element = self.driver.find_element_by_id("output")
#for t in t.xpath('//div[@id="output"]'):
return element.text
def quit(self):
self.driver.quie()
PyYAML==3.10
argparse==1.2.1
selenium==2.33.0
wsgiref==0.1.2
<html>
<head>
<title>Runner</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#output").replaceWith("<div id="output">jQuery!</div>");
});
</script>
</head>
<body><div id="output"></div></body>
</html>
#!/usr/bin/env python
import yaml
from executor import Executor
from translator import Translator
from cmd import Cmd
class REPL(Cmd):
prompt = 'repl:>> '
def __init__(self):
Cmd.__init__(self)
self.ex = Executor()
self.trans = Translator()
def default(self, line):
y = yaml.load(line)
print 'yaml:', y
js = self.trans.translate(y)
print 'generated js:', js
print self.ex.execute(js)
def do_EOF(self, line):
return True
def do_quit(self, line):
return True
repl = REPL()
repl.cmdloop()
#!/usr/bin/env python
class Translator:
def translate(self, y):
if isinstance(y, list):
return self.translate_list(y)
if isinstance(y, str):
return self.translate_str(y)
if isinstance(y, int):
return self.translate_int(y)
if isinstance(y, bool):
return self.translate_bool(y)
return "function(){return none;}()"
def translate_list(self, y):
if y[0] in "+-*/" and len(y) == 3:
return "function(){return %s %s %s;}()"%(self.translate(y[1]), y[0], self.translate(y[2]))
raise
def translate_int(self, y):
return str(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment