def build_class(self, code):
        """Dump class definition, doc string and class body."""

        assert iscode(code)
        self.classes.append(self.currentclass)
        code = Code(code, self.scanner, self.currentclass)

        indent = self.indent
        # self.println(indent, '#flags:\t', int(code.co_flags))
        ast = self.build_ast(code._tokens, code._customize)
        code._tokens = None # save memory
        assert ast == 'stmts'

        first_stmt = ast[0][0]
        if 3.0 <= self.version <= 3.3:
            try:
                if first_stmt[0] == 'store_locals':
                    if self.hide_internal:
                        del ast[0]
                        first_stmt = ast[0][0]
            except:
                pass

        try:
            if first_stmt == NAME_MODULE:
                if self.hide_internal:
                    del ast[0]
                    first_stmt = ast[0][0]
            pass
        except:
            pass

        have_qualname = False
        if self.version < 3.0:
            # Should we ditch this in favor of the "else" case?
            qualname = '.'.join(self.classes)
            QUAL_NAME = SyntaxTree('stmt',
                            [ SyntaxTree('assign',
                                  [ SyntaxTree('expr', [Token('LOAD_CONST', pattr=qualname)]),
                                    SyntaxTree('store', [ Token('STORE_NAME', pattr='__qualname__')])
                                  ])])
            have_qualname = (ast[0][0] == QUAL_NAME)
        else:
            # Python 3.4+ has constants like 'cmp_to_key.<locals>.K'
            # which are not simple classes like the < 3 case.
            try:
                if (first_stmt[0] == 'assign' and
                    first_stmt[0][0][0] == 'LOAD_CONST' and
                    first_stmt[0][1] == 'store' and
                    first_stmt[0][1][0] == Token('STORE_NAME', pattr='__qualname__')):
                    have_qualname = True
            except:
                pass

        if have_qualname:
            if self.hide_internal: del ast[0]
            pass

        # if docstring exists, dump it
        if (code.co_consts and code.co_consts[0] is not None and len(ast) > 0):
            do_doc = False
            if is_docstring(ast[0]):
                i = 0
                do_doc = True
            elif (len(ast) > 1 and is_docstring(ast[1])):
                i = 1
                do_doc = True
            if do_doc and self.hide_internal:
                try:
                    docstring = ast[i][0][0][0][0].pattr
                except:
                    docstring = code.co_consts[0]
                if print_docstring(self, indent, docstring):
                    self.println()
                    del ast[i]

        # the function defining a class normally returns locals(); we
        # don't want this to show up in the source, thus remove the node
        if len(ast) > 0 and ast[-1][0] == RETURN_LOCALS:
            if self.hide_internal: del ast[-1] # remove last node
        # else:
        #    print ast[-1][-1]

        globals, nonlocals = find_globals_and_nonlocals(ast, set(), set(),
                                                        code, self.version)
        # Add "global" declaration statements at the top
        # of the function
        for g in sorted(globals):
            self.println(indent, 'global ', g)

        for nl in sorted(nonlocals):
            self.println(indent, 'nonlocal ', nl)

        old_name = self.name
        self.gen_source(ast, code.co_name, code._customize)
        self.name = old_name
        code._tokens = None; code._customize = None # save memory
        self.classes.pop(-1)
def build_class ( self , code ) : assert iscode ( code ) self . classes . append ( self . currentclass ) code = Code ( code , self . scanner , self . currentclass ) indent = self . indent ast = self . build_ast ( code . _tokens , code . _customize ) code . _tokens = None assert ast == 'stmts' first_stmt = ast [ 0 ] [ 0 ] if 3.0 <= self . version <= 3.3 : try : if first_stmt [ 0 ] == 'store_locals' : if self . hide_internal : del ast [ 0 ] first_stmt = ast [ 0 ] [ 0 ] except : pass try : if first_stmt == NAME_MODULE : if self . hide_internal : del ast [ 0 ] first_stmt = ast [ 0 ] [ 0 ] pass except : pass have_qualname = False if self . version < 3.0 : qualname = '.' . join ( self . classes ) QUAL_NAME = SyntaxTree ( 'stmt' , [ SyntaxTree ( 'assign' , [ SyntaxTree ( 'expr' , [ Token ( 'LOAD_CONST' , pattr = qualname ) ] ) , SyntaxTree ( 'store' , [ Token ( 'STORE_NAME' , pattr = '__qualname__' ) ] ) ] ) ] ) have_qualname = ( ast [ 0 ] [ 0 ] == QUAL_NAME ) else : try : if ( first_stmt [ 0 ] == 'assign' and first_stmt [ 0 ] [ 0 ] [ 0 ] == 'LOAD_CONST' and first_stmt [ 0 ] [ 1 ] == 'store' and first_stmt [ 0 ] [ 1 ] [ 0 ] == Token ( 'STORE_NAME' , pattr = '__qualname__' ) ) : have_qualname = True except : pass if have_qualname : if self . hide_internal : del ast [ 0 ] pass if ( code . co_consts and code . co_consts [ 0 ] is not None and len ( ast ) > 0 ) : do_doc = False if is_docstring ( ast [ 0 ] ) : i = 0 do_doc = True elif ( len ( ast ) > 1 and is_docstring ( ast [ 1 ] ) ) : i = 1 do_doc = True if do_doc and self . hide_internal : try : docstring = ast [ i ] [ 0 ] [ 0 ] [ 0 ] [ 0 ] . pattr except : docstring = code . co_consts [ 0 ] if print_docstring ( self , indent , docstring ) : self . println ( ) del ast [ i ] if len ( ast ) > 0 and ast [ - 1 ] [ 0 ] == RETURN_LOCALS : if self . hide_internal : del ast [ - 1 ] globals , nonlocals = find_globals_and_nonlocals ( ast , set ( ) , set ( ) , code , self . version ) for g in sorted ( globals ) : self . println ( indent , 'global ' , g ) for nl in sorted ( nonlocals ) : self . println ( indent , 'nonlocal ' , nl ) old_name = self . name self . gen_source ( ast , code . co_name , code . _customize ) self . name = old_name code . _tokens = None ; code . _customize = None self . classes . pop ( - 1 )
Dump class definition, doc string and class body.