Skip to content

Commit 8417bb7

Browse files
author
Tony Crisci
committed
make gi repository optional
1 parent 9f0c31d commit 8417bb7

11 files changed

+86
-13
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ ignore=
99
# examples
1010
per-file-ignores=
1111
test/*:F821
12+
test/util.py:F401
1213
examples/*:F821
1314
*/__init__.py:F401

Dockerfile

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
FROM ubuntu:19.04
2+
23
WORKDIR /app
3-
RUN apt-get update && apt-get install -y dbus python3-gi python3-pip
4-
ADD . /app
4+
5+
RUN apt-get update && apt-get install -y python3-pip
6+
7+
# uncomment to test a system without gi repository
8+
#RUN apt-get remove -y python3-gi
9+
10+
COPY requirements.txt .
511
RUN pip3 install -r requirements.txt
12+
13+
ADD . /app
14+
615
RUN find -name __pycache__ | xargs rm -r || true
716
CMD ["dbus-run-session", "pytest"]

Makefile

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
.PHONY: test format lint precommit
1+
.PHONY: test format lint all
2+
.DEFAULT_GOAL := all
3+
4+
source_dirs = dbus_next test examples
25

36
lint:
4-
flake8
7+
flake8 $(source_dirs)
58

69
format:
7-
yapf -ipr .
10+
yapf -ipr $(source_dirs)
811

912
test:
10-
dbus-run-session pytest -sq
13+
dbus-run-session pytest -s
14+
15+
docker-test:
16+
docker build -t dbus-next .
17+
docker run -it dbus-next
1118

1219
coverage:
1320
dbus-run-session pytest --cov=dbus_next
1421

15-
precommit: format lint test
22+
all: format lint test

dbus_next/glib/message_bus.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99

1010
import logging
1111
import io
12-
from gi.repository import GLib
1312

13+
# glib is optional
14+
_import_error = None
15+
try:
16+
from gi.repository import GLib
17+
_GLibSource = GLib.Source
18+
except ImportError as e:
19+
_import_error = e
20+
class _GLibSource:
21+
pass
1422

15-
class _MessageSource(GLib.Source):
23+
24+
class _MessageSource(_GLibSource):
1625
def __init__(self, bus):
1726
self.unmarshaller = None
1827
self.bus = bus
@@ -42,7 +51,7 @@ def dispatch(self, callback, user_data):
4251
return GLib.SOURCE_CONTINUE
4352

4453

45-
class _MessageWritableSource(GLib.Source):
54+
class _MessageWritableSource(_GLibSource):
4655
def __init__(self, bus):
4756
self.bus = bus
4857
self.buf = b''
@@ -87,7 +96,7 @@ def dispatch(self, callback, user_data):
8796
return GLib.SOURCE_REMOVE
8897

8998

90-
class _AuthLineSource(GLib.Source):
99+
class _AuthLineSource(_GLibSource):
91100
def __init__(self, stream):
92101
self.stream = stream
93102
self.buf = b''
@@ -109,6 +118,9 @@ def dispatch(self, callback, user_data):
109118

110119
class MessageBus(BaseMessageBus):
111120
def __init__(self, bus_address=None, bus_type=BusType.SESSION, main_context=None):
121+
if _import_error:
122+
raise _import_error
123+
112124
super().__init__(bus_address, bus_type)
113125
self.main_context = main_context if main_context else GLib.main_context_default()
114126

dbus_next/glib/proxy_object.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from ..variant import Variant
55
from ..constants import ErrorType
66

7-
from gi.repository import GLib
7+
# glib is optional
8+
try:
9+
from gi.repository import GLib
10+
except ImportError as e:
11+
pass
812

913

1014
class ProxyInterface(BaseProxyInterface):

test/client/test_methods.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from dbus_next.service import ServiceInterface, method
22
import dbus_next.introspection as intr
33
from dbus_next import aio, glib, DBusError
4+
from test.util import check_gi_repository, skip_reason_no_gi
45

