def transform(self, node, results): func = results['func'] touch_import(None, u'collections', node=node) args = [func.clone(), String(u', ')] args.extend(Attr(Name(u'collections'), Name(u'Callable'))) return Call(Name(u'isinstance'), args, prefix=node.prefix)
def _Call(self, name, args=None, prefix=None): """Help the next test""" children = [] if isinstance(args, list): for arg in args: children.append(arg) children.append(Comma()) children.pop() return Call(Name(name), children, prefix)
def transform(self, node, results): # First, find a the sys import. We'll just hope it's global scope. if "sys_import" in results: if self.sys_import is None: self.sys_import = results["sys_import"] return func = results["func"].clone() func.prefix = u"" register = pytree.Node(syms.power, Attr(Name(u"atexit"), Name(u"register"))) call = Call(register, [func], node.prefix) node.replace(call) if self.sys_import is None: # That's interesting. self.warning( node, "Can't find sys import; Please add an atexit " "import at the top of your file.") return # Now add an atexit import after the sys import. names = self.sys_import.children[1] if names.type == syms.dotted_as_names: names.append_child(Comma()) names.append_child(Name(u"atexit", u" ")) else: containing_stmt = self.sys_import.parent position = containing_stmt.children.index(self.sys_import) stmt_container = containing_stmt.parent new_import = pytree.Node( syms.import_name, [Name(u"import"), Name(u"atexit", u" ")]) new = pytree.Node(syms.simple_stmt, [new_import]) containing_stmt.insert_child(position + 1, Newline()) containing_stmt.insert_child(position + 2, new)
def _handle_type2abc(self, node, results, module, abc): touch_import(None, module, node) obj = results["obj"] args = [obj.clone(), String(u", " + u".".join([module, abc]))] return Call(Name(u"isinstance"), args, prefix=node.prefix)
def _isCallable(self, node, results): obj = results["obj"] args = [obj.clone(), String(u", "), String(u"'__call__'")] return Call(Name(u"hasattr"), args, prefix=node.prefix)
def test(self): self.assertStr(Name("a"), "a") self.assertStr(Name("foo.foo().bar"), "foo.foo().bar") self.assertStr(Name("a", prefix="b"), "ba")
def test_returns(self): attr = Attr(Name("a"), Name("b")) self.assertEqual(type(attr), list)
def test(self): call = parse("foo()", strip_levels=2) self.assertStr(Attr(Name("a"), Name("b")), "a.b") self.assertStr(Attr(call, Name("b")), "foo().b")
def transform(self, node, results): name = results["name"] name.replace(Name("cheese", name.prefix))