Class: BasePythonQueryHook

Inherits:
Mumukit::Templates::FileHook
  • Object
show all
Defined in:
lib/base/query_hook.rb

Direct Known Subclasses

Python2QueryHook, Python3QueryHook

Instance Method Summary collapse

Instance Method Details

#build_query(query) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/base/query_hook.rb', line 23

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/base/query_hook.rb', line 31

def build_state(cookie)
  (cookie||[]).map do |statement|
  <<~python
  try:
    #{statement}
  except:
    pass
python
  end
end

#command_line(filename) ⇒ Object



5
6
7
# File 'lib/base/query_hook.rb', line 5

def command_line(filename)
  "python #{filename} 2>&1"
end


42
43
44
# File 'lib/base/query_hook.rb', line 42

def compile_cookie(cookie)
  build_state(cookie).join("\n")
end

#compile_file_content(req) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/base/query_hook.rb', line 9

def compile_file_content(req)
  <<python
# -*- coding: UTF-8 -*-
import string, sys, os

#{req.extra}
#{req.content}
sys.stdout = open(os.devnull, 'w')
#{compile_cookie(req.cookie)}
sys.stdout = sys.__stdout__
#{build_query req.query}
python
end

#error_patternsObject



46
47
48
49
50
# File 'lib/base/query_hook.rb', line 46

def error_patterns
  [
    Mumukit::ErrorPattern::Errored.new(syntax_error_regexp)
  ]
end

#syntax_error_regexpObject



52
53
54
# File 'lib/base/query_hook.rb', line 52

def syntax_error_regexp
  /\A  File .*\n(?m)(?=.*(SyntaxError|IndentationError))/
end