Arguments:
        <file_or_folder> [<file_or_folder> [...]]
            [-- [--no_ocr] [--no_label_guessing] [--append <document_id>]]

    Import a file or a PDF folder. OCR is run by default on images
    and on PDF pages without text (PDF containing only images)

    Please keep in mind that documents that are already in the word directory
    are never imported again and are simply ignored.

    Example: paperwork-shell import -- somefile.pdf --no_label_guessing

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "ocr": [
                "20170602_1513_12|0",
                "20170602_1513_12|1"
            ],
            "imports": {
                # beware this section is filled in *before* label guessing
                "imported_file_uris": [
                    "file:///home/jflesch/tmp/pouet.pdf"
                ],
                "new_docs": [
                    {
                        "docid": "20170602_1513_12",
                        "labels": []
                    }
                ],
                "new_docs_pages": [
                    "20170602_1513_12|0",
                    "20170602_1513_12|1"
                ],
                "stats": {
                    # ~Human readable statistics
                    # exact content of this section is not guaranteed
                    "Document(s)": 1,
                    "Image file(s)": 0,
                    "PDF": 1,
                    "Page(s)": 2
                },
                "upd_docs": [],
                "upd_docs_pages": []
            }
            "guessed_labels": [
                {
                    "docid": "20170602_1513_12",
                    "labels": [
                        "Documentation"
                    ]
                }
            ]
        }

def cmd_import(*args):
    """
    Arguments:
        <file_or_folder> [<file_or_folder> [...]]
            [-- [--no_ocr] [--no_label_guessing] [--append <document_id>]]

    Import a file or a PDF folder. OCR is run by default on images
    and on PDF pages without text (PDF containing only images)

    Please keep in mind that documents that are already in the word directory
    are never imported again and are simply ignored.

    Example: paperwork-shell import -- somefile.pdf --no_label_guessing

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "ocr": [
                "20170602_1513_12|0",
                "20170602_1513_12|1"
            ],
            "imports": {
                # beware this section is filled in *before* label guessing
                "imported_file_uris": [
                    "file:///home/jflesch/tmp/pouet.pdf"
                ],
                "new_docs": [
                    {
                        "docid": "20170602_1513_12",
                        "labels": []
                    }
                ],
                "new_docs_pages": [
                    "20170602_1513_12|0",
                    "20170602_1513_12|1"
                ],
                "stats": {
                    # ~Human readable statistics
                    # exact content of this section is not guaranteed
                    "Document(s)": 1,
                    "Image file(s)": 0,
                    "PDF": 1,
                    "Page(s)": 2
                },
                "upd_docs": [],
                "upd_docs_pages": []
            }
            "guessed_labels": [
                {
                    "docid": "20170602_1513_12",
                    "labels": [
                        "Documentation"
                    ]
                }
            ]
        }
    """
    guess_labels = True
    ocr = pyocr.get_available_tools()
    docid = None
    doc = None

    args = list(args)

    if len(ocr) <= 0:
        raise Exception("No OCR tool found")
    ocr = ocr[0]

    if "--no_label_guessing" in args:
        guess_labels = False
        args.remove("--no_label_guessing")
    if "--no_ocr" in args:
        ocr = None
        args.remove("--no_ocr")
    if "--append" in args:
        idx = args.index("--append")
        docid = args[idx + 1]
        args.pop(idx)
        args.pop(idx)
    if len(args) <= 0:
        sys.stderr.write("Nothing to import.\n")
        return

    dsearch = get_docsearch()

    if docid:
        doc = dsearch.get(docid)
        if doc is None:
            sys.stderr.write("Document {} not found\n".format(docid))
            return

    ocr_lang = None
    if ocr is not None:
        pconfig = config.PaperworkConfig()
        pconfig.read()
        ocr_lang = pconfig.settings['ocr_lang'].value
    return _do_import(args, dsearch, doc, ocr, ocr_lang, guess_labels)