Assign max_participants in activity.info

As part of an effort to "honor" max_participants, this patch supports setting
max_participants in activity.info, thus making it available in the bundle.

By default, if it is not set in the bundle, the previous behavior persists.

In support of this change, a cache of Activity bundles is
maintained. The goal is to eliminate unnecessary calls to the file
system.
This commit is contained in:
Walter Bender 2014-04-30 13:57:57 -04:00 committed by Gonzalo Odiard
parent 72994bd73c
commit d0cca91fe8
3 changed files with 38 additions and 6 deletions

View File

@ -82,7 +82,7 @@ from sugar3.graphics.window import Window
from sugar3.graphics.alert import Alert from sugar3.graphics.alert import Alert
from sugar3.graphics.icon import Icon from sugar3.graphics.icon import Icon
from sugar3.datastore import datastore from sugar3.datastore import datastore
from sugar3.bundle.activitybundle import ActivityBundle from sugar3.bundle.activitybundle import get_bundle_instance
from gi.repository import SugarExt from gi.repository import SugarExt
_ = lambda msg: gettext.dgettext('sugar-toolkit-gtk3', msg) _ = lambda msg: gettext.dgettext('sugar-toolkit-gtk3', msg)
@ -334,7 +334,7 @@ class Activity(Window, Gtk.Container):
self._closing = False self._closing = False
self._quit_requested = False self._quit_requested = False
self._deleting = False self._deleting = False
self._max_participants = 0 self._max_participants = None
self._invites_queue = [] self._invites_queue = []
self._jobject = None self._jobject = None
self._read_file_called = False self._read_file_called = False
@ -495,6 +495,11 @@ class Activity(Window, Gtk.Container):
type=bool, default=False, getter=get_active, setter=set_active) type=bool, default=False, getter=get_active, setter=set_active)
def get_max_participants(self): def get_max_participants(self):
# If max_participants has not been set in the activity, get it
# from the bundle.
if self._max_participants is None:
bundle = get_bundle_instance(get_bundle_path())
self._max_participants = bundle.get_max_participants()
return self._max_participants return self._max_participants
def set_max_participants(self, participants): def set_max_participants(self, participants):
@ -643,7 +648,7 @@ class Activity(Window, Gtk.Container):
Display a notification with the given summary and body. Display a notification with the given summary and body.
The notification will go under the activities icon in the frame. The notification will go under the activities icon in the frame.
""" """
bundle = ActivityBundle(get_bundle_path()) bundle = get_bundle_instance(get_bundle_path())
icon = bundle.get_icon() icon = bundle.get_icon()
bus = dbus.SessionBus() bus = dbus.SessionBus()

View File

@ -27,7 +27,7 @@ from sugar3.graphics.radiopalette import RadioPalette, RadioMenuButton
from sugar3.graphics.radiotoolbutton import RadioToolButton from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.xocolor import XoColor from sugar3.graphics.xocolor import XoColor
from sugar3.graphics.icon import Icon from sugar3.graphics.icon import Icon
from sugar3.bundle.activitybundle import ActivityBundle from sugar3.bundle.activitybundle import get_bundle_instance
from sugar3.graphics import style from sugar3.graphics import style
from sugar3.graphics.palettemenu import PaletteMenuBox from sugar3.graphics.palettemenu import PaletteMenuBox
from sugar3 import profile from sugar3 import profile
@ -43,7 +43,7 @@ def _create_activity_icon(metadata):
color = profile.get_color() color = profile.get_color()
from sugar3.activity.activity import get_bundle_path from sugar3.activity.activity import get_bundle_path
bundle = ActivityBundle(get_bundle_path()) bundle = get_bundle_instance(get_bundle_path())
icon = Icon(file=bundle.get_icon(), xo_color=color) icon = Icon(file=bundle.get_icon(), xo_color=color)
return icon return icon

View File

@ -34,6 +34,9 @@ from sugar3.bundle.bundleversion import NormalizedVersion
from sugar3.bundle.bundleversion import InvalidVersionError from sugar3.bundle.bundleversion import InvalidVersionError
_bundle_instances = {}
def _expand_lang(locale): def _expand_lang(locale):
# Private method from gettext.py # Private method from gettext.py
locale = normalize(locale) locale = normalize(locale)
@ -86,7 +89,8 @@ def _expand_lang(locale):
class ActivityBundle(Bundle): class ActivityBundle(Bundle):
"""A Sugar activity bundle """A Sugar activity bundle
See http://wiki.laptop.org/go/Activity_bundles for details See http://wiki.sugarlabs.org/go/Development_Team/Almanac/Activity_Bundles
for details
""" """
MIME_TYPE = 'application/vnd.olpc-sugar' MIME_TYPE = 'application/vnd.olpc-sugar'
@ -109,6 +113,7 @@ class ActivityBundle(Bundle):
self._activity_version = '0' self._activity_version = '0'
self._summary = None self._summary = None
self._single_instance = False self._single_instance = False
self._max_participants = 1
info_file = self.get_file('activity/activity.info') info_file = self.get_file('activity/activity.info')
if info_file is None: if info_file is None:
@ -120,6 +125,8 @@ class ActivityBundle(Bundle):
if linfo_file: if linfo_file:
self._parse_linfo(linfo_file) self._parse_linfo(linfo_file)
_bundle_instances[path] = self
def _parse_info(self, info_file): def _parse_info(self, info_file):
cp = ConfigParser() cp = ConfigParser()
cp.readfp(info_file) cp.readfp(info_file)
@ -187,6 +194,15 @@ class ActivityBundle(Bundle):
if cp.get(section, 'single_instance') == 'yes': if cp.get(section, 'single_instance') == 'yes':
self._single_instance = True self._single_instance = True
if cp.has_option(section, 'max_participants'):
max_participants = cp.get(section, 'max_participants')
try:
self._max_participants = int(max_participants)
except ValueError:
raise MalformedBundleException(
'Activity bundle %s has invalid max_participants %s' %
(self._path, max_participants))
def _get_linfo_file(self): def _get_linfo_file(self):
# Using method from gettext.py, first find languages from environ # Using method from gettext.py, first find languages from environ
languages = [] languages = []
@ -291,6 +307,10 @@ class ActivityBundle(Bundle):
"""Get whether there should be a single instance for the activity""" """Get whether there should be a single instance for the activity"""
return self._single_instance return self._single_instance
def get_max_participants(self):
"""Get maximum number of participants in share"""
return self._max_participants
def get_show_launcher(self): def get_show_launcher(self):
"""Get whether there should be a visible launcher for the activity""" """Get whether there should be a visible launcher for the activity"""
return self._show_launcher return self._show_launcher
@ -397,3 +417,10 @@ class ActivityBundle(Bundle):
def is_user_activity(self): def is_user_activity(self):
return self.get_path().startswith(env.get_user_activities_path()) return self.get_path().startswith(env.get_user_activities_path())
def get_bundle_instance(path, translated=True):
global _bundle_instances
if path not in _bundle_instances:
_bundle_instances[path] = ActivityBundle(path, translated=translated)
return _bundle_instances[path]