Exemplo n.º 1
0
def get_trailing_text_node(node: Node) -> Leaf:
    """
    Find the dedent subnode containing any trailing text.

    If there are none, return the first.
    """
    # trails = []
    first = None
    for tail in reversed(list(node.leaves())):
        if not tail.type == token.DEDENT:
            break
        if first is None:
            first = tail
        if tail.prefix:
            return tail
    return first
Exemplo n.º 2
0
def convert_regex_to_path_modifier(node: Node, capture: Capture,
                                   filename: Filename) -> None:
    # Replace the import
    if is_import(node):
        name_leafs = [
            Name("path", prefix=" "),
            Comma(),
            Name("re_path", prefix=" "),
        ]
        node.replace([FromImport("django.url", name_leafs=name_leafs)])
    # And function calls from url to path, re_path
    if capture and "function_arguments" in capture:
        function_node: Node = next(node.leaves())
        args = capture.get("function_arguments")
        regex_leaf: Leaf = next(args[0].leaves())
        converted = update_regex_to_path(regex_leaf.value)

        if converted == regex_leaf.value:
            function_node.replace(Name("re_path", prefix=function_node.prefix))
        else:
            function_node.replace(Name("path", prefix=function_node.prefix))
            regex_leaf.value = update_regex_to_path(regex_leaf.value)