Arguments: <document_id> <label_name>

    Remove a label from a document.

    Note that if the document was the last one to use the label,
    the label may disappear entirely from Paperwork.

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "docid": "xxxx",
            "labels": ["aaa", "bbb", "ccc"],  # after deletion
        }

def cmd_remove_label(docid, label_name):
    """
    Arguments: <document_id> <label_name>

    Remove a label from a document.

    Note that if the document was the last one to use the label,
    the label may disappear entirely from Paperwork.

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "docid": "xxxx",
            "labels": ["aaa", "bbb", "ccc"],  # after deletion
        }
    """
    dsearch = get_docsearch()
    doc = dsearch.get(docid)
    if doc is None:
        raise Exception(
            "Document {} not found. Cannot remove label from it".format(
                docid
            )
        )

    for clabel in dsearch.label_list:
        if clabel.name == label_name:
            label = clabel
    else:
        raise Exception("Unknown label {}".format(label_name))

    dsearch.remove_label(doc, label)
    verbose("Label {} removed from document {}".format(label_name, docid))
    reply({
        "docid": docid,
        "labels": [l.name for l in doc.labels],
    })