Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugin/bundleregistry.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/bundleregistry.py')
-rw-r--r--plugin/bundleregistry.py117
1 files changed, 57 insertions, 60 deletions
diff --git a/plugin/bundleregistry.py b/plugin/bundleregistry.py
index 8707d5f..20860da 100644
--- a/plugin/bundleregistry.py
+++ b/plugin/bundleregistry.py
@@ -13,17 +13,17 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import os
import shutil
import logging
-from os.path import lexists
from gettext import gettext as _
import gtk
import gobject
from pylru import lrucache
-from sugar_network import checkins, sugar
+from sugar import util
+
+from sugar_network import clones
from sugar.bundle.activitybundle import ActivityBundle
from sugar.bundle.contentbundle import ContentBundle
from sugar.bundle.bundleversion import NormalizedVersion
@@ -38,6 +38,14 @@ _stub_icon_path = None
_online_cache = lrucache(32)
+def stub_icon():
+ global _stub_icon_path
+ if not _stub_icon_path:
+ theme = gtk.icon_theme_get_default()
+ _stub_icon_path = theme.lookup_icon('empty', 0, 0).get_filename()
+ return _stub_icon_path
+
+
class BundleRegistry(gobject.GObject):
"""Tracks the available activity bundles"""
@@ -54,7 +62,7 @@ class BundleRegistry(gobject.GObject):
gobject.GObject.__init__(self)
self._bundles = {}
- get_client().connect_to_signal('Event', self.__Event_cb)
+ get_client().connect('event', self.__Event_cb)
self._populate()
def __iter__(self):
@@ -78,8 +86,9 @@ class BundleRegistry(gobject.GObject):
return None
try:
- props = get_client().Get(mountpoint, 'context', context_guid,
- ['guid', 'keep', 'keep_impl', 'title'])
+ props = get_client().get(['context', context_guid],
+ mountpoint=mountpoint,
+ reply=['guid', 'keep', 'keep_impl', 'title'])
bundle = _ContextInfo(mountpoint, props)
except Exception:
_logger.warning('Cannot fetch activity metadata for %r on %r',
@@ -120,7 +129,8 @@ class BundleRegistry(gobject.GObject):
bundle = self._bundles.get(bundle_id)
if bundle is None:
return
- get_client().Update('~', 'context', bundle_id, {'keep': keep})
+ get_client().put(['context', bundle_id], {'keep': keep},
+ mountpoint='~')
bundle.props['keep'] = keep
self.emit('bundle-changed', bundle)
@@ -135,7 +145,8 @@ class BundleRegistry(gobject.GObject):
if bundle is None:
return
position = bundle.props['position'] = [int(x), int(y)]
- get_client().Update('~', 'context', bundle_id, {'position': position})
+ get_client().put(['context', bundle_id], {'position': position},
+ mountpoint='~')
self.emit('bundle-changed', bundle)
def is_activity_protected(self, bundle_id):
@@ -184,25 +195,18 @@ class BundleRegistry(gobject.GObject):
self.install(bundle)
def _populate(self):
-
- def reply_handler(entries, total):
- for props in entries:
- if props['guid'] in self._bundles:
- continue
- self._add_bundle(props['guid'], props)
-
- def error_handler(error):
- _logger.warning('Cannot call Find(): %s', error)
-
- get_client().Find('~', 'context',
- ['guid', 'keep', 'keep_impl', 'position'],
+ response = get_client().get(['context'], mountpoint='~',
+ reply=['guid', 'keep', 'keep_impl', 'position'],
# TODO process result by portions less than 1024
- {'keep_impl': 2, 'limit': 1024},
- reply_handler=reply_handler, error_handler=error_handler)
+ limit=1024, keep_impl=2)
+ for props in response['result']:
+ if props['guid'] in self._bundles:
+ continue
+ self._add_bundle(props['guid'], props)
def _add_bundle(self, bundle_id, props):
bundle = None
- for path in checkins(bundle_id):
+ for path in clones(bundle_id):
try:
bundle = _BundleInfo(ActivityBundle(path))
break
@@ -235,8 +239,8 @@ class BundleRegistry(gobject.GObject):
def _set_keep_impl(self, bundle_id, keep_impl):
if keep_impl:
- props = get_client().Get('~', 'context', bundle_id,
- ['guid', 'keep', 'keep_impl', 'position'])
+ props = get_client().get(['context', bundle_id], mountpoint='~',
+ reply=['guid', 'keep', 'keep_impl', 'position'])
bundle = self._bundles.get(bundle_id)
if bundle is None:
bundle = self._add_bundle(bundle_id, props)
@@ -246,22 +250,21 @@ class BundleRegistry(gobject.GObject):
else:
self._remove_bundle(bundle_id)
- def __Event_cb(self, event):
- if 'mountpoint' in event and event['mountpoint'] != '~' or \
- event.get('document') != 'context':
+ def __Event_cb(self, event, data):
+ if 'mountpoint' in data and data['mountpoint'] != '~' or \
+ data.get('document') != 'context':
return
- event_type = event.get('event')
- if event_type in ('create', 'update'):
- bundle_id = event['guid']
- props = event['props']
+ if event in ('create', 'update'):
+ bundle_id = data['guid']
+ props = data['props']
if props.get('keep_impl') in (0, 2):
self._set_keep_impl(bundle_id, props['keep_impl'])
if 'keep' in props:
self._set_keep(bundle_id, props['keep'])
- elif event_type == 'delete':
- bundle_id = event['guid']
+ elif event == 'delete':
+ bundle_id = data['guid']
self._set_keep_impl(bundle_id, 0)
- elif event_type == 'populate':
+ elif event == 'populate':
self._populate()
@@ -270,6 +273,7 @@ class _ContextInfo(object):
def __init__(self, mountpoint, props):
self.mountpoint = mountpoint
self.props = props
+ self._tmp_icon = None
def get_name(self):
return self.props['title']
@@ -278,22 +282,24 @@ class _ContextInfo(object):
return self.props['guid']
def get_icon(self):
- blob = None
- try:
- blob = get_client().GetBlob(self.mountpoint, 'context',
- self.get_bundle_id(), 'artifact_icon')
- except Exception, error:
- _logger.debug('Fail to get icon for %r: %s',
- self.get_bundle_id(), error)
-
- if not blob or os.stat(blob['path']).st_size == 0:
- return _stub_icon()
- else:
- path = sugar.profile_path('data', self.get_bundle_id() + '.svg')
- if lexists(path):
- os.unlink(path)
- os.symlink(blob['path'], path)
- return path
+ if self._tmp_icon is None:
+ blob = None
+ try:
+ blob = get_client().get(
+ ['context', self.get_bundle_id(), 'artifact_icon'],
+ mountpoint=self.mountpoint)
+ except Exception, error:
+ _logger.debug('Fail to get icon for %r: %s',
+ self.get_bundle_id(), error)
+ if not blob:
+ self._tmp_icon = stub_icon()
+ else:
+ self._tmp_icon = util.TempFilePath()
+ with file(self._tmp_icon, 'w') as f:
+ f.write(blob)
+
+ # Cast to `str to avoid spreading `util.TempFilePath`
+ return str(self._tmp_icon)
def get_tags(self):
# Doesn't matter with Sweets' features enabled
@@ -370,12 +376,3 @@ class _BrowserInfo(object):
def get_path(self):
return '/'
-
-
-def _stub_icon():
- global _stub_icon_path
-
- if not _stub_icon_path:
- theme = gtk.icon_theme_get_default()
- _stub_icon_path = theme.lookup_icon('empty', 0, 0).get_filename()
- return _stub_icon_path