56
import pytest
67

8+
has_gi = check_gi_repository()
9+
710

811
class ExampleInterface(ServiceInterface):
912
def __init__(self):
@@ -78,6 +81,7 @@ async def test_aio_proxy_object():
7881
bus2.disconnect()
7982

8083

84+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
8185
def test_glib_proxy_object():
8286
bus_name = 'glib.client.test.methods'
8387
bus = glib.session_bus_sync()

test/client/test_properties.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from dbus_next import aio, glib, Message, DBusError
22
from dbus_next.service import ServiceInterface, dbus_property
3+
from test.util import check_gi_repository, skip_reason_no_gi
34

45
import pytest
56

7+
has_gi = check_gi_repository()
8+
69

710
class ExampleInterface(ServiceInterface):
811
def __init__(self):
@@ -68,6 +71,7 @@ async def test_aio_properties():
6871
bus.disconnect()
6972

7073

74+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
7175
def test_glib_properties():
7276
service_bus = glib.session_bus_sync()
7377
service_interface = ExampleInterface()

test/test_big_message.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from dbus_next import aio, glib, Message, MessageType
22
from dbus_next.service import ServiceInterface, method
3+
from test.util import check_gi_repository, skip_reason_no_gi
34

45
import pytest
56

7+
has_gi = check_gi_repository()
8+
69

710
class ExampleInterface(ServiceInterface):
811
def __init__(self):
@@ -34,6 +37,7 @@ async def test_aio_big_message():
3437
assert result.body[0] == big_body[0]
3538

3639

40+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
3741
def test_glib_big_message():
3842
'this tests that nonblocking reads and writes actually work for glib'
3943
bus1 = glib.session_bus_sync()

test/test_glib_low_level.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
from dbus_next.glib import session_bus_sync
22
from dbus_next import Message, MessageType, MessageFlag
3+
from test.util import check_gi_repository, skip_reason_no_gi
34

4-
from gi.repository import GLib
5+
import pytest
56

7+
has_gi = check_gi_repository()
68

9+
if has_gi:
10+
from gi.repository import GLib
11+
12+
13+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
714
def test_standard_interfaces():
815
bus = session_bus_sync()
916
msg = Message(destination='org.freedesktop.DBus',
@@ -39,6 +46,7 @@ def test_standard_interfaces():
3946
assert type(reply.body[0]) is str
4047

4148

49+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
4250
def test_sending_messages_between_buses():
4351
bus1 = session_bus_sync()
4452
bus2 = session_bus_sync()
@@ -98,6 +106,7 @@ def message_handler_error(sent):
98106
assert reply is None
99107

100108

109+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
101110
def test_sending_signals_between_buses():
102111
bus1 = session_bus_sync()
103112
bus2 = session_bus_sync()

test/test_request_name.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from dbus_next import aio, glib, Message, MessageType, NameFlag, RequestNameReply, ReleaseNameReply
2+
from test.util import check_gi_repository, skip_reason_no_gi
23

34
import pytest
45

6+
has_gi = check_gi_repository()
7+
58

69
@pytest.mark.asyncio
710
async def test_name_requests():
@@ -52,6 +55,7 @@ async def get_name_owner(name):
5255
bus2.disconnect()
5356

5457

58+
@pytest.mark.skipif(not has_gi, reason=skip_reason_no_gi)
5559
def test_request_name_glib():
5660
test_name = 'glib.test.request.name'
5761
bus = glib.session_bus_sync()

test/util.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
_has_gi = None
2+
skip_reason_no_gi = 'glib tests require python3-gi'
3+
4+
5+
def check_gi_repository():
6+
global _has_gi
7+
if _has_gi is not None:
8+
return _has_gi
9+
try:
10+
from gi.repository import GLib
11+
_has_gi = True
12+
return _has_gi
13+
except ImportError:
14+
_has_gi = False
15+
return _has_gi

0 commit comments

Comments
 (0)