Ejemplo n.º 1
0
    def test_file_to_list__class_simple(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('class_simple.py')
        file_list = file_to_list(test_file)

        assert file_list[0] == 'class Foo(object):\n'
Ejemplo n.º 2
0
    def test_file_to_list__class_simple__length(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('class_simple.py')
        file_list = file_to_list(test_file)

        assert len(file_list) == 10
Ejemplo n.º 3
0
    def test_file_to_list__docstrings__length(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('docstrings.py')
        file_list = file_to_list(test_file)

        assert len(file_list) >= 60
Ejemplo n.º 4
0
    def test_file_to_list__docstrings(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('docstrings.py')
        file_list = file_to_list(test_file)

        assert file_list[1] == 'def first_docstring_function():\n'
Ejemplo n.º 5
0
    def test_file_to_list__simple(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('simple.py')
        file_list = file_to_list(test_file)

        assert file_list[1] == 'def first_function():\n'
Ejemplo n.º 6
0
    def test_file_to_list__simplest(self):
        from parsefilelib.lib.parsefile import file_to_list

        test_file = self.get_test_file_path('simplest.py')
        file_list = file_to_list(test_file)

        assert file_list[0] == 'def foo():\n'
Ejemplo n.º 7
0
    def __init__(self, def_name=None, ast_node=None, parent_obj=None,
                    parent_file=None, file_lines=None, line_number=0, indent=0,
                    file_path=None, line_index_end=0, get_children=True):
        """
        init method for the BaseLinesObj
        """
        if file_lines:
            self.file_lines = file_lines
            self.file_str = ''.join(self.file_lines)
        else:
            self.file_lines = file_to_list(file_path)
            self.file_str = ''.join(self.file_lines)

        if line_index_end == 0:
            self.line_index_end = len(self.file_lines)-1
        else:
            self.line_index_end = line_index_end

        # Set ast_node
        if ast_node:
            self.ast_node = ast_node
        else:
            try:
                self.ast_node, self.line_index_end = fetch_ast_node(file_lines=self.file_str,
                                                                    name=def_name,
                                                                    max_index=self.line_index_end)
            #TODO: There are errors when you try to compile non-python objects.
            #       Need to catch them and do something proper with them
            except SyntaxError as e:
                self.ast_node = None
            except TypeError as e:
                self.ast_node = None

        if not self.ast_node:
            return

        self.obj_type = node_type(self.ast_node)

        # Put this conditional stuff into the file object
        if node_type(self.ast_node) == 'file':
            #self.name = self.file_name
            self.indent = -1
            self.line_index_start = 0
            self.line_index_def_class = 0
            self.decorators = []
            self.parent_file = self
            self.parent_obj = parent_obj
        else:
            self.name = self.ast_node.name
            self.indent = self.ast_node.col_offset
            self.line_index_start = self.ast_node.lineno - 1
            self.line_index_def_class = get_line_index_def_class(self.file_lines,
                                                        self.line_index_start,
                                                        self.line_index_end)
            self.decorators = []

            self.decorators = self.fetch_child_decorators()
            if parent_obj:
                self.parent_file = parent_file
                self.parent_obj = parent_obj
            else:
                from parsefilelib.models.file_obj import FileObj
                self.parent_obj = FileObj(file_path, child=self)
                self.parent_file = self.parent_obj

        self.comments = []

        self.lines = self.file_lines[self.line_index_start:self.line_index_end+1]
        self.line_str = ''.join(self.lines)

        if get_children:
            self.classes = self.fetch_child_classes()
            self.functions = self.fetch_child_functions()

        self.docstring = ast.get_docstring(self.ast_node)
        self.imports = self.fetch_child_imports()
        self.returns = self.fetch_child_returns()
        self.variables = self.fetch_child_variables()