~3v1n0/unity/overlay-border-scale

« back to all changes in this revision

Viewing changes to tests/autopilot/autopilot/emulators/ibus.py

  • Committer: Daniel van Vugt
  • Date: 2012-03-14 06:24:18 UTC
  • mfrom: (2108 unity)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: daniel.van.vugt@canonical.com-20120314062418-nprucpbr0m7qky5e
MergedĀ latestĀ lp:unity

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2012 Canonical
 
3
# Author: Thomi Richards
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
 
 
9
"Functions to deal with ibus service."
 
10
 
 
11
# without this the 'import ibus' imports us, and import ibus.common fails. 0.O
 
12
from __future__ import absolute_import
 
13
import ibus
 
14
import ibus.common
 
15
import os
 
16
import logging
 
17
from time import sleep
 
18
 
 
19
logger = logging.getLogger(__name__)
 
20
 
 
21
 
 
22
def get_ibus_bus():
 
23
    """Get the ibus bus object, possibly starting the ibus daemon if it's
 
24
    not already running.
 
25
 
 
26
    """
 
27
    max_tries = 5
 
28
    for i in range(max_tries):
 
29
        if ibus.common.get_address() is None:
 
30
            pid = os.spawnlp(os.P_NOWAIT, "ibus-daemon", "ibus-daemon", "-d", "--xim")
 
31
            logger.info("Started ibus-daemon with pid %i." % (pid))
 
32
            sleep(2)
 
33
        else:
 
34
            return ibus.Bus()
 
35
    raise RuntimeError("Could not start ibus-daemon after %d tries." % (max_tries))
 
36
 
 
37
 
 
38
def get_available_input_engines():
 
39
    """Get a list of available input engines."""
 
40
    bus = get_ibus_bus()
 
41
    return [e.name for e in bus.list_engines()]
 
42
 
 
43
 
 
44
def get_active_input_engines():
 
45
    """Get the list of input engines that have been activated."""
 
46
    bus = get_ibus_bus()
 
47
    return [e.name for e in bus.list_active_engines()]
 
48
 
 
49
def set_active_engines(engine_list):
 
50
    """Installs the engines in 'engine_list' into the list of active iBus engines.
 
51
 
 
52
    The specified engines must appear in the return list from
 
53
    get_available_input_engines(). This function removes all other engines.
 
54
 
 
55
    This function returns the list of engines installed before this function was
 
56
    called. The caller should pass this list to set_active_engines to restore
 
57
    ibus to it's old state once the test has finished.
 
58
    """
 
59
    if type(engine_list) is not list:
 
60
        raise TypeError("engine_list must be a list of valid engine names.")
 
61
    available_engines = get_available_input_engines()
 
62
    for engine in engine_list:
 
63
        if not isinstance(engine, basestring):
 
64
            raise TypeError("Engines in engine_list must all be strings.")
 
65
        if engine not in available_engines:
 
66
            raise ValueError("engine_list contains invalid engine name: '%s'", engine)
 
67
 
 
68
    bus = get_ibus_bus()
 
69
    config = bus.get_config()
 
70
    old_engines = get_active_input_engines()
 
71
    config.set_list("general", "preload_engines", engine_list, "s")
 
72
    # need to restart the ibus bus before it'll pick up the new engine.
 
73
    # see bug report here:
 
74
    # http://code.google.com/p/ibus/issues/detail?id=1418&thanks=1418&ts=1329885137
 
75
    bus.exit(restart=True)
 
76
    sleep(1)
 
77
    return old_engines
 
78
 
 
79
def set_global_input_engine(engine_name):
 
80
    """Set the global iBus input engine by name.
 
81
 
 
82
    This function enables the global input engine. To turn it off again, pass None
 
83
    as the engine name.
 
84
 
 
85
    """
 
86
    if not (engine_name is None or isinstance(engine_name, basestring)):
 
87
        raise TypeError("engine_name type must be either str or None.")
 
88
 
 
89
    bus = get_ibus_bus()
 
90
 
 
91
    if engine_name:
 
92
        available_engines = get_available_input_engines()
 
93
        if not engine_name in available_engines:
 
94
            raise ValueError("Unknown engine '%s'" % (engine_name))
 
95
        bus.get_config().set_value("general", "use_global_engine", True)
 
96
        bus.set_global_engine(engine_name)
 
97
        logger.info('Enabling global ibus engine "%s".' % (engine_name))
 
98
    else:
 
99
        bus.get_config().set_value("general", "use_global_engine", False)
 
100
        logger.info('Disabling global ibus engine.')