# Path: pythoscope/astvisitor.py
# def descend(tree, visitor_type):
#     """Walk over the AST using a visitor of a given type and return the visitor
#     object once done.
#     """
#     visitor = visitor_type()
#     visitor.visit(tree)
#     return visitor
# 
# class ASTVisitor(object):
#     DEFAULT_PATTERNS = [
#         ('_visit_all', "file_input< nodes=any* >"),
#         ('_visit_all', "suite< nodes=any* >"),
#         ('_visit_class', "body=classdef< 'class' name=NAME ['(' bases=any ')'] ':' any >"),
#         ('_visit_function', "body=funcdef< 'def' name=NAME parameters< '(' [args=any] ')' > ':' any >"),
#         ('_visit_import', "body=import_name< 'import' names=any > | body=import_from< 'from' import_from=any 'import' names=any >"),
#         ('_visit_lambda_assign', "expr_stmt< name=NAME '=' lambdef< 'lambda' [args=any] ':' any > >"),
#         ('_visit_main_snippet', "body=if_stmt< 'if' comparison< '__name__' '==' (\"'__main__'\" | '\"__main__\"' ) > ':' any >"),
#     ]
# 
#     def __init__(self):
#         self.patterns = []
#         for method, pattern in self.DEFAULT_PATTERNS:
#             self.register_pattern(method, pattern)
# 
#     def register_pattern(self, method, pattern):
#         """Register method to handle given pattern.
#         """
#         self.patterns.append((method, compile_pattern(pattern)))
# 
#     def visit(self, tree):
#         """Main entry point of the ASTVisitor class.
#         """
#         if isinstance(tree, Leaf):
#             self.visit_leaf(tree)
#         elif isinstance(tree, Node):
#             self.visit_node(tree)
#         elif isinstance(tree, list):
#             for subtree in tree:
#                 self.visit(subtree)
#         else:
#             raise ASTError("Unknown tree type: %r." % tree)
# 
#     def visit_leaf(self, leaf):
#         pass
# 
#     def visit_node(self, node):
#         for method, pattern in self.patterns:
#             results = {}
#             if pattern.match(node, results):
#                 getattr(self, method)(results)
#                 break
#         else:
#             # For unknown nodes simply descend to their list of children.
#             self.visit(node.children)
# 
#     def visit_class(self, name, bases, body):
#         self.visit(body.children)
# 
#     def visit_function(self, name, args, body):
#         self.visit(body.children)
# 
#     def visit_import(self, names, import_from, body):
#         pass
# 
#     def visit_lambda_assign(self, name, args):
#         pass
# 
#     def visit_main_snippet(self, body):
#         pass
# 
#     def _visit_all(self, results):
#         self.visit(results['nodes'])
# 
#     def _visit_class(self, results):
#         self.visit_class(name=results['name'].value,
#                          bases=derive_class_names(results.get('bases')),
#                          body=results['body'])
# 
#     def _visit_function(self, results):
#         self.visit_function(name=results['name'].value,
#                             args=derive_arguments(results.get('args', [])),
#                             body=results['body'])
# 
#     def _visit_import(self, results):
#         self.visit_import(names=derive_import_names(results['names']),
#                           import_from=derive_import_name(results.get('import_from')),
#                           body=results['body'])
# 
#     def _visit_lambda_assign(self, results):
#         self.visit_lambda_assign(name=results['name'].value,
#                                  args=derive_arguments(results.get('args', [])))
# 
#     def _visit_main_snippet(self, results):
#         self.visit_main_snippet(body=results['body'])
# 
# Path: pythoscope/astbuilder.py
# def clone(tree):
# def create_import(import_desc):
# def index(node):
# def insert_after(node, code):
# def insert_before(node, code):
# def parse(code):
# def parse_fragment(code):
# def regenerate(tree):


import commands
import glob
import os
import shutil
import sys
import tarfile
import tempfile
import os, sys
from pythoscope.astvisitor import descend, ASTVisitor
from pythoscope.astbuilder import parse, ParseError
# Make pythoscope importable directly.
pythoscope_path = os.path.join(os.path.dirname(__file__), os.pardir)
sys.path.insert(0, os.path.abspath(pythoscope_path))

def get_code_metrics_from_file(code):
    def not_blank_nor_comment(line):
        return not (line.lstrip() == '' or line.lstrip().startswith('#'))
    class ModuleVisitor(ASTVisitor):
        def __init__(self):
            ASTVisitor.__init__(self)
            self.functions = 0
            self.classes = 0
            self.methods = 0
        def visit_class(self, name, bases, body):
            visitor = descend(body.children, ClassVisitor)
            self.methods += visitor.methods
            self.classes += 1
        def visit_function(self, name, args, body):
            self.functions += 1
    class ClassVisitor(ASTVisitor):
        def __init__(self):
            ASTVisitor.__init__(self)
            self.methods = 0
        def visit_function(self, name, args, body):
            self.methods += 1
    try:
        tree = parse(code)
    except ParseError, e:
        notify("Caught parse error: %s." % e)
        raise GatheringError("Failed at code metrics.")

