D-Bus examples

From Apertium
Revision as of 00:03, 24 December 2007 by 198.54.202.218 (talk) (Added Clojure example)
Jump to navigation Jump to search

Here are some code snippets for various programming languages (complete with completely unnecessary comments) showing how you can interface with Apertium by means of D-Bus. You'll need to install the D-Bus service for Apertium.

Python

#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-

import dbus, sys, codecs;

sys.stdin = codecs.getwriter('utf-8')(sys.stdin);
sys.stdout = codecs.getwriter('utf-8')(sys.stdout);

mode_name = 'en-af'; # The name of the installed mode

bus = dbus.SessionBus(); # Create a new session bus

# Get the translator
translator = dbus.Interface(bus.get_object('org.apertium.mode', '/'), 'org.apertium.Translate'); 

input = sys.stdin.read(); # Read the data

print translator.translate(mode_name, {}, input); # Print the translation

Java

See also: Installing the Java D-Bus bindings and Compiling a Java D-Bus program
import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

import org.apertium.Info; // The interface we use to access org.apertium.info/

class TestDBus {

        public static void main(String[] args) throws org.freedesktop.dbus.exceptions.DBusException {
                DBusConnection bus = null;
                Info info = null;

                bus = DBusConnection.getConnection(DBusConnection.SESSION);
                info = bus.getRemoteObject("org.apertium.info", "/", Info.class);

                for (String s : info.modes()) {
                        System.out.println(s);
                }

                bus.disconnect();
        }
}

In this program, we simply use the proxy object to invoke the modes</mode> method on the D-Bus object org.apertium.info. This returns a Java List<String>; we enumerate this list, printing each string.

C++

See also: Installing the C++ D-Bus bindings and Compiling a C++ D-Bus program
#include <map>
#include <iostream>
#include <sstream>
#include <string>

#include <dbus-c++/dbus.h>
#include "Translate-glue.h"

static const char* TRANSLATE_SERVICE_NAME = "org.apertium.mode";
static const char* TRANSLATE_OBJECT_PATH = "/";

class Translate
: public org::apertium::Translate,
  public DBus::IntrospectableProxy,
  public DBus::ObjectProxy
{
public:
    Translate(DBus::Connection& connection, const char* path, const char* name)
    : DBus::ObjectProxy(connection, path, name) {
    }
};

DBus::BusDispatcher dispatcher;

int main(int argc, char** argv) {
    DBus::default_dispatcher = &dispatcher;
    DBus::Connection bus = DBus::Connection::SessionBus();

    std::stringstream input;
    std::string mode;
    std::map< ::DBus::String, ::DBus::String> translate_options;

    if (argc != 2) {
        std::cerr << "usage: dbus_test <mode>" << std::endl;
        return 1;
    }

    mode = std::string(argv[1]);

    while (!std::cin.eof() and std::cin) {
        std::string str;
        std::cin >> str;
        input << str << " ";
    }

    Translate translator(bus, TRANSLATE_OBJECT_PATH, TRANSLATE_SERVICE_NAME);
    std::cout << translator.translate(mode, translate_options, input.str()) 
              << std::endl;
}

This program is a simple emulation of the main apertium program. It takes a single parameter, which is the translation mode (something like en-ca). It reads words from stdin and calls the method translate on the D-Bus proxy object translator to translate the text read from stdin. When the translation is complete, it writes the translated string to stdout.

Clojure

(import '(org.freedesktop.dbus DBusConnection))

(def bus (. DBusConnection (getConnection (. DBusConnection SESSION))))
(def info (. bus (getRemoteObject "org.apertium.info" "/" (class org.apertium.Info))))

(doseq mode (. info (modes))
       (prn mode))

(. bus (disconnect))

Clojure is a new Lisp dialect aimed at the Java Virtual Machine. You can see that the Clojure implementation is close to the Java implementation. To run the Clojure code, you will need to pass the same command line parameters to the Java Virtual Machine as for the Java example above.