Пример #1
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)))
Пример #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)))
Пример #3
0
def test_long_string():
    text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse faucibus nibh id urna euismod, " \
           "vitae blandit nisi blandit. Nam eu odio ex. Praesent iaculis sapien justo. Proin vehicula orci rhoncus " \
           "risus mattis cursus. Sed quis commodo diam. Morbi dictum fermentum ex. Ut augue lorem, facilisis eu " \
           "posuere ut, ullamcorper et quam. Donec porta neque eget erat lacinia, in convallis elit scelerisque. " \
           "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent " \
           "felis metus, venenatis eu aliquam vel, fringilla in turpis. Praesent interdum pulvinar enim, et mattis " \
           "urna dapibus et. Sed ut egestas mauris. Etiam eleifend dui."

    primitive_token = py2toml.create_primitive_token(text)

    assert primitive_token.source_substring[3:-3] == r"""
Пример #4
0
def test_token_date(dt, content):
    token = create_primitive_token(dt)
    assert token == tokens.Token(tokens.TYPE_DATE, content)
Пример #5
0
def test_string():
    assert py2toml.create_string_token('fawzy', bare_string_allowed=True) == tokens.Token(tokens.TYPE_BARE_STRING, 'fawzy')
    assert py2toml.create_primitive_token('I am a "cr\'azy" sentence.') == \
           tokens.Token(tokens.TYPE_STRING, '"I am a \\"cr\'azy\\" sentence."')
Пример #6
0
def test_none():
    t = py2toml.create_primitive_token(None)
    assert t.type == tokens.TYPE_STRING and t.source_substring == '""'
Пример #7
0
def test_date():
    ts = strict_rfc3339.rfc3339_to_timestamp('1979-05-27T00:32:00-07:00')
    dt = datetime.datetime.fromtimestamp(ts)
    assert py2toml.create_primitive_token(dt) == tokens.Token(tokens.TYPE_DATE, '1979-05-27T07:32:00Z')
Пример #8
0
def test_bool():
    assert py2toml.create_primitive_token(False) == tokens.Token(tokens.TYPE_BOOLEAN, 'false')
    assert py2toml.create_primitive_token(True) == tokens.Token(tokens.TYPE_BOOLEAN, 'true')
Пример #9
0
def test_float():
    assert py2toml.create_primitive_token(4.2) == tokens.Token(tokens.TYPE_FLOAT, '4.2')
Пример #10
0
def test_int():
    assert py2toml.create_primitive_token(42) == tokens.Token(tokens.TYPE_INTEGER, '42')
Пример #11
0
def test_multiline_string():
    text = 'The\nSuper\nT"""OML"""\n\nIs coming'

    primitive_token = py2toml.create_primitive_token(text)

    assert primitive_token.source_substring == '"""The\nSuper\nT\"\"\"OML\"\"\"\n\nIs coming"""'