def run(self, tokens): ''' The main line of PythonTokenBeautifier class. Called by prettPrintNode & test_beautifier. ''' def oops(): g.trace('unknown kind', self.kind) self.code_list = [] self.state_stack = [] self.file_start() for token5tuple in tokens: t1, t2, t3, t4, t5 = token5tuple srow, scol = t3 self.kind = token.tok_name[t1].lower() self.val = g.toUnicode(t2) self.raw_val = g.toUnicode(t5) if srow != self.last_line_number: # Handle a previous backslash. if self.backslash_seen: self.backslash() # Start a new row. raw_val = self.raw_val.rstrip() self.backslash_seen = raw_val.endswith('\\') # g.trace('backslash_seen',self.backslash_seen) if self.paren_level > 0: s = self.raw_val.rstrip() n = g.computeLeadingWhitespaceWidth(s, self.tab_width) # This n will be one-too-many if formatting has # changed: foo ( # to: foo( self.line_indent(ws=' ' * n) # Do not set self.lws here! self.last_line_number = srow # g.trace('%10s %r'% (self.kind,self.val)) func = getattr(self, 'do_' + self.kind, oops) func() self.file_end() return ''.join([z.to_string() for z in self.code_list])
def run(self, tokens): ''' The main line of PythonTokenBeautifier class. Called by prettPrintNode & test_beautifier. ''' def oops(): g.trace('unknown kind', self.kind) self.code_list = [] self.state_stack = [] self.file_start() for token5tuple in tokens: t1, t2, t3, t4, t5 = token5tuple srow, scol = t3 self.kind = token.tok_name[t1].lower() self.val = g.toUnicode(t2) self.raw_val = g.toUnicode(t5) if srow != self.last_line_number: # Handle a previous backslash. if self.backslash_seen: self.backslash() # Start a new row. raw_val = self.raw_val.rstrip() self.backslash_seen = raw_val.endswith('\\') # g.trace('backslash_seen',self.backslash_seen) if self.paren_level > 0: s = self.raw_val.rstrip() n = g.computeLeadingWhitespaceWidth(s, self.tab_width) # This n will be one-too-many if formatting has # changed: foo ( # to: foo( self.line_indent(ws=' ' * n) # Do not set self.lws here! self.last_line_number = srow # g.trace('%10s %r'% (self.kind,self.val)) func = getattr(self, 'do_' + self.kind, oops) func() self.file_end() return ''.join([z.to_string() for z in self.code_list])
def get_int_lws(self, s): '''Return the the lws (a number) of line s.''' # Important: use self.tab_width, *not* c.tab_width. return g.computeLeadingWhitespaceWidth(s, self.tab_width)
def get_int_lws(self, s): """Return the the lws (a number) of line s.""" # Important: use self.tab_width, *not* c.tab_width. return g.computeLeadingWhitespaceWidth(s, self.tab_width)
def lws(self, s): '''Return the indentation of line s.''' return g.computeLeadingWhitespaceWidth(s, self.tab_width)
def lws(self, s): '''Return the indentation of line s.''' return g.computeLeadingWhitespaceWidth(s, self.tab_width)