Exemplo n.º 1
0
    def parse(self, node: ast.Num, module: Module, context: Context):
        """Process an ``ast.Num`` code and identify which type of number does it hold.
        """
        number_entity = NumberEntity(node)
        module.add_number(number_entity)

        return number_entity
Exemplo n.º 2
0
    def parse(self, node: ast.Str, module: Module, context: Context):
        """Process an ``ast.Str`` code and identify which type of number does it hold.
        """
        string_entity = StringEntity(node)
        module.add_string(string_entity)

        return string_entity
Exemplo n.º 3
0
    def parse(self, node: ast.Call, module: Module, context: Context):
        """Process a ``ast.Call`` node and extract relevant stats.

        A ``ast.Call`` node has the following fields:

            - ``func`` that represents the function, which will often be a ``ast.Name`` or ``ast.Attribute`` node
            - ``args`` that represents a list of the arguments passed by position
            - ``keywords`` that represents a list of keyword objects representing arguments passed by keyword
            - ``starargs`` holds a single node, for arguments passed as *args. This is removed in Python 3.5.
            - ``kwargs`` holds a single node, for arguments passed as **kwargs. This is removed in Python 3.5.

        :param node: The node that represents a call expression.
        :param module: The python module where this call is encountered.
        :param context: The context of the given node.
        :return: The CallEntity instance created from the given ``ast.Call`` node.
        """
        call: CallEntity = CallEntity(node, context)
        context.stack_ast_node(call)

        super().parse(node, module, context)

        context.unstack_ast_node()
        module.add_call(call)

        return call
Exemplo n.º 4
0
    def parse(self, node: ast.Starred, module: Module, context: Context):
        """Parse the value type of the ``ast.Starred`` node.

        Typically, the value is going to be a ``ast.Name`` node, but might be
        something else, although it's very improbable.

        Still, something like the following is valid Python code and will get executed ::

            def f1():
                return [1, 2, 3]

            def f2(*args):
                for arg in args:
                    print(arg)

            f2(*f1())

        The above example will yield ::

            1
            2
            3
        """
        starred_variable: StarredEntity = StarredEntity(node)
        context.stack_ast_node(starred_variable)

        parser = self.get_parser(node.value)
        entity = parser.parse(node.value, module, context)
        starred_variable.set_value_entity(entity)

        context.unstack_ast_node()
        module.add_starred_variable(starred_variable)

        return starred_variable
Exemplo n.º 5
0
    def parse(self, node: Any, module: Module, context: Dict[str, Any]):
        """
        Add an if/else conditional to the module stats with details on the complexity
        of the condition and size of the if/else block.

        There are three fields an `ast.If` node has:
            - "test" holds a single node that describes the condition
            - "body" holds a list of nodes
            - "orelse" holds a list of nodes

        :param node: The node that represents a variable name to be added to the statistics.
        :param module: The python module where this variable is encountered.
        :param context: The context of the given node in the Abstract Syntax Tree.
        :return: ???
        """
        if isinstance(node.ctx, ast.Load):
            context[LOAD_CONTEXT] = True
        elif isinstance(node.ctx, ast.Store):
            context[STORE_CONTEXT] = True
        elif isinstance(node.ctx, ast.Del):
            context[DEL_CONTEXT] = True
        else:
            raise UnknownContextException(
                f'Unknown context {node.ctx.__class__.__name__}')

        module.add_variable(node.id, node.lineno, context)
Exemplo n.º 6
0
    def parse(self, node: ast.Name, module: Module, context: Context):
        """
        Add a variable to the statistics based on the type of context it
        is encountered in the source code.

        There are three possible context values:
            - `ast.Load` that denotes loading a variable's value, like in `print(a)`
            - `ast.Store` that denotes storing a value in a variable, like in `a = 1`
            - `ast.Del` that denotes deleting a variable, like in `del a`

        :param node: The node that represents a variable name to be added to the statistics.
        :param module: The python module where this variable is encountered.
        :param context: The context of the given node in the Abstract Syntax Tree.
        :return: ???
        """
        variable = VariableEntity(node)
        module.add_variable(variable)

        return variable
Exemplo n.º 7
0
    def parse(self, node: ast.Assign, module: Module, context: Context):
        """Process an `ast.Assign` node and extract relevant stats.

        An ``ast.Assign`` node has two fields:
            - ``targets`` that represents a list of nodes
            - ``value`` that is a single node that is assigned to targets

        :param node: The node that represents an assignment operation.
        :param module: The python module where this variable is encountered.
        :param context: The context of the given node.
        :return: The AssignmentEntity instance created from the given ``ast.Assign`` node.
        """
        assignment = AssignmentEntity(node)
        context.stack_ast_node(assignment)

        super().parse(node, module, context)

        context.unstack_ast_node()
        module.add_assignment(assignment)

        return assignment
Exemplo n.º 8
0
    def parse(self, node: Any, module: Module, context: Context):
        """Process a ``ast.IfExp`` node and extract relevant stats.

        A ``ast.IfExp`` node has the following fields:

            - ``test`` that represents the condition to be evaluated
            - ``body`` that represents the value to be assigned in case the condition evaluates to true
            - ``orelse`` that represents the value to be assigned in case the condition evaluates to false

        :param node: The node that represents an if expression.
        :param module: The python module where this expression is encountered.
        :param context: The context of the given node.
        :return: The ``pycodealizer.entities.IfExpressionEntity`` instance created from the given ``ast.IfExp`` node.
        """
        if_expression: IfExpressionEntity = IfExpressionEntity(node)
        context.stack_ast_node(if_expression)

        super().parse(node, module, context)

        context.unstack_ast_node()
        module.add_if_expression(if_expression)

        return if_expression
Exemplo n.º 9
0
 def update_file_occurrence(self, path: str) -> None:
     """Set the new file occurrence path for the upcoming nodes."""
     self.parser.modules.append(Module(path))
Exemplo n.º 10
0
 def add_entity_to_module(self, entity: SetEntity, module: Module):
     module.add_set(entity)
Exemplo n.º 11
0
 def add_entity_to_module(self, entity: ListEntity, module: Module):
     module.add_list(entity)
Exemplo n.º 12
0
 def add_entity_to_module(self, entity: TupleEntity, module: Module):
     module.add_tuple(entity)
Exemplo n.º 13
0
def call_entity():
    return CallEntity(ast_call, Context(Module('test')))
Exemplo n.º 14
0
def context():
    return Context(Module('test'))
Exemplo n.º 15
0
def module_entity():
    return Module('test')