'Inserts a new card before or after an existing card; used to
implement support for the legacy before/after keyword arguments to
Header.update().
If replace=True, move an existing card with the same keyword.'

def _relativeinsert(self, card, before=None, after=None, replace=False):

    if (before is None):
        insertionkey = after
    else:
        insertionkey = before
    def get_insertion_idx():
        if (not (isinstance(insertionkey, int) and (insertionkey >= len(self._cards)))):
            idx = self._cardindex(insertionkey)
        else:
            idx = insertionkey
        if (before is None):
            idx += 1
        return idx
    if replace:
        old_idx = self._cardindex(card.keyword)
        insertion_idx = get_insertion_idx()
        if ((insertion_idx >= len(self._cards)) and (old_idx == (len(self._cards) - 1))):
            return
        if (before is not None):
            if (old_idx == (insertion_idx - 1)):
                return
        elif ((after is not None) and (old_idx == insertion_idx)):
            return
        del self[old_idx]
    idx = get_insertion_idx()
    if (card[0] in Card._commentary_keywords):
        cards = reversed(self._splitcommentary(card[0], card[1]))
    else:
        cards = [card]
    for c in cards:
        self.insert(idx, c)
