Difference between revisions of "D-Bus examples"
		
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
		
		
		
		
		
	
|  (→Python:  Updated to match the new D-Bus interface) |  (→Java) | ||
| Line 27: | Line 27: | ||
| ==Java== | ==Java== | ||
| {{see-also|Installing the Java D-Bus bindings|Compiling a Java D-Bus program}} | |||
| {{Java_D-Bus_snippet}} | {{Java_D-Bus_snippet}} | ||
| We have a guide on [[Installing the Java D-Bus bindings|installing the Java D-Bus bindings]] and a guide on [[Compiling a Java D-Bus program|compiling a small Java application which uses an Apertium D-Bus service]]. | |||
| [[Category:Development]] | [[Category:Development]] | ||
Revision as of 17:08, 19 December 2007
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
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();
        }
}

