示例#1
0
def convert_except_clause(config: ParserConfig, children: Sequence[Any]) -> Any:
    if len(children) == 1:
        (except_token,) = children
        whitespace_after_except = SimpleWhitespace("")
        test = None
        name = None
    elif len(children) == 2:
        (except_token, test_node) = children
        whitespace_after_except = parse_simple_whitespace(
            config, except_token.whitespace_after
        )
        test = test_node.value
        name = None
    else:
        (except_token, test_node, as_token, name_token) = children
        whitespace_after_except = parse_simple_whitespace(
            config, except_token.whitespace_after
        )
        test = test_node.value
        name = AsName(
            whitespace_before_as=parse_simple_whitespace(
                config, as_token.whitespace_before
            ),
            whitespace_after_as=parse_simple_whitespace(
                config, as_token.whitespace_after
            ),
            name=Name(name_token.string),
        )

    return ExceptClausePartial(
        leading_lines=parse_empty_lines(config, except_token.whitespace_before),
        whitespace_after_except=whitespace_after_except,
        type=test,
        name=name,
    )
示例#2
0
def convert_with_item(config: ParserConfig, children: Sequence[Any]) -> Any:
    if len(children) == 3:
        (test, as_token, expr_node) = children
        test_node = test.value
        asname = AsName(
            whitespace_before_as=parse_simple_whitespace(
                config, as_token.whitespace_before),
            whitespace_after_as=parse_simple_whitespace(
                config, as_token.whitespace_after),
            name=expr_node.value,
        )
    else:
        (test, ) = children
        test_node = test.value
        asname = None

    return WithItem(item=test_node, asname=asname)
示例#3
0
def convert_dotted_as_name(config: ParserConfig,
                           children: Sequence[Any]) -> Any:
    if len(children) == 1:
        (dotted_name, ) = children
        return ImportAlias(name=dotted_name, asname=None)
    else:
        dotted_name, astoken, name = children
        return ImportAlias(
            name=dotted_name,
            asname=AsName(
                whitespace_before_as=parse_parenthesizable_whitespace(
                    config, astoken.whitespace_before),
                whitespace_after_as=parse_parenthesizable_whitespace(
                    config, astoken.whitespace_after),
                name=Name(name.string),
            ),
        )