def _insert_tree(cursor, tree, parent_id=None, index=<NUM_LIT:0>, is_collated=False):
if isinstance(tree, dict):<EOL><INDENT>if tree['<STR_LIT:id>'] == '<STR_LIT>':<EOL><INDENT>document_id = None<EOL>title = tree['<STR_LIT:title>']<EOL><DEDENT>else:<EOL><INDENT>cursor.execute("""<STR_LIT>""", (tree['<STR_LIT:id>'],))<EOL>try:<EOL><INDENT>document_id, document_title = cursor.fetchone()<EOL><DEDENT>except TypeError:  <EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>.format(tree['<STR_LIT:id>']))<EOL><DEDENT>if tree.get('<STR_LIT:title>', None):<EOL><INDENT>title = tree['<STR_LIT:title>']<EOL><DEDENT>else:<EOL><INDENT>title = document_title<EOL><DEDENT><DEDENT>is_latest = True<EOL>cursor.execute(TREE_NODE_INSERT,<EOL>dict(document_id=document_id, parent_id=parent_id,<EOL>title=title, child_order=index,<EOL>is_latest=is_latest, is_collated=is_collated))<EOL>node_id = cursor.fetchone()[<NUM_LIT:0>]<EOL>if '<STR_LIT>' in tree:<EOL><INDENT>_insert_tree(cursor, tree['<STR_LIT>'], parent_id=node_id,<EOL>is_collated=is_collated)<EOL><DEDENT><DEDENT>elif isinstance(tree, list):<EOL><INDENT>for tree_node in tree:<EOL><INDENT>_insert_tree(cursor, tree_node, parent_id=parent_id,<EOL>index=tree.index(tree_node), is_collated=is_collated)<EOL><DEDENT><DEDENT>
Inserts a binder tree into the archive.