Let Kiu change activities randomly
This commit is contained in:
parent
f9012b88f6
commit
66a1539895
@ -2,6 +2,8 @@ import os
|
||||
|
||||
import gtk
|
||||
import gobject
|
||||
import base64
|
||||
import dbus
|
||||
|
||||
from sugar.session.TestSession import TestSession
|
||||
from sugar.presence import PresenceService
|
||||
@ -17,12 +19,18 @@ class _SimulatedActivity:
|
||||
def get_id(self):
|
||||
return self._id
|
||||
|
||||
class _ShellOwner(object):
|
||||
def __init__(self, nick, color):
|
||||
class _SimulatedShellOwner(object):
|
||||
def __init__(self, nick, color, icon_file=None):
|
||||
self._pservice = PresenceService.get_instance()
|
||||
self._color = color
|
||||
self._nick = nick
|
||||
|
||||
self._icon = None
|
||||
if icon_file:
|
||||
fd = open(icon_file, "r")
|
||||
self._icon = fd.read()
|
||||
fd.close()
|
||||
|
||||
def announce(self):
|
||||
props = { 'color': self._color.to_string() }
|
||||
self._service = self._pservice.register_service(self._nick,
|
||||
@ -32,11 +40,16 @@ class _ShellOwner(object):
|
||||
self._stream.register_reader_handler(self._handle_invite, "invite")
|
||||
|
||||
def _handle_buddy_icon_request(self):
|
||||
if self._icon:
|
||||
return base64.b64encode(self._icon)
|
||||
return ''
|
||||
|
||||
def _handle_invite(self, issuer, bundle_id, activity_id):
|
||||
return ''
|
||||
|
||||
def set_current_activity(self, activity_id):
|
||||
self._service.set_published_value('curact', dbus.String(activity_id))
|
||||
|
||||
class _Timeline:
|
||||
def __init__(self, time_factor):
|
||||
self._time_factor = time_factor
|
||||
@ -50,22 +63,27 @@ class _Timeline:
|
||||
return False
|
||||
|
||||
class ShareActivityAction:
|
||||
def __init__(self, title, activity_type):
|
||||
def __init__(self, title, activity_type, callback=None):
|
||||
self._title = title
|
||||
self._type = activity_type
|
||||
self._callback = callback
|
||||
|
||||
def execute(self):
|
||||
activity = _SimulatedActivity()
|
||||
properties = { 'title' : self._title }
|
||||
|
||||
pservice = PresenceService.get_instance()
|
||||
pservice.share_activity(activity, self._type, properties)
|
||||
act_service = pservice.share_activity(activity, self._type, properties)
|
||||
if self._callback is not None:
|
||||
self._callback(activity, act_service)
|
||||
|
||||
class Bot:
|
||||
def __init__(self, nick, color):
|
||||
self._nick = nick
|
||||
self._color = color
|
||||
self._timeline = _Timeline(0.01)
|
||||
self._owner = None
|
||||
self._icon_file = None
|
||||
|
||||
os.environ['SUGAR_NICK_NAME'] = nick
|
||||
os.environ['SUGAR_COLOR'] = color.to_string()
|
||||
@ -76,10 +94,10 @@ class Bot:
|
||||
|
||||
PresenceService.start()
|
||||
|
||||
owner = _ShellOwner(self._nick, self._color)
|
||||
owner.announce()
|
||||
self._owner = _SimulatedShellOwner(self._nick, self._color, self._icon_file)
|
||||
self._owner.announce()
|
||||
|
||||
pservice = PresenceService.get_instance()
|
||||
self._pservice = PresenceService.get_instance()
|
||||
|
||||
gtk.main()
|
||||
|
||||
|
@ -3,15 +3,62 @@
|
||||
from sugar.simulator import Bot
|
||||
from sugar.simulator import ShareActivityAction
|
||||
from sugar.canvas.IconColor import IconColor
|
||||
import os, random, gobject
|
||||
|
||||
bot = Bot('kiu', IconColor('#5E4505,#0F8A0F'))
|
||||
class KiuBot(Bot):
|
||||
def __init__(self):
|
||||
Bot.__init__(self, 'kiu', IconColor('#5E4505,#0F8A0F'))
|
||||
self._olpc_channel_service = None
|
||||
self._sugar_channel_service = None
|
||||
self._activity_switch_timeout = None
|
||||
|
||||
action = ShareActivityAction('OLPC channel',
|
||||
'_GroupChatActivity_Sugar_redhat_com._udp')
|
||||
bot.add_action(action, 10)
|
||||
action = ShareActivityAction('OLPC channel',
|
||||
'_GroupChatActivity_Sugar_redhat_com._udp',
|
||||
self.__share_olpc_channel_cb)
|
||||
self.add_action(action, 10)
|
||||
|
||||
action = ShareActivityAction('Sugar channel',
|
||||
'_GroupChatActivity_Sugar_redhat_com._udp')
|
||||
bot.add_action(action, 20)
|
||||
action = ShareActivityAction('Sugar channel',
|
||||
'_GroupChatActivity_Sugar_redhat_com._udp',
|
||||
self.__share_sugar_channel_cb)
|
||||
self.add_action(action, 20)
|
||||
|
||||
bot.start()
|
||||
self._icon_file = os.path.abspath("kiu.jpg")
|
||||
|
||||
def __activity_switch_cb(self):
|
||||
self._activity_switch_timeout = None
|
||||
which = random.randint(1, 2)
|
||||
if which == 1:
|
||||
actid = self._olpc_channel_activity.get_id()
|
||||
elif which == 2:
|
||||
actid = self._sugar_channel_activity.get_id()
|
||||
else:
|
||||
raise RuntimeError("WTF? unexpected value")
|
||||
print "KIU: now setting current activity to %s" % actid
|
||||
self._owner.set_current_activity(actid)
|
||||
self._schedule_activity_switch_timeout()
|
||||
return False
|
||||
|
||||
def _schedule_activity_switch_timeout(self):
|
||||
if self._activity_switch_timeout:
|
||||
return
|
||||
interval = random.randint(1000, 20000)
|
||||
self._activity_switch_timeout = gobject.timeout_add(interval,
|
||||
self.__activity_switch_cb)
|
||||
|
||||
def __share_olpc_channel_cb(self, sim_activity, service):
|
||||
self._olpc_channel_service = service
|
||||
self._olpc_channel_activity = sim_activity
|
||||
self._schedule_activity_switch_timeout()
|
||||
|
||||
def __share_sugar_channel_cb(self, sim_activity, service):
|
||||
self._sugar_channel_service = service
|
||||
self._sugar_channel_activity = sim_activity
|
||||
self._schedule_activity_switch_timeout()
|
||||
|
||||
def main():
|
||||
bot = KiuBot()
|
||||
bot.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user