Last active
February 18, 2016 21:05
-
-
Save enomado/ee6ca0f5f4b8e4ea489f to your computer and use it in GitHub Desktop.
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
from IPython.terminal.embed import InteractiveShellEmbed | |
from IPython.core.interactiveshell import DummyMod | |
import sys | |
import mako.template | |
from mako import util, compat | |
# call - ${ invoke_ipython() } | |
# - shows template line | |
# - passes template variables e.g. context, template, ... | |
def mako_rewrite_line(filename, lineno): | |
try: | |
info = mako.template._get_module_info(filename) | |
module_source = info.code | |
template_source = info.source | |
template_filename = info.template_filename or filename | |
except KeyError: | |
print filename # python file | |
raise Exception('sdfsdfsd') | |
template_ln = 1 | |
source_map = mako.template.ModuleInfo.\ | |
get_module_source_metadata( | |
module_source, full_line_map=True) | |
line_map = source_map['full_line_map'] | |
template_lines = [line_ for line_ in | |
template_source.split("\n")] | |
# mods[filename] = (line_map, template_lines) | |
template_ln = line_map[lineno - 1] | |
if template_ln <= len(template_lines): | |
template_line = template_lines[template_ln - 1] | |
else: | |
template_line = None | |
return filename, lineno, template_filename, template_ln, template_line #, template_source | |
def invoke_ipython(): | |
shell = InteractiveShellEmbed(display_banner = False) | |
module = DummyMod() | |
call_frame = sys._getframe(1) | |
filename, linenum = call_frame.f_code.co_filename, call_frame.f_lineno | |
q = mako_rewrite_line(filename, linenum) | |
line = q[3] | |
template_name = q[2] | |
local_ns = call_frame.f_locals | |
shell.write(" File \"{}\", line {}".format(template_name, line)) | |
shell(stack_depth=2, local_ns = local_ns, module = module) | |
def ipython_embed(): | |
shell = InteractiveShellEmbed(display_banner = False) | |
call_frame = sys._getframe(1) | |
filename, linenum = call_frame.f_code.co_filename, call_frame.f_lineno | |
shell.write(" File \"{}\", line {}".format(filename, linenum)) | |
shell(stack_depth=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment