#!/usr/bin/env python

import sys
from traceback import print_exc

import dbus

from contextlib import contextmanager
@contextmanager
def timeit(s):
    import gc, time
    gc.collect()
    gc.collect()
    gc.collect()
    t = time.time()
    yield None
    print "time: %fms%s" % ((time.time() - t)*1000, " for "+s if s else "")


def main():
    bus = dbus.SessionBus()
    choices = []

    try:
        service = bus.get_object("org.freedesktop.WordPrediction",
                                       "/WordPredictor")

        #iface = dbus.Interface(service, "com.example.SampleInterface")
        #choices = iface.predict(["We", "saw", ""], 20)
        with timeit("client calling Predict"):
            choices = service.predict(["lm:system:en"], "We saw", 20)

    except dbus.DBusException:
        print_exc()
        #sys.exit(1)

    print choices

    if sys.argv[1:] == ['--exit-service']:
        service.Exit()

if __name__ == '__main__':
    main()

