def make_replacement_nodes(parts, prefix):
    """Convert namespace tokens into parso nodes.

    Args:
        parts (list[str]):
            The namespace import to convert. e.g. ["foo", "bar", "etc"].
        prefix (str):
            A string that will get placed before the joined result of
            `parts`. Usually, this is just a single " ".

    Returns:
        list[:class:`parso.python.tree.Name` or :class:`parso.python.tree.Operator`]:
            The converted nodes of `parts`.

    """
    new = []

    for index, name in enumerate(parts):
        new.append(tree.Name(name, (0, 0)))

        if index + 1 < len(parts):
            new.append(tree.Operator(".", (0, 0)))

    new[0].prefix = prefix

    return new
Пример #2
0
    def convert_leaf(self, type, value, prefix, start_pos):
        # print('leaf', repr(value), token.tok_name[type])
        if type == NAME:
            if value in self._pgen_grammar.reserved_syntax_strings:
                return tree.Keyword(value, start_pos, prefix)
            else:
                return tree.Name(value, start_pos, prefix)

        return self._leaf_map.get(type, tree.Operator)(value, start_pos, prefix)
    def _make_import(namespace_parts, prefix="", alias=None):
        """Make a new from-import node and insert it into an existing parso graph.

        Args:
            namespace_parts (list[str]):
                The import that will be converted into a parso from-import node.
            prefix (str, optional):
                The leading whitespace (indentation) placed before the new import.

        Returns:
            :class:`parso.python.tree.ImportFrom`: A newly generated import statement.

        """
        base_names = namespace_parts[:-1]
        base_count = len(base_names)
        base_nodes = []

        if base_count > 1:
            for is_last, name in iterbot.iter_is_last(base_names):
                base_nodes.append(tree.Name(name, (0, 0)))

                if not is_last:
                    base_nodes.append(tree.Operator(".", (0, 0)))

            base_nodes[0].prefix = " "
            base = tree.PythonNode("dotted_name", base_nodes)
        else:
            base = tree.Name(base_names[0], (0, 0), prefix=" ")

        tail = tree.Name(namespace_parts[-1], (0, 0), prefix=" ")

        if alias:
            tail = tree.PythonNode(
                "import_as_name",
                [tail, tree.Keyword("as", (0, 0), prefix=" "), alias])

        return tree.ImportFrom([
            tree.Keyword("from", (0, 0), prefix=prefix),
            base,
            tree.Keyword("import", (0, 0), prefix=" "),
            tail,
        ])
Пример #4
0
 def convert_leaf(self, pgen_grammar, type, value, prefix, start_pos):
     # print('leaf', repr(value), token.tok_name[type])
     if type == NAME:
         if value in pgen_grammar.keywords:
             return tree.Keyword(value, start_pos, prefix)
         else:
             return tree.Name(value, start_pos, prefix)
     elif type == STRING:
         return tree.String(value, start_pos, prefix)
     elif type == NUMBER:
         return tree.Number(value, start_pos, prefix)
     elif type == NEWLINE:
         return tree.Newline(value, start_pos, prefix)
     elif type == ENDMARKER:
         return tree.EndMarker(value, start_pos, prefix)
     else:
         return tree.Operator(value, start_pos, prefix)