from pychecker2.Check import Check
from pychecker2.Warning import Warning
from pychecker2 import symbols

from compiler import parseFile, walk
import parser

class ParseCheck(Check):

    syntaxErrors = Warning('Report/ignore syntax errors',
                           'Unable to parse: %s')

    def check(self, file):
        try:
            file.parseTree = parseFile(file.name)
            file.scopes = walk(file.parseTree, symbols.SymbolVisitor()).scopes
            file.root_scope = file.scopes[file.parseTree]
            
            # add starting lineno into scopes, since they don't have it
            for k, v in file.scopes.items():
                v.lineno = k.lineno

            # define the root of the scope tree (global scope, within
            # the module)
            file.root_scope.lineno = 1

            # create a mapping from scopes back to the nodes which made 'em
            for node, scope in file.scopes.items():
                scope.node = node

            # create a mapping from each scope back to it's enclosing scope
            for s in file.scopes.values():
                for c in s.get_children():
                    c.parent = s
            file.root_scope.parent = None

            # initialize the mapping of imported names to modules
            for s in file.scopes.values():
                s.imports = {}

        except parser.ParserError, detail:
            file.warning(1, self.syntaxErrors, detail)
        except IOError, detail:
            file.warning(0, self.syntaxErrors, detail)
            

@@ -67,16 +67,55 @@
 Warning%0A
+from pychecker2.Options import BoolOpt%0A
 from pyc
@@ -190,16 +190,124 @@
 parser%0A%0A
+def _parent_link(node):%0A    for c in node.getChildNodes():%0A        c.parent = node%0A        _parent_link(c)%0A%0A
 class Pa
@@ -436,103 +436,1595 @@
 s')%0A
-%0A    def check(self, file):%0A        try:%0A            file.parseTree = parseFile(file.name)%0A    
+    %0A    def get_options(self, options):%0A        desc = 'Ignore module-level code protected by __name__ == %22__main__%22'%0A        options.add(BoolOpt(self, 'main', desc, 1))%0A    %0A    def check(self, file):%0A        try:%0A            file.parseTree = parseFile(file.name)%0A            # link each node to it's parent%0A            _parent_link(file.parseTree)%0A            file.parseTree.parent = None%0A        except parser.ParserError, detail:%0A            file.warning(1, self.syntaxErrors, detail)%0A        except IOError, detail:%0A            file.warning(0, self.syntaxErrors, detail)%0A        if not file.parseTree:%0A            return%0A%0A        if not self.main:%0A            # remove __name__ == '__main__' code from module-level%0A            for n in file.parseTree.node.nodes%5B:%5D:%0A                is_main = None%0A                try:%0A                    test, code = n.tests%5B0%5D%0A                    comparison, value = test.ops%5B0%5D%0A                    try:%0A                        if test.expr.name == '__name__' and %5C%0A                           comparison == '==' and %5C%0A                           value.value == '__main__':%0A                            is_main = 1%0A                    except (AttributeError, IndexError), unused:%0A                        if test.expr.value == '__main__' and %5C%0A                           comparison == '==' and %5C%0A                           value.name == '__name__':%0A                            is_main = 1%0A                except AttributeError, IndexError:%0A                    pass%0A                if is_main:%0A                    file.parseTree.node.nodes.remove(n)%0A%0A
     
@@ -2094,36 +2094,32 @@
 .scopes%0A        
-  
-  
 file.root_scope 
@@ -2152,30 +2152,17 @@
 ee%5D%0A
-            %0A 
+%0A
         
    #
@@ -2157,19 +2157,16 @@
         
-   
 # add st
@@ -2217,20 +2217,16 @@
 have it%0A
-    
         
@@ -2270,20 +2270,16 @@
         
-    
 v.lineno
@@ -2299,20 +2299,16 @@
         
-   
- 
 # define
@@ -2365,20 +2365,16 @@
         
-    
 # the mo
@@ -2379,36 +2379,32 @@
 module)%0A        
-   
- 
 file.root_scope.
@@ -2415,20 +2415,16 @@
 no = 1%0A%0A
-    
         
@@ -2491,28 +2491,24 @@
 'em%0A        
-  
-  
 for node, sc
@@ -2547,20 +2547,16 @@
         
-    
 scope.no
@@ -2570,28 +2570,24 @@
 de%0A%0A        
-  
-  
 # create a m
@@ -2638,36 +2638,32 @@
 g scope%0A        
-    
 for s in file.sc
@@ -2685,28 +2685,24 @@
             
- 
-   
 for c in s.g
@@ -2728,28 +2728,24 @@
             
-    
 c.parent = s
@@ -2749,28 +2749,24 @@
 = s%0A        
- 
-   
 file.root_sc
@@ -2784,20 +2784,16 @@
 = None%0A%0A
-    
         
@@ -2850,28 +2850,24 @@
 les%0A        
-    
 for s in fil
@@ -2897,20 +2897,16 @@
         
-    
 s.import
@@ -2917,193 +2917,8 @@
 %7B%7D%0A%0A
-        except parser.ParserError, detail:%0A            file.warning(1, self.syntaxErrors, detail)%0A        except IOError, detail:%0A            file.warning(0, self.syntaxErrors, detail)%0A
     

add --no-main option (warning: way, way too much code for this) add parent pointer in nodes... I just couldn't take it anymore