├── favicon.ico ├── README.mk ├── app.yaml ├── index.yaml ├── staticfiles ├── zen.css └── staila.css ├── templates ├── zen.html └── formater.html ├── main.py └── pythontidy.py /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanantoniofm/pythoniter/HEAD/favicon.ico -------------------------------------------------------------------------------- /README.mk: -------------------------------------------------------------------------------- 1 | # Pythoniter 2 | 3 | 4 | Small web Python code formatter. 5 | Implemented using Google Appengine, with StrinIO and based on pythontidy 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app.yaml: -------------------------------------------------------------------------------- 1 | application: pythoniter 2 | version: 1 3 | runtime: python27 4 | api_version: 1 5 | threadsafe: yes 6 | 7 | handlers: 8 | - url: /favicon\.ico 9 | static_files: favicon.ico 10 | upload: favicon\.ico 11 | 12 | - url: /staticfiles 13 | static_dir: staticfiles 14 | 15 | - url: .* 16 | script: main.app 17 | - url: /down 18 | script: main.app 19 | 20 | libraries: 21 | - name: webapp2 22 | version: "2.5.1" 23 | - name: jinja2 24 | version: "2.6" 25 | 26 | -------------------------------------------------------------------------------- /index.yaml: -------------------------------------------------------------------------------- 1 | indexes: 2 | 3 | # AUTOGENERATED 4 | 5 | # This index.yaml is automatically updated whenever the dev_appserver 6 | # detects that a new type of query is run. If you want to manage the 7 | # index.yaml file manually, remove the above marker line (the line 8 | # saying "# AUTOGENERATED"). If you want to manage some indexes 9 | # manually, move them above the marker line. The index.yaml file is 10 | # automatically uploaded to the admin console when you next deploy 11 | # your application using appcfg.py. 12 | 13 | -------------------------------------------------------------------------------- /staticfiles/zen.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | body{ 4 | text-align:center; 5 | margin:0 auto; 6 | background-color:#EEE9D6; //soft bone white 7 | } 8 | 9 | button{ 10 | color: white; 11 | background-color: black; 12 | font: 13px Helvetica, arial, freesans, clean, sans-serif; 13 | } 14 | 15 | textarea{ 16 | width: 100%; 17 | height: 95%; 18 | padding: 10px 100px; 19 | color: #073642; 20 | background-color: #eee9d6; 21 | outline:none; 22 | border: 0; 23 | font: 14px Helvetica, arial, freesans, clean, sans-serif; 24 | 25 | } 26 | 27 | #zenbar{ 28 | color:#657b83; 29 | text-decoration: none; 30 | } 31 | 32 | #zenbar a{ 33 | color:#073642; 34 | text-decoration: none; 35 | } 36 | 37 | .mini{ 38 | font-size:small; 39 | font-family:Arial, Sans-Serif; 40 | margin: 0 0 9%; 41 | } 42 | -------------------------------------------------------------------------------- /staticfiles/staila.css: -------------------------------------------------------------------------------- 1 | body{ 2 | text-align:center; 3 | margin:0 auto; 4 | background-color:#EEE9D6; //soft bone white 5 | } 6 | 7 | h2{ 8 | margin: 10% auto 2%; 9 | } 10 | 11 | #container{ 12 | width: 90%; 13 | text-align: left; 14 | margin: 0 auto; 15 | color: #576E74; 16 | } 17 | 18 | .mini{ 19 | font-size:small; 20 | font-family:Arial, Sans-Serif; 21 | margin: 0 0 9%; 22 | } 23 | textarea{ 24 | width: 100%; 25 | padding: 10px 30px; 26 | margin: auto; 27 | } 28 | 29 | #errorConsole{ 30 | padding: 0.25% 2.5%; 31 | color:#93a1a1; //kinda light grey-blueish 32 | background-color:#002A38; //dark blueish 33 | } 34 | 35 | .error{ 36 | color:#DD312F; //red 37 | } 38 | 39 | //Other Colors 40 | // orange : #ca4711 41 | // yellow: #b58a00 42 | // magenta:#d63581 43 | // violet: #6b71c7 44 | // blue : #258bd3 45 | // cyan : #2ba098 46 | // green : #859A01 47 | -------------------------------------------------------------------------------- /templates/zen.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pythoiter - Pretty Python Online Formatter 4 | 5 | 6 | 7 |
8 |
9 |
10 | Switch to Normal Mode 11 | Powered by: Juan Antonio FM || 12 | Google Appengine 13 | 14 | 15 |
16 | 17 |
18 |
19 | {% if error %} 20 |
21 |

Error Console

22 |
23 |

Error example

24 |
25 |
26 | {% endif %} 27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /templates/formater.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pythoiter - Pretty Python Online Formatter 4 | 5 | 6 | 7 |
8 |

Python Formatter

9 |
10 | Fork me on github.com 11 | || 12 | About this appengine tool 13 | || 14 | Report or Sugest a feature 15 |
16 |
17 |

Welcome. Use this text box to input your dirty-formatted python code, and get a nice, well ordered file.

18 |

If you have made syntax mistakes, It will complain and not give you the cookie ;)

19 |
20 |
21 | 25 |
26 |

Input Your Code:

27 | 30 |
31 |
32 |

You can also try the Zen Version of the editor

33 |
34 | {% if error %} 35 |
36 |

Error Console

37 |
38 |

Error example

39 |
40 |
41 | {% endif %} 42 |
43 |

Powered by: Juan Antonio FM || Google Appengine

