Merge lp:~pitti/ubuntuone-client/gi-fixes into lp:ubuntuone-client

Proposed by Martin Pitt on 2011-08-22
Status: Merged
Approved by: Natalia Bidart on 2011-08-24
Approved revision: 1113
Merged at revision: 1112
Proposed branch: lp:~pitti/ubuntuone-client/gi-fixes
Merge into: lp:ubuntuone-client
Diff against target: 131 lines (+58/-25)
2 files modified
ubuntuone/platform/linux/notification.py (+28/-14)
ubuntuone/platform/linux/os_helper.py (+30/-11)
To merge this branch: bzr merge lp:~pitti/ubuntuone-client/gi-fixes
Reviewer Review Type Date Requested Status
Natalia Bidart 2011-08-22 Approve on 2011-08-24
Martin Pitt (community) Resubmit on 2011-08-24
Facundo Batista Approve on 2011-08-23
Review via email: mp+72447@code.launchpad.net

Commit message

Conditionally use GI or static gobject depending on whether gobject is already loaded. That way this module works with both static and GI programs. (LP: #829186)

Description of the change

pygobject >= 2.90 is now absolutely zero tolerant against importing both the
static and the GI version of a particular library. This was mostly the case
with 2.28 as well, but did work in some cases (like "import gobject; from
gi.repository import Gtk", in particular for "glib" and "gobject"). These now
cause errors as well.

With this fix we can use the library from both programs with static bindings
(gobject, gtk), as well as from programs which use the gobject-introspection
bindings.

See bug 829186 for more details.

To post a comment you must log in.
Facundo Batista (facundo) wrote :

Can't we leave the latest way only?

I mean, only do...

    from gi.repository import Gio, GLib

...and use that.

Thanks!

review: Needs Information
Martin Pitt (pitti) wrote :

Unfortunately not. The conditional static/GI import needs to stay until all users of python-ubuntone are converted to use GI. With pygobject >= 2.90 you cannot mix static and GI imports, and the ubuntuone-control-panel is still using pygtk, gobject, etc. See the description of bug 828751 for the background why we have to do this.

For the record, I also already had the test suite of this ported to GI. However, that does not work, as the test suite uses twisted, which uses static gtk/gobject modules. So I'm afraid for the time being the test suite can only check the static part here, not the GI part.

lp:~pitti/ubuntuone-client/gi-fixes updated on 2011-08-23
1112. By Martin Pitt on 2011-08-23

ubuntuone/platform/linux/notification.py: Use GI Notify module when using GI

Martin Pitt (pitti) wrote :

I also fixed the notification module to use gi.repository.Notify when the process is already using GI. Otherwise pynotify once again pulls in the static gobject.

Facundo Batista (facundo) wrote :

Go for it

review: Approve
Natalia Bidart (nataliabidart) wrote :

Code looks good, but there is a link issue here:

./ubuntuone/platform/linux/notification.py:
    34: redefinition of unused 'Notify' from line 28

To run the suite, please do the following:

./autogen.sh
make check

Thanks!

review: Needs Fixing
Martin Pitt (pitti) wrote :

I fixed the pyflakes complaint.

review: Resubmit
lp:~pitti/ubuntuone-client/gi-fixes updated on 2011-08-24
1113. By Martin Pitt on 2011-08-24

work around pyflakes complaint

Natalia Bidart (nataliabidart) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntuone/platform/linux/notification.py'
2--- ubuntuone/platform/linux/notification.py 2011-03-03 23:29:37 +0000
3+++ ubuntuone/platform/linux/notification.py 2011-08-24 10:39:28 +0000
4@@ -21,18 +21,27 @@
5 # are available, we should fall back to silently discarding
6 # notifications.
7
8-try:
9- import pynotify
10- USE_PYNOTIFY = True
11-except ImportError:
12- USE_PYNOTIFY = False
13+import sys
14+NOTIFY_MODULE = None
15+if 'gi' in sys.modules:
16+ try:
17+ from gi.repository import Notify
18+ Notify # pyflakes
19+ NOTIFY_MODULE = 'gi'
20+ except ImportError:
21+ pass
22+else:
23+ try:
24+ import pynotify as Notify
25+ NOTIFY_MODULE = 'pynotify'
26+ except ImportError:
27+ pass
28
29 from ubuntuone.status.notification import AbstractNotification
30
31 APPLICATION_NAME = 'Ubuntu One Client'
32 ICON_NAME = "ubuntuone"
33
34-
35 class Notification(AbstractNotification):
36 """Notification of the end user."""
37
38@@ -44,12 +53,17 @@
39
40 def send_notification(self, title, message, icon=ICON_NAME, append=False):
41 """Send a notification using the underlying library."""
42- if USE_PYNOTIFY:
43- if self.notification is None:
44- pynotify.init(self.application_name)
45- self.notification = pynotify.Notification(title, message, icon)
46+ if not NOTIFY_MODULE:
47+ return
48+
49+ if self.notification is None:
50+ Notify.init(self.application_name)
51+ if NOTIFY_MODULE == 'gi':
52+ self.notification = Notify.Notification.new(title, message, icon)
53 else:
54- self.notification.update(title, message, icon)
55- if append:
56- self.notification.set_hint_string('x-canonical-append', '')
57- self.notification.show()
58+ self.notification = Notify.Notification(title, message, icon)
59+ else:
60+ self.notification.update(title, message, icon)
61+ if append:
62+ self.notification.set_hint_string('x-canonical-append', '')
63+ self.notification.show()
64
65=== modified file 'ubuntuone/platform/linux/os_helper.py'
66--- ubuntuone/platform/linux/os_helper.py 2011-08-17 20:20:21 +0000
67+++ ubuntuone/platform/linux/os_helper.py 2011-08-24 10:39:28 +0000
68@@ -27,8 +27,14 @@
69 import os
70 import shutil
71 import stat
72+import sys
73
74-import gio
75+if 'gobject' in sys.modules:
76+ import gio
77+ has_gi = False
78+else:
79+ from gi.repository import Gio, GLib
80+ has_gi = True
81
82 from contextlib import contextmanager
83
84@@ -164,14 +170,24 @@
85
86 If had any error, or the system can't do it, just remove it.
87 """
88- try:
89- return_code = gio.File(path).trash()
90- except gio.Error:
91- exc = OSError()
92- exc.errno = errno.ENOENT
93- raise exc
94+ if has_gi:
95+ not_supported = Gio.IOErrorEnum.NOT_SUPPORTED
96+ try:
97+ return_code = Gio.File.new_for_path(path).trash(None)
98+ except GLib.GError:
99+ exc = OSError()
100+ exc.errno = errno.ENOENT
101+ raise exc
102+ else:
103+ not_supported = gio.ERROR_NOT_SUPPORTED
104+ try:
105+ return_code = gio.File(path).trash()
106+ except gio.Error:
107+ exc = OSError()
108+ exc.errno = errno.ENOENT
109+ raise exc
110
111- if not return_code or return_code == gio.ERROR_NOT_SUPPORTED:
112+ if not return_code or return_code == not_supported:
113 logger.warning("Problems moving to trash! (%s) Removing anyway: %r",
114 return_code, path)
115 if os.path.isdir(path):
116@@ -182,9 +198,12 @@
117
118 def set_application_name(app_name):
119 """Set the name of the application."""
120- import gobject
121- gobject.set_application_name(app_name)
122-
123+ if has_gi:
124+ from gi.repository import GObject
125+ GObject.set_application_name(app_name)
126+ else:
127+ import gobject
128+ gobject.set_application_name(app_name)
129
130 def is_root():
131 """Return if the user is running as root."""

Subscribers

People subscribed via source and target branches