pygen.py 9.5 KB

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