class ZeitgeistPlugin(PluginClass): plugin_info = { 'name': _('Log events with Zeitgeist'), # T: plugin name 'description': _('Pushes events to the Zeitgeist daemon.'), # T: plugin description 'author': 'Marcel Stimberg', 'help': 'Plugins:Log events with Zeitgeist', } @classmethod def check_dependencies(klass): has_zeitgeist = not ZeitgeistClient is None return has_zeitgeist, [('libzeitgeist', has_zeitgeist, False)] def __init__(self, ui): PluginClass.__init__(self, ui) try: self.zeitgeist_client = ZeitgeistClient() self.zeitgeist_client.register_data_source( 'application://zim.desktop', 'Zim', _('Zim Desktop Wiki'), []) # T: short description of zim except RuntimeError, e: logger.exception( 'Loading zeitgeist client failed, will not log events') self.zeitgeist_client = None
class ZeitgeistPlugin(PluginClass): plugin_info = { 'name': _('Log events with Zeitgeist'), # T: plugin name 'description': _('Pushes events to the Zeitgeist daemon.'), # T: plugin description 'author': 'Marcel Stimberg', 'help': 'Plugins:Log events with Zeitgeist', } @classmethod def check_dependencies(klass): has_zeitgeist = not ZeitgeistClient is None return has_zeitgeist, [('libzeitgeist', has_zeitgeist, False)] def __init__(self, config=None): PluginClass.__init__(self, config) try: self.zeitgeist_client = ZeitgeistClient() self.zeitgeist_client.register_data_source( 'application://zim.desktop', 'Zim', _('Zim Desktop Wiki'), []) # T: short description of zim except RuntimeError as e: logger.exception( 'Loading zeitgeist client failed, will not log events') self.zeitgeist_client = None def create_and_send_event(self, page, event_type): if not self.zeitgeist_client: return if not hasattr(page, 'source') \ or not isinstance(page.source, File): return uri = page.source.uri origin = Gio.File(uri).get_parent().get_uri() text = _('Wiki page: %s') % page.name # T: label for how zim pages show up in the recent files menu, %s is the page name subject = Subject.new_for_values( mimetype='text/x-zim-wiki', uri=uri, origin=origin, interpretation=Interpretation.TEXT_DOCUMENT, manifestation=Manifestation.FILE_DATA_OBJECT, text=text) event = Event.new_for_values(actor='application://zim.desktop', interpretation=event_type, manifestation=Manifestation.USER_ACTIVITY, subjects=[ subject, ]) self.zeitgeist_client.insert_event(event)
def _get_client(self, bus_name, app_uri, app_name, app_description, event_template): client = None try: client = ZeitgeistClient() if hasattr(self.client, "register_data_source"): client.register_data_source( bus_name, app_name or bus_name, app_description or bus_name, event_template or \ [Event.new_for_values(actor=app_uri)]) self.log(logging.DEBUG, 'your client was set') except Exception as e: self.log(logging.ERROR, 'S.O.S: %s', e) raise return client
def _get_client(self, bus_name, app_uri, app_name, app_description, event_template): client = None try: client = ZeitgeistClient() if hasattr(self.client, "register_data_source"): client.register_data_source( bus_name, app_name or bus_name, app_description or bus_name, event_template or \ [Event.new_for_values(actor=app_uri)]) self.log(logging.DEBUG, 'your client was set') except Exception as e: self.log(logging.ERROR, 'S.O.S: %s', e) raise return client
class ZeitgeistPlugin(PluginClass): plugin_info = { 'name': _('Log events with Zeitgeist'), # T: plugin name 'description': _('Pushes events to the Zeitgeist daemon.'), # T: plugin description 'author': 'Marcel Stimberg', 'help': 'Plugins:Log events with Zeitgeist', } @classmethod def check_dependencies(klass): has_zeitgeist = not ZeitgeistClient is None return has_zeitgeist, [('libzeitgeist', has_zeitgeist, False)] def __init__(self, ui): PluginClass.__init__(self, ui) try: self.zeitgeist_client = ZeitgeistClient() self.zeitgeist_client.register_data_source('application://zim.desktop', 'Zim', _('Zim Desktop Wiki'), []) # T: short description of zim except RuntimeError, e: logger.exception('Loading zeitgeist client failed, will not log events') self.zeitgeist_client = None
class ZeitgeistIntegration(plugin.Plugin): ''' Plugin that enables integration with the Zeitgeist Framework. ''' def initialize(self): ''' Inicializes the Zeitgeist client and registers itself as a Zeitgeist data source. ''' self.logger.info('Initialiazing zeitgeist plugin') editor = self.locator.get_service('editor') editor.fileOpened.connect(self._zeitgeist_log_file_open) editor.fileSaved.connect(self._zeitgeist_log_file_modified) #initialize zeitgeist client self.zeitgeist = ZeitgeistClient() self._register_data_source() def finish(self): ''' Deletes the reference to the Zeitgeist client. ''' # remove zeitgeist client del self.zeitgeist self.logger.info('Shutting down zeitgeist plugin') def _register_data_source(self): ''' Registers the plugin as a Zeitgeist data source. ''' unique_id = 'org.ninja.ide' name = 'Ninja IDE' description = 'Very versatile Python IDE' # Describe what sort of events will be inserted templates = [] for interp in (Interpretation.EVENT_INTERPRETATION.ACCESS_EVENT, Interpretation.EVENT_INTERPRETATION.MODIFY_EVENT, Interpretation.EVENT_INTERPRETATION.LEAVE_EVENT, Interpretation.EVENT_INTERPRETATION.CREATE_EVENT): event_template = Event() event_template.interpretation = interp event_template.manifestation = Manifestation.USER_ACTIVITY for subj_interp in (Interpretation.DOCUMENT.TEXT_DOCUMENT. PLAIN_TEXT_DOCUMENT.SOURCE_CODE, Interpretation.DOCUMENT.TEXT_DOCUMENT.PLAIN_TEXT_DOCUMENT): subject_template = Subject() subject_template.interpretation = subj_interp subject_template.manifestation = Manifestation.FILE_DATA_OBJECT event_template.append_subject(subject_template) templates.append(event_template) self.zeitgeist.register_data_source(unique_id, name, description, templates) def _zeitgeist_log_event(self, fileName, interpretation): ''' Registers an event over the file with a given interpretation. ''' fileName = unicode(fileName) self.logger.info('Inserting event for %s' % fileName) subject = Subject.new_for_values( uri='file://%s' % fileName, origin='file://%s' % path.dirname(fileName), text=path.basename(fileName)) event = Event.new_for_values( timestamp=int(time.time() * 1000), interpretation=interpretation, manifestation=Manifestation.USER_ACTIVITY, actor='application://ninja-ide.desktop', subjects=[subject]) def on_id_received(event_ids): self.logger.info( 'Logged %r with event id %d.' % (fileName, event_ids[0])) self.zeitgeist.insert_events([event], on_id_received) def _zeitgeist_log_file_open(self, fileName): ''' Registers an event everytime Ninja IDE opens a file. ''' self._zeitgeist_log_event(unicode(fileName), Interpretation.EVENT_INTERPRETATION.ACCESS_EVENT) def _zeitgeist_log_file_modified(self, fileName): ''' Registers an event everytime Ninja IDE modifies a file. ''' self._zeitgeist_log_event(unicode(fileName), Interpretation.EVENT_INTERPRETATION.MODIFY_EVENT)
class Zeitgeist(modules.ThreadedModule): def __init__(self): """ Constructor """ handlers = { consts.MSG_EVT_APP_QUIT: self.onModUnloaded, consts.MSG_EVT_NEW_TRACK: self.onNewTrack, consts.MSG_EVT_MOD_LOADED: self.onModLoaded, consts.MSG_EVT_APP_STARTED: self.onModLoaded, consts.MSG_EVT_MOD_UNLOADED: self.onModUnloaded, } modules.ThreadedModule.__init__(self, handlers) # --== Message handlers ==-- def onModLoaded(self): """ The module has been loaded """ self.client = None try: from zeitgeist.client import ZeitgeistClient from zeitgeist.datamodel import Event self.client = ZeitgeistClient() if self.client.get_version() >= [0, 3, 2, 999]: self.client.register_data_source("Pogo", "Pogo", "Play your music", [Event.new_for_values(actor="application://pogo.desktop")]) except: logger.info('[%s] Could not create Zeitgeist client\n\n%s' % (MOD_INFO[modules.MODINFO_NAME], traceback.format_exc())) def onModUnloaded(self): """ The module has been unloaded """ self.client = None def onNewTrack(self, track): """ Send track information to Zeitgeist """ if self.client is None: return from zeitgeist.datamodel import Interpretation if hasattr(Interpretation, 'ACCESS_EVENT'): event_type = Interpretation.ACCESS_EVENT else: event_type = Interpretation.OPEN_EVENT self.send_to_zeitgeist(track, event_type) def send_to_zeitgeist(self, track, event_type): """ Other players (e.g. Rhythmbox) log the playing of individual files, but we want to log albums. Maybe it would be better to log individual files, but then we would have to distinguish between Manifestation.USER_ACTIVITY and Manifestation.SCHEDULED_ACTIVITY. Another possible addition would be logging Interpretation.LEAVE_EVENT. """ import mimetypes, os.path from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation mime, encoding = mimetypes.guess_type(track.getFilePath(), strict=False) title = track.getTitle() album = track.getAlbum() artist = track.getArtist() uri = track.getURI() origin = os.path.dirname(uri) # Handle "unknown" tags if 'unknown' in title.lower(): title = track.getBasename() if 'unknown' in album.lower(): album = '' if 'unknown' in artist.lower(): artist = '' subject = Subject.new_for_values( uri = origin, text = ' - '.join([part for part in [artist, album] if part]), mimetype = mime, manifestation = unicode(Manifestation.FILE_DATA_OBJECT), interpretation = unicode(Interpretation.AUDIO), origin = origin, ) event = Event.new_for_values( actor = "application://pogo.desktop", subjects = [subject,], interpretation = unicode(event_type), timestamp = int(time.time() * 1000), ) self.client.insert_event(event)