Esempio n. 1
0
 def fix_explicit_context(self, node, results):
     pre, name, post, source = (results.get(n) for n in (u"pre", u"name", u"post", u"source"))
     pre = [n.clone() for n in pre if n.type == token.NAME]
     name.prefix = u" "
     post = [n.clone() for n in post if n.type == token.NAME]
     target = [n.clone() for n in commatize(pre + [name.clone()] + post)]
     # to make the special-case fix for "*z, = ..." correct with the least
     # amount of modification, make the left-side into a guaranteed tuple
     target.append(Comma())
     source.prefix = u""
     setup_line = Assign(Name(self.LISTNAME), Call(Name(u"list"), [source.clone()]))
     power_line = Assign(target, assignment_source(len(pre), len(post), self.LISTNAME, self.ITERNAME))
     return setup_line, power_line
Esempio n. 2
0
    def visit_NEWLINE(self, node):
        if node.prefix.lstrip().startswith(self.marker):
            # MEMO: <expr> -> _ = <expr>
            target = node
            while True:
                parent = target.parent
                if parent is None:
                    return

                if type_repr(target.parent.type) == "simple_stmt":
                    break
                target = parent

            eol = target  # target is Leaf("\n]")
            target = eol.prev_sibling

            cloned = target.clone()
            cloned.parent = None

            assigned = Assign(Name("_"), cloned)
            assigned.prefix = target.prefix
            target.replace(assigned)

            # MEMO: adding print(SEP_MARKER, _, SEP_MARKER, sep="\n")
            this_stmt = eol.parent
            print_stmt = this_stmt.clone()
            print_stmt.children = []
            print_stmt.append_child(
                Name(
                    "print({ms!r}, repr(_), {me!r}, sep='')".format(
                        ms="{}{}:".format(SEP_MARKER, node.get_lineno()), me=SEP_MARKER
                    )
                )
            )

            print_stmt.prefix = assigned.prefix
            # xxx: for first line
            if not print_stmt.prefix:
                prev_line = assigned.parent.prev_sibling
                if prev_line is not None and prev_line.type == token.INDENT:
                    print_stmt.prefix = prev_line.value

            print_stmt.append_child(Newline())

            for i, stmt in enumerate(this_stmt.parent.children):
                if stmt == this_stmt:
                    this_stmt.parent.insert_child(i + 1, print_stmt)
                    break
        self.prev_newline = node
Esempio n. 3
0
    def finish_tree(self, tree, filename):
        if not self._names:
            return

        names = [Leaf(token.LBRACE, "[", prefix=" "), Newline()]

        for name in self._names:
            names.append(String('"' + name + '"', prefix="    "))
            names.append(Comma())
            names.append(Newline())

        names.append(Leaf(token.LBRACE, "]", prefix=""))

        tree.append_child(Assign(Name("__all__"), names))
        tree.append_child(Newline())

        super(FixAllAttribute, self).finish_tree(tree, filename)
Esempio n. 4
0
    def transform(self, node, results):

        if 'set_arg' in results:  # Case of .set_std_dev()

            # set_std_dev => std_dev
            attribute = node.children[-2]  # .set_std_dev
            attribute.children[1].replace(Name('std_dev'))

            # Call "(arg)": removed
            node.children[-1].remove()

            # Replacement by an assignment:
            node.replace(Assign(node.clone(), results['set_arg'].clone()))
            
        else:
            # '.std_dev' is followed by a call with no argument: the call
            # is removed:
            node.children[-1].remove()
Esempio n. 5
0
    def transform(self, node: LN, capture: Capture) -> None:
        if node.type == syms.import_from:
            module_name = traverse_dotted_name(capture["module_name"])
            if module_name != "baseplate.integration.thrift":
                return

            to_process = capture["module_imports"]
            imports: List[Tuple[str, Optional[str]]] = []
            for n in to_process:
                if n.type in (token.COMMA, token.LPAR, token.RPAR):
                    continue
                elif n.type == token.STAR:
                    imports.append(("*", None))
                elif n.type == token.NAME:
                    if n.value == "BaseplateProcessorEventHandler":
                        imports.append(("baseplateify_processor", None))
                    else:
                        imports.append((n.value, None))
                elif n.type == syms.import_as_name:
                    import_name, import_nick = n.children[0], n.children[2]
                    assert (
                        n.children[1].type == token.NAME and n.children[1].value == "as"
                    )

                    if import_name.value == "BaseplateProcessorEventHandler":
                        self.warn(
                            n,
                            "BaseplateProcessorEventHandler aliased, make sure uses are removed.",
                        )
                        continue

                    imports.append(
                        (import_name.value, import_nick.value if import_nick else None)
                    )
                elif n.type == syms.import_as_names:
                    to_process.extend(n.children)
                else:
                    raise Exception(f"unexpected node {repr(n)}")

            if imports:
                node.replace(FromImport(module_name, imports, prefix=node.prefix))
            else:
                node.remove()
        elif node.type == syms.power:
            capture["iface_class"].replace(Name("Iface"))
        elif node.type == syms.simple_stmt:
            if "make_processor" in capture:
                capture["processor_class"].replace(Name("Processor"))
            elif "make_event_handler" in capture:
                self.arguments = [arg.clone() for arg in capture["arguments"]]
                self.remove_me = node
            elif "set_event_handler" in capture:
                processor_name = capture["processor_name"].clone()
                processor_name.prefix = ""
                arguments = [processor_name, Comma()]
                original_arguments = getattr(self, "arguments", None)
                if original_arguments:
                    original_arguments[0].prefix = " "
                    arguments.extend(original_arguments)
                else:
                    arguments.extend((
                        Name("logger", prefix=" "),
                        Comma(),
                        Name("baseplate", prefix=" "),
                    ))

                node.replace(
                    [
                        Assign(
                            capture["processor_name"].clone(),
                            Node(
                                syms.simple_stmt,
                                [
                                    Call(
                                        Name("baseplateify_processor"),
                                        arguments,
                                    )
                                ],
                            ),
                        ),
                        Newline(),
                    ]
                )

                if hasattr(self, "remove_me"):
                    self.remove_me.remove()
        else:
            raise Exception("unrecognized match")
Esempio n. 6
0
from prestring.python.parse import parse_string, dump_tree
from lib2to3.fixer_util import Name, Assign

t = parse_string("""
1 + 2 + 3 + 4  # => xxx
""".lstrip())

expr = t.children[0].children[0]
print(repr(expr))
xx = expr.clone()
xx.parent = None
expr.replace(Assign(Name("foo"), xx))
print(t)
dump_tree(t)
Esempio n. 7
0
from lib2to3.fixer_util import Name, Assign

print(repr(Name("fooo")))
print(str(Assign(Name("foo"), Name("bar"))))
Esempio n. 8
0
from abc import ABCMeta
from collections import deque
from lib2to3 import fixer_base, pytree
from lib2to3 import patcomp
from lib2to3.fixer_util import Assign, Call, Name, is_import, find_root, \
 find_binding, Newline, Comma, touch_import, String, is_tuple
from lib2to3.pgen2 import token
from lib2to3.pygram import python_symbols as syms
from lib2to3.pytree import Leaf, Node

logger = logging.getLogger(__name__)

parend_expr = patcomp.compile_pattern("""atom< '(' [atom|STRING|NAME] ')' >""")
LOGGER_NAME = u"logger"
LOGGER_GET = Assign(
    Leaf(token.NAME, LOGGER_NAME),
    Call(Name(u"logging.getLogger"), [Leaf(token.NAME, u"__name__")]))
# It defaults to being sysms.atom. I don't know why :s
LOGGER_GET.type = syms.expr_stmt


def is_import_ish_stmt(node):
    # We also pretend gevent.monkey_patch() is an import because it's found
    # amongst them, and we don't want to create a LOGGER right after this.
    return (node.type == syms.simple_stmt and node.children
            and is_import(node.children[0])) or all(
                v in set(l.value for l in node.leaves())
                for v in {u'eventlet', u'monkey_patch', u'.'})


def add_global_assignment_after_imports(_name, assignment, node):