Class: PythonQueryHook
- Inherits:
-
Mumukit::Templates::FileHook
- Object
- Mumukit::Templates::FileHook
- PythonQueryHook
- Defined in:
- lib/query_hook.rb
Instance Method Summary collapse
- #build_query(query) ⇒ Object
- #build_state(cookie) ⇒ Object
- #command_line(filename) ⇒ Object
- #compile_cookie(cookie) ⇒ Object
- #compile_file_content(req) ⇒ Object
Instance Method Details
#build_query(query) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/query_hook.rb', line 21 def build_query(query) if query.match /print *(\(| ).*|^[a-zA-Z_]\w*\s*=.*|^raise\b/ query else "print(string.Template(\"=> $result\").substitute(result = #{query}))" end end |
#build_state(cookie) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/query_hook.rb', line 29 def build_state() (||[]).map do |statement| "try:\n \#{statement}\nexcept:\n pass\n" end end |
#command_line(filename) ⇒ Object
4 5 6 |
# File 'lib/query_hook.rb', line 4 def command_line(filename) "python #{filename} 2>&1" end |
#compile_cookie(cookie) ⇒ Object
40 41 42 |
# File 'lib/query_hook.rb', line 40 def () build_state().join("\n") end |
#compile_file_content(req) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/query_hook.rb', line 8 def compile_file_content(req) "import string, sys, os\n\n\#{req.extra}\n\#{req.content}\nsys.stdout = open(os.devnull, 'w')\n\#{compile_cookie(req.cookie)}\nsys.stdout = sys.__stdout__\n\#{build_query req.query}\n" end |