pygen.py 9.2 KB

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