def test_package_name_repeated_in_path(self):
     package = 'xml'
     file_path = os.path.join('foo','bar','xml','blah',
                              'xml', 'binky','mod.py')
     package_path = os.path.join('foo', 'bar', 'xml', 'blah', 'xml')
     python_path = python_path_from_file_path(package, file_path, package_path=package_path)
     self.assertEqual(python_path, 'xml.binky.mod')
 def test_package_name_in_path(self):
     package = 'xml'
     file_path = os.path.join('blah', 'xml', 'binky', 'mod.py')
     package_path = os.path.join('blah', 'xml')
     python_path = python_path_from_file_path(package,
                                              file_path,
                                              package_path=package_path)
     self.assertEqual(python_path, 'xml.binky.mod')
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
Example #4
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
Example #5
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
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_python_path_from_file_path(self):
     path = imp.find_module('sample_package')[1] + "/sp_func.py"
     actual = python_path_from_file_path('sample_package', path)
     desired = 'sample_package.sp_func'
     self.assertEqual(actual, desired)
 def test_python_path_from_file_path(self):
     path = imp.find_module('sample_package')[1] + "/sp_func.py"
     actual = python_path_from_file_path('sample_package', path)
     desired = 'sample_package.sp_func'
     self.assertEqual(actual, desired)