def pre_insert_event(self, event, sender): for (unique_id, bus_names) in self._running.iteritems(): if sender in bus_names: datasource = self._registry[unique_id] # Update LastSeen time datasource.last_seen = get_timestamp_for_now() self._dirty = True # Check whether the data-source is allowed to insert events if not datasource.enabled: return None return event
def _name_owner_changed(self, owner, old, new): """ Cleanup disconnected clients and mark data-sources as not running when no client remains. """ uid = [uid for uid, ids in self._running.iteritems() if owner in ids] if not uid: return uid = uid[0] datasource = self._registry[uid] # Update LastSeen time datasource.last_seen = get_timestamp_for_now() strid = "%s (%s)" % (uid, datasource.name) log.debug("Client disconnected: %s" % strid) if len(self._running[uid]) == 1: log.debug("No remaining client running: %s" % strid) del self._running[uid] datasource.running = False self.DataSourceDisconnected(datasource) else: self._running[uid].remove(owner)
def _insert_event(self, event, sender=None): if not issubclass(type(event), OrigEvent): raise ValueError("cannot insert object of type %r" %type(event)) if event.id: raise ValueError("Illegal event: Predefined event id") if not event.subjects: raise ValueError("Illegal event format: No subject") if not event.timestamp: event.timestamp = get_timestamp_for_now() if not event.interpretation == Interpretation.MOVE_EVENT: for subject in event.subjects: if not subject.current_uri: subject.current_uri = subject.uri elif not subject.uri == subject.current_uri: raise ValueError("Illegal event: unless " "event.interpretation is 'MOVE_EVENT' then subject.uri " "and subject.current_uri have to be the same") if event.interpretation == Interpretation.MOVE_EVENT: for subject in event.subjects: if subject.uri == subject.current_uri or not subject.current_uri: raise ValueError("Redundant event: event.interpretation " "indicates the uri has been moved yet the subject.uri " "and subject.current_uri are identical") event_id = self.next_event_id() event[0][Event.Id] = event_id event = self.extensions.apply_pre_insert(event, sender) if event is None: raise AssertionError("Inserting of event was blocked by an extension") elif not issubclass(type(event), OrigEvent): raise ValueError("cannot insert object of type %r" %type(event)) payload_id = self._store_payload (event) # Make sure all URIs are inserted _origin = [subject.origin for subject in event.subjects if subject.origin] if event.origin: _origin.append(event.origin) _current_uri = [subject.current_uri for subject in event.subjects if subject.current_uri] self._cursor.execute("INSERT OR IGNORE INTO uri (value) %s" % " UNION ".join(["SELECT ?"] * (len(event.subjects) + len(_origin) + len(_current_uri))), _origin + [subject.uri for subject in event.subjects] + _current_uri) # Make sure all mimetypes are inserted _mimetype = [subject.mimetype for subject in event.subjects \ if subject.mimetype and not subject.mimetype in self._mimetype] if len(_mimetype) > 1: self._cursor.execute("INSERT OR IGNORE INTO mimetype (value) %s" % " UNION ".join(["SELECT ?"] * len(_mimetype)), _mimetype) # Make sure all texts are inserted _text = [subject.text for subject in event.subjects if subject.text] if _text: self._cursor.execute("INSERT OR IGNORE INTO text (value) %s" % " UNION ".join(["SELECT ?"] * len(_text)), _text) # Make sure all storages are inserted _storage = [subject.storage for subject in event.subjects if subject.storage] if _storage: self._cursor.execute("INSERT OR IGNORE INTO storage (value) %s" % " UNION ".join(["SELECT ?"] * len(_storage)), _storage) try: for subject in event.subjects: self._cursor.execute(""" INSERT INTO event ( id, timestamp, interpretation, manifestation, actor, origin, payload, subj_id, subj_id_current, subj_interpretation, subj_manifestation, subj_origin, subj_mimetype, subj_text, subj_storage ) VALUES ( ?, ?, ?, ?, ?, (SELECT id FROM uri WHERE value=?), ?, (SELECT id FROM uri WHERE value=?), (SELECT id FROM uri WHERE value=?), ?, ?, (SELECT id FROM uri WHERE value=?), ?, (SELECT id FROM text WHERE value=?), (SELECT id from storage WHERE value=?) )""", ( event_id, event.timestamp, self._interpretation[event.interpretation], self._manifestation[event.manifestation], self._actor[event.actor], event.origin, payload_id, subject.uri, subject.current_uri, self._interpretation[subject.interpretation], self._manifestation[subject.manifestation], subject.origin, self._mimetype[subject.mimetype], subject.text, subject.storage)) self.extensions.apply_post_insert(event, sender) except sqlite3.IntegrityError: # The event was already registered. # Rollback _last_event_id and return the ID of the original event self._last_event_id -= 1 self._cursor.execute(""" SELECT id FROM event WHERE timestamp=? AND interpretation=? AND manifestation=? AND actor=? """, (event.timestamp, self._interpretation[event.interpretation], self._manifestation[event.manifestation], self._actor[event.actor])) return self._cursor.fetchone()[0] if event.interpretation == Interpretation.MOVE_EVENT: for subject in event.subjects: self._cursor.execute(""" UPDATE event SET subj_id_current=(SELECT id FROM uri WHERE value=?) WHERE subj_id_current=(SELECT id FROM uri WHERE value=?) AND interpretation!=? AND timestamp<? """, (subject.current_uri, subject.uri, self._interpretation[Interpretation.MOVE_EVENT], event.timestamp)) # The cache has to be updated for _id, c_event in self._event_cache: if c_event.timestamp < event.timestamp: for c_subject in c_event.subjects: if c_subject.current_uri == subject.uri and \ not c_event.interpretation == Interpretation.MOVE_EVENT: c_subject.current_uri = subject.current_uri return event_id