Ejemplo n.º 1
0
def dummy_file_elements():
        tokens_ = tuple(lexer.tokenize("""
name = fawzy
another_name=another_fawzy

[details]
id= 42
section =fourth

[[person]]
personname= lefawzy
dest=north

[[person]]
dest=south
personname=lafawzy

[details.extended]
number = 313
type =complex"""))

        elements = \
            [TableElement(primitive_tokens_to_primitive_elements(tokens_[:12]))] + \
            [TableHeaderElement(tokens_[12:16])] + \
            [TableElement(primitive_tokens_to_primitive_elements(tokens_[16:25]))] + \
            [TableHeaderElement(tokens_[25:31])] + \
            [TableElement(primitive_tokens_to_primitive_elements(tokens_[31:39]))] + \
            [TableHeaderElement(tokens_[39:45])] + \
            [TableElement(primitive_tokens_to_primitive_elements(tokens_[45:53]))] + \
            [TableHeaderElement(tokens_[53:60])] + \
            [TableElement(primitive_tokens_to_primitive_elements(tokens_[60:]))]

        return elements
Ejemplo n.º 2
0
    def __init__(self, parent, name, is_array=False):
        TableElement.__init__(self, sub_elements=[])

        self._parent = parent
        self._name = name
        self._is_array = is_array

        # As long as this flag is false, setitem() operations will append the table header and this table
        # to the toml_file's elements
        self.__appended = False
Ejemplo n.º 3
0
def test_table():

    initial_toml = """name = "first"
id=42 # My id


"""

    tokens = tuple(lexer.tokenize(initial_toml))

    elements = (
        AtomicElement(tokens[:1]),
        WhitespaceElement(tokens[1:2]),
        PunctuationElement(tokens[2:3]),
        WhitespaceElement(tokens[3:4]),
        AtomicElement(tokens[4:5]),
        NewlineElement(tokens[5:6]),

        AtomicElement(tokens[6:7]),
        PunctuationElement(tokens[7:8]),
        AtomicElement(tokens[8:9]),
        WhitespaceElement(tokens[9:10]),
        CommentElement(tokens[10:12]),

        NewlineElement(tokens[12:13]),
        NewlineElement(tokens[13:14]),
    )

    table = TableElement(elements)

    assert set(table.items()) == {('name', 'first'), ('id', 42)}

    assert table['name'] == 'first'
    assert table['id'] == 42

    table['relation'] = 'another'

    assert set(table.items()) == {('name', 'first'), ('id', 42), ('relation', 'another')}

    table['name'] = 'fawzy'

    assert set(table.items()) == {('name', 'fawzy'), ('id', 42), ('relation', 'another')}

    expected_toml = """name = "fawzy"
id=42 # My id
relation = "another"


"""

    assert table.serialized() == expected_toml
Ejemplo n.º 4
0
def test_table():
    initial_toml = """id=42 # My id\nage=14"""
    tokens = tuple(lexer.tokenize(initial_toml))
    table = TableElement([
        AtomicElement(tokens[0:1]),
        PunctuationElement(tokens[1:2]),
        AtomicElement(tokens[2:3]),
        WhitespaceElement(tokens[3:4]),
        CommentElement(tokens[4:6]),
        AtomicElement(tokens[6:7]),
        PunctuationElement(tokens[7:8]),
        AtomicElement(tokens[8:9]),
    ])
    assert set(table.items()) == {('id', 42), ('age', 14)}
    del table['id']
    assert set(table.items()) == {('age', 14)}
