def test_debug(mock_print): assert_parse( grammar=debug(Keyword('something')), text="something", tokens_list=[ [mock_print.return_value], ], interval_list=[(0, 9)], ) assert mock_print.call_count == 1
from undebt.pattern.common import INDENT from undebt.pattern.common import LPAREN_IND from undebt.pattern.lang.python import EXPR from undebt.pattern.util import tokens_as_dict from undebt.pyparsing import Keyword from undebt.pyparsing import Optional from undebt.pyparsing import ZeroOrMore expr_list = ( LPAREN_IND.suppress() + EXPR + ZeroOrMore(COMMA_IND.suppress() + EXPR) + Optional(COMMA_IND.suppress()) + IND_RPAREN.suppress() ) grammar = ( INDENT("leading indent") + Keyword("with") + Optional(Keyword("contextlib") + DOT) + Keyword("nested") + expr_list("nested calls") + Optional(Keyword("as").suppress() + expr_list("as assignments")) + COLON ) @tokens_as_dict(assert_keys_in=["leading indent", "nested calls", "as assignments"]) def replace(tokens): """ with contextlib.nested(a, b, c) as (x, y, z): -> with a as x, \\ b as y, \\ c as z: """ leading_indent, nested_calls = tokens["leading indent"], tokens["nested calls"]
from __future__ import print_function from undebt.pattern.common import COMMA from undebt.pattern.common import NAME from undebt.pattern.common import STRING from undebt.pattern.util import condense from undebt.pattern.util import in_string from undebt.pattern.util import tokens_as_list from undebt.pyparsing import Keyword from undebt.pyparsing import Literal from undebt.pyparsing import Optional from undebt.pyparsing import originalTextFor from undebt.pyparsing import ZeroOrMore unicode_literals = Keyword("unicode_literals") non_unicode_literals_name = ~unicode_literals + NAME import_grammar = originalTextFor( Keyword("from") + Keyword("__future__") + Keyword("import") + ZeroOrMore(non_unicode_literals_name + COMMA) + unicode_literals + ZeroOrMore(COMMA + non_unicode_literals_name) + Optional(COMMA) ) @tokens_as_list(assert_len=1) def identity_replace(tokens): return tokens[0]
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import DOT from undebt.pattern.common import LPAREN from undebt.pattern.common import PARENS from undebt.pattern.common import RPAREN from undebt.pattern.util import leading_whitespace from undebt.pattern.util import tokens_as_dict from undebt.pattern.util import trailing_whitespace from undebt.pyparsing import Keyword from undebt.pyparsing import Literal grammar = (Keyword("session") + DOT + Keyword("query") + PARENS + DOT + Literal("filter") + PARENS("filter call") + DOT + Keyword("count") + LPAREN + RPAREN) @tokens_as_dict(assert_keys=["filter call"]) def replace(tokens): """ session.query( ... ).filter( ... ).count() -> session.query( sqlalchemy.func.count()
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import COMMA from undebt.pattern.common import INDENT from undebt.pattern.lang.python import ATOM from undebt.pattern.util import tokens_as_list from undebt.pyparsing import Keyword from undebt.pyparsing import Optional grammar = (INDENT + Keyword("exec").suppress() + ATOM + Keyword("in").suppress() + ATOM + Optional(COMMA.suppress() + ATOM)) @tokens_as_list(assert_len_in=(3, 4)) def replace(tokens): """ exec str in globals(), locals() -> exec(str, globals(), locals()) """ return tokens[0] + "exec(" + ", ".join(tokens[1:]) + ")"
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import BRACKETS from undebt.pattern.common import DOT from undebt.pattern.common import NAME from undebt.pattern.common import PARENS from undebt.pattern.lang.python import ATOM_BASE from undebt.pattern.util import condense from undebt.pattern.util import tokens_as_list from undebt.pyparsing import Keyword from undebt.pyparsing import ZeroOrMore attribute = Keyword("attribute") grammar = (condense(ATOM_BASE + ZeroOrMore(DOT + ~attribute + NAME | PARENS | BRACKETS)) + (DOT + attribute).suppress()) @tokens_as_list(assert_len=1) def replace(tokens): """obj.attribute -> function(obj)""" return "function(" + tokens[0] + ")" extra = "from function_lives_here import function"
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import COLON from undebt.pattern.common import INDENT from undebt.pattern.common import LPAREN from undebt.pattern.common import NAME from undebt.pattern.common import RPAREN from undebt.pattern.util import tokens_as_list from undebt.pyparsing import Keyword from undebt.pyparsing import Optional grammar = INDENT + Keyword("class").suppress() + NAME + ( Optional(LPAREN + RPAREN) + COLON).suppress() @tokens_as_list(assert_len=2) def replace(tokens): return tokens[0] + "class " + tokens[1] + "(object):"
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import NAME from undebt.pattern.lang.python import EXPR from undebt.pattern.util import condense from undebt.pattern.util import tokens_as_dict from undebt.pyparsing import delimitedList from undebt.pyparsing import Keyword from undebt.pyparsing import Literal from undebt.pyparsing import SkipTo if_ = Keyword("if") let = Keyword("let") where = Keyword("where") eq = Literal("=") assign = condense(NAME + eq + EXPR) grammar = ( if_ + let + delimitedList(assign)("let-bindings") + where + SkipTo("{")("where-clause") ) @tokens_as_dict(assert_keys=["let-bindings", "where-clause"]) def replace(tokens): assigns = tokens["let-bindings"]
from __future__ import absolute_import from __future__ import division from __future__ import print_function from undebt.pattern.common import BRACKETS from undebt.pattern.common import DOT from undebt.pattern.common import LPAREN from undebt.pattern.common import NAME from undebt.pattern.common import PARENS from undebt.pattern.common import RPAREN from undebt.pattern.lang.python import ATOM_BASE from undebt.pattern.util import condense from undebt.pattern.util import tokens_as_list from undebt.pyparsing import Keyword from undebt.pyparsing import ZeroOrMore method = Keyword("method") grammar = (~(Keyword("self") + DOT + method) + condense(ATOM_BASE + ZeroOrMore(DOT + ~method + NAME | PARENS | BRACKETS)) + (DOT + method + LPAREN + RPAREN).suppress()) @tokens_as_list(assert_len=1) def replace(tokens): """obj.method() -> function(obj)""" return "function(" + tokens[0] + ")" extra = "from function_lives_here import function"
from undebt.pattern.util import addspace from undebt.pattern.util import condense from undebt.pyparsing import Combine from undebt.pyparsing import Keyword from undebt.pyparsing import Literal from undebt.pyparsing import OneOrMore from undebt.pyparsing import Optional from undebt.pyparsing import originalTextFor from undebt.pyparsing import pythonStyleComment from undebt.pyparsing import SkipTo from undebt.pyparsing import Word from undebt.pyparsing import ZeroOrMore ASSIGN_OP = Combine((Word("~%^&*-+|/") | ~Literal("==")) + Literal("=")) UNARY_OP = addspace(OneOrMore(Word("~-+") | Keyword("not"))) BINARY_OP = ~ASSIGN_OP + ( Word("!%^&*-+=|/<>") | Keyword("and") | Keyword("or") | addspace(OneOrMore(Keyword("is") | Keyword("not") | Keyword("in")))) OP = ASSIGN_OP | UNARY_OP | BINARY_OP TRAILER = DOT + NAME | PARENS | BRACKETS TRAILERS = condense(ZeroOrMore(TRAILER)) ATOM_BASE = NAME | NUM | PARENS | BRACKETS | BRACES | STRING