def test_split_at_longest_importable_path(path,
                                          expected_import_part,
                                          expected_remainder):
    actual_import_part, actual_remainder = split_at_longest_importable_path(path)

    assert actual_import_part == expected_import_part
    assert actual_remainder == expected_remainder
예제 #2
0
파일: writer.py 프로젝트: 4gn3s/populus
def serialize_deconstructable(value, level=0, *args, **kwargs):
    import_path, construct_args, construct_kwargs = value.deconstruct()

    ordered_construct_kwargs = OrderedDict(
        sorted(construct_kwargs.items(), key=lambda kv: kv[0])
    )

    import_part, constructor_part = split_at_longest_importable_path(import_path)
    # TODO: This *effectively* just re-joins the two parts but that is ok for
    # now.  Ideally later I can figure out how to do imports in the "from a.b.c
    # import d" but this will do for now.
    constructor = '.'.join((import_part, constructor_part))

    imports = {import_part}
    serialized_value = StringIO()

    serialized_value.write(constructor)
    if not construct_args and not construct_kwargs:
        serialized_value.write("()")
        return imports, serialized_value.getvalue()

    serialized_value.write("(\n")

    inner_kwargs = dict(**kwargs)
    inner_kwargs.pop('suffix', None)

    for construct_arg in construct_args:
        arg_imports, arg_serialized_value = serialize(
            construct_arg,
            level=level + 1,
            suffix=",",
            *args,
            **inner_kwargs
        )

        imports |= arg_imports
        serialized_value.write(arg_serialized_value)

    for construct_kwarg_name, construct_kwarg_value in ordered_construct_kwargs.items():
        kwarg_imports, kwarg_serialized_value = serialize_assignment(
            construct_kwarg_name,
            construct_kwarg_value,
            level=level + 1,
            suffix=",",
            assignment_operator="=",
            *args,
            **kwargs
        )

        imports |= kwarg_imports
        serialized_value.write(kwarg_serialized_value)

    serialized_value.write(indent(")", level))
    return imports, serialized_value.getvalue()
예제 #3
0
def serialize_deconstructable(value, level=0, *args, **kwargs):
    import_path, construct_args, construct_kwargs = value.deconstruct()

    ordered_construct_kwargs = OrderedDict(
        sorted(construct_kwargs.items(), key=lambda kv: kv[0]))

    import_part, constructor_part = split_at_longest_importable_path(
        import_path)
    # TODO: This *effectively* just re-joins the two parts but that is ok for
    # now.  Ideally later I can figure out how to do imports in the "from a.b.c
    # import d" but this will do for now.
    constructor = '.'.join((import_part, constructor_part))

    imports = {import_part}
    serialized_value = StringIO()

    serialized_value.write(constructor)
    if not construct_args and not construct_kwargs:
        serialized_value.write("()")
        return imports, serialized_value.getvalue()

    serialized_value.write("(\n")

    inner_kwargs = dict(**kwargs)
    inner_kwargs.pop('suffix', None)

    for construct_arg in construct_args:
        arg_imports, arg_serialized_value = serialize(construct_arg,
                                                      level=level + 1,
                                                      suffix=",",
                                                      *args,
                                                      **inner_kwargs)

        imports |= arg_imports
        serialized_value.write(arg_serialized_value)

    for construct_kwarg_name, construct_kwarg_value in ordered_construct_kwargs.items(
    ):
        kwarg_imports, kwarg_serialized_value = serialize_assignment(
            construct_kwarg_name,
            construct_kwarg_value,
            level=level + 1,
            suffix=",",
            assignment_operator="=",
            *args,
            **kwargs)

        imports |= kwarg_imports
        serialized_value.write(kwarg_serialized_value)

    serialized_value.write(indent(")", level))
    return imports, serialized_value.getvalue()