Ejemplo n.º 5
0
def sanitize(_elements):
    """
    Finds TableHeader elements that are not followed by TableBody elements and inserts empty TableElement
    right after those.
    """

    output = list(_elements)

    def find_next_table_header(after=-1):
        return next((i for (i, element) in enumerate(output)
                     if i > after and isinstance(element, TableHeaderElement)), float('-inf'))

    def find_next_table_body(after=-1):
        return next((i for (i, element) in enumerate(output)
                     if i > after and isinstance(element, TableElement)), float('-inf'))

    next_table_header_i = find_next_table_header()
    while next_table_header_i >= 0:

        following_table_header_i = find_next_table_header(next_table_header_i)
        following_table_body_i = find_next_table_body(next_table_header_i)

        if (following_table_body_i < 0) or \
                (following_table_header_i >= 0 and (following_table_header_i < following_table_body_i)):
            output.insert(next_table_header_i+1, TableElement(tuple()))

        next_table_header_i = find_next_table_header(next_table_header_i)

    return output
Ejemplo n.º 6
0
def _fixed_table(table_element):
    """
    Returns a new TableElement.
    """
    assert isinstance(table_element, TableElement)
    lines = tuple(common.lines(table_element.sub_elements))
    fixed_lines = tuple(
        _fixed_line(l) if _line_length(l) > MAXIMUM_LINE_LENGTH else l
        for l in lines)
    return TableElement(
        sub_elements=tuple(reduce(operator.concat, fixed_lines)))
Ejemplo n.º 7
0
def _sorted_table(table):
    """
    Returns another TableElement where the table entries are sorted lexicographically by key.
    """
    assert isinstance(table, TableElement)

    # Discarding TokenElements with no tokens in them
    table_elements = common.non_empty_elements(table.sub_elements)
    lines = tuple(common.lines(table_elements))
    sorted_lines = sorted(lines, key=_line_key)
    sorted_elements = reduce(operator.concat, sorted_lines)

    return TableElement(sorted_elements)
Ejemplo n.º 8
0
def create_table(dict_value):
    """
    Creates a TableElement out of a dict instance.
    """
    from prettytoml.elements.table import TableElement

    if not isinstance(dict_value, dict):
        raise ValueError('input must be a dict instance.')

    table_element = TableElement([create_newline_element()])
    for k, v in dict_value.items():
        table_element[k] = create_element(v)

    return table_element
Ejemplo n.º 9
0
def test_table():

    initial_toml = """name = "first"
id=42 # My id


"""

    tokens = tuple(lexer.tokenize(initial_toml))

    elements = (
        AtomicElement(tokens[:1]),
        WhitespaceElement(tokens[1:2]),
        PunctuationElement(tokens[2:3]),
        WhitespaceElement(tokens[3:4]),
        AtomicElement(tokens[4:5]),
        NewlineElement(tokens[5:6]),
        AtomicElement(tokens[6:7]),
        PunctuationElement(tokens[7:8]),
        AtomicElement(tokens[8:9]),
        WhitespaceElement(tokens[9:10]),
        CommentElement(tokens[10:12]),
        NewlineElement(tokens[12:13]),
        NewlineElement(tokens[13:14]),
    )

    table = TableElement(elements)

    assert set(table.items()) == {('name', 'first'), ('id', 42)}

    assert table['name'] == 'first'
    assert table['id'] == 42

    table['relation'] = 'another'

    assert set(table.items()) == {('name', 'first'), ('id', 42),
                                  ('relation', 'another')}

    table['name'] = 'fawzy'

    assert set(table.items()) == {('name', 'fawzy'), ('id', 42),
                                  ('relation', 'another')}

    expected_toml = """name = "fawzy"
id=42 # My id
relation = "another"


"""

    assert table.serialized() == expected_toml
def _unindent_table(table_element):
    table_lines = tuple(common.lines(table_element.sub_elements))
    unindented_lines = tuple(
        tuple(dropwhile(lambda e: isinstance(e, WhitespaceElement), line))
        for line in table_lines)
    return TableElement(reduce(operator.concat, unindented_lines))
Ejemplo n.º 11
0
def table_body_element(token_stream):
    captured = capture_from(token_stream).find(table_body_elements)
    return TableElement(captured.value()), captured.pending_tokens
Ejemplo n.º 12
0
 def __setitem__(self, key, value):
     TableElement.__setitem__(self, key, value)
     self._append_to_parent()