QuickTile   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 19
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A doCommand() 0 7 1
A __init__() 0 9 1
1
"""D-Bus API for controlling QuickTile"""
2
3
__author__ = "Stephan Sokolow (deitarion/SSokolow)"
4
__license__ = "GNU GPL 2.0 or later"
5
6
import logging
7
8
from dbus.service import BusName, Object, method
9
from dbus import SessionBus
10
from dbus.exceptions import DBusException
11
from dbus.mainloop.glib import DBusGMainLoop
12
13
# Allow MyPy to work without depending on the `typing` package
14
# (And silence complaints from only using the imported types in comments)
15
MYPY = False
16
if MYPY:
17
    # pylint: disable=unused-import
18
    from typing import Optional, Tuple  # NOQA
19
20
    from .commands import CommandRegistry  # NOQA
21
    from .wm import WindowManager  # NOQA
22
del MYPY
23
24
class QuickTile(Object):
25
    """D-Bus endpoint definition"""
26
    def __init__(self, bus, commands, winman):
27
        # type: (SessionBus, CommandRegistry, WindowManager) -> None
28
        """
29
        @param bus: The connection on which to export this object.
30
            See the C{dbus.service.Object} documentation for details.
31
        """
32
        Object.__init__(self, bus, '/com/ssokolow/QuickTile')
33
        self.commands = commands
34
        self.winman = winman
35
36
    @method(dbus_interface='com.ssokolow.QuickTile',
37
            in_signature='s', out_signature='b')
38
    def doCommand(self, command):  # type: (str) -> bool
39
        """Execute a QuickTile tiling command
40
41
        @todo 1.0.0: Expose a proper, introspectable D-Bus API"""
42
        return self.commands.call(command, self.winman)
43
        # FIXME: self.commands.call always returns None
44
45
def init(commands,  # type: CommandRegistry
46
         winman     # type: WindowManager
47
         ):  # type: (...) -> Tuple[Optional[BusName], Optional[QuickTile]]
48
    """Initialize the DBus backend"""
49
    try:
50
        DBusGMainLoop(set_as_default=True)
51
        sess_bus = SessionBus()
52
    except DBusException:
53
        logging.warn("Could not connect to the D-Bus Session Bus.")
54
        return None, None
55
56
    dbus_name = BusName("com.ssokolow.QuickTile", sess_bus)
57
    dbus_obj = QuickTile(sess_bus, commands, winman)
58
59
    return dbus_name, dbus_obj
60