~xubuntu-dev/ubiquity/lp1437180_feh

5467 by Colin Watson
Finish Python 3 port: switch over #! lines and (build-)dependencies.
1
#!/usr/bin/python3
5516 by Colin Watson
PEP-8 import ordering.
2
3
import os
4
import sys
5
6
import dbus
7
import dbus.service
8
import dbus.mainloop.glib
5773 by Martin Pitt
Use glib API from the GLib GI module instead of GObject. The latter
9
from gi.repository import GLib
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
10
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
11
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
12
class Rejected(dbus.DBusException):
13
    _dbus_error_name = "org.bluez.Error.Rejected"
14
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
15
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
16
class Agent(dbus.service.Object):
17
    exit_on_release = True
18
19
    def set_exit_on_release(self, exit_on_release):
20
        self.exit_on_release = exit_on_release
21
22
    @dbus.service.method("org.bluez.Agent", in_signature="", out_signature="")
23
    def Release(self):
24
        if self.exit_on_release:
25
            mainloop.quit()
26
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
27
    @dbus.service.method("org.bluez.Agent",
28
                         in_signature="os", out_signature="")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
29
    def Authorize(self, device, uuid):
30
        bus = dbus.SystemBus()
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
31
        attr = dbus.Interface(
32
            bus.get_object("org.bluez", device),
33
            "org.bluez.Device").GetProperties()
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
34
35
        # Only accept input (HID) devices
36
        if 'Class' in attr:
37
            if (attr['Class'] & 0x500) == 0x500:
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
38
                dbus.Interface(
39
                    bus.get_object("org.bluez", device),
40
                    "org.bluez.Device").SetProperty("Trusted", True)
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
41
                return
42
43
        raise Rejected("Connection rejected by user")
44
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
45
    @dbus.service.method("org.bluez.Agent",
46
                         in_signature="o", out_signature="s")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
47
    def RequestPinCode(self, device):
48
        return "0000"
49
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
50
    @dbus.service.method("org.bluez.Agent",
51
                         in_signature="o", out_signature="u")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
52
    def RequestPasskey(self, device):
53
        return dbus.UInt32(0000)
54
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
55
    @dbus.service.method("org.bluez.Agent",
56
                         in_signature="ou", out_signature="")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
57
    def DisplayPasskey(self, device, passkey):
58
        return
59
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
60
    @dbus.service.method("org.bluez.Agent",
61
                         in_signature="ou", out_signature="")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
62
    def RequestConfirmation(self, device, passkey):
63
        return
64
65
    @dbus.service.method("org.bluez.Agent", in_signature="s", out_signature="")
66
    def ConfirmModeChange(self, mode):
67
        return
68
69
    @dbus.service.method("org.bluez.Agent", in_signature="", out_signature="")
70
    def Cancel(self):
71
        return
72
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
73
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
74
def exit_handler():
5249 by Stéphane Graber
Prevent ubiquity-bluetooth-agent crash when it already ran.
75
    if not os.path.exists("/tmp/ubiquity-bluetooth-done"):
5427 by Colin Watson
Fix a slew of file handle leaks, including making much more liberal
76
        with open("/tmp/ubiquity-bluetooth-done", "w+"):
77
            pass
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
78
79
    if os.path.exists("/usr/bin/bluetooth-applet.orig"):
80
        os.execl("/usr/bin/bluetooth-applet.orig", "bluetooth-applet")
81
    elif os.path.exists("/usr/bin/bluetooth-applet"):
82
        os.execl("/usr/bin/bluetooth-applet", "bluetooth-applet")
83
    else:
84
        sys.exit(0)
85
    return False
86
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
87
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
88
if os.path.exists("/tmp/ubiquity-bluetooth-done"):
89
    exit_handler()
90
91
try:
92
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
93
    bus = dbus.SystemBus()
5562 by Colin Watson
Make all Python code pass pep8(1), and add a test to enforce this.
94
    manager = dbus.Interface(
95
        bus.get_object("org.bluez", "/"), "org.bluez.Manager")
96
    adapter = dbus.Interface(
97
        bus.get_object("org.bluez", manager.DefaultAdapter()),
98
        "org.bluez.Adapter")
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
99
    agent = Agent(bus, "/ubiquity/bluetooth/agent")
5773 by Martin Pitt
Use glib API from the GLib GI module instead of GObject. The latter
100
    mainloop = GLib.MainLoop()
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
101
    adapter.RegisterAgent("/ubiquity/bluetooth/agent", "DisplayYesNo")
102
except dbus.exceptions.DBusException:
103
    exit_handler()
104
5773 by Martin Pitt
Use glib API from the GLib GI module instead of GObject. The latter
105
GLib.timeout_add(300000, exit_handler)
5244 by Stéphane Graber
Add ubiquity-bluetooth-agent
106
mainloop.run()