Skip to content

Commit 4f56683

Browse files
committed
Fixes after rebase
1 parent 35493e4 commit 4f56683

4 files changed

Lines changed: 47 additions & 29 deletions

File tree

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ disable=
44
import-outside-toplevel, # Done on purpose to reduce overhead at startup
55
invalid-name, # Module name or methods do not follow snake_case convention
66
line-too-long, # Flake8 is used for testing line-length
7+
max-line-length=160

lib/AddonSignals.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77

88
class WaitTimeoutError(Exception):
9-
pass
9+
""" Exception used when waiting times out"""
1010

1111

1212
def _perf_clock():
1313
"""Provides high resolution timing in seconds"""
14+
import time
1415
if hasattr(time, 'perf_counter'):
1516
return time.perf_counter() # pylint: disable=no-member
1617
if hasattr(time, 'clock'):
@@ -27,6 +28,20 @@ def _addon_id():
2728
return getattr(_addon_id, 'cached')
2829

2930

31+
def _to_unicode(text, encoding='utf-8', errors='strict'):
32+
"""Force text to unicode"""
33+
if isinstance(text, bytes):
34+
return text.decode(encoding, errors=errors)
35+
return text
36+
37+
38+
def _receiver():
39+
"""Return a SignalReceiver instance and cache it as a static variable"""
40+
if not hasattr(_receiver, 'cached'):
41+
_receiver.cached = SignalReceiver()
42+
return getattr(_receiver, 'cached')
43+
44+
3045
def _decode_data(data):
3146
"""Decode base64-encoded JSON data and return Python data structure"""
3247
import json
@@ -50,13 +65,6 @@ def _encode_data(data):
5065
return encoded_data.decode('ascii')
5166

5267

53-
def _receiver():
54-
"""Return a SignalReceiver instance and cache it as a static variable"""
55-
if not hasattr(_receiver, 'cached'):
56-
_receiver.cached = SignalReceiver()
57-
return getattr(_receiver, 'cached')
58-
59-
6068
def _jsonrpc(**kwargs):
6169
"""Perform JSONRPC calls"""
6270
import json
@@ -67,20 +75,11 @@ def _jsonrpc(**kwargs):
6775
return json.loads(executeJSONRPC(json.dumps(kwargs)))
6876

6977

70-
def _to_unicode(text, encoding='utf-8', errors='strict'):
71-
"""Force text to unicode"""
72-
if isinstance(text, bytes):
73-
return text.decode(encoding, errors=errors)
74-
return text
75-
76-
7778
class SignalReceiver(Monitor, object): # pylint: disable=bad-option-value,useless-object-inheritance
7879
"""The AddonSignals receiver class"""
79-
8080
def __init__(self): # pylint: disable=super-init-not-called
81-
"""The SignalReceiver constructor"""
8281
self._slots = {}
83-
super(SignalReceiver, self).__init__()
82+
super(SignalReceiver, self).__init__() # pylint: disable=super-with-arguments
8483

8584
def registerSlot(self, signaler_id, signal, callback):
8685
"""Register a slot in the AddonSignals receiver"""
@@ -111,16 +110,16 @@ def onNotification(self, sender, method, data):
111110

112111
class CallHandler(object): # pylint: disable=bad-option-value,useless-object-inheritance
113112
"""The AddonSignals event handler class"""
114-
115-
def __init__(self, signal, data, source_id, timeout=1000, use_timeout_exception=False):
116-
''' The CallHandler constructor '''
113+
def __init__(self, signal, data, source_id, timeout=1000, use_timeout_exception=False): # pylint: disable=too-many-arguments
114+
"""The CallHandler constructor"""
117115
self.signal = signal
116+
self.data = data
118117
self.timeout = timeout
119118
self.source_id = source_id
120119
self._return = None
121120
self.is_callback_received = False
122121
self.use_timeout_exception = use_timeout_exception
123-
registerSlot(self.source_id, '_return.{0}'.format(self.signal), self.callback)
122+
registerSlot(self.source_id, '_return_{0}'.format(self.signal), self.callback)
124123
sendSignal(signal, data, self.source_id)
125124

126125
def callback(self, data):
@@ -130,17 +129,17 @@ def callback(self, data):
130129

131130
def waitForReturn(self):
132131
"""Wait for callback to trigger"""
133-
monitor = xbmc.Monitor()
132+
monitor = Monitor()
134133
end_time = _perf_clock() + (self.timeout / 1000)
135134
while not self.is_callback_received:
136135
if _perf_clock() > end_time:
137136
if self.use_timeout_exception:
138137
unRegisterSlot(self.source_id, self.signal)
139138
raise WaitTimeoutError
140139
break
141-
elif monitor.abortRequested():
140+
if monitor.abortRequested():
142141
raise OSError
143-
xbmc.sleep(10)
142+
sleep(10)
144143
unRegisterSlot(self.source_id, self.signal)
145144
return self._return
146145

@@ -183,7 +182,7 @@ def returnCall(signal, data=None, source_id=None):
183182
:param data: data to send
184183
:param source_id: the name used for call/answer (e.g. add-on id)
185184
"""
186-
sendSignal('_return.{0}'.format(signal), data, source_id)
185+
sendSignal('_return_{0}'.format(signal), data, source_id)
187186

188187

189188
def makeCall(signal, data=None, source_id=None, timeout_ms=1000, use_timeout_exception=False):

tests/test_calls.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ def test_unicode(self):
5858
self.assertEqual(ret, dict(value='return_unicode', data=data))
5959

6060
def test_bogus_sender(self):
61-
ret = makeCall(signal='test_bogus_sender', data=dict(bogus='data'), source_id='bogus.sender.id')
61+
with self.assertRaises(OSError) as ctxt:
62+
makeCall(signal='test_bogus_sender', data=dict(bogus='data'), source_id='bogus.sender.id')
63+
self.assertTrue(isinstance(ctxt.exception, OSError))
6264
# self.assertNotEqual(data, callback_bogus_sender.data)
63-
self.assertEqual(ret, None)
6465

6566
def test_send_multiple(self):
6667
for idx in range(3):

tests/xbmc.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,30 @@ class Monitor(object):
2626

2727
def __init__(self, line='', heading=''): # pylint: disable=unused-argument
2828
"""A stub constructor for the xbmc Monitor class"""
29+
self.iteration = 0
2930
self._instances.add(weakref.ref(self))
3031

32+
def abortRequested(self):
33+
"""A stub implementation for the xbmc Keyboard class abortRequested() method"""
34+
self.iteration += 1
35+
print('Iteration: %s' % self.iteration)
36+
return self.iteration % 5 == 0
37+
38+
def waitForAbort(self, timeout=None): # pylint: disable=no-self-use
39+
"""A stub implementation for the xbmc Monitor class waitForAbort() method"""
40+
try:
41+
time.sleep(timeout)
42+
except KeyboardInterrupt:
43+
return True
44+
except Exception: # pylint: disable=broad-except
45+
return True
46+
return False
47+
3148
@classmethod
3249
def getinstances(cls):
3350
"""Return the instances for this class"""
3451
dead = set()
35-
for ref in cls._instances:
52+
for ref in cls._instances.copy():
3653
obj = ref()
3754
if obj is not None:
3855
yield obj

0 commit comments

Comments
 (0)