Merge lp:~alecu/ubuntuone-client/add-simple-zeitgeist into lp:ubuntuone-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: John Lenton
Approved revision: 755
Merged at revision: 764
Proposed branch: lp:~alecu/ubuntuone-client/add-simple-zeitgeist
Merge into: lp:ubuntuone-client
Diff against target: 277 lines (+235/-2)
6 files modified
tests/eventlog/__init__.py (+14/-0)
tests/eventlog/test_zglog.py (+155/-0)
tests/syncdaemon/__init__.py (+1/-1)
ubuntuone/eventlog/__init__.py (+16/-0)
ubuntuone/eventlog/zglog.py (+48/-0)
ubuntuone/syncdaemon/__init__.py (+1/-1)
To merge this branch: bzr merge lp:~alecu/ubuntuone-client/add-simple-zeitgeist
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Natalia Bidart (community) Approve
Review via email: mp+41667@code.launchpad.net

Commit message

A twisted class to log zeitgeist events

Description of the change

A twisted class to log zeitgeist events

To post a comment you must log in.
752. By Alejandro J. Cura

renamed the logging module

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Tests will not run due to:

== Python Lint Notices ==

./tests/eventlog/test_zglog.py:
    94: undefined name 'NotFoundError'

* Please remove spaces from docstring and add ending dot and initial upper case for:

""" Tests module """
""" event logging module """

* Is this correct? stdout has an extra -

121 + tempstdout = tempfile.TemporaryFile(prefix="test-u1-stdout-")
122 + tempstderr = tempfile.TemporaryFile(prefix="test-u1-stderr")

* This code:

233 + except RuntimeError, e:
234 + print e

should use the ubuntuone.logger and log the exception

review: Needs Fixing
753. By Alejandro J. Cura

fixes requested on review

754. By Alejandro J. Cura

merged with trunk

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Great!

review: Approve
Revision history for this message
John Lenton (chipaca) wrote :

Like it!

But...

Could you change the strings in zlog.py from "blah""" to just "blah"? I didn't even know the former was valid python, and it looks horrible.

review: Needs Fixing
Revision history for this message
Alejandro J. Cura (alecu) wrote :

> Could you change the strings in zlog.py from "blah""" to just "blah"? I didn't
> even know the former was valid python, and it looks horrible.

Yes, yes it is, and yes it looks horrible.

Revision history for this message
Alejandro J. Cura (alecu) wrote :

Fixed and pushed. Good catch!

755. By Alejandro J. Cura

Fix horrible looking strings.

Revision history for this message
Nicola Larosa (teknico) wrote :

> Could you change the strings in zlog.py from "blah""" to just "blah"? I didn't
> even know the former was valid python, and it looks horrible.

It works because they are interpreted as two strings: "blah" and "" (empty), and then they are merged into one by implicit string concatenation, since they are in a parenthesized context.

Revision history for this message
Alejandro J. Cura (alecu) wrote :

> > Could you change the strings in zlog.py from "blah""" to just "blah"? I
> didn't
> > even know the former was valid python, and it looks horrible.
>
> It works because they are interpreted as two strings: "blah" and "" (empty),
> and then they are merged into one by implicit string concatenation, since they
> are in a parenthesized context.

Yeah, we discussed about this in PyAr a while ago, and I still find that it's ugly that this is valid python:
   print "abc"""

