pygen.py 9.3 KB

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