crystalfontz.dbus.report

ReportHandler derived classes for use with DBus interfaces.

This module is most likely to be used with the DbusClient. Of particular interest will be the DbusClientReportHandler, and its subclasses DbusClientLoggingReportHandler and DbusClientCliReportHandler.

DbusClientCliReportHandler

Bases: DbusClientReportHandler

A DBus report handler intended for use by the client command line interface.

Source code in crystalfontz/dbus/report.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class DbusClientCliReportHandler(DbusClientReportHandler):
    """
    A DBus report handler intended for use by the client command line interface.
    """

    mode: Optional[OutputMode] = None

    async def on_key_activity(self: Self, report: KeyActivityReport) -> None:
        if self.mode == "json":
            print(json.dumps(report.as_dict()))
        elif self.mode == "text":
            print(repr(report))

    async def on_temperature(self: Self, report: TemperatureReport) -> None:
        if self.mode == "json":
            print(json.dumps(report.as_dict()))
        elif self.mode == "text":
            print(repr(report))

DbusClientLoggingReportHandler

Bases: DbusClientReportHandler

A DBus client report handler which logs, using Python's logging module.

Source code in crystalfontz/dbus/report.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class DbusClientLoggingReportHandler(DbusClientReportHandler):
    """
    A DBus client report handler which logs, using Python's logging module.
    """

    def __init__(self: Self) -> None:
        super().__init__()
        self.logger = logging.getLogger(__name__)

    async def on_key_activity(self: Self, report: KeyActivityReport) -> None:
        self.logger.info(report)

    async def on_temperature(self: Self, report: TemperatureReport) -> None:
        self.logger.info(report)

DbusClientReportHandler

Bases: DbusReportHandler

A DBus report handler which listens to reports emitted by a dbus interface.

This report handler is for use by DBus clients.

Source code in crystalfontz/dbus/report.py
 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
122
123
124
125
126
127
128
129
130
class DbusClientReportHandler(DbusReportHandler):
    """
    A DBus report handler which listens to reports emitted by a dbus interface.

    This report handler is for use by DBus clients.
    """

    def __init__(self: Self) -> None:
        super().__init__()

        self._key_activity_task: Optional[asyncio.Task] = None
        self._temperature_task: Optional[asyncio.Task] = None

    async def listen(self: Self) -> None:
        """
        Listen for reports. Any reports emitted on the key_activity_reports and
        temperature_reports signals will be passed to the on_key_activity and
        on_temperature handler methods, respectively.
        """

        self._key_activity_task = asyncio.create_task(self._listen_key_activity())
        self._temperature_task = asyncio.create_task(self._listen_temperature())

    async def _listen_key_activity(self: Self) -> None:
        if self.iface:
            async for report in self.iface.key_activity_reports:
                await self.on_key_activity(KeyActivityReportM.unpack(report))

    async def _listen_temperature(self: Self) -> None:
        if self.iface:
            async for report in self.iface.temperature_reports:
                await self.on_temperature(TemperatureReportM.unpack(report))

    @property
    def done(self: Self) -> asyncio.Task[None]:
        """
        An asyncio.Task that resolves when the report handler is done listening. This
        is typically due to calling `report_handler.stop()`, but may be due to an
        exception.
        """

        return asyncio.create_task(self._done())

    async def _done(self: Self) -> None:
        if self._key_activity_task:
            try:
                await self._key_activity_task
            except asyncio.CancelledError:
                pass

        if self._temperature_task:
            try:
                await self._temperature_task
            except asyncio.CancelledError:
                pass

    def stop(self: Self) -> None:
        """
        Stop listening for reports.
        """

        if self._key_activity_task:
            self._key_activity_task.cancel()
        if self._temperature_task:
            self._temperature_task.cancel()

done property

An asyncio.Task that resolves when the report handler is done listening. This is typically due to calling report_handler.stop(), but may be due to an exception.

listen() async

Listen for reports. Any reports emitted on the key_activity_reports and temperature_reports signals will be passed to the on_key_activity and on_temperature handler methods, respectively.

Source code in crystalfontz/dbus/report.py
79
80
81
82
83
84
85
86
87
async def listen(self: Self) -> None:
    """
    Listen for reports. Any reports emitted on the key_activity_reports and
    temperature_reports signals will be passed to the on_key_activity and
    on_temperature handler methods, respectively.
    """

    self._key_activity_task = asyncio.create_task(self._listen_key_activity())
    self._temperature_task = asyncio.create_task(self._listen_temperature())

stop()

Stop listening for reports.

Source code in crystalfontz/dbus/report.py
122
123
124
125
126
127
128
129
130
def stop(self: Self) -> None:
    """
    Stop listening for reports.
    """

    if self._key_activity_task:
        self._key_activity_task.cancel()
    if self._temperature_task:
        self._temperature_task.cancel()