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
=== added directory 'tests/eventlog'
=== added file 'tests/eventlog/__init__.py'
--- tests/eventlog/__init__.py 1970-01-01 00:00:00 +0000
+++ tests/eventlog/__init__.py 2010-12-03 12:41:45 +0000
@@ -0,0 +1,14 @@
1# Copyright 2010 Canonical Ltd.
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License version 3, as published
5# by the Free Software Foundation.
6#
7# This program is distributed in the hope that it will be useful, but
8# WITHOUT ANY WARRANTY; without even the implied warranties of
9# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10# PURPOSE. See the GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License along
13# with this program. If not, see <http://www.gnu.org/licenses/>.
14"""Tests module."""
015
=== added file 'tests/eventlog/test_zglog.py'
--- tests/eventlog/test_zglog.py 1970-01-01 00:00:00 +0000
+++ tests/eventlog/test_zglog.py 2010-12-03 12:41:45 +0000
@@ -0,0 +1,155 @@
1# -*- coding: utf-8 -*-
2#
3# Author: Alejandro J. Cura <alecu@canonical.com>
4#
5# Copyright 2010 Canonical Ltd.
6#
7# This program is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 3, as published
9# by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranties of
13# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14# PURPOSE. See the GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program. If not, see <http://www.gnu.org/licenses/>.
18"""Tests for the Zeitgeist logging."""
19
20import os
21import shutil
22import subprocess
23import tempfile
24import time
25
26from distutils.spawn import find_executable
27
28from twisted.internet.defer import Deferred
29from zeitgeist.client import ZeitgeistClient
30from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
31
32from tests.platform.linux.test_dbus import DBusTwistedTestCase
33from ubuntuone.eventlog.zglog import ZeitgeistLogger
34
35SRCDIR = os.environ.get('SRCDIR', os.getcwd())
36
37
38class NotFoundError(Exception):
39 """Not found error."""
40
41
42class ZeitgeistNotStartedTests(DBusTwistedTestCase):
43 """Tests for the zeitgeist logging module."""
44
45 def test_log_does_not_err_when_daemon_not_started(self):
46 """zeitgeist-daemon was not started."""
47 timestamp = int(time.time() * 1000)
48 subject = Subject.new_for_values(
49 uri="file:///tmp/folder1",
50 interpretation=Interpretation.FOLDER,
51 manifestation=Manifestation.FILE_DATA_OBJECT,
52 origin="ubuntuone:uuid",
53 mimetype="inode/directory",
54 text="sample folder"
55 )
56 sample_event = Event.new_for_values(
57 timestamp=timestamp,
58 interpretation=Interpretation.ACCESS_EVENT,
59 manifestation=Manifestation.USER_ACTIVITY,
60 actor="mailto:sample_subject",
61 subjects=[subject]
62 )
63
64 zg = ZeitgeistLogger()
65 d = zg.log(sample_event)
66 self.assertEqual(zg.client, None)
67
68 def verify(result):
69 """Stored result is the empty list, because zg not available."""
70 self.assertEqual(result, [])
71 return result
72
73 d.addCallback(verify)
74 return d
75
76def wait_zeitgeist_started(seconds=10):
77 """Wait a few seconds until zg is started, or fail if it can't."""
78 client = None
79 count = 0
80 while client is None:
81 count += 1
82 try:
83 client = ZeitgeistClient()
84 break
85 except RuntimeError:
86 if count > seconds*10:
87 raise
88 time.sleep(0.1)
89
90
91class ZeitgeistTestCase(DBusTwistedTestCase):
92 """Tests for the zeitgeist logging module."""
93
94 def setUp(self):
95 super(ZeitgeistTestCase, self).setUp()
96 zgdaemon = find_executable("zeitgeist-daemon")
97 if not zgdaemon:
98 raise NotFoundError("zeitgeist-daemon was not found.")
99
100 tempfolder = tempfile.mkdtemp(prefix="test-u1-zeitgeist-")
101 tempstdout = tempfile.TemporaryFile(prefix="test-u1-stdout-")
102 tempstderr = tempfile.TemporaryFile(prefix="test-u1-stderr-")
103 os.environ["ZEITGEIST_DATA_PATH"] = tempfolder
104 p = subprocess.Popen([zgdaemon], bufsize=4096, stdout=tempstdout,
105 stderr=tempstderr)
106 def cleanup():
107 """Wait for the process to finish."""
108 p.terminate()
109 p.wait()
110 del(os.environ["ZEITGEIST_DATA_PATH"])
111 shutil.rmtree(tempfolder)
112
113 wait_zeitgeist_started()
114 self.addCleanup(cleanup)
115
116 def test_log_records_the_event(self):
117 """The log method records the event in zg."""
118 timestamp = int(time.time() * 1000)
119 subject = Subject.new_for_values(
120 uri="file:///tmp/folder1",
121 interpretation=Interpretation.FOLDER,
122 manifestation=Manifestation.FILE_DATA_OBJECT,
123 origin="ubuntuone:uuid",
124 mimetype="inode/directory",
125 text="sample folder"
126 )
127 sample_event = Event.new_for_values(
128 timestamp=timestamp,
129 interpretation=Interpretation.ACCESS_EVENT,
130 manifestation=Manifestation.USER_ACTIVITY,
131 actor="mailto:sample_subject",
132 subjects=[subject]
133 )
134
135 sample_template = Event.new_for_values()
136
137 zg = ZeitgeistLogger()
138 self.assertNotEqual(zg.client, None)
139
140 d2 = Deferred()
141
142 def logged(id_list):
143 """The event was logged to zeitgeist."""
144
145 def events_found(event_list):
146 """zg returned the list of events."""
147 self.assertEqual(event_list[0].id, id_list[0])
148 d2.callback("ok")
149
150 zg.client.find_events_for_template(sample_template, events_found)
151
152 d = zg.log(sample_event)
153 d.addCallbacks(logged, d2.errback)
154
155 return d2
0156
=== modified file 'tests/syncdaemon/__init__.py'
--- tests/syncdaemon/__init__.py 2009-05-12 13:36:05 +0000
+++ tests/syncdaemon/__init__.py 2010-12-03 12:41:45 +0000
@@ -11,4 +11,4 @@
11#11#
12# You should have received a copy of the GNU General Public License along12# You should have received a copy of the GNU General Public License along
13# with this program. If not, see <http://www.gnu.org/licenses/>.13# with this program. If not, see <http://www.gnu.org/licenses/>.
14""" Tests module """14"""Tests module."""
1515
=== added directory 'ubuntuone/eventlog'
=== added file 'ubuntuone/eventlog/__init__.py'
--- ubuntuone/eventlog/__init__.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/eventlog/__init__.py 2010-12-03 12:41:45 +0000
@@ -0,0 +1,16 @@
1# ubuntuone.eventlog - Ubuntu One event logging modules
2#
3# Copyright 2010 Canonical Ltd.
4#
5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranties of
11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12# PURPOSE. See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program. If not, see <http://www.gnu.org/licenses/>.
16"""Event logging module."""
017
=== added file 'ubuntuone/eventlog/zglog.py'
--- ubuntuone/eventlog/zglog.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/eventlog/zglog.py 2010-12-03 12:41:45 +0000
@@ -0,0 +1,48 @@
1# -*- coding: utf-8 -*-
2#
3# Author: Alejandro J. Cura <alecu@canonical.com>
4#
5# Copyright 2010 Canonical Ltd.
6#
7# This program is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 3, as published
9# by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranties of
13# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14# PURPOSE. See the GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program. If not, see <http://www.gnu.org/licenses/>.
18"""Log into the Zeitgeist daemon."""
19
20from twisted.internet.defer import Deferred
21
22from ubuntuone.logger import logging
23
24logger = logging.getLogger('ubuntuone.eventlog.zglog')
25
26
27class ZeitgeistLogger(object):
28 """A class that logs zeitgeist events."""
29 client = None
30
31 def __init__(self):
32 """Initialize this instance."""
33 try:
34 from zeitgeist.client import ZeitgeistClient
35 self.client = ZeitgeistClient()
36 logger.info("Zeitgeist support initialized.")
37 except Exception:
38 logger.exception("Zeitgeist support not started:")
39
40 def log(self, event):
41 """Log a zeitgeist event."""
42 d = Deferred()
43 if self.client:
44 logger.info("Logging Zeitgeist event: %r", event)
45 self.client.insert_event(event, d.callback, d.errback)
46 else:
47 d.callback([])
48 return d
049
=== modified file 'ubuntuone/syncdaemon/__init__.py'
--- ubuntuone/syncdaemon/__init__.py 2010-07-23 14:52:03 +0000
+++ ubuntuone/syncdaemon/__init__.py 2010-12-03 12:41:45 +0000
@@ -13,7 +13,7 @@
13#13#
14# You should have received a copy of the GNU General Public License along14# You should have received a copy of the GNU General Public License along
15# with this program. If not, see <http://www.gnu.org/licenses/>.15# with this program. If not, see <http://www.gnu.org/licenses/>.
16""" client module """16"""Client module."""
1717
18# required capabilities18# required capabilities
19REQUIRED_CAPS = frozenset(["no-content",19REQUIRED_CAPS = frozenset(["no-content",

Subscribers

People subscribed via source and target branches