def add_class_doc_to_ast(ast_class: ast.ClassDef) -> None:
    """<#TODO Description>

    Parameters
    ----------
    ast_class : ast
        <#TODO Description>

    Returns
    -------
    None : <#TODO return description>

    Examples
    --------
    >>> from crawto-quality import crawto_doc
    >>> add_class_doc_to_ast(ast_class=<#TODO Example Value>)
    <#TODO Method Return Value>
    """
    classdef = CrawtoClass(**ast_class.__dict__,
                           doc_string=ast.get_docstring(ast_class))
    class_docstring = classdef.docs
    new_class_doc = [ast.parse(class_docstring).body[0]]
    if ast.get_docstring(ast_class):
        ast_class.body.pop(0)
    ast_class.body = new_class_doc + ast_class.body
    for j in ast_class.body:
        if type(
                j
        ) is ast.FunctionDef and j.name in classdef.method_docs_dict.keys():
            method_docstring = classdef.method_docs_dict[j.name]
            new_method_doc = [ast.parse(method_docstring).body[0]]
            if ast.get_docstring(j):
                j.body.pop(0)
            j.body = new_method_doc + j.body
Exemplo n.º 2
0
    def visit_ClassDef(self, node: ast.ClassDef) -> Any:
        new_body = []
        for body_el in node.body:
            if _is_view(body_el):
                new_body.append(self._expand_view(body_el))
            else:
                new_body.append(body_el)

        node.body = new_body

        return node
Exemplo n.º 3
0
    def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
        def add_profile_decorator(node: ast.stmt) -> ast.stmt:
            if isinstance(node, ast.FunctionDef):
                node.decorator_list.append(ast.Name("profile", ast.Load()))
            return node

        node = self.generic_visit(node)  # type: ignore

        criteria = (lambda decorator: isinstance(decorator, ast.Name) and
                    decorator.id == "add_profile_decorator_to_class_methods")
        if any(map(criteria, node.decorator_list)):
            node.body = list(map(add_profile_decorator, node.body))

        return node
Exemplo n.º 4
0
def _visit_cls(self: 'ASTTagger', node: ast.ClassDef):
    bases = visit_suite(self.visit, node.bases)
    keywords = visit_suite(self.visit, node.keywords)
    decorator_list = visit_suite(self.visit, node.decorator_list)

    self.symtable.entered.add(node.name)

    new = self.symtable.enter_new()

    new.entered.add('__module__')
    new.entered.add('__qualname__')  # pep-3155 nested name.

    new_tagger = ASTTagger(new)
    new.cts.add(ContextType.ClassDef)
    body = visit_suite(new_tagger.visit, node.body)

    node.bases = bases
    node.keywords = keywords
    node.decorator_list = decorator_list
    node.body = body

    return Tag(node, new)
Exemplo n.º 5
0
    def visit_ClassDef(self, node: ast.ClassDef) -> Any:
        # only expand contract methods
        if node.name != "Contract":
            return node

        # if there is a `deploy` method, do nothing
        for body_node in node.body:
            if type(body_node) == ast.FunctionDef:
                if body_node.name == "deploy":
                    return node

        # Factor body ast.AnnAssign into dataclass
        storage_keys_spec = {}
        new_node_body = []
        for i, body_node in enumerate(node.body):
            if type(body_node) == ast.AnnAssign:
                storage_keys_spec[body_node.target.id] = body_node.annotation
            else:
                new_node_body.append(body_node)
        node.body = new_node_body

        self.storage_dataclass = make_dataclass('Storage', storage_keys_spec)

        # For all methods, update `self.<storage_key>` into `self.storage.<key>`
        # and add return `self.storage`
        new_body = []
        for body_node in node.body:
            new_body_node = ExpandStorageInEntrypoints().visit(body_node)
            if type(body_node) == ast.FunctionDef:
                if not body_node.returns:
                    body_node.returns = ast.Name(id='Storage', ctx=ast.Load())
                return_storage_node = ast.Return(value=ast.Attribute(
                    value=ast.Name(id='self', ctx=ast.Load()),
                    attr='storage',
                    ctx=ast.Load()))
                new_body_node.body.append(return_storage_node)
            new_body.append(new_body_node)
        node.body = new_body

        # Create deploy function
        deploy_function_node = ast.FunctionDef(
            name='deploy',
            args=ast.arguments(posonlyargs=[],
                               args=[],
                               vararg=None,
                               kwonlyargs=[],
                               kw_defaults=[],
                               kwarg=None,
                               defaults=[]),
            body=[
                ast.Return(
                    value=ast.Call(func=ast.Name(id='Storage', ctx=ast.Load()),
                                   args=[],
                                   keywords=[]))
            ],
            decorator_list=[],
            returns=None,
            type_comment=None,
        )
        node.body = [deploy_function_node] + node.body

        return node
Exemplo n.º 6
0
 def visit_ClassDef(self, node: ast.ClassDef) -> Any:
     self.defined_class_names.append(node.name)
     node.body = [self.visit(body_element) for body_element in node.body]
     return node