Beispiel #1
0
from __future__ import division
from __future__ import print_function

from pyparsing import Keyword
from pyparsing import ZeroOrMore

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

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"
Beispiel #2
0
LPAREN = Literal("(")
RPAREN = Literal(")")
COMMA = Literal(",")
COLON = Literal(":")

LINE_START = NL | fixto(START_OF_FILE, "")
NO_BS_NL = Regex(r"(?<!\\)").suppress() + NL

SKIP_TO_TEXT = SkipTo(CharsNotIn(WHITESPACE_OR_NL_CHARS))
SKIP_TO_TEXT_OR_NL = SkipTo(CharsNotIn(WHITESPACE_CHARS))
INDENT = originalTextFor(LINE_START + SKIP_TO_TEXT_OR_NL)

WHITE = ~START_OF_FILE + Word(WHITESPACE_CHARS).setWhitespaceChars("")
NL_WHITE = ~START_OF_FILE + Word(WHITESPACE_OR_NL_CHARS).setWhitespaceChars("")

COMMA_IND = condense(COMMA + Optional(INDENT))
LPAREN_IND = condense(LPAREN + Optional(INDENT))
IND_RPAREN = condense(Optional(INDENT) + RPAREN)


def _untouched_string(quote):
    """Matches a string from quote to quote. Does not handle escapes."""
    return originalTextFor(Literal(quote) + SkipTo(quote) + Literal(quote))


TRIPLE_DBL_QUOTE_STRING = _untouched_string('"""')
TRIPLE_SGL_QUOTE_STRING = _untouched_string("'''")
TRIPLE_QUOTE_STRING = TRIPLE_DBL_QUOTE_STRING | TRIPLE_SGL_QUOTE_STRING
STRING = TRIPLE_QUOTE_STRING | quotedString

Beispiel #3
0
from pyparsing import delimitedList
from pyparsing import Keyword
from pyparsing import Literal
from pyparsing import SkipTo

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

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"]
    cond = tokens['where-clause']

    def pretty_assign(assign):
        op = assign.index("=")
        l, r = assign[:op], assign[op + 1:]
        return "{0} = {1}".format(l, r)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from pyparsing import Keyword
from pyparsing import ZeroOrMore

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.python import ATOM_BASE
from undebt.pattern.util import condense
from undebt.pattern.util import tokens_as_list


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"
Beispiel #5
0
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
ATOM = condense(ATOM_BASE + TRAILERS)
UNARY_OP_ATOM = addspace(Optional(UNARY_OP) + ATOM)

EXPR = addspace(UNARY_OP_ATOM + ZeroOrMore(BINARY_OP + UNARY_OP_ATOM))

HEADER = originalTextFor(
    START_OF_FILE + ZeroOrMore(SKIP_TO_TEXT +
                               (STRING
                                | pythonStyleComment
                                | Optional(Keyword("from") + DOTTED_NAME) +
                                Keyword("import") + SkipTo(NO_BS_NL)) + NL))
Beispiel #6
0
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
ATOM = condense(ATOM_BASE + TRAILERS)
UNARY_OP_ATOM = addspace(Optional(UNARY_OP) + ATOM)

EXPR = addspace(UNARY_OP_ATOM + ZeroOrMore(BINARY_OP + UNARY_OP_ATOM))
EXPR_LIST = condense(EXPR + ZeroOrMore(addspace(COMMA + EXPR)) +
                     Optional(COMMA))
EXPR_IND_LIST = originalTextFor(EXPR + ZeroOrMore(COMMA_IND + EXPR) +
                                Optional(COMMA_IND))

HEADER = originalTextFor(
    START_OF_FILE + ZeroOrMore(SKIP_TO_TEXT +
                               (STRING
                                | pythonStyleComment
from undebt.pattern.util import tokens_as_list

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]


u_string_grammar = Literal("u").suppress() + condense(
    Optional(Literal("r")) + STRING)


@tokens_as_list(assert_len=1)
def u_string_replace(original, location, tokens):
    if in_string(location, original):
        return None
    else:
        return tokens[0]


patterns = [
    # first, check to see if from __future__ import unicode_literals exists
    (import_grammar, identity_replace),
    # if it does, then find u strings and return them with the u suppressed
    (u_string_grammar, u_string_replace),
Beispiel #8
0
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
ATOM = condense(ATOM_BASE + TRAILERS)
UNARY_OP_ATOM = addspace(Optional(UNARY_OP) + ATOM)

EXPR = addspace(UNARY_OP_ATOM + ZeroOrMore(BINARY_OP + UNARY_OP_ATOM))
EXPR_LIST = condense(EXPR + ZeroOrMore(addspace(COMMA + EXPR)) +
                     Optional(COMMA))
EXPR_IND_LIST = originalTextFor(EXPR + ZeroOrMore(COMMA_IND + EXPR) +
                                Optional(COMMA_IND))

PARAM = condense(Optional(Word("*") | NAME + Literal("=")) + EXPR)
PARAMS = originalTextFor(PARAM + ZeroOrMore(COMMA_IND + PARAM) +
                         Optional(COMMA_IND))