From 5b31e2b134a9ba6ad0d1d1bf945407450b0a25a6 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 10 Sep 2006 13:31:08 +0200 Subject: [PATCH] Abtract the bot code out to Bot --- sugar/Makefile.am | 1 + sugar/simulator.py | 71 ++++++++++++++++++++++++++++++++++++++++++ tests/simulator/kiu.py | 67 ++------------------------------------- 3 files changed, 75 insertions(+), 64 deletions(-) create mode 100644 sugar/simulator.py diff --git a/sugar/Makefile.am b/sugar/Makefile.am index 71f60528..2cee687d 100644 --- a/sugar/Makefile.am +++ b/sugar/Makefile.am @@ -7,6 +7,7 @@ sugar_PYTHON = \ env.py \ logger.py \ setup.py \ + simulator.py \ TracebackUtils.py \ util.py diff --git a/sugar/simulator.py b/sugar/simulator.py new file mode 100644 index 00000000..4f97fb3c --- /dev/null +++ b/sugar/simulator.py @@ -0,0 +1,71 @@ +import os + +import gtk +import gobject + +from sugar.session.TestSession import TestSession +from sugar.presence import PresenceService +from sugar.p2p import Stream +from sugar import util + +_PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp" + +class _SimulatedActivity: + def __init__(self): + self._id = util.unique_id() + + def get_id(self): + return self._id + +class _ShellOwner(object): + def __init__(self, nick, color): + self._pservice = PresenceService.get_instance() + self._color = color + self._nick = nick + + def announce(self): + props = { 'color': self._color.to_string() } + self._service = self._pservice.register_service(self._nick, + _PRESENCE_SERVICE_TYPE, properties=props) + self._stream = Stream.Stream.new_from_service(self._service) + self._stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon") + self._stream.register_reader_handler(self._handle_invite, "invite") + + def _handle_buddy_icon_request(self): + return '' + + def _handle_invite(self, issuer, bundle_id, activity_id): + return '' + +class Bot: + def __init__(self, nick, color): + self._nick = nick + self._color = color + + os.environ['SUGAR_NICK_NAME'] = self._nick + + def start(self): + session = TestSession() + session.start() + + PresenceService.start() + + owner = _ShellOwner(self._nick, self._color) + owner.announce() + + gobject.timeout_add(1000, self._real_start) + + gtk.main() + + def _real_start(self): + pservice = PresenceService.get_instance() + + if not pservice.get_owner().get_color(): + return True + + activity = _SimulatedActivity() + properties = { 'title' : 'OLPC' } + activity_type = '_GroupChatActivity_Sugar_redhat_com._udp' + service = pservice.share_activity(activity, activity_type, properties) + + return False diff --git a/tests/simulator/kiu.py b/tests/simulator/kiu.py index fb114195..26eee4f0 100755 --- a/tests/simulator/kiu.py +++ b/tests/simulator/kiu.py @@ -1,68 +1,7 @@ #!/usr/bin/python -import os -import gtk -import gobject - -from sugar.session.TestSession import TestSession -from sugar.presence import PresenceService +from sugar.simulator import Bot from sugar.canvas.IconColor import IconColor -from sugar.p2p import Stream -from sugar import util -PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp" - -class SimulatedActivity: - def __init__(self): - self._id = util.unique_id() - - def get_id(self): - return self._id - -class ShellOwner(object): - def __init__(self): - self._pservice = PresenceService.get_instance() - self._color = IconColor() - self._nick = 'kiu' - - def announce(self): - props = { 'color': self._color.to_string() } - self._service = self._pservice.register_service(self._nick, - PRESENCE_SERVICE_TYPE, properties=props) - self._stream = Stream.Stream.new_from_service(self._service) - self._stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon") - self._stream.register_reader_handler(self._handle_invite, "invite") - - def _handle_buddy_icon_request(self): - return '' - - def _handle_invite(self, issuer, bundle_id, activity_id): - return '' - -def start(): - pservice = PresenceService.get_instance() - - if not pservice.get_owner().get_color(): - print 'Color not found' - return True - - activity = SimulatedActivity() - properties = { 'title' : 'OLPC' } - activity_type = '_GroupChatActivity_Sugar_redhat_com._udp' - service = pservice.share_activity(activity, activity_type, properties) - - return False - -os.environ['SUGAR_NICK_NAME'] = 'kiu' - -session = TestSession() -session.start() - -PresenceService.start() - -owner = ShellOwner() -owner.announce() - -gobject.timeout_add(1000, start) - -gtk.main() +bot = Bot('kiu', IconColor('#5E4505,#0F8A0F')) +bot.start()