And that this is not:
   print """abc"

I think that implicit string concatenation in python *should* mandate a space between strings, but I guess it's a bit late to change the interpreter and third part parsers, and such.

Also, I think that in this case parenthesized context does not matters.

Revision history for this message
John Lenton (chipaca) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'tests/eventlog'
2=== added file 'tests/eventlog/__init__.py'
3--- tests/eventlog/__init__.py 1970-01-01 00:00:00 +0000
4+++ tests/eventlog/__init__.py 2010-12-03 12:41:45 +0000
5@@ -0,0 +1,14 @@
6+# Copyright 2010 Canonical Ltd.
7+#
8+# This program is free software: you can redistribute it and/or modify it
9+# under the terms of the GNU General Public License version 3, as published
10+# by the Free Software Foundation.
11+#
12+# This program is distributed in the hope that it will be useful, but
13+# WITHOUT ANY WARRANTY; without even the implied warranties of
14+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15+# PURPOSE. See the GNU General Public License for more details.
16+#
17+# You should have received a copy of the GNU General Public License along
18+# with this program. If not, see <http://www.gnu.org/licenses/>.
19+"""Tests module."""
20
21=== added file 'tests/eventlog/test_zglog.py'
22--- tests/eventlog/test_zglog.py 1970-01-01 00:00:00 +0000
23+++ tests/eventlog/test_zglog.py 2010-12-03 12:41:45 +0000
24@@ -0,0 +1,155 @@
25+# -*- coding: utf-8 -*-
26+#
27+# Author: Alejandro J. Cura <alecu@canonical.com>
28+#
29+# Copyright 2010 Canonical Ltd.
30+#
31+# This program is free software: you can redistribute it and/or modify it
32+# under the terms of the GNU General Public License version 3, as published
33+# by the Free Software Foundation.
34+#
35+# This program is distributed in the hope that it will be useful, but
36+# WITHOUT ANY WARRANTY; without even the implied warranties of
37+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
38+# PURPOSE. See the GNU General Public License for more details.
39+#
40+# You should have received a copy of the GNU General Public License along
41+# with this program. If not, see <http://www.gnu.org/licenses/>.
42+"""Tests for the Zeitgeist logging."""
43+
44+import os
45+import shutil
46+import subprocess
47+import tempfile
48+import time
49+
50+from distutils.spawn import find_executable
51+
52+from twisted.internet.defer import Deferred
53+from zeitgeist.client import ZeitgeistClient
54+from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
55+
56+from tests.platform.linux.test_dbus import DBusTwistedTestCase
57+from ubuntuone.eventlog.zglog import ZeitgeistLogger
58+
59+SRCDIR = os.environ.get('SRCDIR', os.getcwd())
60+
61+
62+class NotFoundError(Exception):
63+ """Not found error."""
64+
65+
66+class ZeitgeistNotStartedTests(DBusTwistedTestCase):
67+ """Tests for the zeitgeist logging module."""
68+
69+ def test_log_does_not_err_when_daemon_not_started(self):
70+ """zeitgeist-daemon was not started."""
71+ timestamp = int(time.time() * 1000)
72+ subject = Subject.new_for_values(
73+ uri="file:///tmp/folder1",
74+ interpretation=Interpretation.FOLDER,
75+ manifestation=Manifestation.FILE_DATA_OBJECT,
76+ origin="ubuntuone:uuid",
77+ mimetype="inode/directory",
78+ text="sample folder"
79+ )
80+ sample_event = Event.new_for_values(
81+ timestamp=timestamp,
82+ interpretation=Interpretation.ACCESS_EVENT,
83+ manifestation=Manifestation.USER_ACTIVITY,
84+ actor="mailto:sample_subject",
85+ subjects=[subject]
86+ )
87+
88+ zg = ZeitgeistLogger()
89+ d = zg.log(sample_event)
90+ self.assertEqual(zg.client, None)
91+
92+ def verify(result):
93+ """Stored result is the empty list, because zg not available."""
94+ self.assertEqual(result, [])
95+ return result
96+
97+ d.addCallback(verify)
98+ return d
99+
100+def wait_zeitgeist_started(seconds=10):
101+ """Wait a few seconds until zg is started, or fail if it can't."""
102+ client = None
103+ count = 0
104+ while client is None:
105+ count += 1
106+ try:
107+ client = ZeitgeistClient()
108+ break
109+ except RuntimeError:
110+ if count > seconds*10:
111+ raise
112+ time.sleep(0.1)
113+
114+
115+class ZeitgeistTestCase(DBusTwistedTestCase):
116+ """Tests for the zeitgeist logging module."""
117+
118+ def setUp(self):
119+ super(ZeitgeistTestCase, self).setUp()
120+ zgdaemon = find_executable("zeitgeist-daemon")
121+ if not zgdaemon:
122+ raise NotFoundError("zeitgeist-daemon was not found.")
123+
124+ tempfolder = tempfile.mkdtemp(prefix="test-u1-zeitgeist-")
125+ tempstdout = tempfile.TemporaryFile(prefix="test-u1-stdout-")
126+ tempstderr = tempfile.TemporaryFile(prefix="test-u1-stderr-")
127+ os.environ["ZEITGEIST_DATA_PATH"] = tempfolder
128+ p = subprocess.Popen([zgdaemon], bufsize=4096, stdout=tempstdout,
129+ stderr=tempstderr)
130+ def cleanup():
131+ """Wait for the process to finish."""
132+ p.terminate()
133+ p.wait()
134+ del(os.environ["ZEITGEIST_DATA_PATH"])
135+ shutil.rmtree(tempfolder)
136+
137+ wait_zeitgeist_started()
138+ self.addCleanup(cleanup)
139+
140+ def test_log_records_the_event(self):
141+ """The log method records the event in zg."""
142+ timestamp = int(time.time() * 1000)
143+ subject = Subject.new_for_values(
144+ uri="file:///tmp/folder1",
145+ interpretation=Interpretation.FOLDER,
146+ manifestation=Manifestation.FILE_DATA_OBJECT,
147+ origin="ubuntuone:uuid",
148+ mimetype="inode/directory",
149+ text="sample folder"
150+ )
151+ sample_event = Event.new_for_values(
152+ timestamp=timestamp,
153+ interpretation=Interpretation.ACCESS_EVENT,
154+ manifestation=Manifestation.USER_ACTIVITY,
155+ actor="mailto:sample_subject",
156+ subjects=[subject]
157+ )
158+
159+ sample_template = Event.new_for_values()
160+
161+ zg = ZeitgeistLogger()
162+ self.assertNotEqual(zg.client, None)
163+
164+ d2 = Deferred()
165+
166+ def logged(id_list):
167+ """The event was logged to zeitgeist."""
168+
169+ def events_found(event_list):
170+ """zg returned the list of events."""
171+ self.assertEqual(event_list[0].id, id_list[0])
172+ d2.callback("ok")
173+
174+ zg.client.find_events_for_template(sample_template, events_found)
175+
176+ d = zg.log(sample_event)
177+ d.addCallbacks(logged, d2.errback)
178+
179+ return d2
180
181=== modified file 'tests/syncdaemon/__init__.py'
182--- tests/syncdaemon/__init__.py 2009-05-12 13:36:05 +0000
183+++ tests/syncdaemon/__init__.py 2010-12-03 12:41:45 +0000
184@@ -11,4 +11,4 @@
185 #
186 # You should have received a copy of the GNU General Public License along
187 # with this program. If not, see <http://www.gnu.org/licenses/>.
188-""" Tests module """
189+"""Tests module."""
190
191=== added directory 'ubuntuone/eventlog'
192=== added file 'ubuntuone/eventlog/__init__.py'
193--- ubuntuone/eventlog/__init__.py 1970-01-01 00:00:00 +0000
194+++ ubuntuone/eventlog/__init__.py 2010-12-03 12:41:45 +0000
195@@ -0,0 +1,16 @@
196+# ubuntuone.eventlog - Ubuntu One event logging modules
197+#
198+# Copyright 2010 Canonical Ltd.
199+#
200+# This program is free software: you can redistribute it and/or modify it
201+# under the terms of the GNU General Public License version 3, as published
202+# by the Free Software Foundation.
203+#
204+# This program is distributed in the hope that it will be useful, but
205+# WITHOUT ANY WARRANTY; without even the implied warranties of
206+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
207+# PURPOSE. See the GNU General Public License for more details.
208+#
209+# You should have received a copy of the GNU General Public License along
210+# with this program. If not, see <http://www.gnu.org/licenses/>.
211+"""Event logging module."""
212
213=== added file 'ubuntuone/eventlog/zglog.py'
214--- ubuntuone/eventlog/zglog.py 1970-01-01 00:00:00 +0000
215+++ ubuntuone/eventlog/zglog.py 2010-12-03 12:41:45 +0000
216@@ -0,0 +1,48 @@
217+# -*- coding: utf-8 -*-
218+#
219+# Author: Alejandro J. Cura <alecu@canonical.com>
220+#
221+# Copyright 2010 Canonical Ltd.
222+#
223+# This program is free software: you can redistribute it and/or modify it
224+# under the terms of the GNU General Public License version 3, as published
225+# by the Free Software Foundation.
226+#
227+# This program is distributed in the hope that it will be useful, but
228+# WITHOUT ANY WARRANTY; without even the implied warranties of
229+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
230+# PURPOSE. See the GNU General Public License for more details.
231+#
232+# You should have received a copy of the GNU General Public License along
233+# with this program. If not, see <http://www.gnu.org/licenses/>.
234+"""Log into the Zeitgeist daemon."""
235+
236+from twisted.internet.defer import Deferred
237+
238+from ubuntuone.logger import logging
239+
240+logger = logging.getLogger('ubuntuone.eventlog.zglog')
241+
242+
243+class ZeitgeistLogger(object):
244+ """A class that logs zeitgeist events."""
245+ client = None
246+
247+ def __init__(self):
248+ """Initialize this instance."""
249+ try:
250+ from zeitgeist.client import ZeitgeistClient
251+ self.client = ZeitgeistClient()
252+ logger.info("Zeitgeist support initialized.")
253+ except Exception:
254+ logger.exception("Zeitgeist support not started:")
255+
256+ def log(self, event):
257+ """Log a zeitgeist event."""
258+ d = Deferred()
259+ if self.client:
260+ logger.info("Logging Zeitgeist event: %r", event)
261+ self.client.insert_event(event, d.callback, d.errback)
262+ else:
263+ d.callback([])
264+ return d
265
266=== modified file 'ubuntuone/syncdaemon/__init__.py'
267--- ubuntuone/syncdaemon/__init__.py 2010-07-23 14:52:03 +0000
268+++ ubuntuone/syncdaemon/__init__.py 2010-12-03 12:41:45 +0000
269@@ -13,7 +13,7 @@
270 #
271 # You should have received a copy of the GNU General Public License along
272 # with this program. If not, see <http://www.gnu.org/licenses/>.
273-""" client module """
274+"""Client module."""
275
276 # required capabilities
277 REQUIRED_CAPS = frozenset(["no-content",

Subscribers

People subscribed via source and target branches