# Source code for tabletree.src.api.generate

    import traceback
    from collections.abc import Sequence

    from ..lib import (
        I18nResourcesWriter,
        ModelInfo,
        TableTree,
        TableTreeBuilder,
        TableTreeWriter,
    )
    from .column import Column
    from .feature import Feature
    from .table import Table
    from .view import View, ViewKeySuffix

    def generate_tabletree(
        items: Sequence[Feature | Table],
        columns: Sequence[Column],
        views: Sequence[View],
        model_info: ModelInfo,
    ) -> TableTree:
        """Generate a tabletree from top-level items.

        Args:
            items:
                The items of the root table.
            columns:
                The columns of the root table.
            views:
                The views of the root table.
            model_info:
                The model information.
        """
        tabletree_builder = TableTreeBuilder()
        tabletree_builder.set_model_info(model_info)
        root = tabletree_builder.get_root_table_builder()
        for f in items:
            root.add_child_builder(f._get_data())
        for column in columns:
            root.add_column(column._get_data())
        for i in range(len(views)):
            op = views[i]._get_data()
            root.add_operation(op)
            if i == 0:
                root.set_operation_on_select(op)
            elif i == 1:
                root.set_operation_on_double_click(op)
        for view in views:  # _write_zoom_property for root table
            for key in view._zoomed_keys:
                for child in root.children:
                    child.set_property(
                        f"{key}_{view._op_bdr.id}{ViewKeySuffix.zoomed}", True
                    )
        return tabletree_builder.build()

    def write_tabletree(
        tabletree: TableTree,
        tabletree_out_directory: str,
        blobs_out_directory: str,
    ) -> bool:
        """Write a tabletree into a file.

        Args:
            tabletree:
                The tabletree to be written.
            tabletree_out_directory:
                The output directory for tabletree file.
            blobs_out_directory:
                The output directory for resources.
        """
        try:
            TableTreeWriter(tabletree).write(
                tabletree_out_directory, blobs_out_directory
            )
            I18nResourcesWriter.get_instance().write(tabletree_out_directory)
            return True
        except Exception:
            traceback.print_exc()
            return False