def test_get_backend_by_mimetype():
    Conf()
    assert get_backend_by_file("foo.kdb") is python_keepass_backend.Backend
    assert get_backend_by_file("foo.kdbx") is libkeepass_backend.Backend

    with pytest.raises(NoBackendError):
        get_backend_by_file("eggs.kdbfx")

    with pytest.raises(NoBackendError):
        get_backend_by_file("/tmp/foo.gif")
def main():
    # avoid: UnboundLocalError: local variable '__doc__' referenced before assignment
    doc_ = __doc__
    kpconf = Conf()

    if has_gui_support():
        doc_ += "  --gui                     Use TKinter for a graphical interface"

    # handle arguments
    arguments = docopt.docopt(doc_)

    is_daemon = arguments["--daemon"]
    database_path = arguments["<database_path>"]
    host = arguments["--host"]
    port = arguments["--port"]
    assert port.isdigit()
    loglevel = arguments["--loglevel"]

    gui = arguments.get("--gui", False)
    if gui:
        ui = Conf.UI.GUI
    else:
        ui = Conf.UI.CLI

    kpconf.select_ui(ui)
    kpconf.set_loglevel(loglevel)

    # server
    #server = KeepassHTTPServer(host, int(port))

    # backend
    backend_class = backends.get_backend_by_file(database_path)

    try_count = 1
    max_try_count = 3
    success = False
    while try_count <= max_try_count:
        passphrase = getpass.getpass(
            "Please enter the passphrase for database %s: \n" %
            database_path)
        try:
            backend = backend_class(database_path, passphrase)
        except backends.WrongPassword:
            log.info(
                "Wrong passphrase, please try again. (attempt [%s/%s]" %
                (try_count, max_try_count))
            try_count += 1
        else:
            success = True
            log.info("Passphrase accepted")
            break

    if success is False:
        sys.exit("Wrong passphrase after %d attempts" % max_try_count)

    kpconf.set_backend(backend)

    # config daemon
    run_server = partial(app.run, debug=False, host=host, port=int(port))
    if is_daemon:
        pid_file = os.path.join(kpconf.confdir, "process.pid")
        log.info("Server started as daemon on %s:%s" % (host, port))
        daemon = daemonize.Daemonize(app=APP_NAME,
                                     pid=pid_file,
                                     action=run_server,
                                     keep_fds=get_logging_filehandlers_streams_to_keep())
        daemon.start()

    else:
        log.info("Server started on %s:%s" % (host, port))
        run_server()