def test_two_nested_tables(self):
        text = """
[table.nested]
key = "value"

[table]
key2 = 11

[table.nested.twice]
pair = "shoes"
        """
        self.assertParsedCorrectly(
            text,
            Root(
                nodes={
                    'table':
                    Table(
                        nodes={
                            'nested':
                            Table(
                                nodes={
                                    'key': 'value',
                                    'twice': Table(nodes={'pair': 'shoes'})
                                }),
                            'key2':
                            11
                        })
                }))
Esempio n. 2
0
def parse_toml(text: Union[str, bytes]) -> Root:
    if isinstance(text, bytes):
        text = text.decode()

    text = text.replace('\r\n', '\n')
    statements = parse_statements(text)

    tables = [UnmergedTable((), Root())]  # type: List[UnmergedTable]
    current_table = tables[0].content

    # statement_nodes = list()  # type: List[Tuple[str, ...]]
    statement_nodes = TomlStatementNodes()
    current_table_name = ()  # type: Tuple[str, ...]
    comment_counter = 1  # type: int
    newline_counter = 1  # type: int

    for statement in statements:
        if isinstance(statement, KVEntry):
            absolute_key_name = current_table_name + (statement.key, )
            if absolute_key_name in statement_nodes:
                raise NotSupported('Key overwrite is not permitted.')
            statement_nodes[absolute_key_name] = statement.val

            current_table.nodes[statement.key] = statement.val

        if isinstance(statement, Comment):
            comment_name = '#' + str(comment_counter)
            comment_counter += 1
            absolute_comment_name = current_table_name + (comment_name, )
            statement_nodes[absolute_comment_name] = statement.text

            current_table.nodes[comment_name] = statement.text
        #
        # if isinstance(statement, NewLine):
        #     newline_name = '%' + str(newline_counter)
        #     newline_counter += 1
        #     absolute_newline_name = current_table_name + (newline_name,)
        #     statement_nodes[absolute_newline_name] = NewLine()
        #
        #     current_table.nodes[newline_name] = NewLine()

        if isinstance(statement, ParsedTable):
            if statement.name in statement_nodes:
                raise NotSupported('Table overwrite is not permitted.')
            statement_nodes[statement.name] = Table()

            new_table = Table()
            tables.append(UnmergedTable(statement.name, new_table))
            current_table = new_table

            current_table_name = statement.name

    root = merge_tables(tables)
    root.set_statement_nodes(statement_nodes)
    return root
Esempio n. 3
0
    def __init__(self, root=None):
        self._root = root or Root()

        if 'deps' not in self._root:
            self._root['deps'] = Table()
        self._deps_table = self._root['deps']
        self.deps = []  # type: List[Dependency]
        for dep_name, dep_metadata in self._root['deps'].items():
            print(dep_name, dep_metadata)

        self.dev_deps = []  # type: List[Dependency]
        if 'dev_deps' not in self._root:
            self._root['dev_deps'] = Table()
        self._dev_deps_table = self._root['dev_deps']
    def test_parse_table(self):
        text = """
[test]
three = 3
        """
        self.assertParsedCorrectly(
            text, Root(nodes={'test': Table(nodes={'three': 3})}))
Esempio n. 5
0
    def test_insert_before(self):
        od = OrderedDict()
        od[('deps', )] = Table()
        od[('django', )] = '1.10.2'

        toml_nodes = TomlStatementNodes(od)
        toml_nodes.insert_before(('django', ), ('flask', ), '1.3')
        self.assertEqual(toml_nodes.keys(), [('deps', ), ('flask', ),
                                             ('django', )])
    def test_commented_supported(self):
        text = """
#hello
[table]
key2 = 11
        """
        self.assertParsedCorrectly(
            text,
            Root(nodes={
                '#1': 'hello',
                'table': Table(nodes={'key2': 11})
            }))
    def test_parse_various_values(self):
        text = """
[table]
one = 1
two = 2.2
three = [1, 2]
four = {one = 1, 1 = 3}
        """
        self.assertParsedCorrectly(
            text,
            Root(
                nodes={
                    'table':
                    Table(
                        nodes={
                            'one': 1,
                            'two': 2.2,
                            'three': [1, 2],
                            'four': InlineTable([('one', 1), ('1', 3)])
                        })
                }))
Esempio n. 8
0
def merge_tables(tables: List[UnmergedTable]) -> Root:
    sorted_tables = sorted(tables, key=lambda table: len(table.name))

    root = Root()
    for name, content in sorted_tables:
        if name == ():
            root = content
            continue

        internal_table = root
        for part in name[:-1]:
            if part not in internal_table:
                internal_table[part] = Table(parent=internal_table,
                                             parent_table_ref_key=part)

            internal_table = internal_table[part]

        content.parent = internal_table
        content.parent_table_ref_key = name[-1]
        internal_table.nodes[name[-1]] = content

    return root