# Path: pythoscope/astvisitor.py
# def descend(tree, visitor_type):
#     """Walk over the AST using a visitor of a given type and return the visitor
#     object once done.
#     """
#     visitor = visitor_type()
#     visitor.visit(tree)
#     return visitor
# 
# class ASTVisitor(object):
#     DEFAULT_PATTERNS = [
#         ('_visit_all', "file_input< nodes=any* >"),
#         ('_visit_all', "suite< nodes=any* >"),
#         ('_visit_class', "body=classdef< 'class' name=NAME ['(' bases=any ')'] ':' any >"),
#         ('_visit_function', "body=funcdef< 'def' name=NAME parameters< '(' [args=any] ')' > ':' any >"),
#         ('_visit_import', "body=import_name< 'import' names=any > | body=import_from< 'from' import_from=any 'import' names=any >"),
#         ('_visit_lambda_assign', "expr_stmt< name=NAME '=' lambdef< 'lambda' [args=any] ':' any > >"),
#         ('_visit_main_snippet', "body=if_stmt< 'if' comparison< '__name__' '==' (\"'__main__'\" | '\"__main__\"' ) > ':' any >"),
#     ]
# 
#     def __init__(self):
#         self.patterns = []
#         for method, pattern in self.DEFAULT_PATTERNS:
#             self.register_pattern(method, pattern)
# 
#     def register_pattern(self, method, pattern):
#         """Register method to handle given pattern.
#         """
#         self.patterns.append((method, compile_pattern(pattern)))
# 
#     def visit(self, tree):
#         """Main entry point of the ASTVisitor class.
#         """
#         if isinstance(tree, Leaf):
#             self.visit_leaf(tree)
#         elif isinstance(tree, Node):
#             self.visit_node(tree)
#         elif isinstance(tree, list):
#             for subtree in tree:
#                 self.visit(subtree)
#         else:
#             raise ASTError("Unknown tree type: %r." % tree)
# 
#     def visit_leaf(self, leaf):
#         pass
# 
#     def visit_node(self, node):
#         for method, pattern in self.patterns:
#             results = {}
#             if pattern.match(node, results):
#                 getattr(self, method)(results)
#                 break
#         else:
#             # For unknown nodes simply descend to their list of children.
#             self.visit(node.children)
# 
#     def visit_class(self, name, bases, body):
#         self.visit(body.children)
# 
#     def visit_function(self, name, args, body):
#         self.visit(body.children)
# 
#     def visit_import(self, names, import_from, body):
#         pass
# 
#     def visit_lambda_assign(self, name, args):
#         pass
# 
#     def visit_main_snippet(self, body):
#         pass
# 
#     def _visit_all(self, results):
#         self.visit(results['nodes'])
# 
#     def _visit_class(self, results):
#         self.visit_class(name=results['name'].value,
#                          bases=derive_class_names(results.get('bases')),
#                          body=results['body'])
# 
#     def _visit_function(self, results):
#         self.visit_function(name=results['name'].value,
#                             args=derive_arguments(results.get('args', [])),
#                             body=results['body'])
# 
#     def _visit_import(self, results):
#         self.visit_import(names=derive_import_names(results['names']),
#                           import_from=derive_import_name(results.get('import_from')),
#                           body=results['body'])
# 
#     def _visit_lambda_assign(self, results):
#         self.visit_lambda_assign(name=results['name'].value,
#                                  args=derive_arguments(results.get('args', [])))
# 
#     def _visit_main_snippet(self, results):
#         self.visit_main_snippet(body=results['body'])
# 
# Path: pythoscope/astbuilder.py
# def clone(tree):
# def create_import(import_desc):
# def index(node):
# def insert_after(node, code):
# def insert_before(node, code):
# def parse(code):
# def parse_fragment(code):
# def regenerate(tree):


# Path: tools/gather-metrics.py
import commands
import glob
import os
import shutil
import sys
import tarfile
import tempfile
import os, sys
from pythoscope.astvisitor import descend, ASTVisitor
from pythoscope.astbuilder import parse, ParseError

# Make pythoscope importable directly.
pythoscope_path = os.path.join(os.path.dirname(__file__), os.pardir)
sys.path.insert(0, os.path.abspath(pythoscope_path))

def get_code_metrics_from_file(code):
    def not_blank_nor_comment(line):
        return not (line.lstrip() == '' or line.lstrip().startswith('#'))
    class ModuleVisitor(ASTVisitor):
        def __init__(self):
            ASTVisitor.__init__(self)
            self.functions = 0
            self.classes = 0
            self.methods = 0
        def visit_class(self, name, bases, body):
            visitor = descend(body.children, ClassVisitor)
            self.methods += visitor.methods
            self.classes += 1
        def visit_function(self, name, args, body):
            self.functions += 1
    class ClassVisitor(ASTVisitor):
        def __init__(self):
            ASTVisitor.__init__(self)
            self.methods = 0
        def visit_function(self, name, args, body):
            self.methods += 1
    try:
        tree = parse(code)
    except ParseError, e:
        notify("Caught parse error: %s." % e)
        raise GatheringError("Failed at code metrics.")

    visitor = descend(tree, ModuleVisitor)