summaryrefslogtreecommitdiff
path: root/src/tests/dbusmock-templates/colord.py
blob: d1912af445f1c6ffa78703cfd2328b41e65404cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'''colord proxy mock template
'''

# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.

__author__ = 'Jonas Ã…dahl'
__copyright__ = '(c) 2021 Red Hat Inc.'

import dbus
import os
import pwd
from dbusmock import MOCK_IFACE


BUS_PREFIX = 'org.freedesktop.ColorManager'
PATH_PREFIX = '/org/freedesktop/ColorManager'

BUS_NAME = BUS_PREFIX
MAIN_OBJ = PATH_PREFIX
MAIN_IFACE = BUS_NAME
DEVICE_IFACE = BUS_PREFIX + '.Device'
PROFILE_IFACE = BUS_PREFIX + '.Profile'
SYSTEM_BUS = True


def load(mock, parameters=None):
    mock.devices = {}
    mock.profiles = {}

def escape_unit_name(name):
    for s in ['.', '-', '\'', ' ']:
        name = name.replace(s, '_')
    return name

def get_username(uid):
    return pwd.getpwuid(uid).pw_name

def device_id_from_path(mock, path):
    for device_id in mock.devices:
        device_path = mock.devices[device_id]
        if device_path == path:
            return device_id
    return None

def profile_id_from_path(mock, path):
  for profile_id in mock.profiles:
     profile_path = mock.profiles[profile_id]
     if profile_path == path:
         return profile_id
  return None

@dbus.service.method(MAIN_IFACE, in_signature='ssa{sv}', out_signature='o')
def CreateDevice(self, device_id, scope, props):
    uid = os.getuid()
    username = get_username(uid)
    device_path = PATH_PREFIX + '/devices/' + \
        escape_unit_name(device_id) + \
        '_' + username + '_' + str(uid)
    self.devices[device_id] = device_path
    self.AddObject(device_path,
                   DEVICE_IFACE,
                   {
                     'DeviceId': device_id,
                     'Enabled': True,
                   },
                   [])
    self.EmitSignal(MAIN_IFACE, 'DeviceAdded', 'o', [device_path])
    return device_path

@dbus.service.method(MAIN_IFACE, in_signature='o')
def DeleteDevice(self, device_path):
    self.RemoveObject(device_path)
    device_id = device_id_from_path(self, device_path)
    del self.devices[device_id]
    self.EmitSignal(MAIN_IFACE, 'DeviceRemoved', 'o', [device_path])


@dbus.service.method(MAIN_IFACE, in_signature='s', out_signature='o')
def FindDeviceById(self, device_id):
    return self.devices[device_id]


@dbus.service.method(MAIN_IFACE, in_signature='ssha{sv}', out_signature='o')
def CreateProfileWithFd(self, profile_id, scope, handle, props):
    uid = os.getuid()
    username = get_username(uid)
    profile_path = PATH_PREFIX + '/profiles/' + \
        escape_unit_name(profile_id) + \
        '_' + username + '_' + str(uid)
    self.profiles[profile_id] = profile_path
    self.AddObject(profile_path,
                   PROFILE_IFACE,
                   {
                     'ProfileId': profile_id,
                     'Enabled': True,
                     'Filename': props['Filename'],
                   },
                   [])
    self.EmitSignal(MAIN_IFACE, 'ProfileAdded', 'o', [profile_path])
    return profile_path

@dbus.service.method(MAIN_IFACE, in_signature='o')
def DeleteProfile(self, profile_path):
    self.RemoveObject(profile_path)
    profile_id = profile_id_from_path(self, profile_path)
    del self.profiles[profile_id]
    self.EmitSignal(MAIN_IFACE, 'ProfileRemoved', 'o', [profile_path])


@dbus.service.method(MOCK_IFACE)
def Reset(self):
    for device_path in self.devices.values():
        self.RemoveObject(device_path)
    self.devices = {}
    for profile_path in self.profiles.values():
        self.RemoveObject(profile_path)
    self.profiles = {}