diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2011-12-19 21:56:33 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2011-12-19 21:56:33 +0100 |
commit | ab7c351eeb4c0541f5f4e010e3dc20801d0e6e83 (patch) | |
tree | 53f4c18e2f9933953801715ce2cbccb00f6d3631 | |
parent | e1792e1a4901ab62f140acaafb9b0a115ce1cfb6 (diff) |
Lazily load hooks.
-rw-r--r-- | bzr/__init__.py | 141 | ||||
-rw-r--r-- | bzr/hooks.py | 144 |
2 files changed, 155 insertions, 130 deletions
diff --git a/bzr/__init__.py b/bzr/__init__.py index 7ede025..8b57b47 100644 --- a/bzr/__init__.py +++ b/bzr/__init__.py @@ -30,133 +30,14 @@ Installation: Copy this directory to ~/.bazaar/plugins/zeitgeist/* """ -from bzrlib import ( - branch, - errors, - trace, - urlutils, - ) - -_client = None -_client_checked = None - -def get_client(): - global _client_checked, _client - if _client_checked: - return _client - _client_checked = True - try: - from zeitgeist.client import ZeitgeistClient - except ImportError: - _client = None - return _client - import dbus - try: - _client = ZeitgeistClient() - except dbus.DBusException, e: - trace.warning("zeitgeist: %s. No events will be sent." % e.message) - _client = None - except Exception, e: - trace.warning("Unable to connect to Zeitgeist, won't send events. " - "Reason: '%s'" % e) - _client = None - return _client - - -def subject_for_branch(branch): - from zeitgeist.datamodel import Subject, Interpretation, Manifestation - location = urlutils.unescape_for_display(branch.base, "utf-8").decode("utf-8") - return Subject.new_for_values( - uri=unicode(branch.base), - interpretation=unicode(Interpretation.FOLDER), - manifestation=unicode(Manifestation.FILE_DATA_OBJECT), - text=u"Bazaar branch at %s" % location, - origin=unicode(branch.base), - mimetype="application/x-bzr", - ) - - -def get_actor(): - # FIXME: Allow overriding this by e.g. qbzr and bzr-gtk - return u"application://bzr.desktop" - - -def post_commit(local, master, old_revno, old_revid, new_revno, new_revid): - client = get_client() - if client is None: - return - from zeitgeist.datamodel import Event, Interpretation, Manifestation - revision = master.repository.get_revision(new_revid) - if new_revno == 1: - interpretation = Interpretation.CREATE_EVENT - else: - interpretation = Interpretation.MODIFY_EVENT - _text = _(" Revision no. : ") - _text += str(new_revno) + "\n" - _text += revision.message.rstrip() - - subject = subject_for_branch(master) - event = Event.new_for_values( - timestamp=int(revision.timestamp*1000), - interpretation=unicode(interpretation), - manifestation=unicode(Manifestation.USER_ACTIVITY), - actor=get_actor(), - subjects=[subject,], - ) - client.insert_event(event) - - -def post_pull(pull_result): - client = get_client() - if client is None: - return - from zeitgeist.datamodel import Event, Interpretation, Manifestation - try: - revision = pull_result.master_branch.repository.get_revision( - pull_result.new_revid) - except errors.UnsupportedOperation: - # Some remote repository formats (e.g. git) don't support looking at invidual - # revisions - revision = pull_result.source_branch.repository.get_revision( - pull_result.new_revid) - _text = _("Pulled to revision %s:\n %s") % (pull_result.new_revno, - revision.get_summary()) - subject = subject_for_branch(pull_result.master_branch) - event = Event.new_for_values( - interpretation=unicode(Interpretation.RECEIVE_EVENT), - manifestation=unicode(Manifestation.USER_ACTIVITY), - actor=get_actor(), - subjects=[subject,] - ) - client.insert_event(event) - - -def post_push(push_result): - client = get_client() - if client is None: - return - from zeitgeist.datamodel import Event, Interpretation, Manifestation - try: - revision = push_result.master_branch.repository.get_revision( - push_result.new_revid) - except errors.UnsupportedOperation: - revision = push_result.source_branch.repository.get_revision( - push_result.new_revid) - _text = _("Pushed to revision %s:\n %s") % (push_result.new_revno, - revision.get_summary()) - subject = subject_for_branch(push_result.master_branch) - event = Event.new_for_values( - interpretation=unicode(Interpretation.SEND_EVENT), - manifestation=unicode(Manifestation.USER_ACTIVITY), - actor=get_actor(), - subjects=[subject,] - ) - client.insert_event(event) - - -branch.Branch.hooks.install_named_hook("post_commit", post_commit, - "Zeitgeist dataprovider for bzr") -branch.Branch.hooks.install_named_hook("post_pull", post_pull, - "Zeitgeist dataprovider for bzr") -branch.Branch.hooks.install_named_hook("post_push", post_push, - "Zeitgeist dataprovider for bzr") +from bzrlib import branch + +branch.Branch.hooks.install_lazy_named_hook("post_commit", + "bzrlib.plugins.zeitgeist.hooks", "post_commit", + "Zeitgeist dataprovider for bzr") +branch.Branch.hooks.install_lazy_named_hook("post_pull", + "bzrlib.plugins.zeitgeist.hooks", "post_pull", + "Zeitgeist dataprovider for bzr") +branch.Branch.hooks.install_lazy_named_hook("post_push", + "bzrlib.plugins.zeitgeist.hooks", "post_push", + "Zeitgeist dataprovider for bzr") diff --git a/bzr/hooks.py b/bzr/hooks.py new file mode 100644 index 0000000..f18161a --- /dev/null +++ b/bzr/hooks.py @@ -0,0 +1,144 @@ +# -.- coding: utf-8 -.- + +# Zeitgeist +# +# Copyright © 2009 Markus Korn <thekorn@gmx.de> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from bzrlib import ( + branch, + errors, + trace, + urlutils, + ) + +_client = None +_client_checked = None + +def get_client(): + global _client_checked, _client + if _client_checked: + return _client + _client_checked = True + try: + from zeitgeist.client import ZeitgeistClient + except ImportError: + _client = None + return _client + import dbus + try: + _client = ZeitgeistClient() + except dbus.DBusException, e: + trace.warning("zeitgeist: %s. No events will be sent." % e.message) + _client = None + except Exception, e: + trace.warning("Unable to connect to Zeitgeist, won't send events. " + "Reason: '%s'" % e) + _client = None + return _client + + +def subject_for_branch(branch): + from zeitgeist.datamodel import Subject, Interpretation, Manifestation + location = urlutils.unescape_for_display(branch.base, "utf-8").decode("utf-8") + return Subject.new_for_values( + uri=unicode(branch.base), + interpretation=unicode(Interpretation.FOLDER), + manifestation=unicode(Manifestation.FILE_DATA_OBJECT), + text=u"Bazaar branch at %s" % location, + origin=unicode(branch.base), + mimetype="application/x-bzr", + ) + + +def get_actor(): + # FIXME: Allow overriding this by e.g. qbzr and bzr-gtk + return u"application://bzr.desktop" + + +def post_commit(local, master, old_revno, old_revid, new_revno, new_revid): + client = get_client() + if client is None: + return + from zeitgeist.datamodel import Event, Interpretation, Manifestation + revision = master.repository.get_revision(new_revid) + if new_revno == 1: + interpretation = Interpretation.CREATE_EVENT + else: + interpretation = Interpretation.MODIFY_EVENT + _text = _(" Revision no. : ") + _text += str(new_revno) + "\n" + _text += revision.message.rstrip() + + subject = subject_for_branch(master) + event = Event.new_for_values( + timestamp=int(revision.timestamp*1000), + interpretation=unicode(interpretation), + manifestation=unicode(Manifestation.USER_ACTIVITY), + actor=get_actor(), + subjects=[subject,], + ) + client.insert_event(event) + + +def post_pull(pull_result): + client = get_client() + if client is None: + return + from zeitgeist.datamodel import Event, Interpretation, Manifestation + try: + revision = pull_result.master_branch.repository.get_revision( + pull_result.new_revid) + except errors.UnsupportedOperation: + # Some remote repository formats (e.g. git) don't support looking at invidual + # revisions + revision = pull_result.source_branch.repository.get_revision( + pull_result.new_revid) + _text = _("Pulled to revision %s:\n %s") % (pull_result.new_revno, + revision.get_summary()) + subject = subject_for_branch(pull_result.master_branch) + event = Event.new_for_values( + interpretation=unicode(Interpretation.RECEIVE_EVENT), + manifestation=unicode(Manifestation.USER_ACTIVITY), + actor=get_actor(), + subjects=[subject,] + ) + client.insert_event(event) + + +def post_push(push_result): + client = get_client() + if client is None: + return + from zeitgeist.datamodel import Event, Interpretation, Manifestation + try: + revision = push_result.master_branch.repository.get_revision( + push_result.new_revid) + except errors.UnsupportedOperation: + revision = push_result.source_branch.repository.get_revision( + push_result.new_revid) + _text = _("Pushed to revision %s:\n %s") % (push_result.new_revno, + revision.get_summary()) + subject = subject_for_branch(push_result.master_branch) + event = Event.new_for_values( + interpretation=unicode(Interpretation.SEND_EVENT), + manifestation=unicode(Manifestation.USER_ACTIVITY), + actor=get_actor(), + subjects=[subject,] + ) + client.insert_event(event) + + + |