Skip to content

Commit

Permalink
q-dev: fix event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 14, 2024
1 parent 8772108 commit 4077b09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
20 changes: 14 additions & 6 deletions qubesadmin/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,17 @@ def from_qarg(
if blind:
get_domain = domains.get_blind
else:
get_domain = domains.get
get_domain = domains.__getitem__
return cls._parse(representation, devclass, get_domain, '+')

@classmethod
def from_str(cls, representation: str, devclass, domains) -> 'Port':
get_domain = domains.get
def from_str(
cls, representation: str, devclass, domains, blind=False
) -> 'Port':
if blind:
get_domain = domains.get_blind
else:
get_domain = domains.__getitem__
return cls._parse(representation, devclass, get_domain, ':')

@classmethod
Expand Down Expand Up @@ -450,18 +455,21 @@ def from_qarg(
if blind:
get_domain = domains.get_blind
else:
get_domain = domains.get
get_domain = domains.__getitem__
else:
get_domain = None
return cls._parse(representation, devclass, get_domain, backend, '+')

@classmethod
def from_str(
cls, representation: str, devclass: Optional[str], domains,
backend=None
blind=False, backend=None
) -> 'Device':
if backend is None:
get_domain = domains.get
if blind:
get_domain = domains.get_blind
else:
get_domain = domains.__getitem__
else:
get_domain = None
return cls._parse(representation, devclass, get_domain, backend, ':')
Expand Down
20 changes: 15 additions & 5 deletions qubesadmin/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import qubesadmin.config
import qubesadmin.exc
from qubesadmin.device_protocol import Device, Port


class EventsDispatcher(object):
Expand Down Expand Up @@ -229,12 +230,21 @@ def handle(self, subject, event, **kwargs):
self.app.domains.clear_cache(invalidate_name=str(vm))
subject = None
# deserialize known attributes
if event.startswith('device-') and 'device' in kwargs:
if event.startswith('device-'):
try:
devclass = event.split(':', 1)[1]
backend_domain, port_id = kwargs['device'].split(':', 1)
kwargs['device'] = self.app.domains.get_blind(backend_domain)\
.devices[devclass][port_id]
if 'device' in kwargs:
devclass = event.split(':', 1)[1]
device = Device.from_str(
kwargs['device'],
devclass,
self.app.domains,
blind=True)
kwargs['device'] = self.app.domains.get_blind(
device.backend_domain).devices[devclass][device.port_id]
if 'port' in kwargs:
devclass = event.split(':', 1)[1]
kwargs['port'] = Port.from_str(
kwargs['port'], devclass, self.app.domains, blind=True)
except (KeyError, ValueError):
pass
# invalidate cache if needed; call it before other handlers
Expand Down

0 comments on commit 4077b09

Please sign in to comment.