def grouped_insert(t, value):
    """Insert value into the target tree 't' with correct grouping."""
    collator = Collator.createInstance(Locale(t.lang) if t.lang else Locale())
    if value.tail is not None:
        val_prev = value.getprevious()
        if val_prev is not None:
            val_prev.tail = (val_prev.tail or '') + value.tail
        else:
            val_parent = value.getparent()
            if val_parent is not None:
                val_parent.text = (val_parent.text or '') + value.tail
        value.tail = None
    if t.isgroup and t.sort(value) is not None:
        if t.groupby:
            for child in t.tree:
                if child.get('class') == 'group-by':
                    # child[0] is the label span
                    order = collator.compare(
                        t.groupby(child[1]) or '', t.groupby(value) or '')
                    if order == 0:
                        c_target = Target(child, sort=t.sort, lang=t.lang)
                        insert_group(value, c_target)
                        break
                    elif order > 0:
                        group = create_group(t.groupby(value))
                        group.append(value)
                        child.addprevious(group)
                        break
            else:
                group = create_group(t.groupby(value))
                group.append(value)
                t.tree.append(group)
        else:
            insert_group(value, t)

    elif t.sort and t.sort(value) is not None:
        insert_sort(value, t)

    elif t.location == 'inside':
        for child in t.tree:
            value.append(child)
        value.text = t.tree.text
        t.tree.text = None
        t.tree.append(value)

    elif t.location == 'outside':
        value.tail = t.tree.tail
        t.tree.tail = None
        target_parent_descendants = (
            [n.getparent() for n in t.parent.iterdescendants() if n == t.tree])
        try:
            parent = target_parent_descendants[0]
            parent.insert(parent.index(t.tree), value)
            value.append(t.tree)
        except IndexError as e:
            logger.error('Target of outside has been moved or deleted')
            raise e

    elif t.location == 'before':
        value.tail = t.tree.text
        t.tree.text = None
        t.tree.insert(0, value)
    else:
        t.tree.append(value)
def grouped_insert ( t , value ) : collator = Collator . createInstance ( Locale ( t . lang ) if t . lang else Locale ( ) ) if value . tail is not None : val_prev = value . getprevious ( ) if val_prev is not None : val_prev . tail = ( val_prev . tail or '' ) + value . tail else : val_parent = value . getparent ( ) if val_parent is not None : val_parent . text = ( val_parent . text or '' ) + value . tail value . tail = None if t . isgroup and t . sort ( value ) is not None : if t . groupby : for child in t . tree : if child . get ( 'class' ) == 'group-by' : order = collator . compare ( t . groupby ( child [ 1 ] ) or '' , t . groupby ( value ) or '' ) if order == 0 : c_target = Target ( child , sort = t . sort , lang = t . lang ) insert_group ( value , c_target ) break elif order > 0 : group = create_group ( t . groupby ( value ) ) group . append ( value ) child . addprevious ( group ) break else : group = create_group ( t . groupby ( value ) ) group . append ( value ) t . tree . append ( group ) else : insert_group ( value , t ) elif t . sort and t . sort ( value ) is not None : insert_sort ( value , t ) elif t . location == 'inside' : for child in t . tree : value . append ( child ) value . text = t . tree . text t . tree . text = None t . tree . append ( value ) elif t . location == 'outside' : value . tail = t . tree . tail t . tree . tail = None target_parent_descendants = ( [ n . getparent ( ) for n in t . parent . iterdescendants ( ) if n == t . tree ] ) try : parent = target_parent_descendants [ 0 ] parent . insert ( parent . index ( t . tree ) , value ) value . append ( t . tree ) except IndexError as e : logger . error ( 'Target of outside has been moved or deleted' ) raise e elif t . location == 'before' : value . tail = t . tree . text t . tree . text = None t . tree . insert ( 0 , value ) else : t . tree . append ( value )
Insert value into the target tree 't' with correct grouping.