44 | 45 | 46 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright 2007 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | import os 18 | import webapp2 19 | import jinja2 20 | import logging 21 | 22 | import StringIO 23 | import pythontidy as pt 24 | template_path=os.path.dirname(__file__) + "/templates" 25 | jinja_environment = jinja2.Environment( 26 | loader=jinja2.FileSystemLoader(template_path)) 27 | logging.debug("The path for templates is") 28 | logging.debug(template_path) 29 | 30 | class MainHandler(webapp2.RequestHandler): 31 | 32 | def renderAndWrite(self, values, template="formater.html"): 33 | """render values in the template""" 34 | template = jinja_environment.get_template(template) 35 | self.response.out.write(template.render(values)) 36 | 37 | def formater(self, pythonshit): 38 | #- receive text in string format 39 | #- convert it to file-like object 40 | inf = StringIO.StringIO() 41 | ouf = StringIO.StringIO() 42 | inf.write(pythonshit) 43 | inf.seek(0) 44 | #- pass it through the tidyer 45 | pt.tidy_up(inf, ouf) 46 | pythonshit = ouf.getvalue() 47 | #- return the output as string 48 | return pythonshit 49 | 50 | def post(self): 51 | """get receive text and prettyprint it""" 52 | #- take the text from the request 53 | txt = self.request.get('chorizo') 54 | #- send it to the formater 55 | bonitico = self.formater(txt) 56 | #- and show a website with the pretty code 57 | self.renderAndWrite({"codechunk": bonitico}) 58 | 59 | def get(self): 60 | """shows just atext box for input""" 61 | self.renderAndWrite({"codechunk" : "Aqui pegar tu python fulero"}) 62 | 63 | 64 | class DownHandler(MainHandler): 65 | def post(self): 66 | self.renderAndWrite({"codechunk" : 67 | self.formater( 68 | self.request.get('chorizo'))}) 69 | 70 | def get(self): 71 | self.response.out.write("""Visit this using POST to upload your code, and 72 | download it as a file""") 73 | 74 | 75 | class ZenHandler(MainHandler): 76 | def post(self): 77 | self.renderAndWrite({"codechunk" : 78 | self.formater( 79 | self.request.get('chorizo'))}, 80 | "zen.html") 81 | 82 | def get(self): 83 | self.renderAndWrite({"codechunk" : "Here goes your Python"}, 84 | "zen.html") 85 | 86 | 87 | 88 | app = webapp2.WSGIApplication([ 89 | ('/', MainHandler), 90 | ('/zen', ZenHandler), 91 | ('/down', DownHandler) 92 | ], debug=True) 93 | -------------------------------------------------------------------------------- /pythontidy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # PythonTidy.py 5 | # 2006 Oct 27 . ccr 6 | 7 | '''PythonTidy.py cleans up, regularizes, and reformats the text of 8 | Python scripts. 9 | 10 | =========================================== 11 | Copyright © 2006 Charles Curtis Rhode 12 | =========================================== 13 | 14 | This program is free software; you can redistribute it and/or modify 15 | it under the terms of the GNU General Public License as published by 16 | the Free Software Foundation; either version 2 of the License, or (at 17 | your option) any later version. 18 | 19 | This program is distributed in the hope that it will be useful, but 20 | WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 | General Public License for more details. 23 | 24 | You should have received a copy of the GNU General Public License 25 | along with this program; if not, write to the Free Software 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 27 | USA. 28 | 29 | =========================================== 30 | Charles Curtis Rhode, 31 | 1518 N 3rd, Sheboygan, WI 53081 32 | mailto:CRhode@LacusVeris.com?subject=PythonTidy 33 | =========================================== 34 | 35 | This script reads Python code from standard input and writes a revised 36 | version to standard output. 37 | 38 | Alternatively, it may be invoked with file names as arguments: 39 | 40 | o python PythonTidy.py input output 41 | 42 | Suffice it to say that *input* defaults to \'-\', the standard input, 43 | and *output* defaults to \'-\', the standard output. 44 | 45 | It means to encapsulate the wisdom revealed in: 46 | 47 | o Rossum, Guido van, and Barry Warsaw. "PEP 8: Style Guide for Python 48 | Code." 23 Mar. 2006. Python.org. 28 Nov. 2006 49 | . 50 | 51 | Python scripts are usually so good looking that no beautification is 52 | required. However, from time to time, it may be necessary to alter 53 | the style to conform to changing standards. This script converts 54 | programs in a consistent way. It abstracts the pretty presentation of 55 | the symbolic code from the humdrum[1] process of writing it and 56 | getting it to work. 57 | 58 | This script assumes that the input Python code is well-formed and 59 | works to begin with. It doesn\'t check. If all goes well, the output 60 | Python code will work, too. Of course, you are advised to test it 61 | fully to be sure. 62 | 63 | This script should be run only by python.2.5 (and perhaps higher) on 64 | scripts written for that version (and perhaps lower) because of its 65 | limited knowledge of and expectations for the abstract syntax tree 66 | node classes returned by the *compiler* module. It wouldn\'t hurt 67 | much to try it from (and on) other versions, though, and it might 68 | actually work. 69 | 70 | Search this script for "Python Version Dependency." 71 | 72 | Most of the Python 2.5 test suite passes through PythonTidy.py 73 | unimpaired. I ran the Python regression tests for 2.5.2 which is the 74 | version supported by Debian 5.0 "Lenny." 75 | 76 | On my system these tests fail before tidying: 77 | 78 | o test_imageop 79 | o test_pyclbr 80 | o test_sys 81 | 82 | 282 tests succeed after tidying with the default PythonTidy global 83 | settings, but these tests fail: 84 | 85 | *test_grammar* exposes bug 6978 in the *compiler* module. Tuples are 86 | immutable and hashable and thus suitable as dict indices. Whereas a 87 | singleton tuple literal (x,) is valid as an index, the *compiler* 88 | module parses it as x when it appears. 89 | 90 | *test_dis* compares "disassembled" Python byte code to what is 91 | expected. While byte code for a tidied script should be functionally 92 | equivalent to the untidied version, it will not necessarily be 93 | identical. 94 | 95 | *test_trace* compares the line numbers in a functional trace of a 96 | running script with those expected. A statement in a tidied script 97 | will generally have a line number slightly different from the same 98 | statement in the untidied version. 99 | 100 | *test_doctest* is an extensive suite of tests of the *doctest* module, 101 | which itself is used to document test code within doc strings and at 102 | need to compare instant results against those expected. One of the 103 | tests in *test_doctest* appears to require line numbers consistent 104 | with expectations, but tidied scripts generally violate such 105 | conditions as explained above. 106 | 107 | The more esoteric capabilities of PythonTidy.py had to be turned off 108 | to avoid corrupting the test-suite code. In practice, you\'ll want to 109 | run with PERSONAL = True (See, below.) to use all the functionality, 110 | and of course you\'ll have the good taste to find and patch all the 111 | glitches it introduces. 112 | 113 | [1] "Humdrum: A low cart with three wheels, drawn by one horse." The 114 | Collaborative International Dictionary of English v.0.48. 115 | 116 | ''' 117 | 118 | from __future__ import division 119 | 120 | DEBUG = False 121 | PERSONAL = False 122 | 123 | VERSION = '1.22' # 2012 Mar 04 124 | 125 | # 2012 Mar 04 . v1.22 . ccr . For Sakae Kobayashi: Fixed wrong default 126 | # . . for OVERRIDE_NEWLINE. 127 | # 2010 Sep 08 . v1.21 . ccr . For Nikolai Prokoschenko: 128 | # 129 | # o When double spacing is prescribed by PEP 8, do it before 130 | # leading comments. 131 | # 132 | # o Per Pep 8, double space around top-level classes only. 133 | # 134 | # o Don't split index values from keys before colon. 135 | # 136 | # o Preserve spelling of words in long strings containing special 137 | # characters. 138 | # 139 | # o Optionally, bring closing brackets, braces, and parens of split 140 | # series back left to the margin of the enclosing statement. See 141 | # JAVA_STYLE_LIST_DEDENT. 142 | # 143 | # 2010 Mar 10 . v1.20 . ccr . For Kuang-che Wu: 144 | # 145 | # o Optionally preserve unassigned constants so that code to be tidied 146 | # may contain blocks of commented-out lines that have been no-op'ed 147 | # with leading and trailing triple quotes. Python scripts may declare 148 | # constants without assigning them to a variables, but PythonTidy 149 | # considers this wasteful and normally elides them. 150 | # 151 | # o Generalize an earlier exception made for PythonDoc sentinels so 152 | # that the COMMENT_PREFIX is not inserted before any comments that 153 | # start with doubled number-signs. 154 | # 155 | # o Optionally omit parentheses around tuples, which are superfluous 156 | # after all. Normal PythonTidy behavior will be still to include them 157 | # as a sort of tuple display analogous to list displays, dict 158 | # displays, and yet-to-come set displays. 159 | # 160 | # o Kuang-che Wu has provided code that removes superfluous parens in 161 | # complex algebraic and logical expressions, which PythonTidy used to 162 | # interpolate to make operator precedence explicit. From now on 163 | # PythonTidy will rely upon default operator precedence and insert 164 | # parens only to enforce order of evaluation that is not default. 165 | # This should make tidied code more succinct, which usually results in 166 | # improved legibility. This fixes a PythonTidy bug noticed by 167 | # Kuang-che Wu having to do with order of evaluation of comparisons. 168 | # 169 | # o As a matter of style per PEP 308, parentheses are preferred around 170 | # conditional expressions. 171 | # 172 | # o Give the bitwise invert operator the same precedence as unary plus 173 | # and unary minus. 174 | # 175 | # I am making other changes to PythonTidy so that a few more of the 176 | # examples from the Python *test* module will pass: 177 | # 178 | # o Index literal pool by type. (Use *repr*.) 179 | # 180 | # o Never append a trailing comma to starred or double-starred 181 | # arguments. 182 | 183 | # 2009 Jun 29 . v1.19 . ccr . For Daniel G. Siegel at 184 | # http://home.cs.tum.edu, *python* 2.6 tokenizer returns newlines 185 | # separate from comments, so, though it may be necessary to save 186 | # newlines, it won't do for them to overlay comments. 187 | 188 | # 2009 Feb 05 . v1.18 . ccr . For Massimo Di Pierro at 189 | # http://mdp.cti.depaul.edu/, do not break up raw literals. 190 | 191 | # 2008 Jan 30 . v1.17 . ccr . This fixes regression in newline support 192 | # introduced at v1.11, which was first reported by Dr0id. 193 | 194 | # 2008 Jan 06 . v1.16 . ccr . John Machin demonstrates that hex values 195 | # are not in fact stored in the literal pool. They should always have 196 | # been and should always be. 197 | 198 | # Apparently, doubled number-signs in columns one and two are 199 | # sacrosanct sentinels in Fredrik Lundh's PythonDoc documentation 200 | # generator and must not therefore be disturbed. 201 | 202 | # Fix a crash caused by indents' crossing the centerline. 203 | 204 | # 2007 May 25 . v1.15 . ccr . Don't split lines in the middle of 205 | # function-parameter assignment. 206 | 207 | # Optionally wrap doc strings and comments to COL_LIMIT. 208 | 209 | # 2007 May 01, 23, 24 . v1.14 . ccr . Gaëtan de Menten at 210 | # http://openhex.org points out that a null statement is generated by 211 | # a trailing semicolon. This has been fixed. He has been helpful by, 212 | # among other things, insisting that I clean up the rendering of doc 213 | # strings and comments. 214 | 215 | # Forcing string-literal delimiters to quotes or apostrophes (if 216 | # required) is now done before storing them to the literal pool. 217 | 218 | # Wrap chunks of code whose successors cannot be wrapped. 219 | 220 | # Don't elide leading tabs in comments and doc strings. Instead 221 | # substitute DOC_TAB_REPLACEMENT so they can be edited out manually. 222 | 223 | # Split long string literals at spaces when CAN_SPLIT_STRINGS is True. 224 | 225 | # String literals with attributes are no longer parenthesized. 226 | 227 | # For François Pinard, wrap before operators. 228 | 229 | # Subscripted class attributes are no longer parenthesized. 230 | 231 | # Differentiate MAX_SEPS for different situations. 232 | 233 | # 2007 Mar 06 . v1.12 . ccr . The requests of Aaron Bingham: Specify 234 | # boilerplate to be inserted after the module doc string. Optionally 235 | # split wide string literals at the column limit. Force trailing 236 | # newline. 237 | 238 | # 2007 Jan 22 . v1.11 . ccr . This update implements a couple of 239 | # well-taken user requests: 240 | 241 | # Jens Diemer wants a module-level function, *tidy_up*, to accept file 242 | # names or file-like objects. 243 | 244 | # Wolfgang Grafen wants trailing spaces eliminated to avoid spurious 245 | # differences with pre-tidied code. 246 | 247 | # 2007 Jan 14 . v1.10 . ccr . There was a big problem with earlier 248 | # versions: Canonical values were substituted for strings and numbers. 249 | # For example, decimal integers were substituted for hexadecimal, and 250 | # escaped strings for raw strings. Authors of Python scripts usually 251 | # use peculiar notations for peculiar purposes, and doing away with 252 | # them negatively impacts the readability of the code. 253 | 254 | # This version preserves the original constants (parsed by *tokenize*) 255 | # in a literal pool indexed by the value they evaluate to. The 256 | # canonical values (output by *compiler*) are then translated back 257 | # (when possible) to the original constants by looking them up in the 258 | # literal pool. 259 | 260 | # 2006 Dec 19 . v1.9 . ccr . If class name is a string, pass it to 261 | # personal substitutions routine to distinguish module globals like 262 | # gtk.VBox from class attributes like gtk.Dialog.vbox. 263 | 264 | # 2006 Dec 17 . v1.8 . ccr . Trailing comma in function parameter list 265 | # is not allowed in all cases. Catch substitutions that collide with 266 | # built-ins. 267 | 268 | # 2006 Dec 14 . v1.7 . ccr . Track line numbers on output. 269 | # Write a "Name Substitutions Report" on stderr. 270 | 271 | # 2006 Dec 13 . v1.6 . ccr . A *yield* may appear in parens when it is 272 | # the subject of an assignment; otherwise, not. 273 | 274 | # 2006 Dec 05 . v1.5 . ccr . Strings default to single quotes when 275 | # DOUBLE_QUOTED_STRINGS = False. Pass the newline convention from 276 | # input to output (transparently :-) ). 277 | 278 | # 2006 Dec 01 . v1.4 . ccr . Tighten qualifications for in-line 279 | # comments. Decode string nodes. Enclose doc strings in double 280 | # quotes. Allow file-name arguments. 281 | 282 | # 2006 Nov 30 . v1.3 . ccr . Safe check against names of *compiler* . 283 | # abstract syntax tree nodes rather than their classes to step around 284 | # one Python Version Dependency. 285 | 286 | import sys 287 | import os 288 | import codecs 289 | import StringIO 290 | import re 291 | import textwrap # 2007 May 25 292 | if DEBUG: 293 | import token 294 | import doctest 295 | import tokenize 296 | import compiler 297 | 298 | ZERO = 0 299 | SPACE = ' ' 300 | NULL = '' 301 | NA = -1 302 | APOST = "'" 303 | 304 | # Old code is parsed. New code is generated from the parsed version, 305 | # using these literals: 306 | 307 | COL_LIMIT = 72 308 | INDENTATION = ' ' 309 | ASSIGNMENT = ' = ' 310 | FUNCTION_PARAM_ASSIGNMENT = '=' 311 | FUNCTION_PARAM_SEP = ', ' 312 | LIST_SEP = ', ' 313 | SUBSCRIPT_SEP = ', ' 314 | DICT_COLON = ': ' 315 | SLICE_COLON = ':' 316 | COMMENT_PREFIX = '# ' # 2007 May 25 317 | SHEBANG = '#!/usr/bin/python' 318 | CODING = 'utf-8' 319 | CODING_SPEC = '# -*- coding: %s -*-' % CODING 320 | BOILERPLATE = NULL # 2007 Mar 06 321 | BLANK_LINE = NULL 322 | KEEP_BLANK_LINES = True 323 | ADD_BLANK_LINES_AROUND_COMMENTS = True 324 | MAX_SEPS_FUNC_DEF = 3 # 2007 May 24 325 | MAX_SEPS_FUNC_REF = 5 # 2007 May 24 326 | MAX_SEPS_SERIES = 5 # 2007 May 24 327 | MAX_SEPS_DICT = 3 # 2007 May 24 328 | MAX_LINES_BEFORE_SPLIT_LIT = 2 329 | LEFT_MARGIN = NULL 330 | LEFTJUST_DOC_STRINGS = False 331 | WRAP_DOC_STRINGS = False # 2007 May 25 332 | DOUBLE_QUOTED_STRINGS = False # 2006 Dec 05 333 | SINGLE_QUOTED_STRINGS = False # 2007 May 01 334 | RECODE_STRINGS = False # 2006 Dec 01 335 | OVERRIDE_NEWLINE = None # 2012 Mar 04 336 | CAN_SPLIT_STRINGS = False # 2007 Mar 06 337 | DOC_TAB_REPLACEMENT = '....' # 2007 May 24 338 | KEEP_UNASSIGNED_CONSTANTS = False # 2010 Mar 10 339 | PARENTHESIZE_TUPLE_DISPLAY = True # 2010 Mar 10 340 | JAVA_STYLE_LIST_DEDENT = False # 2010 Sep 08 341 | 342 | # Repertoire of name-transformation functions: 343 | 344 | 345 | def all_lower_case(str, **attribs): 346 | return str.lower() 347 | 348 | 349 | def all_upper_case(str, **attribs): 350 | return str.upper() 351 | 352 | 353 | def title_case(str, **attribs): 354 | return str.title() 355 | 356 | 357 | def strip_underscores(str, **attribs): 358 | return str.replace('_', NULL) 359 | 360 | 361 | def insert_underscores(str, **attribs): 362 | return UNDERSCORE_PATTERN.sub('_\\1', str) 363 | 364 | 365 | def is_magic(str): 366 | return str in ['self', 'cls'] or str.startswith('__') and str.endswith('__') 367 | 368 | 369 | def underscore_to_camel_case(str, **attribs): 370 | if is_magic(str): 371 | return str 372 | else: 373 | return strip_underscores(title_case(camel_case_to_underscore(str))) 374 | 375 | 376 | def camel_case_to_underscore(str, **attribs): 377 | if is_magic(str): 378 | return str 379 | else: 380 | return all_lower_case(insert_underscores(str)) 381 | 382 | 383 | def unmangle(str, **attribs): 384 | if str.startswith('__'): 385 | str = str[2:] 386 | return str 387 | 388 | 389 | def munge(str, **attribs): 390 | """Create an unparsable name. 391 | 392 | """ 393 | 394 | return '<*%s*>' % str 395 | 396 | 397 | def substitutions(str, **attribs): 398 | result = SUBSTITUTE_FOR.get(str, str) 399 | module = attribs.get('module') # 2006 Dec 19 400 | if module is None: 401 | pass 402 | else: 403 | result = SUBSTITUTE_FOR.get('%s.%s' % (module, str), result) 404 | return result 405 | 406 | 407 | def elide_c(str, **attribs): 408 | return ELIDE_C_PATTERN.sub('\\1', str) 409 | 410 | 411 | def elide_a(str, **attribs): 412 | return ELIDE_A_PATTERN.sub('\\1', str) 413 | 414 | 415 | def elide_f(str, **attribs): 416 | return ELIDE_F_PATTERN.sub('\\1', str) 417 | 418 | 419 | # Name-transformation scripts: 420 | 421 | LOCAL_NAME_SCRIPT = [] 422 | GLOBAL_NAME_SCRIPT = [] 423 | CLASS_NAME_SCRIPT = [] 424 | FUNCTION_NAME_SCRIPT = [] 425 | 426 | # It is not wise to monkey with the 427 | # spelling of function names (methods) 428 | # where they are defined unless you are 429 | # willing to change their spelling where 430 | # they are referred to as class 431 | # attributes, too. 432 | 433 | FORMAL_PARAM_NAME_SCRIPT = [] 434 | 435 | # It is not wise to monkey with the 436 | # spelling of formal parameters for fear 437 | # of changing those of functions 438 | # (methods) defined in other modules. 439 | 440 | ATTR_NAME_SCRIPT = [] 441 | 442 | # It is not wise to monkey with the 443 | # spelling of attributes (methods) for 444 | # fear of changing those of classes 445 | # defined in other modules. 446 | 447 | # Author's preferences: 448 | 449 | if PERSONAL: 450 | LEFTJUST_DOC_STRINGS = True 451 | LOCAL_NAME_SCRIPT.extend([unmangle, camel_case_to_underscore]) 452 | GLOBAL_NAME_SCRIPT.extend([unmangle, camel_case_to_underscore, 453 | all_upper_case]) 454 | CLASS_NAME_SCRIPT.extend([elide_c, underscore_to_camel_case]) 455 | FUNCTION_NAME_SCRIPT.extend([camel_case_to_underscore]) 456 | FORMAL_PARAM_NAME_SCRIPT.extend([elide_a, camel_case_to_underscore]) 457 | ATTR_NAME_SCRIPT.extend([elide_f, camel_case_to_underscore, 458 | substitutions]) 459 | 460 | # Other global constants: 461 | 462 | UNDERSCORE_PATTERN = re.compile('(?<=[a-z])([A-Z])') 463 | COMMENT_PATTERN = re.compile('([^#]*?)#\s?') # 2007 May 25 464 | SHEBANG_PATTERN = re.compile('#!') 465 | CODING_PATTERN = re.compile('coding[=:]\\s*([.\\w\\-_]+)') 466 | NEW_LINE_PATTERN = re.compile(r'(?>> force_quote("See the cat.", quoted=False) 681 | '"See the cat."' 682 | >>> force_quote("'See the cat.'") 683 | '"See the cat."' 684 | >>> force_quote("'See the cat.'", double=False) 685 | "'See the cat.'" 686 | >>> force_quote('"See the cat."') 687 | '"See the cat."' 688 | >>> force_quote('"See the cat."', double=False) 689 | "'See the cat.'" 690 | >>> force_quote('"\"That\'s that,\" said the cat."') 691 | '"\\"That\'s that,\\" said the cat."' 692 | >>> force_quote('"\"That\'s that,\" said the cat."', double=False) 693 | '\'"That\\\'s that," said the cat.\'' 694 | >>> force_quote("'\"That\'s that,\" said the cat.'") 695 | '"\\"That\'s that,\\" said the cat."' 696 | >>> force_quote("ru'ick'") 697 | 'ru"ick"' 698 | >>> force_quote("ru'ick'", double=False) 699 | "ru'ick'" 700 | >>> force_quote('ru"ick"') 701 | 'ru"ick"' 702 | >>> force_quote('ru"ick"', double=False) 703 | "ru'ick'" 704 | >>> force_quote("'''ick'''", double=False) 705 | "'''ick'''" 706 | 707 | """ 708 | 709 | if quoted: # 2007 May 23 710 | match = QUOTE_PATTERN.match(encoded) 711 | if match is None: # 2008 Jan 06 712 | prefix = NULL 713 | size = 1 714 | else: 715 | (prefix, quote_old) = match.group(1, 2) 716 | encoded = QUOTE_PATTERN.sub(NULL, encoded, 1) 717 | size = len(quote_old) 718 | assert encoded[-size:] == quote_old 719 | encoded = encoded[:-size] 720 | else: 721 | prefix = NULL 722 | size = 1 723 | double_backslash_delimited_substrings = encoded.split(r'\\') 724 | for (ndx, substring) in enumerate(double_backslash_delimited_substrings): 725 | substring = substring.replace(r'\"','"').replace(r"\'","'") 726 | if double: 727 | substring = substring.replace('"',r'\"') 728 | else: 729 | substring = substring.replace("'",r"\'") 730 | double_backslash_delimited_substrings[ndx] = substring 731 | encoded = r'\\'.join(double_backslash_delimited_substrings) 732 | if double: 733 | quote_new = '"' * size 734 | else: 735 | quote_new = "'" * size 736 | result = NULL.join([prefix, quote_new, encoded, quote_new]) 737 | return result 738 | 739 | 740 | def wrap_lines(lines, width=COL_LIMIT, 741 | initial_indent=NULL, subsequent_indent=NULL): # 2007 May 25 742 | 743 | """Wrap lines of text, preserving blank lines. 744 | 745 | Lines is a Python list of strings *without* new-line terminators. 746 | 747 | Initial_indent is a string that will be prepended to the first 748 | line of wrapped output. 749 | 750 | Subsequent_indent is a string that will be prepended to all lines 751 | of wrapped output except the first. 752 | 753 | The result is a Python list of strings *without* new-Line terminators. 754 | 755 | >>> print '\\n'.join(wrap_lines('''Now is the time 756 | ... for every good man 757 | ... to come to the aid of his party. 758 | ... 759 | ... 760 | ... Don't pass the buck 761 | ... but give your buck 762 | ... to the party of your choice.'''.splitlines(), width=40)) 763 | Now is the time for every good man to 764 | come to the aid of his party. 765 | 766 | Don't pass the buck but give your buck 767 | to the party of your choice. 768 | 769 | """ 770 | 771 | DOC_WRAPPER.width = width 772 | DOC_WRAPPER.initial_indent = initial_indent 773 | DOC_WRAPPER.subsequent_indent = subsequent_indent 774 | result = [line.strip() for line in lines] 775 | result = '\n'.join(result) 776 | pgraphs = PGRAPH_PATTERN.split(result) 777 | result = [] 778 | while pgraphs: 779 | pgraph = DOC_WRAPPER.fill(pgraphs.pop(ZERO)) 780 | result.extend(pgraph.splitlines()) 781 | if pgraphs: 782 | result.append(NULL) 783 | return result 784 | 785 | 786 | def leftjust_lines(lines): # 2007 May 25 787 | 788 | """Left justify lines of text. 789 | 790 | Lines is a Python list of strings *without* new-line terminators. 791 | 792 | The result is a Python list of strings *without* new-Line terminators. 793 | 794 | """ 795 | 796 | result = [line.strip() for line in lines] 797 | return result 798 | 799 | 800 | class InputUnit(object): 801 | 802 | """File-buffered wrapper for sys.stdin. 803 | 804 | """ 805 | 806 | def __init__(self, file_in): 807 | object.__init__(self) 808 | self.is_file_like = hasattr(file_in, 'read') # 2007 Jan 22 809 | if self.is_file_like: 810 | buffer = file_in.read() # 2006 Dec 05 811 | else: 812 | unit = open(os.path.expanduser(file_in), 'rb') 813 | buffer = unit.read() # 2006 Dec 05 814 | unit.close() 815 | self.lines = UNIVERSAL_NEW_LINE_PATTERN.split(buffer) # 2006 Dec 05 816 | if len(self.lines) > 2: 817 | if OVERRIDE_NEWLINE is None: 818 | self.newline = self.lines[1] # ... the first delimiter. 819 | else: 820 | self.newline = OVERRIDE_NEWLINE 821 | look_ahead = '\n'.join([self.lines[ZERO],self.lines[2]]) 822 | else: 823 | self.newline = '\n' 824 | look_ahead = NULL 825 | match = CODING_PATTERN.search(look_ahead) 826 | if match is None: 827 | self.coding = 'ascii' 828 | else: 829 | self.coding = match.group(1) 830 | self.rewind() # 2006 Dec 05 831 | return 832 | 833 | def rewind(self): # 2006 Dec 05 834 | self.ndx = ZERO 835 | self.end = len(self.lines) - 1 836 | return self 837 | 838 | def next(self): # 2006 Dec 05 839 | if self.ndx > self.end: 840 | raise StopIteration 841 | elif self.ndx == self.end: 842 | result = self.lines[self.ndx] 843 | else: 844 | result = self.lines[self.ndx] + '\n' 845 | self.ndx += 2 846 | return result 847 | 848 | def __iter__(self): # 2006 Dec 05 849 | return self 850 | 851 | def readline(self): # 2006 Dec 05 852 | try: 853 | result = self.next() 854 | except StopIteration: 855 | result = NULL 856 | return result 857 | 858 | def readlines(self): # 2006 Dec 05 859 | self.rewind() 860 | return [line for line in self] 861 | 862 | def __str__(self): # 2006 Dec 05 863 | result = self.readlines() 864 | while result[:-1] == NULL: 865 | result.pop(-1) 866 | last_line = result[-1] 867 | if last_line[:-1] == '\n': # 2007 Mar 07 868 | pass 869 | else: 870 | last_line += '\n' 871 | result[-1] = last_line 872 | return NULL.join(result) 873 | 874 | def decode(self, str): 875 | return str # It will not do to feed Unicode to *compiler.parse*. 876 | 877 | 878 | class OutputUnit(object): 879 | 880 | """Line-buffered wrapper for sys.stdout. 881 | 882 | """ 883 | 884 | def __init__(self, file_out): 885 | object.__init__(self) 886 | self.is_file_like = hasattr(file_out, 'write') # 2007 Jan 22 887 | if self.is_file_like: 888 | self.unit = codecs.getwriter(CODING)(file_out) 889 | else: 890 | self.unit = codecs.open(os.path.expanduser(file_out), 'wb', CODING) 891 | self.blank_line_count = 1 892 | self.margin = LEFT_MARGIN 893 | self.newline = INPUT.newline # 2006 Dec 05 894 | self.lineno = ZERO # 2006 Dec 14 895 | self.buffer = NULL 896 | self.chunks = None # 2009 Oct 26 897 | return 898 | 899 | def close(self): # 2006 Dec 01 900 | self.unit.write(self.buffer) # 2007 Jan 22 901 | if self.is_file_like: 902 | pass 903 | else: 904 | self.unit.close() 905 | return self 906 | 907 | def line_init(self, indent=ZERO, lineno=ZERO): 908 | self.blank_line_count = ZERO 909 | self.col = ZERO 910 | if DEBUG: 911 | margin = '%5i %s' % (lineno, INDENTATION * indent) 912 | else: 913 | margin = self.margin + INDENTATION * indent 914 | self.tab_stack = [] 915 | self.tab_set(len(margin) + len(INDENTATION)) 916 | self.chunks = [] 917 | self.line_more(margin) 918 | return self 919 | 920 | def line_more( 921 | self, 922 | chunk=NULL, 923 | tab_set=False, 924 | tab_clear=False, 925 | can_split_str=False, 926 | can_split_after=False, 927 | can_break_after=False, 928 | ): # 2007 Mar 06 929 | self.chunks.append([ 930 | chunk, 931 | tab_set, 932 | tab_clear, 933 | can_split_str, 934 | can_split_after, 935 | can_break_after, 936 | ]) 937 | self.col += len(chunk) 938 | return self 939 | 940 | def line_term(self, pause=False): # 2007 May 25 941 | 942 | def is_split_needed(cumulative_width): 943 | pos = self.pos 944 | return ((pos + cumulative_width) > COL_LIMIT) and (pos > ZERO) # 2007 May 01 945 | 946 | def drop_word(chunk, can_split_after): # 2007 May 23 947 | result = COL_LIMIT - self.pos 948 | if can_split_after: 949 | result -= 1 950 | else: 951 | result -= 2 952 | ndx = result - 1 953 | while (ndx >= 20) and ((result - ndx) <= 20): 954 | if chunk[ndx] in [SPACE]: 955 | result = ndx + 1 956 | break 957 | ndx -= 1 958 | return result 959 | 960 | self.pos = ZERO 961 | can_split_before = False 962 | can_break_before = False 963 | cumulative_width = ZERO 964 | chunk_lengths = [] 965 | self.chunks.reverse() 966 | for ( 967 | chunk, 968 | tab_set, 969 | tab_clear, 970 | can_split_str, 971 | can_split_after, 972 | can_break_after, 973 | ) in self.chunks: # 2007 May 01 974 | if can_split_after or can_break_after: 975 | cumulative_width = ZERO 976 | cumulative_width += len(chunk) 977 | chunk_lengths.insert(ZERO, [ 978 | chunk, 979 | cumulative_width, 980 | tab_set, 981 | tab_clear, 982 | can_split_str, 983 | can_split_after, 984 | can_break_after, 985 | ]) 986 | for ( 987 | chunk, 988 | cumulative_width, 989 | tab_set, 990 | tab_clear, 991 | can_split_str, 992 | can_split_after, 993 | can_break_after, 994 | ) in chunk_lengths: # 2007 May 01 995 | if is_split_needed(cumulative_width): 996 | if can_split_before: 997 | self.line_split() 998 | elif can_break_before: 999 | self.line_break() 1000 | if can_split_str: # 2007 Mar 06 1001 | quote = chunk[:1] 1002 | while is_split_needed(len(chunk)): 1003 | take = drop_word(chunk, can_split_after) # 2007 May 23 1004 | if take < 20: 1005 | break 1006 | self.put(chunk[:take] + quote) 1007 | chunk = quote + chunk[take:] 1008 | if can_split_after: 1009 | self.line_split() 1010 | else: 1011 | self.line_break() 1012 | self.put(chunk) 1013 | else: 1014 | self.put(chunk) # 2006 Dec 14 1015 | self.pos += len(chunk) 1016 | if tab_set: 1017 | self.tab_set(self.pos) 1018 | if tab_clear: 1019 | self.tab_clear() 1020 | can_split_before = can_split_after 1021 | can_break_before = can_break_after 1022 | if pause: # 2007 May 25 1023 | pass 1024 | else: 1025 | self.put(self.newline) # 2006 Dec 05 1026 | return self 1027 | 1028 | def line_split(self): 1029 | self.put(self.newline) # 2006 Dec 05 1030 | self.pos = self.tab_forward() 1031 | return self 1032 | 1033 | def line_break(self): 1034 | self.put('\\%s' % self.newline) # 2006 Dec 14 1035 | self.pos = self.tab_forward() 1036 | return self 1037 | 1038 | def tab_forward(self): 1039 | if len(self.tab_stack) > 1: 1040 | col = (self.tab_stack)[1] 1041 | else: 1042 | col = (self.tab_stack)[ZERO] 1043 | self.put(SPACE * col) # 2006 Dec 14 1044 | return col 1045 | 1046 | def put(self, text): # 2006 Dec 14 1047 | self.lineno += text.count(self.newline) 1048 | self.buffer += text # 2007 Jan 22 1049 | if self.buffer.endswith('\n') or self.buffer.endswith('\r'): # 2008 Jan 30 1050 | self.unit.write(self.buffer.rstrip()) 1051 | self.unit.write(self.newline) # 2008 Jan 30 1052 | self.buffer = NULL 1053 | return self 1054 | 1055 | def put_blank_line(self, trace, count=1): 1056 | count -= self.blank_line_count 1057 | while count > ZERO: 1058 | self.put(BLANK_LINE) # 2006 Dec 14 1059 | self.put(self.newline) # 2006 Dec 05 1060 | if DEBUG: 1061 | self.put('blank(%s)' % str(trace)) # 2006 Dec 14 1062 | self.blank_line_count += 1 1063 | count -= 1 1064 | return self 1065 | 1066 | def tab_set(self, col): 1067 | if col > COL_LIMIT / 2: 1068 | if self.tab_stack: # 2008 Jan 06 1069 | col = (self.tab_stack)[-1] + 4 1070 | else: 1071 | col = 4 1072 | self.tab_stack.append(col) 1073 | return self 1074 | 1075 | def tab_clear(self): 1076 | if len(self.tab_stack) > 1: 1077 | result = self.tab_stack.pop() 1078 | else: 1079 | result = None 1080 | return result 1081 | 1082 | def inc_margin(self): 1083 | self.margin += INDENTATION 1084 | return self 1085 | 1086 | def dec_margin(self): 1087 | self.margin = (self.margin)[:-len(INDENTATION)] 1088 | return self 1089 | 1090 | 1091 | class Comments(dict): 1092 | 1093 | """Collection of comments (blank lines) parsed out of the 1094 | input Python code and indexed by line number. 1095 | 1096 | """ 1097 | 1098 | def __init__(self): 1099 | 1100 | def quote_original(token_type, original): # 2007 May 01 1101 | if token_type in [tokenize.STRING]: 1102 | if DOUBLE_QUOTED_STRINGS: 1103 | result = force_quote(original, double=True) 1104 | elif SINGLE_QUOTED_STRINGS: 1105 | result = force_quote(original, double=False) 1106 | else: 1107 | result = original 1108 | else: 1109 | result = original 1110 | return result 1111 | 1112 | def compensate_for_tabs(line, scol): # 2007 May 25 1113 | match = COMMENT_PATTERN.match(line) 1114 | if match is None: 1115 | pass 1116 | else: 1117 | margin = match.group(1) 1118 | tab_count = margin.count('\t') 1119 | scol += (len(INDENTATION) - 1) * tab_count 1120 | return scol 1121 | 1122 | def merge_concatenated_strings(lines): # 2010 Sep 08 1123 | 1124 | """Save whole string in literal pool. 1125 | 1126 | Python (and the *compiler* module) treat adjacent strings 1127 | without an intervening operator as one string. The 1128 | *tokenize* module does not. Thus, although the parts are 1129 | easily saved in PythonTidy's literal pool, the whole 1130 | string is not. This routine makes a full pass through the 1131 | tokens and accumulates adjacent strings so that the whole 1132 | string is saved in the literal pool along with its 1133 | original spelling. 1134 | 1135 | This preserves the original spelling of words even in 1136 | especially long phrases that would otherwise be normalized 1137 | with escape sequences for embedded \"special\" characters. 1138 | 1139 | The original spelling is stored in the literal pool, 1140 | indexed by the normalized version. If a lookup of the 1141 | normalized version succeeds, the original spelling is 1142 | output; otherwise, the normalized version is used instead. 1143 | 1144 | """ 1145 | try: 1146 | while True: 1147 | prev_item = lines.next() 1148 | yield prev_item 1149 | ( 1150 | prev_token_type, 1151 | prev_token_string, 1152 | prev_start, 1153 | prev_end, 1154 | prev_line, 1155 | ) = prev_item 1156 | if prev_token_type in [tokenize.STRING]: 1157 | on1 = True 1158 | while True: 1159 | next_item = lines.next() 1160 | yield next_item 1161 | ( 1162 | next_token_type, 1163 | next_token_string, 1164 | next_start, 1165 | next_end, 1166 | next_line, 1167 | ) = next_item 1168 | if next_token_type in [tokenize.STRING]: 1169 | if prev_token_string[-1] == next_token_string[ZERO]: 1170 | prev_token_string = prev_token_string[:-1] + \ 1171 | next_token_string[1:] 1172 | on1 = False 1173 | else: 1174 | if on1: 1175 | pass 1176 | else: 1177 | prev_item = ( 1178 | prev_token_type, 1179 | prev_token_string, 1180 | prev_start, 1181 | prev_end, 1182 | prev_line, 1183 | ) 1184 | yield prev_item 1185 | break 1186 | except NotImplementedError: 1187 | pass 1188 | return 1189 | 1190 | self.literal_pool = {} # 2007 Jan 14 1191 | lines = tokenize.generate_tokens(INPUT.readline) 1192 | lines = merge_concatenated_strings(lines) # 2010 Sep 08 1193 | for (token_type, token_string, start, end, line) in lines: 1194 | if DEBUG: 1195 | print (token.tok_name)[token_type], token_string, start, \ 1196 | end, line 1197 | (self.max_lineno, scol) = start 1198 | (erow, ecol) = end 1199 | if token_type in [tokenize.COMMENT, tokenize.NL]: 1200 | original = token_string 1201 | original = original.decode(INPUT.coding) 1202 | original = original.replace('\t', DOC_TAB_REPLACEMENT) # 2007 May 24 1203 | original = original.strip() 1204 | if SHEBANG_PATTERN.match(original) is not None: 1205 | pass 1206 | elif CODING_PATTERN.search(original) is not None and \ 1207 | self.max_lineno <= 2: 1208 | pass 1209 | else: 1210 | scol = compensate_for_tabs(line, scol) # 2007 May 25 1211 | original = COMMENT_PATTERN.sub(NULL, original, 1) # 2007 May 25 1212 | if (token_type in [tokenize.COMMENT]) and (original in [NULL]): 1213 | original = SPACE 1214 | if self.max_lineno in self: # 2009 Jun 29 1215 | pass 1216 | else: 1217 | self[self.max_lineno] = [scol, original] 1218 | elif token_type in [tokenize.NUMBER, tokenize.STRING]: # 2007 Jan 14 1219 | try: 1220 | original = token_string.strip().decode(INPUT.coding, 'backslashreplace') 1221 | decoded = eval(original) # 2007 May 01 1222 | encoded = repr(decoded) 1223 | if (encoded == original) or (encoded == force_quote(original, double=False)): 1224 | pass 1225 | else: 1226 | original = quote_original(token_type, original) # 2007 May 01 1227 | original_values = \ 1228 | self.literal_pool.setdefault(encoded, []) # 2010 Mar 10 1229 | for (tok, lineno) in original_values: # 2007 Jan 17 1230 | if tok == original: 1231 | break 1232 | else: 1233 | original_values.append([original, self.max_lineno]) 1234 | except: 1235 | pass 1236 | self.prev_lineno = -2 # 2010 Mar 10 1237 | self[self.prev_lineno] = (NA, SHEBANG) # 2007 May 25 1238 | self[NA] = (NA, CODING_SPEC) # 2007 May 25 1239 | return 1240 | 1241 | def merge(self, lineno=None, fin=False): 1242 | 1243 | def is_blank(): 1244 | return token_string in [NULL, BLANK_LINE] 1245 | 1246 | def is_blank_line_needed(): 1247 | return ADD_BLANK_LINES_AROUND_COMMENTS and not (is_blank() and 1248 | KEEP_BLANK_LINES) 1249 | 1250 | def margin(scol): 1251 | (quotient, remainder) = divmod(scol, len(INDENTATION)) 1252 | result = INDENTATION * quotient + SPACE * remainder + COMMENT_PREFIX 1253 | return result 1254 | 1255 | def strip_blank_lines(text_lines): 1256 | first = NA 1257 | last = NA 1258 | is_first_blank = False 1259 | is_last_blank = False 1260 | if text_lines: 1261 | first = ZERO 1262 | (scol, line) = text_lines[first] 1263 | is_first_blank = (scol == NA) 1264 | if is_first_blank: 1265 | first += 1 1266 | last = len(text_lines) 1267 | (scol, line) = text_lines[last - 1] 1268 | is_last_blank = (scol == NA) 1269 | if is_last_blank: 1270 | last -= 1 1271 | return (first, last, is_first_blank, is_last_blank) 1272 | 1273 | if fin: 1274 | lineno = self.max_lineno + 1 1275 | on1 = True 1276 | text=[] # 2007 May 25 1277 | while self.prev_lineno < lineno: 1278 | if self.prev_lineno in self: 1279 | (scol, token_string) = self[self.prev_lineno] 1280 | if on1 and is_blank_line_needed(): 1281 | OUTPUT.put_blank_line(1) 1282 | if is_blank(): 1283 | if KEEP_BLANK_LINES: 1284 | # OUTPUT.put_blank_line(2) 1285 | text.append([NA, NULL]) # 2007 May 25 1286 | else: 1287 | if scol == NA: # 2007 May 25 1288 | 1289 | # Output the Shebang and Coding-Spec. 1290 | 1291 | OUTPUT.line_init().line_more(token_string).line_term() 1292 | else: 1293 | text.append([scol, token_string]) # 2007 May 25 1294 | on1 = False 1295 | self.prev_lineno += 1 1296 | if text and LEFTJUST_DOC_STRINGS: # 2007 May 25 1297 | (first, last, is_first_blank, is_last_blank) = strip_blank_lines(text) 1298 | lines = [line for (scol, line) in text[first: last]] 1299 | lines = leftjust_lines(lines) 1300 | text = [(ZERO, line) for line in lines] 1301 | if is_first_blank: 1302 | text.insert(ZERO, [NA, NULL]) 1303 | if is_last_blank: 1304 | text.append([NA, NULL]) 1305 | if text and WRAP_DOC_STRINGS: # 2007 May 25 1306 | (first, last, is_first_blank, is_last_blank) = strip_blank_lines(text) 1307 | text = text[first: last] 1308 | if text: 1309 | (save_col, line) = text[ZERO] 1310 | lines = [line for (scol, line) in text] 1311 | line_length = COL_LIMIT - (save_col + len(COMMENT_PREFIX)) 1312 | line_length = max(line_length, 20) 1313 | lines = wrap_lines(lines, width=line_length) 1314 | text = [(save_col, line) for line in lines] 1315 | if is_first_blank: 1316 | text.insert(ZERO, [NA, NULL]) 1317 | if is_last_blank: 1318 | text.append([NA, NULL]) 1319 | for (scol, line) in text: # 2007 May 25 1320 | if scol == NA: 1321 | OUTPUT.put_blank_line(2) 1322 | else: 1323 | OUTPUT.line_init() 1324 | margin_string = margin(scol) 1325 | if (margin_string == '# ') and (line.startswith('#')): # 2010 Mar 10 1326 | OUTPUT.line_more('#') # 2010 Mar 10 1327 | else: 1328 | OUTPUT.line_more(margin(scol)) 1329 | OUTPUT.line_more(line) 1330 | OUTPUT.line_term() 1331 | if text and is_blank_line_needed() and not fin: 1332 | OUTPUT.put_blank_line(3) 1333 | return self 1334 | 1335 | def put_inline(self, lineno): 1336 | 1337 | def margin(scol): 1338 | result = SPACE * scol + COMMENT_PREFIX 1339 | return result 1340 | 1341 | def new_line(): 1342 | OUTPUT.put(OUTPUT.newline) 1343 | return 1344 | 1345 | text=[] # 2007 May 25 1346 | while self.prev_lineno <= lineno: 1347 | if self.prev_lineno in self: 1348 | (scol, token_string) = self[self.prev_lineno] 1349 | if token_string in [NULL]: 1350 | pass 1351 | else: 1352 | text.append(token_string) # 2007 May 25 1353 | self.prev_lineno += 1 1354 | OUTPUT.line_term(pause=True) # 2007 May 25 1355 | col = OUTPUT.pos + 2 1356 | if WRAP_DOC_STRINGS: 1357 | line_length = COL_LIMIT - (col + len(COMMENT_PREFIX)) 1358 | line_length = max(line_length, 20) 1359 | text = wrap_lines(text, width=line_length) 1360 | for line in text[:1]: 1361 | OUTPUT.put(SPACE * 2) 1362 | OUTPUT.put(COMMENT_PREFIX) 1363 | OUTPUT.put(line) 1364 | new_line() 1365 | for line in text[1:]: 1366 | OUTPUT.line_init() 1367 | OUTPUT.line_more(margin(col)) 1368 | OUTPUT.line_more(line) 1369 | OUTPUT.line_term() 1370 | if text: 1371 | pass 1372 | else: 1373 | new_line() 1374 | return self 1375 | 1376 | 1377 | class Name(list): # 2006 Dec 14 1378 | 1379 | """Maps new name to old names. 1380 | 1381 | """ 1382 | 1383 | def __init__(self, new): 1384 | self.new = new 1385 | self.is_reported = False 1386 | return 1387 | 1388 | def append(self, item): 1389 | if item in self: 1390 | pass 1391 | else: 1392 | list.append(self, item) 1393 | return 1394 | 1395 | def rept_collision(self, key): 1396 | self.append(key) # 2006 Dec 17 1397 | if len(self) == 1: 1398 | pass 1399 | elif self.is_reported: 1400 | pass 1401 | else: 1402 | sys.stderr.write("Error: %s ambiguously replaced by '%s' at line %i.\n" % \ 1403 | (str(self), self.new, OUTPUT.lineno + 1)) 1404 | self.is_reported = True 1405 | return self 1406 | 1407 | def rept_external(self, expr): 1408 | if isinstance(expr, NodeName): 1409 | expr = expr.name.str 1410 | else: 1411 | expr = str(expr) 1412 | if expr in ['self','cls']: 1413 | pass 1414 | elif self.new == self[ZERO]: 1415 | pass 1416 | else: 1417 | sys.stderr.write("Warning: '%s.%s,' defined elsewhere, replaced by '.%s' at line %i.\n" % \ 1418 | (expr, self[ZERO], self.new, OUTPUT.lineno + 1)) 1419 | return self 1420 | 1421 | 1422 | class NameSpace(list): 1423 | 1424 | """Dictionary of names (variables). 1425 | 1426 | Actually a list of dictionaries. The current scope is the top one 1427 | (ZEROth member). 1428 | 1429 | """ 1430 | 1431 | def push_scope(self): 1432 | self.insert(ZERO, {}) 1433 | return self 1434 | 1435 | def pop_scope(self): 1436 | return self.pop(ZERO) 1437 | 1438 | def make_name(self, name, rules): 1439 | name = name.get_as_str() 1440 | key = name 1441 | for rule in rules: 1442 | name = rule(name) 1443 | name = self[ZERO].setdefault(name,Name(name)) # 2006 Dec 14 1444 | self[ZERO].setdefault(key,name) 1445 | name.append(key) 1446 | return name 1447 | 1448 | def make_local_name(self, name): 1449 | if self.is_global(): 1450 | result = self.make_global_name(name) 1451 | else: 1452 | result = self.make_name(name, LOCAL_NAME_SCRIPT) 1453 | return result 1454 | 1455 | def make_global_name(self, name): 1456 | return self.make_name(name, GLOBAL_NAME_SCRIPT) 1457 | 1458 | def make_class_name(self, name): 1459 | return self.make_name(name, CLASS_NAME_SCRIPT) 1460 | 1461 | def make_function_name(self, name): 1462 | return self.make_name(name, FUNCTION_NAME_SCRIPT) 1463 | 1464 | def make_formal_param_name(self, name): 1465 | return self.make_name(name, FORMAL_PARAM_NAME_SCRIPT) 1466 | 1467 | def make_imported_name(self, name): 1468 | return self.make_name(name, []) 1469 | 1470 | def make_attr_name(self, expr, name): 1471 | if isinstance(expr, NodeName): # 2006 Dec 19 1472 | module = expr.name.str 1473 | else: 1474 | module = None 1475 | name = name.get_as_str() 1476 | key = name 1477 | for rule in ATTR_NAME_SCRIPT: 1478 | name = rule(name, module=module) # 2006 Dec 19 1479 | name = Name(name) # 2006 Dec 14 1480 | name.append(key) 1481 | name.rept_external(expr) 1482 | return name.new 1483 | 1484 | def make_keyword_name(self, name): 1485 | name = name.get_as_str() 1486 | key = name 1487 | for rule in FORMAL_PARAM_NAME_SCRIPT: 1488 | name = rule(name) 1489 | name = Name(name) # 2006 Dec 14 1490 | name.append(key) 1491 | return name.new 1492 | 1493 | def get_name(self, node): 1494 | name = key = node.get_as_str() # 2006 Dec 17 1495 | for scope in self: 1496 | if key in scope: 1497 | name = scope[key] 1498 | name.rept_collision(key) # 2006 Dec 14 1499 | name = name.new 1500 | break 1501 | return name 1502 | 1503 | def has_name(self, node): 1504 | name = node.get_as_str() 1505 | return name in self[ZERO] 1506 | 1507 | def is_global(self): 1508 | return len(self) == 1 1509 | 1510 | 1511 | def transform(indent, lineno, node): 1512 | """Convert the nodes in the abstract syntax tree returned by the 1513 | *compiler* module to objects with *put* methods. 1514 | 1515 | The kinds of nodes are a Python Version Dependency. 1516 | 1517 | """ 1518 | 1519 | def isinstance_(node, class_name): # 2006 Nov 30 1520 | """Safe check against name of a node class rather than the 1521 | class itself, which may or may not be supported at the current 1522 | Python version. 1523 | 1524 | """ 1525 | 1526 | class_ = getattr(compiler.ast, class_name, None) 1527 | if class_ is None: 1528 | result = False 1529 | else: 1530 | result = isinstance(node, class_) 1531 | return result 1532 | 1533 | if isinstance_(node, 'Node') and node.lineno is not None: 1534 | lineno = node.lineno 1535 | if isinstance_(node, 'Add'): 1536 | result = NodeAdd(indent, lineno, node.left, node.right) 1537 | elif isinstance_(node, 'And'): 1538 | result = NodeAnd(indent, lineno, node.nodes) 1539 | elif isinstance_(node, 'AssAttr'): 1540 | result = NodeAsgAttr(indent, lineno, node.expr, node.attrname, 1541 | node.flags) 1542 | elif isinstance_(node, 'AssList'): 1543 | result = NodeAsgList(indent, lineno, node.nodes) 1544 | elif isinstance_(node, 'AssName'): 1545 | result = NodeAsgName(indent, lineno, node.name, node.flags) 1546 | elif isinstance_(node, 'AssTuple'): 1547 | result = NodeAsgTuple(indent, lineno, node.nodes) 1548 | elif isinstance_(node, 'Assert'): 1549 | result = NodeAssert(indent, lineno, node.test, node.fail) 1550 | elif isinstance_(node, 'Assign'): 1551 | result = NodeAssign(indent, lineno, node.nodes, node.expr) 1552 | elif isinstance_(node, 'AugAssign'): 1553 | result = NodeAugAssign(indent, lineno, node.node, node.op, node.expr) 1554 | elif isinstance_(node, 'Backquote'): 1555 | result = NodeBackquote(indent, lineno, node.expr) 1556 | elif isinstance_(node, 'Bitand'): 1557 | result = NodeBitAnd(indent, lineno, node.nodes) 1558 | elif isinstance_(node, 'Bitor'): 1559 | result = NodeBitOr(indent, lineno, node.nodes) 1560 | elif isinstance_(node, 'Bitxor'): 1561 | result = NodeBitXor(indent, lineno, node.nodes) 1562 | elif isinstance_(node, 'Break'): 1563 | result = NodeBreak(indent, lineno) 1564 | elif isinstance_(node, 'CallFunc'): 1565 | result = NodeCallFunc(indent, lineno, node.node, node.args, node.star_args, 1566 | node.dstar_args) 1567 | elif isinstance_(node, 'Class'): 1568 | result = NodeClass(indent, lineno, node.name, node.bases, node.doc, 1569 | node.code) 1570 | elif isinstance_(node, 'Compare'): 1571 | result = NodeCompare(indent, lineno, node.expr, node.ops) 1572 | elif isinstance_(node, 'Const'): 1573 | result = NodeConst(indent, lineno, node.value) 1574 | elif isinstance_(node, 'Continue'): 1575 | result = NodeContinue(indent, lineno) 1576 | elif isinstance_(node, 'Decorators'): 1577 | result = NodeDecorators(indent, lineno, node.nodes) 1578 | elif isinstance_(node, 'Dict'): 1579 | result = NodeDict(indent, lineno, node.items) 1580 | elif isinstance_(node, 'Discard'): 1581 | result = NodeDiscard(indent, lineno, node.expr) 1582 | elif isinstance_(node, 'Div'): 1583 | result = NodeDiv(indent, lineno, node.left, node.right) 1584 | elif isinstance_(node, 'Ellipsis'): 1585 | result = NodeEllipsis(indent, lineno) 1586 | elif isinstance_(node, 'Exec'): 1587 | result = NodeExec(indent, lineno, node.expr, node.locals, node.globals) 1588 | elif isinstance_(node, 'FloorDiv'): 1589 | result = NodeFloorDiv(indent, lineno, node.left, node.right) 1590 | elif isinstance_(node, 'For'): 1591 | result = NodeFor(indent, lineno, node.assign, node.list, node.body, 1592 | node.else_) 1593 | elif isinstance_(node, 'From'): 1594 | result = NodeFrom(indent, lineno, node.modname, node.names) 1595 | elif isinstance_(node, 'Function'): 1596 | result = NodeFunction( 1597 | indent, 1598 | lineno, 1599 | getattr(node, 'decorators', None), 1600 | node.name, 1601 | node.argnames, 1602 | node.defaults, 1603 | node.flags, 1604 | node.doc, 1605 | node.code, 1606 | ) 1607 | elif isinstance_(node, 'GenExpr'): 1608 | result = NodeGenExpr(indent, lineno, node.code) 1609 | elif isinstance_(node, 'GenExprFor'): 1610 | result = NodeGenExprFor(indent, lineno, node.assign, node.iter, 1611 | node.ifs) 1612 | elif isinstance_(node, 'GenExprIf'): 1613 | result = NodeGenExprIf(indent, lineno, node.test) 1614 | elif isinstance_(node, 'GenExprInner'): 1615 | result = NodeGenExprInner(indent, lineno, node.expr, node.quals) 1616 | elif isinstance_(node, 'Getattr'): 1617 | result = NodeGetAttr(indent, lineno, node.expr, node.attrname) 1618 | elif isinstance_(node, 'Global'): 1619 | result = NodeGlobal(indent, lineno, node.names) 1620 | elif isinstance_(node, 'If'): 1621 | result = NodeIf(indent, lineno, node.tests, node.else_) 1622 | elif isinstance_(node, 'IfExp'): 1623 | result = NodeIfExp(indent, lineno, node.test, node.then, node.else_) 1624 | elif isinstance_(node, 'Import'): 1625 | result = NodeImport(indent, lineno, node.names) 1626 | elif isinstance_(node, 'Invert'): 1627 | result = NodeInvert(indent, lineno, node.expr) 1628 | elif isinstance_(node, 'Keyword'): 1629 | result = NodeKeyword(indent, lineno, node.name, node.expr) 1630 | elif isinstance_(node, 'Lambda'): 1631 | result = NodeLambda(indent, lineno, node.argnames, node.defaults, 1632 | node.flags, node.code) 1633 | elif isinstance_(node, 'LeftShift'): 1634 | result = NodeLeftShift(indent, lineno, node.left, node.right) 1635 | elif isinstance_(node, 'List'): 1636 | result = NodeList(indent, lineno, node.nodes) 1637 | elif isinstance_(node, 'ListComp'): 1638 | result = NodeListComp(indent, lineno, node.expr, node.quals) 1639 | elif isinstance_(node, 'ListCompFor'): 1640 | result = NodeListCompFor(indent, lineno, node.assign, node.list, 1641 | node.ifs) 1642 | elif isinstance_(node, 'ListCompIf'): 1643 | result = NodeListCompIf(indent, lineno, node.test) 1644 | elif isinstance_(node, 'Mod'): 1645 | result = NodeMod(indent, lineno, node.left, node.right) 1646 | elif isinstance_(node, 'Module'): 1647 | result = NodeModule(indent, lineno, node.doc, node.node) 1648 | elif isinstance_(node, 'Mul'): 1649 | result = NodeMul(indent, lineno, node.left, node.right) 1650 | elif isinstance_(node, 'Name'): 1651 | result = NodeName(indent, lineno, node.name) 1652 | elif isinstance_(node, 'Not'): 1653 | result = NodeNot(indent, lineno, node.expr) 1654 | elif isinstance_(node, 'Or'): 1655 | result = NodeOr(indent, lineno, node.nodes) 1656 | elif isinstance_(node, 'Pass'): 1657 | result = NodePass(indent, lineno) 1658 | elif isinstance_(node, 'Power'): 1659 | result = NodePower(indent, lineno, node.left, node.right) 1660 | elif isinstance_(node, 'Print'): 1661 | result = NodePrint(indent, lineno, node.nodes, node.dest) 1662 | elif isinstance_(node, 'Printnl'): 1663 | result = NodePrintnl(indent, lineno, node.nodes, node.dest) 1664 | elif isinstance_(node, 'Raise'): 1665 | result = NodeRaise(indent, lineno, node.expr1, node.expr2, node.expr3) 1666 | elif isinstance_(node, 'Return'): 1667 | result = NodeReturn(indent, lineno, node.value) 1668 | elif isinstance_(node, 'RightShift'): 1669 | result = NodeRightShift(indent, lineno, node.left, node.right) 1670 | elif isinstance_(node, 'Slice'): 1671 | result = NodeSlice(indent, lineno, node.expr, node.flags, node.lower, 1672 | node.upper) 1673 | elif isinstance_(node, 'Sliceobj'): 1674 | result = NodeSliceobj(indent, lineno, node.nodes) 1675 | elif isinstance_(node, 'Stmt'): 1676 | result = NodeStmt(indent, lineno, node.nodes) 1677 | elif isinstance_(node, 'Sub'): 1678 | result = NodeSub(indent, lineno, node.left, node.right) 1679 | elif isinstance_(node, 'Subscript'): 1680 | result = NodeSubscript(indent, lineno, node.expr, node.flags, 1681 | node.subs) 1682 | elif isinstance_(node, 'TryExcept'): 1683 | result = NodeTryExcept(indent, lineno, node.body, node.handlers, 1684 | node.else_) 1685 | elif isinstance_(node, 'TryFinally'): 1686 | result = NodeTryFinally(indent, lineno, node.body, node.final) 1687 | elif isinstance_(node, 'Tuple'): 1688 | result = NodeTuple(indent, lineno, node.nodes) 1689 | elif isinstance_(node, 'UnaryAdd'): 1690 | result = NodeUnaryAdd(indent, lineno, node.expr) 1691 | elif isinstance_(node, 'UnarySub'): 1692 | result = NodeUnarySub(indent, lineno, node.expr) 1693 | elif isinstance_(node, 'While'): 1694 | result = NodeWhile(indent, lineno, node.test, node.body, node.else_) 1695 | elif isinstance_(node, 'With'): 1696 | result = NodeWith(indent, lineno, node.expr, node.vars, node.body) 1697 | elif isinstance_(node, 'Yield'): 1698 | result = NodeYield(indent, lineno, node.value) 1699 | elif isinstance(node, basestring): 1700 | result = NodeStr(indent, lineno, node) 1701 | elif isinstance(node, int): 1702 | result = NodeInt(indent, lineno, node) 1703 | else: 1704 | result = node 1705 | return result 1706 | 1707 | 1708 | class Node(object): 1709 | 1710 | """Parent of parsed tokens. 1711 | 1712 | """ 1713 | 1714 | tag = 'Generic node' 1715 | 1716 | def __init__(self, indent, lineno): 1717 | object.__init__(self) 1718 | self.indent = indent 1719 | self.lineno = lineno 1720 | if DEBUG: 1721 | sys.stderr.write('%5i %s\n' % (self.lineno, self.tag)) 1722 | return 1723 | 1724 | def line_init(self, need_blank_line=ZERO): 1725 | if COMMENTS.prev_lineno > ZERO: # 2010 Sep 08 1726 | OUTPUT.put_blank_line(41, count=need_blank_line) # 2010 Sep 08 1727 | need_blank_line -= 1 # 2010 Sep 08 1728 | COMMENTS.merge(self.get_lineno()) 1729 | OUTPUT.put_blank_line(4, count=need_blank_line) 1730 | OUTPUT.line_init(self.indent, self.get_lineno()) 1731 | return self 1732 | 1733 | def line_more( 1734 | self, 1735 | chunk=NULL, 1736 | tab_set=False, 1737 | tab_clear=False, 1738 | can_split_str=False, 1739 | can_split_after=False, 1740 | can_break_after=False, 1741 | ): 1742 | OUTPUT.line_more( 1743 | chunk, 1744 | tab_set, 1745 | tab_clear, 1746 | can_split_str, 1747 | can_split_after, 1748 | can_break_after, 1749 | ) 1750 | return self 1751 | 1752 | def line_term(self, lineno=ZERO): 1753 | lineno = max(self.get_hi_lineno(), self.get_lineno()) # , lineno) # 2006 Dec 01 1754 | COMMENTS.put_inline(lineno) 1755 | return self 1756 | 1757 | def put(self, can_split=False): 1758 | '''Place self on output. 1759 | 1760 | For the "Generic" node, this is abstract. A Generic node *is* 1761 | instantiated for nodes of unrecognized type, and we don\'t 1762 | know what to do for them, so we just place a string on output 1763 | that should force an error when Python is used to interpret 1764 | the result. 1765 | 1766 | ''' 1767 | 1768 | self.line_more(' /* %s at line %i */ ' % (self.tag, self.get_lineno())) 1769 | return self 1770 | 1771 | def get_lineno(self): 1772 | return self.lineno 1773 | 1774 | def get_hi_lineno(self): 1775 | return self.get_lineno() 1776 | 1777 | def inc_margin(self): 1778 | OUTPUT.inc_margin() 1779 | return self 1780 | 1781 | def dec_margin(self): 1782 | OUTPUT.dec_margin() 1783 | return self 1784 | 1785 | def marshal_names(self): 1786 | return self 1787 | 1788 | def make_local_name(self): 1789 | return self 1790 | 1791 | 1792 | class NodeOpr(Node): # 2010 Mar 10 1793 | 1794 | tag = 'Opr' 1795 | 1796 | def put_expr(self, node, can_split=False, pos=None): 1797 | if self.is_paren_needed(node, pos): 1798 | self.line_more('(', tab_set=True) 1799 | node.put(can_split=True) 1800 | self.line_more(')', tab_clear=True) 1801 | else: 1802 | node.put(can_split=can_split) 1803 | return self 1804 | 1805 | def is_paren_needed(self, node, pos): 1806 | return type(node) in OPERATOR_TRUMPS[type(self)] 1807 | 1808 | 1809 | class NodeOprAssoc(NodeOpr): # 2010 Mar 10 1810 | 1811 | tag = 'A_Opr' 1812 | 1813 | 1814 | class NodeOprNotAssoc(NodeOpr): # 2010 Mar 10 1815 | 1816 | tag = 'NA_Opr' 1817 | 1818 | def is_paren_needed(self, node, pos): 1819 | if NodeOpr.is_paren_needed(self, node, pos): 1820 | result = True 1821 | elif type(node) in OPERATOR_LEVEL[type(self)]: 1822 | result = True 1823 | else: 1824 | result = False 1825 | return result 1826 | 1827 | 1828 | class NodeOprLeftAssoc(NodeOpr): # 2010 Mar 10 1829 | 1830 | """Left-associative operator. 1831 | 1832 | """ 1833 | 1834 | tag = 'LA_Opr' 1835 | 1836 | def is_paren_needed(self, node, pos): 1837 | if NodeOpr.is_paren_needed(self, node, pos): 1838 | result = True 1839 | elif type(node) in OPERATOR_LEVEL[type(self)]: 1840 | result = not (pos == 'left') 1841 | else: 1842 | result = False 1843 | return result 1844 | 1845 | 1846 | class NodeOprRightAssoc(NodeOpr): # 2010 Mar 10 1847 | 1848 | """Right-associative operator. 1849 | 1850 | """ 1851 | 1852 | tag = 'RA_Opr' 1853 | 1854 | def is_paren_needed(self, node, pos): 1855 | if NodeOpr.is_paren_needed(self, node, pos): 1856 | if type(node) in [NodeUnaryAdd, NodeUnarySub]: 1857 | result = not (pos == 'right') 1858 | else: 1859 | result = True 1860 | elif type(node) in OPERATOR_LEVEL[type(self)]: 1861 | result = not (pos == 'right') 1862 | else: 1863 | result = False 1864 | return result 1865 | 1866 | 1867 | class NodeStr(Node): 1868 | 1869 | """String value. 1870 | 1871 | """ 1872 | 1873 | tag = 'Str' 1874 | 1875 | def __init__(self, indent, lineno, str): 1876 | Node.__init__(self, indent, lineno) 1877 | self.set_as_str(str) 1878 | return 1879 | 1880 | def put(self, can_split=False): 1881 | self.line_more(self.get_as_str()) 1882 | return self 1883 | 1884 | def get_as_str(self): 1885 | return self.str 1886 | 1887 | def set_as_str(self, str_): 1888 | self.str = str_ 1889 | if isinstance(self.str, unicode): 1890 | pass 1891 | elif not RECODE_STRINGS: # 2006 Dec 01 1892 | pass 1893 | else: 1894 | try: 1895 | self.str = self.str.decode(INPUT.coding) 1896 | except UnicodeError: 1897 | pass 1898 | try: 1899 | self.str = str(self.str) 1900 | except UnicodeError: 1901 | pass 1902 | return self 1903 | 1904 | def get_as_repr(self): # 2007 May 01 1905 | original_values = COMMENTS.literal_pool.get(repr(self.get_as_str()), []) # 2010 Mar 10 1906 | if len(original_values) == 1: 1907 | (result, lineno) = original_values[ZERO] 1908 | else: 1909 | result = repr(self.get_as_str()) 1910 | if DOUBLE_QUOTED_STRINGS: 1911 | result = force_quote(result, double=True) 1912 | elif SINGLE_QUOTED_STRINGS: 1913 | result = force_quote(result, double=False) 1914 | return result 1915 | 1916 | def put_doc(self, need_blank_line=ZERO): 1917 | 1918 | def fix_newlines(text): # 2010 Mar 10 1919 | lines = text.splitlines() 1920 | result = OUTPUT.newline.join(lines) # 2006 Dec 05 1921 | return result 1922 | 1923 | doc = self.get_as_repr() # 2010 Mar 10 1924 | doc = doc.replace('\t', DOC_TAB_REPLACEMENT) # 2007 May 24 1925 | if LEFTJUST_DOC_STRINGS: 1926 | lines = leftjust_lines(doc.strip().splitlines()) # 2007 May 25 1927 | lines.extend([NULL, NULL]) 1928 | margin = '%s%s' % (OUTPUT.newline, INDENTATION * self.indent) # 2006 Dec 05 1929 | doc = margin.join(lines) 1930 | if WRAP_DOC_STRINGS: # 2007 May 25 1931 | margin = '%s%s' % (OUTPUT.newline, INDENTATION * self.indent) # 2006 Dec 05 1932 | line_length = COL_LIMIT - (len(INDENTATION) * self.indent) 1933 | line_length = max(line_length, 20) 1934 | lines = wrap_lines(doc.strip().splitlines(), width=line_length) 1935 | lines.extend([NULL, NULL]) 1936 | doc = margin.join(lines) 1937 | self.line_init(need_blank_line=need_blank_line) # 2006 Dec 01 1938 | doc = fix_newlines(doc) # 2010 Mar 10 1939 | self.put_multi_line(doc) 1940 | self.line_term() 1941 | OUTPUT.put_blank_line(5) 1942 | return self 1943 | 1944 | def put_lit(self, can_split=False): 1945 | lit = self.get_as_repr() # 2007 May 01 1946 | match = QUOTE_PATTERN.match(lit) # 2009 Feb 05 1947 | (prefix, quote) = match.group(1, 2) 1948 | if ('r' in prefix.lower()): # 2009 Feb 05 1949 | self.line_more(lit, can_split_str=CAN_SPLIT_STRINGS, can_split_after=can_split) 1950 | else: 1951 | lines = NEW_LINE_PATTERN.split(lit) 1952 | if len(lines) > MAX_LINES_BEFORE_SPLIT_LIT: 1953 | lit = OUTPUT.newline.join(lines) # 2006 Dec 05 1954 | self.put_multi_line(lit) 1955 | else: 1956 | self.line_more(lit, can_split_str=CAN_SPLIT_STRINGS, can_split_after=can_split) 1957 | return self 1958 | 1959 | def put_multi_line(self, lit): # 2006 Dec 01 1960 | match = QUOTE_PATTERN.match(lit) 1961 | (prefix, quote) = match.group(1, 2) # 2007 May 01 1962 | if len(quote) == 3: # 2006 Jan 14 1963 | head = prefix + quote 1964 | tail = NULL 1965 | else: 1966 | head = prefix + quote * 3 1967 | tail = quote * 2 1968 | lit = QUOTE_PATTERN.sub(head, lit, 1) + tail 1969 | self.line_more(lit, can_split_str=False) # 2007 May 23 1970 | return self 1971 | 1972 | 1973 | class NodeInt(Node): 1974 | 1975 | """Integer value. 1976 | 1977 | """ 1978 | 1979 | tag = 'Int' 1980 | 1981 | def __init__(self, indent, lineno, int): 1982 | Node.__init__(self, indent, lineno) 1983 | self.int = int 1984 | return 1985 | 1986 | def put(self, can_split=False): 1987 | self.line_more(self.get_as_repr()) 1988 | return self 1989 | 1990 | def get_as_repr(self): 1991 | original_values = COMMENTS.literal_pool.get(repr(self.int), []) # 2010 Mar 10 1992 | if len(original_values) == 1: 1993 | (result, lineno) = original_values[ZERO] 1994 | else: 1995 | result = repr(self.int) 1996 | return result 1997 | 1998 | 1999 | class NodeAdd(NodeOprAssoc): # 2010 Mar 10 2000 | 2001 | """Add operation. 2002 | 2003 | """ 2004 | 2005 | tag = 'Add' 2006 | 2007 | def __init__(self, indent, lineno, left, right): 2008 | Node.__init__(self, indent, lineno) 2009 | self.left = transform(indent, lineno, left) 2010 | self.right = transform(indent, lineno, right) 2011 | return 2012 | 2013 | def put(self, can_split=False): 2014 | self.put_expr(self.left, can_split=can_split) 2015 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2016 | self.line_more('+ ') 2017 | self.put_expr(self.right, can_split=can_split) 2018 | return self 2019 | 2020 | def get_hi_lineno(self): 2021 | return self.right.get_hi_lineno() 2022 | 2023 | 2024 | class NodeAnd(NodeOprAssoc): # 2010 Mar 10 2025 | 2026 | '''Logical "and" operation. 2027 | 2028 | ''' 2029 | 2030 | tag = 'And' 2031 | 2032 | def __init__(self, indent, lineno, nodes): 2033 | Node.__init__(self, indent, lineno) 2034 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2035 | return 2036 | 2037 | def put(self, can_split=False): 2038 | for node in (self.nodes)[:1]: 2039 | self.put_expr(node, can_split=can_split) 2040 | for node in (self.nodes)[1:]: 2041 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2042 | self.line_more('and ') 2043 | self.put_expr(node, can_split=can_split) 2044 | return self 2045 | 2046 | def get_hi_lineno(self): 2047 | return (self.nodes)[-1].get_hi_lineno() 2048 | 2049 | 2050 | class NodeAsgAttr(NodeOpr): 2051 | 2052 | """Assignment to a class attribute. 2053 | 2054 | """ 2055 | 2056 | tag = 'AsgAttr' 2057 | 2058 | def __init__(self, indent, lineno, expr, attrname, flags): 2059 | Node.__init__(self, indent, lineno) 2060 | self.expr = transform(indent, lineno, expr) 2061 | self.attrname = transform(indent, lineno, attrname) 2062 | self.flags = transform(indent, lineno, flags) 2063 | return 2064 | 2065 | def put(self, can_split=False): 2066 | is_del = self.flags.get_as_str() in ['OP_DELETE'] 2067 | if is_del: 2068 | self.line_init() 2069 | self.line_more('del ') 2070 | if isinstance(self.expr, NodeConst): 2071 | if self.expr.is_str(): # 2007 May 01 2072 | self.expr.put() 2073 | else: 2074 | self.line_more('(') 2075 | self.expr.put(can_split=True) 2076 | self.line_more(')') 2077 | else: 2078 | self.put_expr(self.expr, can_split=can_split) 2079 | self.line_more('.') 2080 | self.line_more(NAME_SPACE.make_attr_name(self.expr, self.attrname)) 2081 | if DEBUG: 2082 | self.line_more(' /* AsgAttr flags: ') 2083 | self.flags.put() 2084 | self.line_more(' */ ') 2085 | if is_del: 2086 | self.line_term() 2087 | return self 2088 | 2089 | def get_hi_lineno(self): 2090 | return self.expr.get_hi_lineno() 2091 | 2092 | 2093 | class NodeAsgList(Node): 2094 | 2095 | """A list as a destination of an assignment operation. 2096 | 2097 | """ 2098 | 2099 | tag = 'AsgList' 2100 | 2101 | def __init__(self, indent, lineno, nodes): 2102 | Node.__init__(self, indent, lineno) 2103 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2104 | return 2105 | 2106 | def put(self, can_split=False): 2107 | self.line_more('[', tab_set=True) 2108 | if len(self.nodes) > MAX_SEPS_SERIES: # 2007 May 24 2109 | self.line_term() 2110 | self.inc_margin() 2111 | for node in self.nodes: 2112 | self.line_init() 2113 | node.put(can_split=True) 2114 | self.line_more(LIST_SEP) 2115 | self.line_term() 2116 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 2117 | self.dec_margin() 2118 | self.line_init() 2119 | else: 2120 | self.line_init() 2121 | self.dec_margin() 2122 | else: 2123 | for node in (self.nodes)[:1]: 2124 | node.put(can_split=True) 2125 | self.line_more(LIST_SEP, can_split_after=True) 2126 | for node in (self.nodes)[1:2]: 2127 | node.put(can_split=True) 2128 | for node in (self.nodes)[2:]: 2129 | self.line_more(LIST_SEP, can_split_after=True) 2130 | node.put(can_split=True) 2131 | self.line_more(']', tab_clear=True) 2132 | return self 2133 | 2134 | def make_local_name(self): 2135 | for node in self.nodes: 2136 | node.make_local_name() 2137 | return self 2138 | 2139 | def get_hi_lineno(self): 2140 | return node[-1].get_hi_lineno() 2141 | 2142 | 2143 | class NodeAsgName(Node): 2144 | 2145 | """Destination of an assignment operation. 2146 | 2147 | """ 2148 | 2149 | tag = 'AsgName' 2150 | 2151 | def __init__(self, indent, lineno, name, flags): 2152 | Node.__init__(self, indent, lineno) 2153 | self.name = transform(indent, lineno, name) 2154 | self.flags = transform(indent, lineno, flags) 2155 | return 2156 | 2157 | def put(self, can_split=False): 2158 | is_del = self.flags.get_as_str() in ['OP_DELETE'] 2159 | if is_del: 2160 | self.line_init() 2161 | self.line_more('del ') 2162 | self.line_more(NAME_SPACE.get_name(self.name)) 2163 | if DEBUG: 2164 | self.line_more(' /* AsgName flags: ') 2165 | self.flags.put() 2166 | self.line_more(' */ ') 2167 | if is_del: 2168 | self.line_term() 2169 | return self 2170 | 2171 | def make_local_name(self): 2172 | if NAME_SPACE.has_name(self.name): 2173 | pass 2174 | else: 2175 | NAME_SPACE.make_local_name(self.name) 2176 | return self 2177 | 2178 | def get_hi_lineno(self): 2179 | return self.name.get_hi_lineno() 2180 | 2181 | 2182 | class NodeAsgTuple(Node): 2183 | 2184 | """A tuple as a destination of an assignment operation. 2185 | 2186 | """ 2187 | 2188 | tag = 'AsgTuple' 2189 | 2190 | def __init__(self, indent, lineno, nodes): 2191 | Node.__init__(self, indent, lineno) 2192 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2193 | return 2194 | 2195 | def put(self, can_split=False, is_paren_required=True): # 2010 Mar 10 2196 | if len(self.nodes) > MAX_SEPS_SERIES: # 2007 May 24 2197 | self.line_more('(', tab_set=True) # 2010 Mar 10 2198 | self.line_term() 2199 | self.inc_margin() 2200 | for node in self.nodes: 2201 | self.line_init() 2202 | node.put(can_split=True) 2203 | self.line_more(LIST_SEP) 2204 | self.line_term() 2205 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 2206 | self.dec_margin() 2207 | self.line_init() 2208 | else: 2209 | self.line_init() 2210 | self.dec_margin() 2211 | self.line_more(')', tab_clear=True) # 2010 Mar 10 2212 | elif is_paren_required or PARENTHESIZE_TUPLE_DISPLAY: # 2010 Mar 10 2213 | self.line_more('(', tab_set=True) # 2010 Mar 10 2214 | for node in (self.nodes)[:1]: 2215 | node.put(can_split=True) 2216 | self.line_more(LIST_SEP, can_split_after=True) 2217 | for node in (self.nodes)[1:2]: 2218 | node.put(can_split=True) 2219 | for node in (self.nodes)[2:]: 2220 | self.line_more(LIST_SEP, can_split_after=True) 2221 | node.put(can_split=True) 2222 | self.line_more(')', tab_clear=True) # 2010 Mar 10 2223 | else: 2224 | for node in (self.nodes)[:1]: 2225 | node.put() 2226 | self.line_more(LIST_SEP, can_break_after=True) # 2010 Mar 10 2227 | for node in (self.nodes)[1:2]: 2228 | node.put() 2229 | for node in (self.nodes)[2:]: 2230 | self.line_more(LIST_SEP, can_break_after=True) # 2010 Mar 10 2231 | node.put() 2232 | return self 2233 | 2234 | def make_local_name(self): 2235 | for node in self.nodes: 2236 | node.make_local_name() 2237 | return self 2238 | 2239 | def get_hi_lineno(self): 2240 | return (self.nodes)[-1].get_hi_lineno() 2241 | 2242 | 2243 | class NodeAssert(Node): 2244 | 2245 | """Assertion. 2246 | 2247 | """ 2248 | 2249 | tag = 'Assert' 2250 | 2251 | def __init__(self, indent, lineno, test, fail): 2252 | Node.__init__(self, indent, lineno) 2253 | self.test = transform(indent, lineno, test) 2254 | self.fail = transform(indent, lineno, fail) 2255 | return 2256 | 2257 | def put(self, can_split=False): 2258 | self.line_init() 2259 | self.line_more('assert ') 2260 | self.test.put(can_split=can_split) 2261 | if self.fail is None: 2262 | pass 2263 | else: 2264 | self.line_more(LIST_SEP, can_break_after=True) 2265 | self.fail.put() 2266 | self.line_term() 2267 | return self 2268 | 2269 | def get_hi_lineno(self): 2270 | lineno = self.test.get_hi_lineno() 2271 | if self.fail is None: 2272 | pass 2273 | else: 2274 | lineno = self.fail.get_hi_lineno() 2275 | return lineno 2276 | 2277 | 2278 | class NodeAssign(Node): 2279 | 2280 | """Set one or more destinations to the value of the expression. 2281 | 2282 | """ 2283 | 2284 | tag = 'Assign' 2285 | 2286 | def __init__(self, indent, lineno, nodes, expr): 2287 | Node.__init__(self, indent, lineno) 2288 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2289 | self.expr = transform(indent, lineno, expr) 2290 | return 2291 | 2292 | def put(self, can_split=False): 2293 | self.line_init() 2294 | for node in self.nodes: 2295 | if isinstance(node, NodeAsgTuple): 2296 | node.put(can_split=can_split, is_paren_required=False) # 2010 Mar 10 2297 | else: 2298 | node.put(can_split=can_split) 2299 | self.line_more(ASSIGNMENT, can_break_after=True) 2300 | if isinstance(self.expr, NodeYield): # 2006 Dec 13 2301 | self.line_more('(') 2302 | self.expr.put(can_split=True) 2303 | self.line_more(')') 2304 | elif isinstance(self.expr, NodeTuple): 2305 | self.expr.put(can_split=can_split, is_paren_required=False) # 2010 Mar 10 2306 | else: 2307 | self.expr.put(can_split=can_split) 2308 | self.line_term() 2309 | return self 2310 | 2311 | def marshal_names(self): 2312 | for node in self.nodes: 2313 | node.make_local_name() 2314 | return self 2315 | 2316 | def get_hi_lineno(self): 2317 | return self.expr.get_hi_lineno() 2318 | 2319 | 2320 | class NodeAugAssign(Node): 2321 | 2322 | """Augment the destination by the value of the expression. 2323 | 2324 | """ 2325 | 2326 | tag = 'AugAssign' 2327 | 2328 | def __init__(self, indent, lineno, node, op, expr): 2329 | Node.__init__(self, indent, lineno) 2330 | self.node = transform(indent, lineno, node) 2331 | self.op = transform(indent, lineno, op) 2332 | self.expr = transform(indent, lineno, expr) 2333 | return 2334 | 2335 | def put(self, can_split=False): 2336 | self.line_init() 2337 | self.node.put(can_split=can_split) 2338 | op = ASSIGNMENT.replace('=', self.op.get_as_str()) 2339 | self.line_more(op, can_break_after=True) 2340 | self.expr.put(can_split=can_split) 2341 | self.line_term() 2342 | return self 2343 | 2344 | def marshal_names(self): 2345 | self.node.make_local_name() 2346 | return self 2347 | 2348 | def get_hi_lineno(self): 2349 | return self.expr.get_hi_lineno() 2350 | 2351 | 2352 | class NodeBackquote(Node): 2353 | 2354 | """String conversion a'la *repr*. 2355 | 2356 | """ 2357 | 2358 | tag = 'Backquote' 2359 | 2360 | def __init__(self, indent, lineno, expr): 2361 | Node.__init__(self, indent, lineno) 2362 | self.expr = transform(indent, lineno, expr) 2363 | return 2364 | 2365 | def put(self, can_split=False): 2366 | self.line_more('`') 2367 | self.expr.put(can_split=can_split) 2368 | self.line_more('`') 2369 | return self 2370 | 2371 | def get_hi_lineno(self): 2372 | return self.expr.get_hi_lineno() 2373 | 2374 | 2375 | class NodeBitAnd(NodeOprAssoc): # 2010 Mar 10 2376 | 2377 | '''Bitwise "and" operation (set union). 2378 | 2379 | ''' 2380 | 2381 | tag = 'BitAnd' 2382 | 2383 | def __init__(self, indent, lineno, nodes): 2384 | Node.__init__(self, indent, lineno) 2385 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2386 | return 2387 | 2388 | def put(self, can_split=False): 2389 | for node in (self.nodes)[:1]: 2390 | self.put_expr(node, can_split=can_split) 2391 | for node in (self.nodes)[1:]: 2392 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2393 | self.line_more('& ') 2394 | self.put_expr(node, can_split=can_split) 2395 | return self 2396 | 2397 | def get_hi_lineno(self): 2398 | return (self.nodes)[-1].get_hi_lineno() 2399 | 2400 | 2401 | class NodeBitOr(NodeOprAssoc): # 2010 Mar 01 2402 | 2403 | '''Bitwise "or" operation (set intersection). 2404 | 2405 | ''' 2406 | 2407 | tag = 'BitOr' 2408 | 2409 | def __init__(self, indent, lineno, nodes): 2410 | Node.__init__(self, indent, lineno) 2411 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2412 | return 2413 | 2414 | def put(self, can_split=False): 2415 | for node in (self.nodes)[:1]: 2416 | self.put_expr(node, can_split=can_split) 2417 | for node in (self.nodes)[1:]: 2418 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2419 | self.line_more('| ') 2420 | self.put_expr(node, can_split=can_split) 2421 | return self 2422 | 2423 | def get_hi_lineno(self): 2424 | return (self.nodes)[-1].get_hi_lineno() 2425 | 2426 | 2427 | class NodeBitXor(NodeOprAssoc): # 2010 Mar 01 2428 | 2429 | '''Bitwise "xor" operation. 2430 | 2431 | ''' 2432 | 2433 | tag = 'BitXor' 2434 | 2435 | def __init__(self, indent, lineno, nodes): 2436 | Node.__init__(self, indent, lineno) 2437 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2438 | return 2439 | 2440 | def put(self, can_split=False): 2441 | for node in (self.nodes)[:1]: 2442 | self.put_expr(node, can_split=can_split) 2443 | for node in (self.nodes)[1:]: 2444 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2445 | self.line_more('^ ') 2446 | self.put_expr(node, can_split=can_split) 2447 | return self 2448 | 2449 | def get_hi_lineno(self): 2450 | return (self.nodes)[-1].get_hi_lineno() 2451 | 2452 | 2453 | class NodeBreak(Node): 2454 | 2455 | """Escape from a loop. 2456 | 2457 | """ 2458 | 2459 | tag = 'Break' 2460 | 2461 | def __init__(self, indent, lineno): 2462 | Node.__init__(self, indent, lineno) 2463 | return 2464 | 2465 | def put(self, can_split=False): 2466 | self.line_init() 2467 | self.line_more('break') 2468 | self.line_term() 2469 | return self 2470 | 2471 | 2472 | class NodeCallFunc(Node): 2473 | 2474 | """Function invocation. 2475 | 2476 | """ 2477 | 2478 | tag = 'CallFunc' 2479 | 2480 | def __init__(self, indent, lineno, node, args, star_args, dstar_args): 2481 | Node.__init__(self, indent, lineno) 2482 | self.node = transform(indent, lineno, node) 2483 | self.args = [transform(indent, lineno, arg) for arg in args] 2484 | self.star_args = transform(indent, lineno, star_args) 2485 | self.dstar_args = transform(indent, lineno, dstar_args) 2486 | if len(self.args) == 1: 2487 | arg = (self.args)[ZERO] 2488 | if isinstance(arg, NodeGenExpr): 2489 | arg.need_parens = False 2490 | return 2491 | 2492 | def put(self, can_split=False): 2493 | 2494 | def count_seps(): 2495 | result = len(self.args) 2496 | if self.star_args is None: 2497 | pass 2498 | else: 2499 | result += 1 2500 | if self.dstar_args is None: 2501 | pass 2502 | else: 2503 | result += 1 2504 | return result 2505 | 2506 | if isinstance(self.node, NodeLambda): 2507 | self.line_more('(') 2508 | self.node.put(can_split=True) 2509 | self.line_more(')') 2510 | else: 2511 | self.node.put(can_split=can_split) 2512 | self.line_more('(', tab_set=True) 2513 | if count_seps() > MAX_SEPS_FUNC_REF: # 2007 May 24 2514 | self.line_term() 2515 | self.inc_margin() 2516 | arg_list = [(NULL, arg) for arg in self.args] # 2010 Mar 10 2517 | has_stars = False # 2010 Mar 10 2518 | if self.star_args is None: 2519 | pass 2520 | else: 2521 | arg_list.append(('*', self.star_args)) 2522 | has_stars = True 2523 | if self.dstar_args is None: 2524 | pass 2525 | else: 2526 | arg_list.append(('**', self.dstar_args)) 2527 | has_stars = True 2528 | for (sentinel, arg) in arg_list[:-1]: # 2010 Mar 10 2529 | self.line_init() 2530 | self.line_more(sentinel) 2531 | arg.put(can_split=True) 2532 | self.line_more(LIST_SEP) 2533 | self.line_term() 2534 | for (sentinel, arg) in arg_list[-1:]: # 2010 Mar 10 2535 | self.line_init() 2536 | self.line_more(sentinel) 2537 | arg.put(can_split=True) 2538 | if has_stars: 2539 | pass 2540 | else: 2541 | self.line_more(LIST_SEP) 2542 | self.line_term() 2543 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 2544 | self.dec_margin() 2545 | self.line_init() 2546 | else: 2547 | self.line_init() 2548 | self.dec_margin() 2549 | else: 2550 | for arg in (self.args)[:-1]: 2551 | arg.put(can_split=True) 2552 | self.line_more(FUNCTION_PARAM_SEP, can_split_after=True) 2553 | for arg in (self.args)[-1:]: 2554 | arg.put(can_split=True) 2555 | if self.star_args is None and self.dstar_args is None: 2556 | pass 2557 | else: 2558 | self.line_more(FUNCTION_PARAM_SEP, can_split_after=True) 2559 | if self.star_args is None: 2560 | pass 2561 | else: 2562 | self.line_more('*') 2563 | self.star_args.put(can_split=True) 2564 | if self.dstar_args is None: 2565 | pass 2566 | else: 2567 | self.line_more(FUNCTION_PARAM_SEP, can_split_after=True) 2568 | if self.dstar_args is None: 2569 | pass 2570 | else: 2571 | self.line_more('**') 2572 | self.dstar_args.put(can_split=True) 2573 | self.line_more(')', tab_clear=True) 2574 | return self 2575 | 2576 | def get_lineno(self): 2577 | return self.node.get_lineno() 2578 | 2579 | def get_hi_lineno(self): 2580 | lineno = Node.get_hi_lineno(self) 2581 | if self.args: 2582 | lineno = (self.args)[-1].get_hi_lineno() 2583 | if self.star_args is None: 2584 | pass 2585 | else: 2586 | lineno = self.star_args.get_hi_lineno() 2587 | if self.dstar_args is None: 2588 | pass 2589 | else: 2590 | lineno = self.dstar_args.get_hi_lineno() 2591 | return lineno 2592 | 2593 | 2594 | class NodeClass(Node): 2595 | 2596 | """Class declaration. 2597 | 2598 | """ 2599 | 2600 | tag = 'Class' 2601 | 2602 | def __init__(self, indent, lineno, name, bases, doc, code): 2603 | Node.__init__(self, indent, lineno) 2604 | self.name = transform(indent, lineno, name) 2605 | self.bases = [transform(indent, lineno, base) for base in bases] 2606 | self.doc = transform(indent + 1, lineno, doc) 2607 | self.code = transform(indent + 1, lineno, code) 2608 | return 2609 | 2610 | def put(self, can_split=False): 2611 | if NAME_SPACE.is_global(): # 2010 Sep 08 2612 | spacing = 2 2613 | else: 2614 | spacing = 1 2615 | self.line_init(need_blank_line=spacing) 2616 | self.line_more('class ') 2617 | self.line_more(NAME_SPACE.get_name(self.name)) 2618 | if self.bases: 2619 | self.line_more('(') 2620 | for base in (self.bases)[:1]: 2621 | base.put(can_split=True) 2622 | for base in (self.bases)[1:]: 2623 | self.line_more(LIST_SEP, can_split_after=True) 2624 | base.put(can_split=True) 2625 | self.line_more(')') 2626 | self.line_more(':') 2627 | self.line_term(self.code.get_lineno() - 1) 2628 | if self.doc is None: 2629 | pass 2630 | else: 2631 | self.doc.put_doc(need_blank_line=1) 2632 | OUTPUT.put_blank_line(6) 2633 | self.push_scope() 2634 | self.code.marshal_names() 2635 | self.code.put() 2636 | self.pop_scope() 2637 | OUTPUT.put_blank_line(7, count=spacing) 2638 | return self 2639 | 2640 | def push_scope(self): 2641 | NAME_SPACE.push_scope() 2642 | return self 2643 | 2644 | def pop_scope(self): 2645 | NAME_SPACE.pop_scope() 2646 | return self 2647 | 2648 | def marshal_names(self): 2649 | NAME_SPACE.make_class_name(self.name) 2650 | return self 2651 | 2652 | def get_hi_lineno(self): 2653 | lineno = self.name.get_hi_lineno() 2654 | if self.bases: 2655 | lineno = (self.bases)[-1].get_hi_lineno() 2656 | return lineno 2657 | 2658 | 2659 | class NodeCompare(NodeOprNotAssoc): 2660 | 2661 | """Logical comparison. 2662 | 2663 | """ 2664 | 2665 | tag = 'Compare' 2666 | 2667 | def __init__(self, indent, lineno, expr, ops): 2668 | Node.__init__(self, indent, lineno) 2669 | self.expr = transform(indent, lineno, expr) 2670 | self.ops = [(op, transform(indent, lineno, ex)) for (op, ex) in 2671 | ops] 2672 | return 2673 | 2674 | def put(self, can_split=False): 2675 | self.put_expr(self.expr, can_split=can_split) 2676 | for (op, ex) in self.ops: 2677 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2678 | self.line_more('%s ' % op) 2679 | self.put_expr(ex, can_split=can_split) 2680 | return self 2681 | 2682 | def get_hi_lineno(self): 2683 | (op, ex) = (self.ops)[-1] 2684 | return ex.get_hi_lineno() 2685 | 2686 | 2687 | class NodeConst(Node): 2688 | 2689 | """Literal or expression. 2690 | 2691 | """ 2692 | 2693 | tag = 'Const' 2694 | 2695 | def __init__(self, indent, lineno, value): 2696 | Node.__init__(self, indent, lineno) 2697 | self.value = transform(indent, lineno, value) 2698 | return 2699 | 2700 | def put(self, can_split=False): 2701 | if self.is_str(): # 2007 May 01 2702 | self.value.put_lit(can_split=can_split) 2703 | elif isinstance(self.value, Node): 2704 | self.value.put(can_split=can_split) 2705 | else: 2706 | self.line_more(self.get_as_repr()) # 2007 May 01 2707 | return self 2708 | 2709 | def is_none(self): 2710 | return self.value is None 2711 | 2712 | def is_str(self): # 2007 May 01 2713 | return isinstance(self.value, NodeStr) 2714 | 2715 | def get_as_repr(self): # 2007 May 01 2716 | original_values = COMMENTS.literal_pool.get(repr(self.value), []) # 2010 Mar 10 2717 | if len(original_values) == 1: 2718 | (result, lineno) = original_values[ZERO] 2719 | else: 2720 | result = repr(self.value) 2721 | return result 2722 | 2723 | 2724 | class NodeContinue(Node): 2725 | 2726 | """Start a new trip through a loop. 2727 | 2728 | """ 2729 | 2730 | tag = 'Continue' 2731 | 2732 | def __init__(self, indent, lineno): 2733 | Node.__init__(self, indent, lineno) 2734 | return 2735 | 2736 | def put(self, can_split=False): 2737 | self.line_init() 2738 | self.line_more('continue') 2739 | self.line_term() 2740 | return self 2741 | 2742 | 2743 | class NodeDecorators(Node): 2744 | 2745 | """Functions that take a class method (the next) and a return 2746 | callable object, e.g., *classmethod*. 2747 | 2748 | """ 2749 | 2750 | def __init__(self, indent, lineno, nodes): 2751 | Node.__init__(self, indent, lineno) 2752 | self.nodes = [transform(indent, lineno, node) for node in nodes] 2753 | return 2754 | 2755 | def put(self, spacing=ZERO, can_split=False): 2756 | for node in self.nodes: 2757 | self.line_init(need_blank_line=spacing) 2758 | self.line_more('@') 2759 | node.put(can_split=can_split) 2760 | self.line_term() 2761 | spacing = ZERO 2762 | return self 2763 | 2764 | def get_hi_lineno(self): 2765 | return (self.nodes)[-1].get_hi_lineno() 2766 | 2767 | 2768 | class NodeDict(Node): 2769 | 2770 | """Declaration of a map (dictionary). 2771 | 2772 | """ 2773 | 2774 | tag = 'Dict' 2775 | 2776 | def __init__(self, indent, lineno, items): 2777 | Node.__init__(self, indent, lineno) 2778 | self.items = [(transform(indent, lineno, key), transform(indent, 2779 | lineno, value)) for (key, value) in items] 2780 | return 2781 | 2782 | def put(self, can_split=False): 2783 | 2784 | def put_item(): 2785 | key.put(can_split=False) # 2010 Sep 08 2786 | self.line_more(DICT_COLON) 2787 | value.put(can_split=can_split) 2788 | return 2789 | 2790 | self.line_more('{', tab_set=True) 2791 | if len(self.items) > MAX_SEPS_DICT: # 2007 May 24 2792 | self.line_term() 2793 | self.inc_margin() 2794 | for (key, value) in self.items: 2795 | self.line_init() 2796 | put_item() 2797 | self.line_more(LIST_SEP) 2798 | self.line_term() 2799 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 2800 | self.dec_margin() 2801 | self.line_init() 2802 | else: 2803 | self.line_init() 2804 | self.dec_margin() 2805 | else: 2806 | for (key, value) in (self.items)[:1]: 2807 | put_item() 2808 | for (key, value) in (self.items)[1:]: 2809 | self.line_more(LIST_SEP, can_split_after=True) 2810 | put_item() 2811 | self.line_more('}', tab_clear=True) 2812 | return self 2813 | 2814 | def get_hi_lineno(self): 2815 | lineno = Node.get_hi_lineno(self) 2816 | if self.items: 2817 | (key, value) = (self.items)[-1] 2818 | lineno = value.get_hi_lineno() 2819 | return lineno 2820 | 2821 | 2822 | class NodeDiscard(Node): 2823 | 2824 | """Evaluate an expression (function) without saving the result. 2825 | 2826 | """ 2827 | 2828 | tag = 'Discard' 2829 | 2830 | def __init__(self, indent, lineno, expr): 2831 | Node.__init__(self, indent, lineno) 2832 | self.expr = transform(indent, lineno, expr) 2833 | return 2834 | 2835 | def put(self, can_split=False): 2836 | if isinstance(self.expr, NodeConst) and (not KEEP_UNASSIGNED_CONSTANTS): # 2010 Mar 10 2837 | pass 2838 | else: 2839 | self.line_init() 2840 | self.expr.put(can_split=can_split) 2841 | self.line_term() 2842 | return self 2843 | 2844 | def marshal_names(self): 2845 | self.expr.marshal_names() 2846 | return self 2847 | 2848 | def get_lineno(self): 2849 | return self.expr.get_lineno() 2850 | 2851 | def get_hi_lineno(self): 2852 | return self.expr.get_hi_lineno() 2853 | 2854 | 2855 | class NodeDiv(NodeOprLeftAssoc): # 2010 Mar 10 2856 | 2857 | """Division operation. 2858 | 2859 | """ 2860 | 2861 | tag = 'Div' 2862 | 2863 | def __init__(self, indent, lineno, left, right): 2864 | Node.__init__(self, indent, lineno) 2865 | self.left = transform(indent, lineno, left) 2866 | self.right = transform(indent, lineno, right) 2867 | return 2868 | 2869 | def put(self, can_split=False): 2870 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 2871 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 2872 | self.line_more('/ ') 2873 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 2874 | return self 2875 | 2876 | def get_hi_lineno(self): 2877 | return self.right.get_hi_lineno() 2878 | 2879 | 2880 | class NodeEllipsis(Node): 2881 | 2882 | tag = 'Ellipsis' 2883 | 2884 | def __init__(self, indent, lineno): 2885 | Node.__init__(self, indent, lineno) 2886 | return 2887 | 2888 | def put(self, can_split=False): 2889 | self.line_more('...') 2890 | return self 2891 | 2892 | 2893 | class NodeExec(Node): 2894 | 2895 | """Execute a given string of Python code in a specified namespace. 2896 | 2897 | """ 2898 | 2899 | tag = 'Exec' 2900 | 2901 | def __init__(self, indent, lineno, expr, locals, globals): 2902 | Node.__init__(self, indent, lineno) 2903 | self.expr = transform(indent, lineno, expr) 2904 | self.locals = transform(indent, lineno, locals) 2905 | self.globals = transform(indent, lineno, globals) 2906 | return 2907 | 2908 | def put(self, can_split=False): 2909 | self.line_init() 2910 | self.line_more('exec ') 2911 | self.expr.put(can_split=can_split) 2912 | if self.locals is None: 2913 | pass 2914 | else: 2915 | self.line_more(' in ', can_break_after=True) 2916 | self.locals.put(can_split=can_split) 2917 | if self.globals is None: 2918 | pass 2919 | else: 2920 | self.line_more(LIST_SEP, can_break_after=True) 2921 | self.globals.put(can_split=can_split) 2922 | self.line_term() 2923 | return self 2924 | 2925 | def get_hi_lineno(self): 2926 | lineno = self.expr.get_hi_lineno() 2927 | if self.locals is None: 2928 | pass 2929 | else: 2930 | lineno = self.locals.get_hi_lineno() 2931 | if self.globals is None: 2932 | pass 2933 | else: 2934 | lineno = self.globals.get_hi_lineno() 2935 | return lineno 2936 | 2937 | 2938 | class NodeFor(Node): 2939 | 2940 | """For loop. 2941 | 2942 | """ 2943 | 2944 | tag = 'For' 2945 | 2946 | def __init__(self, indent, lineno, assign, list, body, else_): 2947 | Node.__init__(self, indent, lineno) 2948 | self.assign = transform(indent, lineno, assign) 2949 | self.list = transform(indent, lineno, list) 2950 | self.body = transform(indent + 1, lineno, body) 2951 | self.else_ = transform(indent + 1, lineno, else_) 2952 | return 2953 | 2954 | def put(self, can_split=False): 2955 | self.line_init() 2956 | self.line_more('for ') 2957 | if isinstance(self.assign, NodeAsgTuple): 2958 | self.assign.put(can_split=can_split, is_paren_required=False) # 2010 Mar 10 2959 | else: 2960 | self.assign.put(can_split=can_split) 2961 | self.line_more(' in ', can_break_after=True) 2962 | if isinstance(self.list, NodeTuple): 2963 | self.list.put(can_split=can_split, is_paren_required=False) # 2010 Mar 10 2964 | else: 2965 | self.list.put(can_split=can_split) 2966 | self.line_more(':') 2967 | self.line_term(self.body.get_lineno() - 1) 2968 | self.body.put() 2969 | if self.else_ is None: 2970 | pass 2971 | else: 2972 | self.line_init() 2973 | self.line_more('else:') 2974 | self.line_term(self.else_.get_lineno() - 1) 2975 | self.else_.put() 2976 | return self 2977 | 2978 | def marshal_names(self): 2979 | self.assign.make_local_name() 2980 | self.body.marshal_names() 2981 | if self.else_ is None: 2982 | pass 2983 | else: 2984 | self.else_.marshal_names() 2985 | return self 2986 | 2987 | def get_hi_lineno(self): 2988 | return self.list.get_hi_lineno() 2989 | 2990 | 2991 | class NodeFloorDiv(NodeOprLeftAssoc): # 2010 Mar 10 2992 | 2993 | """Floor division operation. 2994 | 2995 | """ 2996 | 2997 | tag = 'FloorDiv' 2998 | 2999 | 3000 | def __init__(self, indent, lineno, left, right): 3001 | Node.__init__(self, indent, lineno) 3002 | self.left = transform(indent, lineno, left) 3003 | self.right = transform(indent, lineno, right) 3004 | return 3005 | 3006 | def put(self, can_split=False): 3007 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 3008 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 3009 | self.line_more('// ') 3010 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 3011 | return self 3012 | 3013 | def get_hi_lineno(self): 3014 | return self.right.get_hi_lineno() 3015 | 3016 | 3017 | class NodeFrom(Node): 3018 | 3019 | """Import a name space. 3020 | 3021 | """ 3022 | 3023 | tag = 'From' 3024 | 3025 | def __init__(self, indent, lineno, modname, names): 3026 | Node.__init__(self, indent, lineno) 3027 | self.modname = transform(indent, lineno, modname) 3028 | self.names = [(transform(indent, lineno, identifier), transform(indent, 3029 | lineno, name)) for (identifier, name) in names] 3030 | return 3031 | 3032 | def put(self, can_split=False): 3033 | 3034 | def put_name(): 3035 | identifier.put(can_split=can_split) 3036 | if name is None: 3037 | pass 3038 | else: 3039 | self.line_more(' as ') 3040 | name.put(can_split=can_split) 3041 | return 3042 | 3043 | self.line_init() 3044 | self.line_more('from ') 3045 | self.modname.put(can_split=can_split) 3046 | self.line_more(' import ') 3047 | for (identifier, name) in (self.names)[:-1]: 3048 | put_name() 3049 | self.line_more(LIST_SEP, can_break_after=True) 3050 | for (identifier, name) in (self.names)[-1:]: 3051 | put_name() 3052 | self.line_term() 3053 | return self 3054 | 3055 | def marshal_names(self): 3056 | for (identifier, name) in self.names: 3057 | if name is None: 3058 | NAME_SPACE.make_imported_name(identifier) 3059 | else: 3060 | NAME_SPACE.make_local_name(name) 3061 | return self 3062 | 3063 | def get_hi_lineno(self): 3064 | (identifier, name) = (self.names)[-1] 3065 | lineno = identifier.get_hi_lineno() 3066 | if name is None: 3067 | pass 3068 | else: 3069 | lineno = name.get_hi_lineno() 3070 | return lineno 3071 | 3072 | 3073 | class NodeFunction(Node): 3074 | 3075 | """Function declaration. 3076 | 3077 | """ 3078 | 3079 | tag = 'Function' 3080 | 3081 | def __init__( 3082 | self, 3083 | indent, 3084 | lineno, 3085 | decorators, 3086 | name, 3087 | argnames, 3088 | defaults, 3089 | flags, 3090 | doc, 3091 | code, 3092 | ): 3093 | 3094 | Node.__init__(self, indent, lineno) 3095 | self.decorators = transform(indent, lineno, decorators) 3096 | self.name = transform(indent, lineno, name) 3097 | self.argnames = self.walk(argnames, self.xform) 3098 | self.defaults = [transform(indent, lineno, default) for default in 3099 | defaults] 3100 | self.flags = transform(indent, lineno, flags) 3101 | self.doc = transform(indent + 1, lineno, doc) 3102 | self.code = transform(indent + 1, lineno, code) 3103 | return 3104 | 3105 | def walk(self, tuple_, func, need_tuple=False): 3106 | if isinstance(tuple_, tuple) or isinstance(tuple_, list): 3107 | result = [self.walk(item, func, need_tuple) for item in 3108 | tuple_] 3109 | if need_tuple: 3110 | result = tuple(result) 3111 | else: 3112 | result = func(tuple_) 3113 | return result 3114 | 3115 | def xform(self, node): 3116 | result = transform(self.indent, self.lineno, node) 3117 | return result 3118 | 3119 | def pair_up(self, args, defaults): 3120 | args = args[:] # This function manipulates its arguments 3121 | defaults = defaults[:] # destructively, so make copies first. 3122 | stars = [] 3123 | args.reverse() 3124 | defaults.reverse() 3125 | is_excess_positionals = self.flags.int & 4 3126 | is_excess_keywords = self.flags.int & 8 3127 | if is_excess_positionals == ZERO: 3128 | pass 3129 | else: 3130 | stars.insert(ZERO, '*') 3131 | defaults.insert(ZERO, None) 3132 | if is_excess_keywords == ZERO: 3133 | pass 3134 | else: 3135 | stars.insert(ZERO, '**') 3136 | defaults.insert(ZERO, None) 3137 | result = map(None, args, defaults, stars) 3138 | result.reverse() 3139 | return result 3140 | 3141 | def put_parm(self, arg, default, stars, can_split=True): 3142 | if stars is None: 3143 | pass 3144 | else: 3145 | self.line_more(stars) 3146 | tuple_ = self.walk(arg, NAME_SPACE.get_name, need_tuple=True) 3147 | tuple_ = str(tuple_) 3148 | tuple_ = tuple_.replace("'", NULL).replace(',)', ', )') 3149 | self.line_more(tuple_) 3150 | if default is None: 3151 | pass 3152 | else: 3153 | self.line_more(FUNCTION_PARAM_ASSIGNMENT) 3154 | default.put(can_split=can_split) 3155 | return 3156 | 3157 | def put(self, can_split=False): 3158 | 3159 | if NAME_SPACE.is_global(): 3160 | spacing = 2 3161 | else: 3162 | spacing = 1 3163 | if self.decorators is None: 3164 | pass 3165 | else: 3166 | self.decorators.put(spacing) 3167 | spacing = ZERO 3168 | self.line_init(need_blank_line=spacing) 3169 | self.line_more('def ') 3170 | self.line_more(NAME_SPACE.get_name(self.name)) 3171 | self.push_scope() 3172 | parms = self.pair_up(self.argnames, self.defaults) 3173 | for (arg, default, stars) in parms: 3174 | self.walk(arg, NAME_SPACE.make_formal_param_name) 3175 | self.code.marshal_names() 3176 | self.line_more('(', tab_set=True) 3177 | if len(parms) > MAX_SEPS_FUNC_DEF: # 2007 May 24 3178 | self.line_term() 3179 | self.inc_margin() 3180 | for (arg, default, stars) in parms[:-1]: 3181 | self.line_init() 3182 | self.put_parm(arg, default, stars) 3183 | self.line_more(FUNCTION_PARAM_SEP) 3184 | self.line_term() 3185 | for (arg, default, stars) in parms[-1:]: 3186 | self.line_init() 3187 | self.put_parm(arg, default, stars) 3188 | if stars is None: # 2006 Dec 17 3189 | self.line_more(FUNCTION_PARAM_SEP) 3190 | self.line_term() 3191 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 3192 | self.dec_margin() 3193 | self.line_init() 3194 | else: 3195 | self.line_init() 3196 | self.dec_margin() 3197 | else: 3198 | for (arg, default, stars) in parms[:1]: 3199 | self.put_parm(arg, default, stars) 3200 | for (arg, default, stars) in parms[1:]: 3201 | self.line_more(FUNCTION_PARAM_SEP, can_split_after=True) 3202 | self.put_parm(arg, default, stars) 3203 | self.line_more('):', tab_clear=True) 3204 | if DEBUG: 3205 | self.line_more(' /* Function flags: ') 3206 | self.flags.put() 3207 | self.line_more(' */ ') 3208 | self.line_term(self.code.get_lineno() - 1) 3209 | if self.doc is None: 3210 | pass 3211 | else: 3212 | self.doc.put_doc() 3213 | self.code.put() 3214 | self.pop_scope() 3215 | OUTPUT.put_blank_line(8, count=spacing) 3216 | return self 3217 | 3218 | def push_scope(self): 3219 | NAME_SPACE.push_scope() 3220 | return self 3221 | 3222 | def pop_scope(self): 3223 | NAME_SPACE.pop_scope() 3224 | return self 3225 | 3226 | def marshal_names(self): 3227 | NAME_SPACE.make_function_name(self.name) 3228 | return self 3229 | 3230 | 3231 | class NodeLambda(NodeFunction): 3232 | 3233 | tag = 'Lambda' 3234 | 3235 | def __init__(self, indent, lineno, argnames, defaults, flags, code): 3236 | NodeFunction.__init__( 3237 | self, 3238 | indent, 3239 | lineno, 3240 | None, 3241 | None, 3242 | argnames, 3243 | defaults, 3244 | flags, 3245 | None, 3246 | code, 3247 | ) 3248 | return 3249 | 3250 | def put(self, can_split=False): 3251 | self.line_more('lambda ') 3252 | self.push_scope() 3253 | parms = self.pair_up(self.argnames, self.defaults) 3254 | for (arg, default, stars) in parms: 3255 | self.walk(arg, NAME_SPACE.make_formal_param_name) 3256 | for (arg, default, stars) in parms[:1]: 3257 | self.put_parm(arg, default, stars, can_split=False) 3258 | for (arg, default, stars) in parms[1:]: 3259 | self.line_more(FUNCTION_PARAM_SEP, can_break_after=True) 3260 | self.put_parm(arg, default, stars, can_split=False) 3261 | self.line_more(': ', can_break_after=True) 3262 | if DEBUG: 3263 | self.line_more(' /* Function flags: ') 3264 | self.flags.put() 3265 | self.line_more(' */ ') 3266 | self.code.put() 3267 | self.pop_scope() 3268 | return self 3269 | 3270 | def get_hi_lineno(self): 3271 | return self.code.get_hi_lineno() 3272 | 3273 | def marshal_names(self): 3274 | return self 3275 | 3276 | 3277 | class NodeGenExpr(Node): 3278 | 3279 | """Generator expression, which needs its own parentheses. 3280 | 3281 | """ 3282 | 3283 | tag = 'GenExpr' 3284 | 3285 | def __init__(self, indent, lineno, code): 3286 | Node.__init__(self, indent, lineno) 3287 | self.code = transform(indent, lineno, code) 3288 | self.need_parens = True 3289 | return 3290 | 3291 | def put(self, can_split=False): 3292 | if self.need_parens: 3293 | self.line_more('(') 3294 | self.code.put(can_split=True) 3295 | if self.need_parens: 3296 | self.line_more(')') 3297 | return self 3298 | 3299 | def get_hi_lineno(self): 3300 | return self.code.get_hi_lineno() 3301 | 3302 | 3303 | class NodeGenExprInner(Node): 3304 | 3305 | """Generator expression inside parentheses. 3306 | 3307 | """ 3308 | 3309 | tag = 'GenExprInner' 3310 | 3311 | def __init__(self, indent, lineno, expr, quals): 3312 | Node.__init__(self, indent, lineno) 3313 | self.expr = transform(indent, lineno, expr) 3314 | self.quals = [transform(indent, lineno, qual) for qual in quals] 3315 | return 3316 | 3317 | def put(self, can_split=False): 3318 | self.push_scope() 3319 | self.marshal_names() 3320 | self.expr.put(can_split=can_split) 3321 | for qual in self.quals: 3322 | qual.put(can_split=can_split) 3323 | self.pop_scope() 3324 | return self 3325 | 3326 | def push_scope(self): 3327 | NAME_SPACE.push_scope() 3328 | return self 3329 | 3330 | def pop_scope(self): 3331 | NAME_SPACE.pop_scope() 3332 | return self 3333 | 3334 | def marshal_names(self): 3335 | for qual in self.quals: 3336 | qual.marshal_names() 3337 | return self 3338 | 3339 | def get_hi_lineno(self): 3340 | lineno = (self.quals)[-1].get_hi_lineno() 3341 | return lineno 3342 | 3343 | 3344 | class NodeGenExprFor(Node): 3345 | 3346 | '''"For" of a generator expression. 3347 | 3348 | ''' 3349 | 3350 | tag = 'GenExprFor' 3351 | 3352 | def __init__(self, indent, lineno, assign, list, ifs): 3353 | Node.__init__(self, indent, lineno) 3354 | self.assign = transform(indent, lineno, assign) 3355 | self.list = transform(indent, lineno, list) 3356 | self.ifs = [transform(indent, lineno, if_) for if_ in ifs] 3357 | return 3358 | 3359 | def put(self, can_split=False): 3360 | self.line_more(SPACE, can_split_after=True) 3361 | self.line_more('for ') 3362 | self.assign.put(can_split=can_split) 3363 | self.line_more(' in ', can_split_after=True) 3364 | self.list.put(can_split=can_split) 3365 | for if_ in self.ifs: 3366 | if_.put(can_split=can_split) 3367 | return self 3368 | 3369 | def marshal_names(self): 3370 | self.assign.make_local_name() 3371 | return self 3372 | 3373 | def get_hi_lineno(self): 3374 | lineno = self.list.get_hi_lineno() 3375 | if self.ifs: 3376 | lineno = (self.ifs)[-1].get_hi_lineno() 3377 | return lineno 3378 | 3379 | 3380 | class NodeGenExprIf(Node): 3381 | 3382 | '''"If" of a generator expression. 3383 | 3384 | ''' 3385 | 3386 | tag = 'GenExprIf' 3387 | 3388 | def __init__(self, indent, lineno, test): 3389 | Node.__init__(self, indent, lineno) 3390 | self.test = transform(indent, lineno, test) 3391 | return 3392 | 3393 | def put(self, can_split=False): 3394 | self.line_more(SPACE, can_split_after=True) 3395 | self.line_more('if ') 3396 | self.test.put(can_split=can_split) 3397 | return self 3398 | 3399 | def get_hi_lineno(self): 3400 | return self.test.get_hi_lineno() 3401 | 3402 | 3403 | class NodeGetAttr(NodeOpr): 3404 | 3405 | """Class attribute (method). 3406 | 3407 | """ 3408 | 3409 | tag = 'GetAttr' 3410 | 3411 | def __init__(self, indent, lineno, expr, attrname): 3412 | Node.__init__(self, indent, lineno) 3413 | self.expr = transform(indent, lineno, expr) 3414 | self.attrname = transform(indent, lineno, attrname) 3415 | return 3416 | 3417 | def put(self, can_split=False): 3418 | if isinstance(self.expr, NodeConst): 3419 | if self.expr.is_str(): # 2007 May 01 3420 | self.expr.put() 3421 | else: 3422 | self.line_more('(') 3423 | self.expr.put(can_split=True) 3424 | self.line_more(')') 3425 | else: 3426 | self.put_expr(self.expr, can_split=can_split) 3427 | self.line_more('.') 3428 | self.line_more(NAME_SPACE.make_attr_name(self.expr, self.attrname)) 3429 | return self 3430 | 3431 | def get_hi_lineno(self): 3432 | return self.attrname.get_hi_lineno() 3433 | 3434 | 3435 | class NodeGlobal(Node): 3436 | 3437 | tag = 'Global' 3438 | 3439 | def __init__(self, indent, lineno, names): 3440 | Node.__init__(self, indent, lineno) 3441 | self.names = [transform(indent, lineno, name) for name in names] 3442 | return 3443 | 3444 | def put(self, can_split=False): 3445 | self.line_init() 3446 | self.line_more('global ') 3447 | for name in (self.names)[:1]: 3448 | self.line_more(NAME_SPACE.get_name(name)) 3449 | for name in (self.names)[1:]: 3450 | self.line_more(LIST_SEP, can_break_after=True) 3451 | self.line_more(NAME_SPACE.get_name(name)) 3452 | self.line_term() 3453 | return self 3454 | 3455 | def marshal_names(self): 3456 | for name in self.names: 3457 | NAME_SPACE.make_global_name(name) 3458 | return self 3459 | 3460 | def get_hi_lineno(self): 3461 | return (self.names)[-1].get_hi_lineno() 3462 | 3463 | 3464 | class NodeIf(Node): 3465 | 3466 | """True/False test. 3467 | 3468 | """ 3469 | 3470 | tag = 'If' 3471 | 3472 | def __init__(self, indent, lineno, tests, else_): 3473 | Node.__init__(self, indent, lineno) 3474 | self.tests = [(transform(indent, lineno, expr), transform(indent + 3475 | 1, lineno, stmt)) for (expr, stmt) in tests] 3476 | self.else_ = transform(indent + 1, lineno, else_) 3477 | return 3478 | 3479 | def put(self, can_split=False): 3480 | for (expr, stmt) in (self.tests)[:1]: 3481 | self.line_init() 3482 | self.line_more('if ') 3483 | expr.put(can_split=can_split) 3484 | self.line_more(':') 3485 | self.line_term(stmt.get_lineno() - 1) 3486 | stmt.put() 3487 | for (expr, stmt) in (self.tests)[1:]: 3488 | self.line_init() 3489 | self.line_more('elif ') 3490 | expr.put(can_split=can_split) 3491 | self.line_more(':') 3492 | self.line_term(stmt.get_lineno() - 1) 3493 | stmt.put() 3494 | if self.else_ is None: 3495 | pass 3496 | else: 3497 | self.line_init() 3498 | self.line_more('else:') 3499 | self.line_term(self.else_.get_lineno() - 1) 3500 | self.else_.put() 3501 | return self 3502 | 3503 | def marshal_names(self): 3504 | for (expr, stmt) in self.tests: 3505 | stmt.marshal_names() 3506 | if self.else_ is None: 3507 | pass 3508 | else: 3509 | self.else_.marshal_names() 3510 | return self 3511 | 3512 | def get_hi_lineno(self): 3513 | (expr, stmt) = (self.tests)[ZERO] 3514 | return expr.get_hi_lineno() 3515 | 3516 | 3517 | class NodeIfExp(Node): 3518 | 3519 | """Conditional assignment. (Ternary expression.) 3520 | 3521 | """ 3522 | 3523 | tag = 'IfExp' 3524 | 3525 | def __init__(self, indent, lineno, test, then, else_): 3526 | Node.__init__(self, indent, lineno) 3527 | self.test = transform(indent, lineno, test) 3528 | self.then = transform(indent, lineno, then) 3529 | self.else_ = transform(indent, lineno, else_) 3530 | return 3531 | 3532 | def put(self, can_split=False): 3533 | self.line_more('(', tab_set=True) # 2010 Mar 10 3534 | self.then.put(can_split=True) # 2010 Mar 10 3535 | self.line_more(' if ') 3536 | self.test.put(can_split=True) # 2010 Mar 10 3537 | self.line_more(' else ') 3538 | self.else_.put(can_split=True) # 2010 Mar 10 3539 | self.line_more(')', tab_clear=True) # 2010 Mar 10 3540 | return self 3541 | 3542 | def get_hi_lineno(self): 3543 | return self.else_.get_hi_lineno() 3544 | 3545 | 3546 | class NodeImport(Node): 3547 | 3548 | tag = 'Import' 3549 | 3550 | def __init__(self, indent, lineno, names): 3551 | Node.__init__(self, indent, lineno) 3552 | self.names = [(transform(indent, lineno, identifier), transform(indent, 3553 | lineno, name)) for (identifier, name) in names] 3554 | return 3555 | 3556 | def put(self, can_split=False): 3557 | 3558 | def put_name(): 3559 | identifier.put(can_split=can_split) 3560 | if name is None: 3561 | pass 3562 | else: 3563 | self.line_more(' as ') 3564 | name.put(can_split=can_split) 3565 | return 3566 | 3567 | for (identifier, name) in self.names: 3568 | self.line_init() 3569 | self.line_more('import ') 3570 | put_name() 3571 | self.line_term() 3572 | return self 3573 | 3574 | def marshal_names(self): 3575 | for (identifier, name) in self.names: 3576 | if name is None: 3577 | pass 3578 | else: 3579 | NAME_SPACE.make_local_name(name) 3580 | return self 3581 | 3582 | def get_hi_lineno(self): 3583 | (identifier, name) = (self.names)[-1] 3584 | lineno = identifier.get_hi_lineno() 3585 | if name is None: 3586 | pass 3587 | else: 3588 | lineno = name.get_hi_lineno() 3589 | return lineno 3590 | 3591 | 3592 | class NodeInvert(NodeOpr): 3593 | 3594 | """Unary bitwise complement. 3595 | 3596 | """ 3597 | 3598 | tag = 'Invert' 3599 | 3600 | def __init__(self, indent, lineno, expr): 3601 | Node.__init__(self, indent, lineno) 3602 | self.expr = transform(indent, lineno, expr) 3603 | return 3604 | 3605 | def put(self, can_split=False): 3606 | self.line_more('~') 3607 | self.put_expr(self.expr, can_split=can_split) 3608 | return self 3609 | 3610 | def get_hi_lineno(self): 3611 | return self.expr.get_hi_lineno() 3612 | 3613 | 3614 | class NodeKeyword(Node): 3615 | 3616 | """Formal parameter on a function invocation. 3617 | 3618 | """ 3619 | 3620 | tag = 'Keyword' 3621 | 3622 | def __init__(self, indent, lineno, name, expr): 3623 | Node.__init__(self, indent, lineno) 3624 | self.name = transform(indent, lineno, name) 3625 | self.expr = transform(indent, lineno, expr) 3626 | return 3627 | 3628 | def put(self, can_split=False): 3629 | self.line_more(NAME_SPACE.make_keyword_name(self.name)) 3630 | self.line_more(FUNCTION_PARAM_ASSIGNMENT) # 2007 May 25 3631 | self.expr.put(can_split=can_split) 3632 | return self 3633 | 3634 | def get_hi_lineno(self): 3635 | return self.expr.get_hi_lineno() 3636 | 3637 | 3638 | class NodeLeftShift(NodeOprLeftAssoc): # 2010 Mar 01 3639 | 3640 | """Bitwise shift left. 3641 | 3642 | """ 3643 | 3644 | tag = 'LeftShift' 3645 | 3646 | 3647 | def __init__(self, indent, lineno, left, right): 3648 | Node.__init__(self, indent, lineno) 3649 | self.left = transform(indent, lineno, left) 3650 | self.right = transform(indent, lineno, right) 3651 | return 3652 | 3653 | def put(self, can_split=False): 3654 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 3655 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 3656 | self.line_more('<< ') 3657 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 3658 | return self 3659 | 3660 | def get_hi_lineno(self): 3661 | return self.right.get_hi_lineno() 3662 | 3663 | 3664 | class NodeList(Node): 3665 | 3666 | """Declaration of a mutable list. 3667 | 3668 | """ 3669 | 3670 | tag = 'List' 3671 | 3672 | def __init__(self, indent, lineno, nodes): 3673 | Node.__init__(self, indent, lineno) 3674 | self.nodes = [transform(indent, lineno, node) for node in nodes] 3675 | return 3676 | 3677 | def put(self, can_split=False): 3678 | self.line_more('[', tab_set=True) 3679 | if len(self.nodes) > MAX_SEPS_SERIES: # 2007 May 24 3680 | self.line_term() 3681 | self.inc_margin() 3682 | for node in self.nodes: 3683 | self.line_init() 3684 | node.put(can_split=True) 3685 | self.line_more(LIST_SEP) 3686 | self.line_term() 3687 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 3688 | self.dec_margin() 3689 | self.line_init() 3690 | else: 3691 | self.line_init() 3692 | self.dec_margin() 3693 | else: 3694 | for node in (self.nodes)[:1]: 3695 | node.put(can_split=True) 3696 | for node in (self.nodes)[1:]: 3697 | self.line_more(LIST_SEP, can_split_after=True) 3698 | node.put(can_split=True) 3699 | self.line_more(']', tab_clear=True) 3700 | return self 3701 | 3702 | def get_hi_lineno(self): 3703 | lineno = Node.get_hi_lineno(self) 3704 | if self.nodes: 3705 | lineno = (self.nodes)[-1].get_hi_lineno() 3706 | return lineno 3707 | 3708 | 3709 | class NodeListComp(Node): 3710 | 3711 | """List comprehension. 3712 | 3713 | """ 3714 | 3715 | tag = 'ListComp' 3716 | 3717 | def __init__(self, indent, lineno, expr, quals): 3718 | Node.__init__(self, indent, lineno) 3719 | self.expr = transform(indent, lineno, expr) 3720 | self.quals = [transform(indent, lineno, qual) for qual in quals] 3721 | return 3722 | 3723 | def put(self, can_split=False): 3724 | self.push_scope() 3725 | self.marshal_names() 3726 | self.line_more('[', tab_set=True) 3727 | self.expr.put(can_split=True) 3728 | for qual in self.quals: 3729 | qual.put(can_split=True) 3730 | self.line_more(']', tab_clear=True) 3731 | self.pop_scope() 3732 | return self 3733 | 3734 | def push_scope(self): 3735 | NAME_SPACE.push_scope() 3736 | return self 3737 | 3738 | def pop_scope(self): 3739 | NAME_SPACE.pop_scope() 3740 | return self 3741 | 3742 | def marshal_names(self): 3743 | for qual in self.quals: 3744 | qual.marshal_names() 3745 | return self 3746 | 3747 | def get_hi_lineno(self): 3748 | lineno = (self.quals)[-1].get_hi_lineno() 3749 | return lineno 3750 | 3751 | 3752 | class NodeListCompFor(Node): 3753 | 3754 | '''"For" of a list comprehension. 3755 | 3756 | ''' 3757 | 3758 | tag = 'ListCompFor' 3759 | 3760 | def __init__(self, indent, lineno, assign, list, ifs): 3761 | Node.__init__(self, indent, lineno) 3762 | self.assign = transform(indent, lineno, assign) 3763 | self.list = transform(indent, lineno, list) 3764 | self.ifs = [transform(indent, lineno, if_) for if_ in ifs] 3765 | return 3766 | 3767 | def put(self, can_split=False): 3768 | self.line_more(SPACE, can_split_after=True) 3769 | self.line_more('for ') 3770 | self.assign.put(can_split=can_split) 3771 | self.line_more(' in ', can_split_after=True) 3772 | self.list.put(can_split=can_split) 3773 | for if_ in self.ifs: 3774 | if_.put(can_split=can_split) 3775 | return self 3776 | 3777 | def marshal_names(self): 3778 | self.assign.make_local_name() 3779 | return self 3780 | 3781 | def get_hi_lineno(self): 3782 | lineno = self.list.get_hi_lineno() 3783 | if self.ifs: 3784 | lineno = (self.ifs)[-1].get_hi_lineno() 3785 | return lineno 3786 | 3787 | 3788 | class NodeListCompIf(Node): 3789 | 3790 | '''"If" of a list comprehension. 3791 | 3792 | ''' 3793 | 3794 | tag = 'ListCompIf' 3795 | 3796 | def __init__(self, indent, lineno, test): 3797 | Node.__init__(self, indent, lineno) 3798 | self.test = transform(indent, lineno, test) 3799 | return 3800 | 3801 | def put(self, can_split=False): 3802 | self.line_more(SPACE, can_split_after=True) 3803 | self.line_more('if ') 3804 | self.test.put(can_split=can_split) 3805 | return self 3806 | 3807 | def get_hi_lineno(self): 3808 | return self.test.get_hi_lineno() 3809 | 3810 | 3811 | class NodeMod(NodeOprLeftAssoc): # 2010 Mar 10 3812 | 3813 | """Modulus (string formatting) operation. 3814 | 3815 | """ 3816 | 3817 | tag = 'Mod' 3818 | 3819 | def __init__(self, indent, lineno, left, right): 3820 | Node.__init__(self, indent, lineno) 3821 | self.left = transform(indent, lineno, left) 3822 | self.right = transform(indent, lineno, right) 3823 | return 3824 | 3825 | def put(self, can_split=False): 3826 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 3827 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 3828 | self.line_more('% ') 3829 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 3830 | return self 3831 | 3832 | def get_hi_lineno(self): 3833 | return self.right.get_hi_lineno() 3834 | 3835 | 3836 | class NodeModule(Node): 3837 | 3838 | """A whole script. 3839 | 3840 | Contains a doc string and a statement. 3841 | 3842 | """ 3843 | 3844 | tag = 'Module' 3845 | 3846 | def __init__(self, indent, lineno, doc, node): 3847 | Node.__init__(self, indent, lineno) 3848 | self.doc = transform(indent, lineno, doc) 3849 | self.node = transform(indent, lineno, node) 3850 | return 3851 | 3852 | def put(self, can_split=False): 3853 | if self.doc is None: 3854 | pass 3855 | else: 3856 | self.doc.lineno = self.get_lineno() 3857 | self.doc.put_doc() 3858 | if BOILERPLATE == NULL: # 2007 Mar 06 3859 | pass 3860 | else: 3861 | self.line_init() 3862 | self.line_more(BOILERPLATE) 3863 | self.line_term() 3864 | self.node.put() 3865 | return self 3866 | 3867 | def push_scope(self): 3868 | NAME_SPACE.push_scope() 3869 | return self 3870 | 3871 | def pop_scope(self): 3872 | NAME_SPACE.pop_scope() 3873 | return self 3874 | 3875 | def marshal_names(self): 3876 | self.node.marshal_names() 3877 | return self 3878 | 3879 | def get_lineno(self): 3880 | return self.node.get_lineno() 3881 | 3882 | 3883 | class NodeMul(NodeOprLeftAssoc): # 2010 Mar 10 3884 | 3885 | """Multiply operation. 3886 | 3887 | """ 3888 | 3889 | tag = 'Mul' 3890 | 3891 | def __init__(self, indent, lineno, left, right): 3892 | Node.__init__(self, indent, lineno) 3893 | self.left = transform(indent, lineno, left) 3894 | self.right = transform(indent, lineno, right) 3895 | return 3896 | 3897 | def put(self, can_split=False): 3898 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 3899 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 3900 | self.line_more('* ') 3901 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 3902 | return self 3903 | 3904 | def get_hi_lineno(self): 3905 | return self.right.get_hi_lineno() 3906 | 3907 | 3908 | class NodeName(Node): 3909 | 3910 | """Variable. 3911 | 3912 | """ 3913 | 3914 | tag = 'Name' 3915 | 3916 | def __init__(self, indent, lineno, name): 3917 | Node.__init__(self, indent, lineno) 3918 | self.name = transform(indent, lineno, name) 3919 | return 3920 | 3921 | def put(self, can_split=False): 3922 | self.line_more(NAME_SPACE.get_name(self.name)) 3923 | return self 3924 | 3925 | def make_local_name(self): 3926 | if NAME_SPACE.has_name(self.name): 3927 | pass 3928 | else: 3929 | NAME_SPACE.make_local_name(self.name) 3930 | return self 3931 | 3932 | def get_hi_lineno(self): 3933 | return self.name.get_hi_lineno() 3934 | 3935 | 3936 | class NodeNot(NodeOpr): 3937 | 3938 | """Logical negation. 3939 | 3940 | """ 3941 | 3942 | tag = 'Not' 3943 | 3944 | def __init__(self, indent, lineno, expr): 3945 | Node.__init__(self, indent, lineno) 3946 | self.expr = transform(indent, lineno, expr) 3947 | return 3948 | 3949 | def put(self, can_split=False): 3950 | self.line_more('not ') 3951 | self.put_expr(self.expr, can_split=can_split) 3952 | return self 3953 | 3954 | def get_hi_lineno(self): 3955 | return self.expr.get_hi_lineno() 3956 | 3957 | 3958 | class NodeOr(NodeOprAssoc): # 2010 Mar 10 3959 | 3960 | '''Logical "or" operation. 3961 | 3962 | ''' 3963 | 3964 | tag = 'Or' 3965 | 3966 | def __init__(self, indent, lineno, nodes): 3967 | Node.__init__(self, indent, lineno) 3968 | self.nodes = [transform(indent, lineno, node) for node in nodes] 3969 | return 3970 | 3971 | def put(self, can_split=False): 3972 | for node in (self.nodes)[:1]: 3973 | self.put_expr(node, can_split=can_split) 3974 | for node in (self.nodes)[1:]: 3975 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 3976 | self.line_more('or ') 3977 | self.put_expr(node, can_split=can_split) 3978 | return self 3979 | 3980 | def get_hi_lineno(self): 3981 | return (self.nodes)[-1].get_hi_lineno() 3982 | 3983 | 3984 | class NodePass(Node): 3985 | 3986 | """No-op. 3987 | 3988 | """ 3989 | 3990 | tag = 'Pass' 3991 | 3992 | def __init__(self, indent, lineno): 3993 | Node.__init__(self, indent, lineno) 3994 | return 3995 | 3996 | def put(self, can_split=False): 3997 | self.line_init() 3998 | self.line_more('pass') 3999 | self.line_term() 4000 | return self 4001 | 4002 | 4003 | class NodePower(NodeOprRightAssoc): # 2010 Mar 10 4004 | 4005 | """Exponentiation. 4006 | 4007 | """ 4008 | 4009 | tag = 'Power' 4010 | 4011 | def __init__(self, indent, lineno, left, right): 4012 | Node.__init__(self, indent, lineno) 4013 | self.left = transform(indent, lineno, left) 4014 | self.right = transform(indent, lineno, right) 4015 | return 4016 | 4017 | def put(self, can_split=False): 4018 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 4019 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 4020 | self.line_more('** ') 4021 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 4022 | return self 4023 | 4024 | def get_hi_lineno(self): 4025 | return self.right.get_hi_lineno() 4026 | 4027 | 4028 | class NodePrint(Node): 4029 | 4030 | """The print statement with optional chevron and trailing comma. 4031 | 4032 | """ 4033 | 4034 | tag = 'Print' 4035 | 4036 | def __init__(self, indent, lineno, nodes, dest): 4037 | Node.__init__(self, indent, lineno) 4038 | self.nodes = [transform(indent, lineno, node) for node in nodes] 4039 | self.dest = transform(indent, lineno, dest) 4040 | return 4041 | 4042 | def put(self, can_split=False): 4043 | self.line_init() 4044 | self.line_more('print ') 4045 | if self.dest is None: 4046 | pass 4047 | else: 4048 | self.line_more('>> ') 4049 | self.dest.put(can_split=can_split) 4050 | if self.nodes: 4051 | self.line_more(LIST_SEP, can_break_after=True) 4052 | for node in self.nodes: 4053 | node.put(can_split=can_split) 4054 | self.line_more(LIST_SEP, can_break_after=True) 4055 | self.line_term() 4056 | return self 4057 | 4058 | def get_hi_lineno(self): 4059 | lineno = Node.get_hi_lineno(self) 4060 | if self.dest is None: 4061 | pass 4062 | else: 4063 | lineno = self.dest.get_hi_lineno() 4064 | if self.nodes: 4065 | lineno = (self.nodes)[-1].get_hi_lineno() 4066 | return lineno 4067 | 4068 | 4069 | class NodePrintnl(Node): 4070 | 4071 | """The print statement with optional chevron and without trailing comma. 4072 | 4073 | """ 4074 | 4075 | tag = 'Printnl' 4076 | 4077 | def __init__(self, indent, lineno, nodes, dest): 4078 | Node.__init__(self, indent, lineno) 4079 | self.nodes = [transform(indent, lineno, node) for node in nodes] 4080 | self.dest = transform(indent, lineno, dest) 4081 | return 4082 | 4083 | def put(self, can_split=False): 4084 | self.line_init() 4085 | self.line_more('print ') 4086 | if self.dest is None: 4087 | pass 4088 | else: 4089 | self.line_more('>> ') 4090 | self.dest.put(can_split=can_split) 4091 | if self.nodes: 4092 | self.line_more(LIST_SEP, can_break_after=True) 4093 | for node in (self.nodes)[:-1]: 4094 | node.put(can_split=can_split) 4095 | self.line_more(LIST_SEP, can_break_after=True) 4096 | for node in (self.nodes)[-1:]: 4097 | node.put(can_split=can_split) 4098 | self.line_term() 4099 | return self 4100 | 4101 | def get_hi_lineno(self): 4102 | lineno = Node.get_hi_lineno(self) 4103 | if self.dest is None: 4104 | pass 4105 | else: 4106 | lineno = self.dest.get_hi_lineno() 4107 | if self.nodes: 4108 | lineno = (self.nodes)[-1].get_hi_lineno() 4109 | return lineno 4110 | 4111 | 4112 | class NodeRaise(Node): 4113 | 4114 | """Raise an exception. 4115 | 4116 | """ 4117 | 4118 | tag = 'Raise' 4119 | 4120 | def __init__(self, indent, lineno, expr1, expr2, expr3): 4121 | Node.__init__(self, indent, lineno) 4122 | self.expr1 = transform(indent, lineno, expr1) 4123 | self.expr2 = transform(indent, lineno, expr2) 4124 | self.expr3 = transform(indent, lineno, expr3) 4125 | return 4126 | 4127 | def put(self, can_split=False): 4128 | self.line_init() 4129 | self.line_more('raise ') 4130 | if self.expr1 is None: 4131 | pass 4132 | else: 4133 | self.expr1.put(can_split=can_split) 4134 | if self.expr2 is None: 4135 | pass 4136 | else: 4137 | self.line_more(LIST_SEP, can_break_after=True) 4138 | self.expr2.put(can_split=can_split) 4139 | if self.expr3 is None: 4140 | pass 4141 | else: 4142 | self.line_more(LIST_SEP, can_break_after=True) 4143 | self.expr3.put(can_split=can_split) 4144 | self.line_term() 4145 | return self 4146 | 4147 | def get_hi_lineno(self): 4148 | lineno = Node.get_hi_lineno(self) 4149 | if self.expr1 is None: 4150 | pass 4151 | else: 4152 | lineno = self.expr1.get_hi_lineno() 4153 | if self.expr2 is None: 4154 | pass 4155 | else: 4156 | lineno = self.expr2.get_hi_lineno() 4157 | if self.expr3 is None: 4158 | pass 4159 | else: 4160 | lineno = self.expr3.get_hi_lineno() 4161 | return lineno 4162 | 4163 | 4164 | class NodeReturn(Node): 4165 | 4166 | """Return a value from a function. 4167 | 4168 | """ 4169 | 4170 | tag = 'Return' 4171 | 4172 | def __init__(self, indent, lineno, value): 4173 | Node.__init__(self, indent, lineno) 4174 | self.value = transform(indent, lineno, value) 4175 | return 4176 | 4177 | def has_value(self): 4178 | return not (isinstance(self.value, NodeConst) and self.value.is_none()) 4179 | 4180 | def put(self, can_split=False): 4181 | self.line_init() 4182 | self.line_more('return ') 4183 | if self.has_value(): 4184 | if isinstance(self.value, NodeTuple): 4185 | self.value.put(can_split=can_split, is_paren_required=False) # 2010 Mar 10 4186 | else: 4187 | self.value.put(can_split=can_split) 4188 | self.line_term() 4189 | return self 4190 | 4191 | def get_hi_lineno(self): 4192 | lineno = Node.get_hi_lineno(self) 4193 | if self.has_value: 4194 | lineno = self.value.get_hi_lineno() 4195 | return lineno 4196 | 4197 | 4198 | class NodeRightShift(NodeOprLeftAssoc): # 2010 Mar 10 4199 | 4200 | """Bitwise shift right. 4201 | 4202 | """ 4203 | 4204 | tag = 'RightShift' 4205 | 4206 | def __init__(self, indent, lineno, left, right): 4207 | Node.__init__(self, indent, lineno) 4208 | self.left = transform(indent, lineno, left) 4209 | self.right = transform(indent, lineno, right) 4210 | return 4211 | 4212 | def put(self, can_split=False): 4213 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 4214 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 4215 | self.line_more('>> ') 4216 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 4217 | return self 4218 | 4219 | def get_hi_lineno(self): 4220 | return self.right.get_hi_lineno() 4221 | 4222 | 4223 | class NodeSlice(NodeOpr): 4224 | 4225 | """A slice of a series. 4226 | 4227 | """ 4228 | 4229 | tag = 'Slice' 4230 | 4231 | def __init__(self, indent, lineno, expr, flags, lower, upper): 4232 | Node.__init__(self, indent, lineno) 4233 | self.expr = transform(indent, lineno, expr) 4234 | self.flags = transform(indent, lineno, flags) 4235 | self.lower = transform(indent, lineno, lower) 4236 | self.upper = transform(indent, lineno, upper) 4237 | return 4238 | 4239 | def has_value(self, node): 4240 | return not (node is None or isinstance(node, NodeConst) and node.is_none()) 4241 | 4242 | def put(self, can_split=False): 4243 | is_del = self.flags.get_as_str() in ['OP_DELETE'] 4244 | if is_del: 4245 | self.line_init() 4246 | self.line_more('del ') 4247 | if (isinstance(self.expr, NodeGetAttr) 4248 | or isinstance(self.expr, NodeAsgAttr)): # 2007 May 23 4249 | self.expr.put(can_split=can_split) 4250 | else: 4251 | self.put_expr(self.expr, can_split=can_split) 4252 | self.line_more('[') 4253 | if self.has_value(self.lower): 4254 | self.lower.put(can_split=True) 4255 | self.line_more(SLICE_COLON) 4256 | if self.has_value(self.upper): 4257 | self.upper.put(can_split=True) 4258 | self.line_more(']') 4259 | if DEBUG: 4260 | self.line_more(' /* Subscript flags: ') 4261 | self.flags.put() 4262 | self.line_more(' */ ') 4263 | if is_del: 4264 | self.line_term() 4265 | return self 4266 | 4267 | def make_local_name(self): 4268 | self.expr.make_local_name() 4269 | return self 4270 | 4271 | def get_hi_lineno(self): 4272 | lineno = Node.get_hi_lineno(self) 4273 | if self.has_value(self.lower): 4274 | lineno = self.lower.get_hi_lineno() 4275 | if self.has_value(self.upper): 4276 | lineno = self.upper.get_hi_lineno() 4277 | return lineno 4278 | 4279 | 4280 | class NodeSliceobj(Node): 4281 | 4282 | """A subscript range. 4283 | 4284 | This is used for multi-dimensioned arrays. 4285 | 4286 | """ 4287 | 4288 | tag = 'Sliceobj' 4289 | 4290 | def __init__(self, indent, lineno, nodes): 4291 | Node.__init__(self, indent, lineno) 4292 | self.nodes = [transform(indent, lineno, node) for node in nodes] 4293 | return 4294 | 4295 | def has_value(self, node): 4296 | return not (node is None or isinstance(node, NodeConst) and node.is_none()) 4297 | 4298 | def put(self, can_split=False): 4299 | for node in (self.nodes)[:1]: 4300 | if self.has_value(node): 4301 | node.put(can_split=can_split) 4302 | for node in (self.nodes)[1:]: 4303 | self.line_more(SLICE_COLON, can_split_after=True) 4304 | if self.has_value(node): 4305 | node.put(can_split=can_split) 4306 | return self 4307 | 4308 | def get_hi_lineno(self): 4309 | lineno = Node.get_hi_lineno(self) 4310 | for node in self.nodes: 4311 | if self.has_value(node): 4312 | lineno = node.get_hi_lineno() 4313 | return lineno 4314 | 4315 | 4316 | class NodeStmt(Node): 4317 | 4318 | """A list of nodes.. 4319 | 4320 | """ 4321 | 4322 | tag = 'Stmt' 4323 | 4324 | def __init__(self, indent, lineno, nodes): 4325 | Node.__init__(self, indent, lineno) 4326 | self.nodes = [transform(indent, lineno, node) for node in nodes] 4327 | return 4328 | 4329 | def put(self, can_split=False): 4330 | for node in self.nodes: 4331 | node.put() 4332 | return self 4333 | 4334 | def get_lineno(self): 4335 | for node in self.nodes: 4336 | result = node.get_lineno() 4337 | if result == ZERO: 4338 | pass 4339 | else: 4340 | return result 4341 | return ZERO 4342 | 4343 | def marshal_names(self): 4344 | for node in self.nodes: 4345 | node.marshal_names() 4346 | return self 4347 | 4348 | 4349 | class NodeSub(NodeOprLeftAssoc): # 2010 Mar 10 4350 | 4351 | """Subtract operation. 4352 | 4353 | """ 4354 | 4355 | tag = 'Sub' 4356 | 4357 | def __init__(self, indent, lineno, left, right): 4358 | Node.__init__(self, indent, lineno) 4359 | self.left = transform(indent, lineno, left) 4360 | self.right = transform(indent, lineno, right) 4361 | return 4362 | 4363 | def put(self, can_split=False): 4364 | self.put_expr(self.left, can_split=can_split, pos='left') # 2010 Mar 10 4365 | self.line_more(SPACE, can_split_after=can_split, can_break_after=True) # 2007 May 23 4366 | self.line_more('- ') 4367 | self.put_expr(self.right, can_split=can_split, pos='right') # 2010 Mar 10 4368 | return self 4369 | 4370 | def get_hi_lineno(self): 4371 | return self.right.get_hi_lineno() 4372 | 4373 | 4374 | class NodeSubscript(NodeOpr): 4375 | 4376 | """A subscripted sequence. 4377 | 4378 | """ 4379 | 4380 | tag = 'Subscript' 4381 | 4382 | def __init__(self, indent, lineno, expr, flags, subs): 4383 | Node.__init__(self, indent, lineno) 4384 | self.expr = transform(indent, lineno, expr) 4385 | self.flags = transform(indent, lineno, flags) 4386 | self.subs = [transform(indent, lineno, sub) for sub in subs] 4387 | return 4388 | 4389 | def put(self, can_split=False): 4390 | is_del = self.flags.get_as_str() in ['OP_DELETE'] 4391 | if is_del: 4392 | self.line_init() 4393 | self.line_more('del ') 4394 | if (isinstance(self.expr, NodeGetAttr) 4395 | or isinstance(self.expr, NodeAsgAttr)): # 2007 May 23 4396 | self.expr.put(can_split=can_split) 4397 | else: 4398 | self.put_expr(self.expr, can_split=can_split) 4399 | if DEBUG: 4400 | self.line_more(' /* Subscript flags: ') 4401 | self.flags.put() 4402 | self.line_more(' */ ') 4403 | self.line_more('[', tab_set=True) 4404 | for sub in (self.subs)[:1]: 4405 | sub.put(can_split=True) 4406 | for sub in (self.subs)[1:]: 4407 | self.line_more(SUBSCRIPT_SEP, can_split_after=True) 4408 | sub.put(can_split=True) 4409 | self.line_more(']', tab_clear=True) 4410 | if is_del: 4411 | self.line_term() 4412 | return self 4413 | 4414 | def make_local_name(self): 4415 | self.expr.make_local_name() 4416 | return self 4417 | 4418 | def get_hi_lineno(self): 4419 | lineno = self.expr.get_hi_lineno() 4420 | if self.subs: 4421 | lineno = (self.subs)[-1].get_hi_lineno() 4422 | return lineno 4423 | 4424 | 4425 | class NodeTryExcept(Node): 4426 | 4427 | """Define exception handlers. 4428 | 4429 | """ 4430 | 4431 | tag = 'TryExcept' 4432 | 4433 | def __init__(self, indent, lineno, body, handlers, else_): 4434 | Node.__init__(self, indent, lineno) 4435 | self.body = transform(indent + 1, lineno, body) 4436 | self.handlers = [(transform(indent, lineno, expr), transform(indent, 4437 | lineno, target), transform(indent + 1, lineno, 4438 | suite)) for (expr, target, suite) in handlers] 4439 | self.else_ = transform(indent + 1, lineno, else_) 4440 | self.has_finally = False 4441 | return 4442 | 4443 | def put(self, can_split=False): 4444 | if self.has_finally: 4445 | pass 4446 | else: 4447 | self.line_init() 4448 | self.line_more('try:') 4449 | self.line_term(self.body.get_lineno() - 1) 4450 | self.body.put() 4451 | for (expr, target, suite) in self.handlers: 4452 | self.line_init() 4453 | self.line_more('except') 4454 | if expr is None: 4455 | pass 4456 | else: 4457 | self.line_more(SPACE) 4458 | expr.put() 4459 | if target is None: 4460 | pass 4461 | else: 4462 | self.line_more(LIST_SEP, can_break_after=True) 4463 | target.put() 4464 | self.line_more(':') 4465 | self.line_term(suite.get_lineno() - 1) 4466 | suite.put() 4467 | if self.else_ is None: 4468 | pass 4469 | else: 4470 | self.line_init() 4471 | self.line_more('else:') 4472 | self.line_term(self.else_.get_lineno() - 1) 4473 | self.else_.put() 4474 | return self 4475 | 4476 | def marshal_names(self): 4477 | self.body.marshal_names() 4478 | for (expr, target, suite) in self.handlers: 4479 | suite.marshal_names() 4480 | if self.else_ is None: 4481 | pass 4482 | else: 4483 | self.else_.marshal_names() 4484 | return self 4485 | 4486 | 4487 | class NodeTryFinally(Node): 4488 | 4489 | """Force housekeeping code to execute even after an unhandled 4490 | except is raised and before the default handling takes care of it. 4491 | 4492 | """ 4493 | 4494 | tag = 'TryFinally' 4495 | 4496 | def __init__(self, indent, lineno, body, final): 4497 | Node.__init__(self, indent, lineno) 4498 | if isinstance(body, compiler.ast.TryExcept): 4499 | self.body = transform(indent, lineno, body) 4500 | self.body.has_finally = True 4501 | else: 4502 | self.body = transform(indent + 1, lineno, body) 4503 | self.final = transform(indent + 1, lineno, final) 4504 | return 4505 | 4506 | def put(self, can_split=False): 4507 | self.line_init() 4508 | self.line_more('try:') 4509 | self.line_term(self.body.get_lineno() - 1) 4510 | self.body.put() 4511 | self.line_init() 4512 | self.line_more('finally:') 4513 | self.line_term(self.final.get_lineno() - 1) 4514 | self.final.put() 4515 | return self 4516 | 4517 | def marshal_names(self): 4518 | self.body.marshal_names() 4519 | self.final.marshal_names() 4520 | return self 4521 | 4522 | 4523 | class NodeTuple(Node): 4524 | 4525 | """Declaration of an immutable tuple. 4526 | 4527 | """ 4528 | 4529 | tag = 'Tuple' 4530 | 4531 | def __init__(self, indent, lineno, nodes): 4532 | Node.__init__(self, indent, lineno) 4533 | self.nodes = [transform(indent, lineno, node) for node in nodes] 4534 | return 4535 | 4536 | def put(self, can_split=False, is_paren_required=True): # 2010 Mar 10 4537 | if len(self.nodes) > MAX_SEPS_SERIES: # 2007 May 24 4538 | self.line_more('(', tab_set=True) # 2010 Mar 10 4539 | self.line_term() 4540 | self.inc_margin() 4541 | for node in self.nodes: 4542 | self.line_init() 4543 | node.put(can_split=True) 4544 | self.line_more(LIST_SEP) 4545 | self.line_term() 4546 | if JAVA_STYLE_LIST_DEDENT: # 2010 Sep 08 4547 | self.dec_margin() 4548 | self.line_init() 4549 | else: 4550 | self.line_init() 4551 | self.dec_margin() 4552 | self.line_more(')', tab_clear=True) # 2010 Mar 10 4553 | elif ((len(self.nodes) == ZERO) or 4554 | is_paren_required or 4555 | PARENTHESIZE_TUPLE_DISPLAY): # 2010 Mar 10 4556 | self.line_more('(', tab_set=True) # 2010 Mar 10 4557 | for node in (self.nodes)[:1]: 4558 | node.put(can_split=True) 4559 | self.line_more(LIST_SEP, can_split_after=True) 4560 | for node in (self.nodes)[1:2]: 4561 | node.put(can_split=True) 4562 | for node in (self.nodes)[2:]: 4563 | self.line_more(LIST_SEP, can_split_after=True) 4564 | node.put(can_split=True) 4565 | self.line_more(')', tab_clear=True) # 2010 Mar 10 4566 | else: 4567 | for node in (self.nodes)[:1]: 4568 | node.put() 4569 | self.line_more(LIST_SEP, can_break_after=True) # 2010 Mar 10 4570 | for node in (self.nodes)[1:2]: 4571 | node.put() 4572 | for node in (self.nodes)[2:]: 4573 | self.line_more(LIST_SEP, can_break_after=True) # 2010 Mar 10 4574 | node.put() 4575 | return self 4576 | 4577 | def get_hi_lineno(self): 4578 | lineno = Node.get_hi_lineno(self) 4579 | if self.nodes: 4580 | lineno = (self.nodes)[-1].get_hi_lineno() 4581 | return lineno 4582 | 4583 | 4584 | class NodeUnaryAdd(NodeOpr): 4585 | 4586 | """Algebraic positive. 4587 | 4588 | """ 4589 | 4590 | tag = 'UnaryAdd' 4591 | 4592 | def __init__(self, indent, lineno, expr): 4593 | Node.__init__(self, indent, lineno) 4594 | self.expr = transform(indent, lineno, expr) 4595 | return 4596 | 4597 | def put(self, can_split=False): 4598 | self.line_more('+') 4599 | self.put_expr(self.expr, can_split=can_split) 4600 | return self 4601 | 4602 | def get_hi_lineno(self): 4603 | return self.expr.get_hi_lineno() 4604 | 4605 | 4606 | class NodeUnarySub(NodeOpr): 4607 | 4608 | """Algebraic negative. 4609 | 4610 | """ 4611 | 4612 | tag = 'UnarySub' 4613 | 4614 | def __init__(self, indent, lineno, expr): 4615 | Node.__init__(self, indent, lineno) 4616 | self.expr = transform(indent, lineno, expr) 4617 | return 4618 | 4619 | def put(self, can_split=False): 4620 | self.line_more('-') 4621 | self.put_expr(self.expr, can_split=can_split) 4622 | return self 4623 | 4624 | def get_hi_lineno(self): 4625 | return self.expr.get_hi_lineno() 4626 | 4627 | 4628 | class NodeWhile(Node): 4629 | 4630 | """While loop. 4631 | 4632 | """ 4633 | 4634 | tag = 'While' 4635 | 4636 | def __init__(self, indent, lineno, test, body, else_): 4637 | Node.__init__(self, indent, lineno) 4638 | self.test = transform(indent, lineno, test) 4639 | self.body = transform(indent + 1, lineno, body) 4640 | self.else_ = transform(indent + 1, lineno, else_) 4641 | return 4642 | 4643 | def put(self, can_split=False): 4644 | self.line_init() 4645 | self.line_more('while ') 4646 | self.test.put(can_split=can_split) 4647 | self.line_more(':') 4648 | self.line_term(self.body.get_lineno() - 1) 4649 | self.body.put() 4650 | if self.else_ is None: 4651 | pass 4652 | else: 4653 | self.line_init() 4654 | self.line_more('else:') 4655 | self.line_term(self.else_.get_lineno() - 1) 4656 | self.else_.put() 4657 | return self 4658 | 4659 | def marshal_names(self): 4660 | self.body.marshal_names() 4661 | if self.else_ is None: 4662 | pass 4663 | else: 4664 | self.else_.marshal_names() 4665 | return 4666 | 4667 | def get_hi_lineno(self): 4668 | return self.test.get_hi_lineno() 4669 | 4670 | 4671 | class NodeWith(Node): 4672 | 4673 | """Context manager. 4674 | 4675 | """ 4676 | 4677 | tag = 'With' 4678 | 4679 | def __init__(self, indent, lineno, expr, vars, body): 4680 | Node.__init__(self, indent, lineno) 4681 | self.expr = transform(indent, lineno, expr) 4682 | self.vars = transform(indent, lineno, vars) 4683 | self.body = transform(indent + 1, lineno, body) 4684 | return 4685 | 4686 | def put(self, can_split=False): 4687 | self.line_init() 4688 | self.line_more('with ') 4689 | self.expr.put(can_split=can_split) 4690 | if self.vars is None: 4691 | pass 4692 | else: 4693 | self.line_more(' as ', can_break_after=True) 4694 | self.vars.put(can_split=can_split) 4695 | self.line_more(':') 4696 | self.line_term(self.body.get_lineno() - 1) 4697 | self.body.put() 4698 | return self 4699 | 4700 | def marshal_names(self): 4701 | if self.vars is None: 4702 | pass 4703 | else: 4704 | self.vars.make_local_name() 4705 | self.body.marshal_names() 4706 | return self 4707 | 4708 | def get_hi_lineno(self): 4709 | lineno = self.expr.get_hi_lineno() 4710 | if self.vars is None: 4711 | pass 4712 | else: 4713 | lineno = self.vars.get_hi_lineno() 4714 | return lineno 4715 | 4716 | 4717 | class NodeYield(Node): 4718 | 4719 | """Yield a generator value. 4720 | 4721 | """ 4722 | 4723 | tag = 'Yield' 4724 | 4725 | def __init__(self, indent, lineno, value): 4726 | Node.__init__(self, indent, lineno) 4727 | self.value = transform(indent, lineno, value) 4728 | return 4729 | 4730 | def put(self, can_split=False): 4731 | self.line_more('yield ') # 2006 Dec 13 4732 | self.value.put(can_split=can_split) 4733 | return self 4734 | 4735 | def get_hi_lineno(self): 4736 | return self.value.get_hi_lineno() 4737 | 4738 | 4739 | # The abstract syntax tree returns the nodes of arithmetic and logical 4740 | # expressions in the correct order for evaluation, but, to reconstruct 4741 | # the specifying code in general and to output it correctly, we need 4742 | # to insert parentheses to enforce the correct order. 4743 | 4744 | # This is a Python Version Dependency. 4745 | 4746 | OPERATOR_PRECEDENCE = [ 4747 | (NodeIfExp, ), 4748 | (NodeLambda, ), 4749 | (NodeOr, ), 4750 | (NodeAnd, ), 4751 | (NodeNot, ), 4752 | (NodeCompare, ), 4753 | (NodeBitOr, ), 4754 | (NodeBitXor, ), 4755 | (NodeBitAnd, ), 4756 | (NodeLeftShift, NodeRightShift), 4757 | (NodeAdd, NodeSub), 4758 | (NodeMul, NodeDiv, NodeFloorDiv, NodeMod), 4759 | (NodeUnaryAdd, NodeUnarySub, NodeInvert, ), # 2010 Mar 10 4760 | (NodePower, ), 4761 | (NodeAsgAttr, NodeGetAttr), 4762 | (NodeSubscript, ), 4763 | (NodeSlice, ), 4764 | (NodeCallFunc, ), 4765 | (NodeTuple, ), 4766 | (NodeList, ), 4767 | (NodeDict, ), 4768 | (NodeBackquote, ), 4769 | ] 4770 | OPERATORS = [] 4771 | OPERATOR_TRUMPS = {} 4772 | OPERATOR_LEVEL = {} 4773 | for LEVEL in OPERATOR_PRECEDENCE: 4774 | for OPERATOR in LEVEL: 4775 | OPERATOR_LEVEL[OPERATOR] = LEVEL 4776 | OPERATOR_TRUMPS[OPERATOR] = OPERATORS[:] # a static copy. 4777 | OPERATORS.extend(LEVEL) 4778 | 4779 | 4780 | def tidy_up(file_in=sys.stdin, file_out=sys.stdout): # 2007 Jan 22 4781 | 4782 | """Clean up, regularize, and reformat the text of a Python script. 4783 | 4784 | File_in is a file name or a file-like object with a *read* method, 4785 | which contains the input script. 4786 | 4787 | File_out is a file name or a file-like object with a *write* 4788 | method to contain the output script. 4789 | 4790 | """ 4791 | 4792 | global INPUT, OUTPUT, COMMENTS, NAME_SPACE, INPUT_CODING # 2007 May 23 4793 | INPUT = InputUnit(file_in) 4794 | OUTPUT = OutputUnit(file_out) 4795 | COMMENTS = Comments() 4796 | NAME_SPACE = NameSpace() 4797 | module = compiler.parse(str(INPUT)) 4798 | module = transform(indent=ZERO, lineno=ZERO, node=module) 4799 | INPUT_CODING = INPUT.coding # 2007 May 23 4800 | del INPUT 4801 | module.push_scope().marshal_names().put().pop_scope() 4802 | COMMENTS.merge(fin=True) 4803 | OUTPUT.close() 4804 | return 4805 | 4806 | if __name__ == "__main__": # 2007 Jan 22 4807 | if DEBUG: 4808 | print 'Begin doctests.' 4809 | doctest.testmod() 4810 | print ' End doctests.' 4811 | if len(sys.argv) > 1: 4812 | file_in = sys.argv[1] 4813 | else: 4814 | file_in = '-' 4815 | if file_in in ['-']: 4816 | file_in = sys.stdin 4817 | if len(sys.argv) > 2: 4818 | file_out = sys.argv[2] 4819 | else: 4820 | file_out = '-' 4821 | if file_out in ['-']: 4822 | file_out = sys.stdout 4823 | tidy_up(file_in, file_out) 4824 | 4825 | # Fin 4826 | --------------------------------------------------------------------------------