Commits

Mike Bayer committed 6d96ef8

- add module source thing as ModuleInfo.get_module_source_metadata

Comments (0)

Files changed (2)

mako/exceptions.py

 
 import traceback
 import sys
-import re
 from mako import util, compat
 
 class MakoException(Exception):
     def __init__(self, message, source, lineno, pos, filename):
         MakoException.__init__(self,
                               message + _format_filepos(lineno, pos, filename))
-        self.lineno =lineno
+        self.lineno = lineno
         self.pos = pos
         self.filename = filename
         self.source = source
     def __init__(self, message, source, lineno, pos, filename):
         MakoException.__init__(self,
                               message + _format_filepos(lineno, pos, filename))
-        self.lineno =lineno
+        self.lineno = lineno
         self.pos = pos
         self.filename = filename
         self.source = source
         self.records = self._init(traceback)
 
         if isinstance(self.error, (CompileException, SyntaxException)):
-            import mako.template
             self.source = self.error.source
             self.lineno = self.error.lineno
             self._has_source = True
 
                 template_ln = 1
 
-                source_map = re.search(
-                                r"__M_BEGIN_METADATA(.+?)__M_END_METADATA",
-                                module_source, re.S).group(1)
-                source_map = compat.json.loads(source_map)
-                line_map = dict(
-                    (int(k), v) for k, v in source_map['line_map'].items()
-                )
-
-                for mod_line in reversed(sorted(line_map)):
-                    tmpl_line = line_map[mod_line]
-                    while mod_line > 0:
-                        mod_line -= 1
-                        if mod_line in line_map:
-                            break
-                        line_map[mod_line] = tmpl_line
+                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")]
                                 line, template_filename, template_ln,
                                 template_line, template_source))
         if not self.source:
-            for l in range(len(new_trcback)-1, 0, -1):
+            for l in range(len(new_trcback) - 1, 0, -1):
                 if new_trcback[l][5]:
                     self.source = new_trcback[l][7]
                     self.lineno = new_trcback[l][5]
         if module_filename:
             self._modules[module_filename] = self
 
+    @classmethod
+    def get_module_source_metadata(cls, module_source, full_line_map=False):
+        source_map = re.search(
+                        r"__M_BEGIN_METADATA(.+?)__M_END_METADATA",
+                        module_source, re.S).group(1)
+        source_map = compat.json.loads(source_map)
+        if full_line_map:
+            line_map = source_map['full_line_map'] = dict(
+                (int(k), v) for k, v in source_map['line_map'].items()
+            )
+
+            for mod_line in reversed(sorted(line_map)):
+                tmpl_line = line_map[mod_line]
+                while mod_line > 0:
+                    mod_line -= 1
+                    if mod_line in line_map:
+                        break
+                    line_map[mod_line] = tmpl_line
+        return source_map
+
     @property
     def code(self):
         if self.module_source is not None: