Example #1
0
def create_inline_table(from_dict,
                        multiline_table=False,
                        multiline_strings_allowed=True):
    """
    Creates an InlineTable element from the given dict instance.
    """

    from prettytoml.elements.inlinetable import InlineTableElement

    preamble = [create_operator_element('{')]
    postable = [create_operator_element('}')]

    stuffing_elements = ((create_string_element(k, bare_allowed=True),
                          create_whitespace_element(),
                          create_operator_element('='),
                          create_whitespace_element(),
                          create_element(v, multiline_strings_allowed=False))
                         for (k, v) in from_dict.items())

    pair_separator = [
        create_operator_element(','),
        create_newline_element()
        if multiline_table else create_whitespace_element()
    ]
    spaced_elements = join_with(stuffing_elements, separator=pair_separator)

    return InlineTableElement(preamble + spaced_elements + postable)
Example #2
0
def create_element(value, multiline_strings_allowed=True):
    """
    Creates and returns the appropriate elements.Element instance from the given Python primitive, sequence-like,
    or dict-like value.
    """
    from prettytoml.elements.array import ArrayElement

    if isinstance(value, (int, float, bool, datetime.datetime, datetime.date) +
                  six.string_types) or value is None:
        primitive_token = py2toml.create_primitive_token(
            value, multiline_strings_allowed=multiline_strings_allowed)
        return AtomicElement((primitive_token, ))

    elif isinstance(value, (list, tuple)):
        preamble = [create_operator_element('[')]
        postable = [create_operator_element(']')]
        stuffing_elements = [create_element(v) for v in value]
        spaced_stuffing = join_with(stuffing_elements,
                                    separator=[
                                        create_operator_element(','),
                                        create_whitespace_element()
                                    ])

        return ArrayElement(preamble + spaced_stuffing + postable)

    elif isinstance(value, dict):
        return create_inline_table(
            value,
            multiline_table=False,
            multiline_strings_allowed=multiline_strings_allowed)

    else:
        raise RuntimeError('Value type unaccounted for: {} of type {}'.format(
            value, type(value)))
Example #3
0
def create_element(value, multiline_strings_allowed=True):
    """
    Creates and returns the appropriate elements.Element instance from the given Python primitive, sequence-like,
    or dict-like value.
    """
    from prettytoml.elements.array import ArrayElement

    if isinstance(value, (int, float, bool, datetime.datetime, datetime.date) + six.string_types) or value is None:
        primitive_token = py2toml.create_primitive_token(value, multiline_strings_allowed=multiline_strings_allowed)
        return AtomicElement((primitive_token,))

    elif isinstance(value, (list, tuple)):
        preamble = [create_operator_element('[')]
        postable = [create_operator_element(']')]
        stuffing_elements = [create_element(v) for v in value]
        spaced_stuffing = join_with(stuffing_elements,
                                    separator=[create_operator_element(','), create_whitespace_element()])

        return ArrayElement(preamble + spaced_stuffing + postable)

    elif isinstance(value, dict):
        return create_inline_table(value, multiline_table=False, multiline_strings_allowed=multiline_strings_allowed)

    else:
        raise RuntimeError('Value type unaccounted for: {} of type {}'.format(value, type(value)))
Example #4
0
def create_inline_table(from_dict, multiline_table=False, multiline_strings_allowed=True):
    """
    Creates an InlineTable element from the given dict instance.
    """

    from prettytoml.elements.inlinetable import InlineTableElement

    preamble = [create_operator_element('{')]
    postable = [create_operator_element('}')]

    stuffing_elements = (
        (
            create_string_element(k, bare_allowed=True),
            create_whitespace_element(),
            create_operator_element('='),
            create_whitespace_element(),
            create_element(v, multiline_strings_allowed=False)
        ) for (k, v) in from_dict.items())

    pair_separator = [create_operator_element(','),
                      create_newline_element() if multiline_table else create_whitespace_element()]
    spaced_elements = join_with(stuffing_elements, separator=pair_separator)

    return InlineTableElement(preamble + spaced_elements + postable)