pygen.py 10 KB

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