Skip to content

Commit f7948cd

Browse files
authored
Fix listen_* methods without valid last_object provided (#24)
* Fix listen_* methods without valid last_object provided * Reorganized last_object method
1 parent 70b4a22 commit f7948cd

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/core.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,42 +168,35 @@ def recent_blobs(self):
168168

169169
def _listen(self, last_object, object_type, blocking=True, interval=15, query=None):
170170
if last_object is None:
171-
last_object = next(self._recent(object_type, query=query))
171+
last_object = next(self._recent(object_type, query=query), None)
172172
# If there are no elements (even first element): just get new samples from now on
173-
if last_object is not None:
174-
last_id = last_object.id
175-
last_time = last_object.upload_time
176173
elif isinstance(last_object, MWDBObject):
177174
# If we are requesting for typed objects, we should additionally check the object type
178175
if object_type is not MWDBObject and not isinstance(last_object, object_type):
179176
raise TypeError("latest_object type must be 'str' or '{}'".format(object_type.__name__))
180177
# If object instance provided: get ID from instance
181-
last_id = last_object.id
182-
last_time = last_object.upload_time
183178
else:
184179
# If not: first check whether object exists in repository
185-
last_obj = self._query(object_type, last_object, raise_not_found=True)
186-
last_id = last_obj.id
187-
last_time = last_obj.upload_time
180+
last_object = self._query(object_type, last_object, raise_not_found=True)
188181

189182
while True:
190183
objects = []
191184
for obj in self._recent(object_type, query=query):
192-
if obj.id == last_id:
193-
break
194-
195-
if obj.upload_time < last_time:
196-
raise RuntimeError(
197-
"Newly fetched object [{}] is older than the pivot [{}]".format(
198-
obj.id, last_id
185+
if last_object:
186+
if obj.id == last_object.id:
187+
break
188+
189+
if obj.upload_time < last_object.upload_time:
190+
raise RuntimeError(
191+
"Newly fetched object [{}] is older than the pivot [{}]".format(
192+
obj.id, last_object.id
193+
)
199194
)
200-
)
201195
objects.append(obj)
202196

203197
# Return fetched objects in reversed order (from oldest to latest)
204198
for obj in objects[::-1]:
205-
last_id = obj.id
206-
last_time = obj.upload_time
199+
last_object = obj
207200
yield obj
208201
if blocking:
209202
time.sleep(interval)

0 commit comments

Comments
 (0)