Difference between revisions of "D-Bus examples"

From Apertium
Jump to navigation Jump to search
Line 1: Line 1:
Here are some code snippets for various programming languages showing how you can interface with Apertium by means of D-Bus. You'll need to install the [[D-Bus service for Apertium]].
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==
==Python==
Line 13: Line 13:
sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
sys.stdout = codecs.getwriter('utf-8')(sys.stdout);


pair_name = 'en-af';
mode_name = 'en-af'; # The name of the installed mode
dbus_pair_name = '/' + '_'.join(pair_name.split('-'));
dbus_mode_name = '/' + '_'.join(pair_name.split('-')); # Some mandatory mangling


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


translator = dbus.Interface(bus.get_object('org.apertium.mode', dbus_pair_name), 'org.apertium.Mode')
translator = dbus.Interface(bus.get_object('org.apertium.mode', dbus_mode_name), 'org.apertium.Mode'); # Get the translator


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


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


</pre>
</pre>

Revision as of 19:34, 18 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
dbus_mode_name = '/' + '_'.join(pair_name.split('-')); # Some mandatory mangling

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

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

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

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