예제 #1
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_for(value: ast3.For, context: types.Context) -> typing.Text:
    else_ = ""
    if value.orelse:
        elsebody = body.format(value.orelse, context)
        else_ = "\nelse:\n{}".format(elsebody)
    return "for {target} in {iter_}:\n{body}{else_}".format(
        body=body.format(value.body, context),
        else_=else_,
        iter_=_format_value(value.iter, context),
        target=_format_value(value.target,
                             context.override(suppress_tuple_parens=True)),
    )
예제 #2
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_try(value, context: types.Context):
    else_ = ""
    if value.orelse:
        elsebody = body.format(value.orelse, context)
        else_ = "\nelse:\n{}".format(elsebody)
    finally_ = ""
    if value.finalbody:
        finalbody = body.format(value.finalbody, context)
        finally_ = "\nfinally:\n{}".format(finalbody)
    handlers = [
        _format_except_handler(handler, context) for handler in value.handlers
    ]
    return "try:\n{body}\n{handlers}{else_}{finally_}".format(
        body=body.format(value.body, context),
        else_=else_,
        finally_=finally_,
        handlers="\n".join(handlers),
    )
예제 #3
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_except_handler(value: ast3.ExceptHandler,
                           context: types.Context) -> typing.Text:
    body_ = body.format(value.body, context)
    type_ = _format_value(value.type, context.reserve(len("except ")))
    return "except {type_}{name}:\n{body}".format(
        body=body_,
        name=(" as " + value.name) if value.name else "",
        type_=type_,
    )
예제 #4
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_class(value: ast3.ClassDef, context: types.Context) -> typing.Text:
    with context.sub() as sub:
        decorators_ = decorators.format(value.decorator_list, context)
        body_ = body.format(value.body, context)
    return "{decorators}class {name}({bases}):\n{body}".format(
        bases=", ".join([b.id for b in value.bases]),
        body=body_,
        decorators=decorators_,
        name=value.name,
    )
예제 #5
0
파일: base.py 프로젝트: EliRibble/pyfmt
def serialize(content, max_line_length=120, quote="\"", tab="\t"):
    data = ast3.parse(content)
    comments = _extract_comments(content)
    context = types.Context(comments=comments,
                            format_value=_format_value,
                            indent=0,
                            max_line_length=max_line_length,
                            quote=quote,
                            tab=tab)
    result = body.format(data.body, context, do_indent=False).strip() + "\n"
    return result
예제 #6
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_if(value: ast3.If,
               context: types.Context,
               prefix="if") -> typing.Text:
    test = _format_value(value.test, context)
    with context.sub() as sub:
        body_ = body.format(value.body, context)
    orelses = _format_orelse(value.orelse, context)
    return "{prefix} {test}:\n{body}{orelses}".format(
        body=body_,
        orelses=orelses,
        prefix=prefix,
        test=test,
    )
예제 #7
0
def _format_function_def(prefix, func, context):
    decorators_ = decorators.format(func.decorator_list, context)
    def_ = "{} {}".format(prefix, func.name)
    arguments = context.format_value(func.args, context.reserve(len(def_)))
    with context.sub() as sub:
        body_ = body.format(func.body, context=context)
    return "{decorators}{def_}({arguments}){returns}:\n{body}".format(
        arguments=arguments,
        body=body_,
        decorators=decorators_,
        def_=def_,
        returns=" -> " +
        context.format_value(func.returns, context) if func.returns else "",
    )
예제 #8
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_orelse(orelse, context: types.Context) -> typing.Text:
    if not orelse:
        return ""
    # value.orelse will either be a list of ast3.If
    # (if: ... elif: ...) or it will be a list of
    # the content of the "else" clause.
    if isinstance(orelse[0], ast3.If):
        lines = [_format_if(e, context, "elif") for e in orelse]
        results = "\n".join(lines)
        results = "\n" + results
        return results

    with context.sub() as sub:
        body_ = body.format(orelse, context)
    return "\nelse:\n{}".format(body_)
예제 #9
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_with(value, context: types.Context):
    return "with {context}:\n{body}".format(
        body=body.format(value.body, context),
        context=_format_value(value.items[0], context),
    )
예제 #10
0
파일: base.py 프로젝트: EliRibble/pyfmt
def _format_while(value, context: types.Context):
    return "while {condition}:\n{body}{orelse}".format(
        body=body.format(value.body, context),
        condition=_format_value(value.test, context),
        orelse=_format_orelse(value.orelse, context),
    )