Update gdbus.py

This commit is contained in:
Pascal Vizeli 2018-04-20 15:59:04 +02:00 committed by GitHub
parent 2efa9f9483
commit 0f4e557552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
import asyncio
import logging
import shlex
import xml.etree.ElementTree as ET
_LOGGER = logging.getLogger(__name__)
@ -11,9 +12,17 @@ CALL = ("gdbus call --system --dest {bus} --object-path {obj} "
class DbusError(Exception):
"""Dbus generic error."""
pass
class DbusFatalError(DbusError):
"""Dbus call going wrong."""
pass
class DbusReturnError(DbusError):
"""Dbus return error."""
pass
class Dbus(object):
"""Dbus handler."""
@ -50,6 +59,17 @@ class Dbus(object):
args=" ".join(map(str, args))
))
# Run command
try:
data = await self._send(command)
except DBusError:
_LOGGER.w
# Parse and return data
return self._gvariant(data)
async def _send(self, command):
"""Send command over dbus."""
# Run command
try:
proc = await asyncio.create_subprocess_exec(
@ -61,16 +81,14 @@ class Dbus(object):
data, _ = await proc.communicate()
except OSError as err:
_LOGGER.error("Can't send dbus command %s: %s", method, err)
raise DbusError() from None
raise DbusFatalError() from None
# Success?
if proc.returncode != 0:
_LOGGER.info("Error %s.%s: %s", self.object_path, method, data)
raise DbusError()
raise DbusReturnError()
# Parse and return data
return self._gvariant(data)
# End
return data.decode()
def __getattr__(self, name):
"""Mapping to dbus method."""