def test_find_package_sub_modules_recursive(self):
     abs_paths = find_package_sub_modules('sample_package', recurse=True)
     actual = [os.path.split(abs_path)[1] for abs_path in abs_paths]
     desired = [
         '__init__.py', '__init__.py', 'bar_func.py', 'sp_func.py',
         'error_package.py'
     ]
     self.assertEqual(sorted(actual), sorted(desired))
示例#2
0
def find_classes_import(package):
    """ Find classes using an import statement. Sloppier and consumes more
        memory than find_classes_ast, but also get submodules of the modules,
        which the ast method cannot do. For example, giving it 'os', it would
        also find functions from os.path. It also follows import statements in
        the files, which may or may not be desirable.
    """
    classes = []
    if is_package(package):
        package_path = get_module_path(package)
        file_paths = find_package_sub_modules(package)

        for file_path in file_paths:
            python_path = python_path_from_file_path(package, file_path, package_path=package_path)
            results = find_classes_import_recurse(python_path)
            classes.extend(results)
    else:
        classes = find_classes_import_recurse(package)

    return classes
示例#3
0
def find_classes_ast(package):
    """ Find classes by traversing an abstract syntax tree generated by the
        compiler module.
        fixme: expand docstring, possibly provide response about non-existant
        modules/packages
    """
    classes = []
    # It is a package (ie a directory)
    if is_package(package):
        package_path = get_module_path(package)
        file_paths = find_package_sub_modules(package)
        for file_path in file_paths:
            try:
                file = open(file_path)
                # Adding a new line to the source, so that compile wouldn't
                # throw a SyntaxError on EOF comment
                source = file.read().replace('\r\n', '\n') + '\n'
                ast = compile(source, file_path, 'exec', _ast.PyCF_ONLY_AST)
                file.close()
                python_path = python_path_from_file_path(
                    package, file_path, package_path=package_path)
                classes.extend(visit_ast_node(ast, file_path, python_path))
            except SyntaxError:
                msg = 'SyntaxError in parsing file %s' % file_path
                logger.error(msg)

    # It is a module (ie a .py file)
    elif is_module(package):
        file_path = get_module_path(package)
        file = open(file_path)

        # Adding a new line to the source, so that compile wouldn't
        # throw a SyntaxError on EOF comment
        source = file.read().replace('\r\n', '\n') + '\n'

        ast = compile(source, file_path, 'exec', _ast.PyCF_ONLY_AST)
        file.close()
        classes.extend(visit_ast_node(ast, file_path, package))

    return classes
示例#4
0
def find_classes_import(package):
    """ Find classes using an import statement. Sloppier and consumes more
        memory than find_classes_ast, but also get submodules of the modules,
        which the ast method cannot do. For example, giving it 'os', it would
        also find functions from os.path. It also follows import statements in
        the files, which may or may not be desirable.
    """
    classes = []
    if is_package(package):
        package_path = get_module_path(package)
        file_paths = find_package_sub_modules(package)

        for file_path in file_paths:
            python_path = python_path_from_file_path(package,
                                                     file_path,
                                                     package_path=package_path)
            results = find_classes_import_recurse(python_path)
            classes.extend(results)
    else:
        classes = find_classes_import_recurse(package)

    return classes
示例#5
0
def find_classes_ast(package):
    """ Find classes by traversing an abstract syntax tree generated by the
        compiler module.
        fixme: expand docstring, possibly provide response about non-existant
        modules/packages
    """
    classes = []
    # It is a package (ie a directory)
    if is_package(package):
        package_path = get_module_path(package)
        file_paths = find_package_sub_modules(package)
        for file_path in file_paths:
            try:
                file = open(file_path)
                # Adding a new line to the source, so that compile wouldn't
                # throw a SyntaxError on EOF comment
                source = file.read().replace('\r\n','\n')+'\n'
                ast = compile(source, file_path,'exec', _ast.PyCF_ONLY_AST)
                file.close()
                python_path = python_path_from_file_path(package, file_path, package_path=package_path)
                classes.extend(visit_ast_node(ast, file_path, python_path))
            except SyntaxError:
                msg = 'SyntaxError in parsing file %s'% file_path
                logger.error(msg)

    # It is a module (ie a .py file)
    elif is_module(package):
        file_path = get_module_path(package)
        file = open(file_path)

        # Adding a new line to the source, so that compile wouldn't
        # throw a SyntaxError on EOF comment
        source = file.read().replace('\r\n','\n')+'\n'

        ast = compile(source, file_path, 'exec', _ast.PyCF_ONLY_AST)
        file.close()
        classes.extend(visit_ast_node(ast, file_path, package))

    return classes
 def test_find_package_sub_modules_recursive(self):
     abs_paths = find_package_sub_modules('sample_package', recurse=True)
     actual = [os.path.split(abs_path)[1] for abs_path in abs_paths]
     desired = ['__init__.py', '__init__.py', 'bar_func.py', 'sp_func.py',
                'error_package.py']
     self.assertEqual(sorted(actual), sorted(desired))