pygen.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # mako/pygen.py
  2. # Copyright (C) 2006-2016 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """utilities for generating and formatting literal Python code."""
  7. import re
  8. from mako import exceptions
  9. class PythonPrinter(object):
  10. def __init__(self, stream):
  11. # indentation counter
  12. self.indent = 0
  13. # a stack storing information about why we incremented
  14. # the indentation counter, to help us determine if we
  15. # should decrement it
  16. self.indent_detail = []
  17. # the string of whitespace multiplied by the indent
  18. # counter to produce a line
  19. self.indentstring = " "
  20. # the stream we are writing to
  21. self.stream = stream
  22. # current line number
  23. self.lineno = 1
  24. # a list of lines that represents a buffered "block" of code,
  25. # which can be later printed relative to an indent level
  26. self.line_buffer = []
  27. self.in_indent_lines = False
  28. self._reset_multi_line_flags()
  29. # mapping of generated python lines to template
  30. # source lines
  31. self.source_map = {}
  32. def _update_lineno(self, num):
  33. self.lineno += num
  34. def start_source(self, lineno):
  35. if self.lineno not in self.source_map:
  36. self.source_map[self.lineno] = lineno
  37. def write_blanks(self, num):
  38. self.stream.write("\n" * num)
  39. self._update_lineno(num)
  40. def write_indented_block(self, block):
  41. """print a line or lines of python which already contain indentation.
  42. The indentation of the total block of lines will be adjusted to that of
  43. the current indent level."""
  44. self.in_indent_lines = False
  45. for l in re.split(r'\r?\n', block):
  46. self.line_buffer.append(l)
  47. self._update_lineno(1)
  48. def writelines(self, *lines):
  49. """print a series of lines of python."""
  50. for line in lines:
  51. self.writeline(line)
  52. def writeline(self, line):
  53. """print a line of python, indenting it according to the current
  54. indent level.
  55. this also adjusts the indentation counter according to the
  56. content of the line.
  57. """
  58. if not self.in_indent_lines:
  59. self._flush_adjusted_lines()
  60. self.in_indent_lines = True
  61. if (
  62. line is None or
  63. re.match(r"^\s*#", line) or
  64. re.match(r"^\s*$", line)
  65. ):
  66. hastext = False
  67. else:
  68. hastext = True
  69. is_comment = line and len(line) and line[0] == '#'
  70. # see if this line should decrease the indentation level
  71. if (
  72. not is_comment and
  73. (not hastext or self._is_unindentor(line))
  74. ):
  75. if self.indent > 0:
  76. self.indent -= 1
  77. # if the indent_detail stack is empty, the user
  78. # probably put extra closures - the resulting
  79. # module wont compile.
  80. if len(self.indent_detail) == 0:
  81. raise exceptions.SyntaxException(
  82. "Too many whitespace closures")
  83. self.indent_detail.pop()
  84. if line is None:
  85. return
  86. # write the line
  87. self.stream.write(self._indent_line(line) + "\n")
  88. self._update_lineno(len(line.split("\n")))
  89. # see if this line should increase the indentation level.
  90. # note that a line can both decrase (before printing) and
  91. # then increase (after printing) the indentation level.
  92. if re.search(r":[ \t]*(?:#.*)?$", line):
  93. # increment indentation count, and also
  94. # keep track of what the keyword was that indented us,
  95. # if it is a python compound statement keyword
  96. # where we might have to look for an "unindent" keyword
  97. match = re.match(r"^\s*(if|try|elif|while|for|with)", line)
  98. if match:
  99. # its a "compound" keyword, so we will check for "unindentors"
  100. indentor = match.group(1)
  101. self.indent += 1
  102. self.indent_detail.append(indentor)
  103. else:
  104. indentor = None
  105. # its not a "compound" keyword. but lets also
  106. # test for valid Python keywords that might be indenting us,
  107. # else assume its a non-indenting line
  108. m2 = re.match(r"^\s*(def|class|else|elif|except|finally)",
  109. line)
  110. if m2:
  111. self.indent += 1
  112. self.indent_detail.append(indentor)
  113. def close(self):
  114. """close this printer, flushing any remaining lines."""
  115. self._flush_adjusted_lines()
  116. def _is_unindentor(self, line):
  117. """return true if the given line is an 'unindentor',
  118. relative to the last 'indent' event received.
  119. """
  120. # no indentation detail has been pushed on; return False
  121. if len(self.indent_detail) == 0:
  122. return False
  123. indentor = self.indent_detail[-1]
  124. # the last indent keyword we grabbed is not a
  125. # compound statement keyword; return False
  126. if indentor is None:
  127. return False
  128. # if the current line doesnt have one of the "unindentor" keywords,
  129. # return False
  130. match = re.match(r"^\s*(else|elif|except|finally).*\:", line)
  131. if not match:
  132. return False
  133. # whitespace matches up, we have a compound indentor,
  134. # and this line has an unindentor, this
  135. # is probably good enough
  136. return True
  137. # should we decide that its not good enough, heres
  138. # more stuff to check.
  139. # keyword = match.group(1)
  140. # match the original indent keyword
  141. # for crit in [
  142. # (r'if|elif', r'else|elif'),
  143. # (r'try', r'except|finally|else'),
  144. # (r'while|for', r'else'),
  145. # ]:
  146. # if re.match(crit[0], indentor) and re.match(crit[1], keyword):
  147. # return True
  148. # return False
  149. def _indent_line(self, line, stripspace=''):
  150. """indent the given line according to the current indent level.
  151. stripspace is a string of space that will be truncated from the
  152. start of the line before indenting."""
  153. return re.sub(r"^%s" % stripspace, self.indentstring
  154. * self.indent, line)
  155. def _reset_multi_line_flags(self):
  156. """reset the flags which would indicate we are in a backslashed
  157. or triple-quoted section."""
  158. self.backslashed, self.triplequoted = False, False
  159. def _in_multi_line(self, line):
  160. """return true if the given line is part of a multi-line block,
  161. via backslash or triple-quote."""
  162. # we are only looking for explicitly joined lines here, not
  163. # implicit ones (i.e. brackets, braces etc.). this is just to
  164. # guard against the possibility of modifying the space inside of
  165. # a literal multiline string with unfortunately placed
  166. # whitespace
  167. current_state = (self.backslashed or self.triplequoted)
  168. if re.search(r"\\$", line):
  169. self.backslashed = True
  170. else:
  171. self.backslashed = False
  172. triples = len(re.findall(r"\"\"\"|\'\'\'", line))
  173. if triples == 1 or triples % 2 != 0:
  174. self.triplequoted = not self.triplequoted
  175. return current_state
  176. def _flush_adjusted_lines(self):
  177. stripspace = None
  178. self._reset_multi_line_flags()
  179. for entry in self.line_buffer:
  180. if self._in_multi_line(entry):
  181. self.stream.write(entry + "\n")
  182. else:
  183. entry = entry.expandtabs()
  184. if stripspace is None and re.search(r"^[ \t]*[^# \t]", entry):
  185. stripspace = re.match(r"^([ \t]*)", entry).group(1)
  186. self.stream.write(self._indent_line(entry, stripspace) + "\n")
  187. self.line_buffer = []
  188. self._reset_multi_line_flags()
  189. def adjust_whitespace(text):
  190. """remove the left-whitespace margin of a block of Python code."""
  191. state = [False, False]
  192. (backslashed, triplequoted) = (0, 1)
  193. def in_multi_line(line):
  194. start_state = (state[backslashed] or state[triplequoted])
  195. if re.search(r"\\$", line):
  196. state[backslashed] = True
  197. else:
  198. state[backslashed] = False
  199. def match(reg, t):
  200. m = re.match(reg, t)
  201. if m:
  202. return m, t[len(m.group(0)):]
  203. else:
  204. return None, t
  205. while line:
  206. if state[triplequoted]:
  207. m, line = match(r"%s" % state[triplequoted], line)
  208. if m:
  209. state[triplequoted] = False
  210. else:
  211. m, line = match(r".*?(?=%s|$)" % state[triplequoted], line)
  212. else:
  213. m, line = match(r'#', line)
  214. if m:
  215. return start_state
  216. m, line = match(r"\"\"\"|\'\'\'", line)
  217. if m:
  218. state[triplequoted] = m.group(0)
  219. continue
  220. m, line = match(r".*?(?=\"\"\"|\'\'\'|#|$)", line)
  221. return start_state
  222. def _indent_line(line, stripspace=''):
  223. return re.sub(r"^%s" % stripspace, '', line)
  224. lines = []
  225. stripspace = None
  226. for line in re.split(r'\r?\n', text):
  227. if in_multi_line(line):
  228. lines.append(line)
  229. else:
  230. line = line.expandtabs()
  231. if stripspace is None and re.search(r"^[ \t]*[^# \t]", line):
  232. stripspace = re.match(r"^([ \t]*)", line).group(1)
  233. lines.append(_indent_line(line, stripspace))
  234. return "\n".join(lines)