Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sugar/tutorius/bundler.py93
-rw-r--r--src/sugar/tutorius/tests/serializertests.py32
2 files changed, 75 insertions, 50 deletions
diff --git a/src/sugar/tutorius/bundler.py b/src/sugar/tutorius/bundler.py
index c7456c4..d9d06bb 100644
--- a/src/sugar/tutorius/bundler.py
+++ b/src/sugar/tutorius/bundler.py
@@ -30,6 +30,7 @@ from sugar.tutorius import gtkutils, overlayer
from sugar.tutorius.core import Tutorial, State, FiniteStateMachine
from sugar.tutorius.actions import *
import sugar.tutorius.filters
+#from sugar.tutorius.filters import EventFilter, TimerEvent, GtkWidgetEventFilter, GtkWidgetTypeFilter
from ConfigParser import SafeConfigParser
def _get_store_root():
@@ -141,30 +142,32 @@ class XMLSerializer(Serializer):
actionNode = doc.createElement("Action")
actionsList.appendChild(actionNode)
if type(action) is DialogMessage:
- actionNode.setAttribute("Class", type(action))
+ # Using .__class__ since type() doesn't have the same behavior
+ # with class derivating from object and class that don't
+ actionNode.setAttribute("Class", str(action.__class__))
actionNode.setAttribute("Message", action.message)
actionNode.setAttribute("Position", action.position)
elif type(action) is BubbleMessage:
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
actionNode.setAttribute("Message", action.message)
actionNode.setAttribute("Position", str(action.position))
actionNode.setAttribute("Tail_pos", str(action.tail_pos))
# TODO : elif for each type of action
elif type(action) is WidgetIdentifyAction:
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
# TODO
elif type(action) is ChainAction:
# TODO
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
elif type(action) is DisableWidgetAction:
# TODO
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
elif type(action) is TypeTextAction:
# TODO
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
elif type(action) is ClickAction:
# TODO
- actionNode.setAttribute("Class", str(type(action)))
+ actionNode.setAttribute("Class", str(action.__class__))
return actionsList
@@ -176,15 +179,18 @@ class XMLSerializer(Serializer):
for event_f in event_filters:
eventFilterNode = eventFiltersList.appendChild("EventFilter")
# TODO : elif for each type of event filters
- if type(event_f) is TimerEvent:
+ if type(event_f) is sugar.tutorius.filters.TimerEvent:
+ # Using .__class__ since type() doesn't have the same behavior
+ # with class derivating from object and class that don't
+
# TODO
- eventFilterNode.setAttribute("Class", str(type(event_f)))
- elif type(event_f) is GtkWidgetEventFilter:
+ eventFilterNode.setAttribute("Class", str(event_f.__class__))
+ elif type(event_f) is sugar.tutorius.filters.GtkWidgetEventFilter:
# TODO
- eventFilterNode.setAttribute("Class", str(type(event_f)))
- elif type(event_f) is GtkWidgetTypeFilter:
+ eventFilterNode.setAttribute("Class", str(event_f.__class__))
+ elif type(event_f) is sugar.tutorius.filters.GtkWidgetTypeFilter:
# TODO
- eventFilterNode.setAttribute("Class", str(type(event_f)))
+ eventFilterNode.setAttribute("Class", str(event_f.__class__))
return eventFiltersList
@@ -275,21 +281,24 @@ class XMLSerializer(Serializer):
@param filters_elem An XML Element representing a list of event filters
"""
- event_filters_list = []
+ reformed_event_filters_list = []
+ # item(0) because there is always only one <EventFilterList> tag in the xml file
+ # so states_elem should always contain only one element
+ event_filter_element_list = filters_elem.item(0).getElementsByTagName("EventFilter")
- for event_filter in actions_elem.getElementByTagName("EventFilter"):
+ for event_filter in event_filter_element_list:
# TODO : elif for each type of event filter
- if event_filter.getAttribute("Class") is TimerEvent:
+ if event_filter.getAttribute("Class") == str(sugar.tutorius.filters.TimerEvent):
# TODO
pass
- elif event_filter.getAttribute("Class") is GtkWidgetEventFilter:
+ elif event_filter.getAttribute("Class") == str(sugar.tutorius.filters.GtkWidgetEventFilter):
# TODO
pass
- elif event_filter.getAttribute("Class") is GtkWidgetTypeFilter:
+ elif event_filter.getAttribute("Class") == str(sugar.tutorius.filters.GtkWidgetTypeFilter):
# TODO
pass
- return event_filters_list
+ return reformed_event_filters_list
def _load_xml_actions(self, actions_elem):
"""
@@ -297,53 +306,59 @@ class XMLSerializer(Serializer):
@param actions_elem An XML Element representing a list of Actions
"""
- actions_list = []
+ reformed_actions_list = []
+ # item(0) because there is always only one <Actions> tag in the xml file
+ # so states_elem should always contain only one element
+ actions_element_list = actions_elem.item(0).getElementsByTagName("Action")
- for action in actions_elem.getElementByTagName("Action"):
+ for action in actions_element_list:
# TODO : elif for each type of action
- if action.getAttribute("Class") is DialogMessage:
+ if action.getAttribute("Class") == str(DialogMessage):
message = action.getAttribute("Message")
position = action.getAttribute("Postion")
- actions_list.append(DialogMessage(message,position))
- elif action.getAttribute("Class") is BubbleMessage:
+ reformed_actions_list.append(DialogMessage(message,position))
+ elif action.getAttribute("Class") == str(BubbleMessage):
message = action.getAttribute("Message")
position = action.getAttribute("Postion")
tail_pos = action.getAttribute("Tail_pos")
- actions_list.append(BubbleMessage(message,position,None,tail_pos))
- elif action.getAttribute("Class") is WidgetIdentifyAction:
+ reformed_actions_list.append(BubbleMessage(message,position,None,tail_pos))
+ elif action.getAttribute("Class") == str(WidgetIdentifyAction):
# TODO
pass
- elif action.getAttribute("Class") is ChainAction:
+ elif action.getAttribute("Class") == str(ChainAction):
# TODO
pass
- elif action.getAttribute("Class") is DisableWidgetAction:
+ elif action.getAttribute("Class") == str(DisableWidgetAction):
# TODO
pass
- elif action.getAttribute("Class") is TypeTextAction:
+ elif action.getAttribute("Class") == str(TypeTextAction):
# TODO
pass
- elif action.getAttribute("Class") is ClickAction:
+ elif action.getAttribute("Class") == str(ClickAction):
# TODO
pass
- return actions_list
+ return reformed_actions_list
def _load_xml_states(self, states_elem):
"""
- Takes in a States element and fleshes out a complete dictionnary of State
+ Takes in a States element and fleshes out a complete list of State
objects.
@param states_elem An XML Element that represents a list of States
"""
- states_dict = {}
+ reformed_state_list = []
+ # item(0) because there is always only one <States> tag in the xml file
+ # so states_elem should always contain only one element
+ states_element_list = states_elem.item(0).getElementsByTagName("State")
- for state in states_elem.getElementByTagName("State"):
- stateName = states_elem.getAttribute("Name")
- actions_list = _load_xml_actions(state.getElementByTagName("Actions"))
- event_filters_list = _load_xml_event_filters(state.getElementByTagName("EventFiltersList"))
- states_dict[stateName] = State(stateName, actions_list, event_filters_list)
+ for state in states_element_list:
+ stateName = state.getAttribute("Name")
+ actions_list = self._load_xml_actions(state.getElementsByTagName("Actions"))
+ event_filters_list = self._load_xml_event_filters(state.getElementsByTagName("EventFiltersList"))
+ reformed_state_list.append(State(stateName, actions_list, event_filters_list))
- return states_dict
+ return reformed_state_list
def _load_xml_fsm(self, fsm_elem):
"""
diff --git a/src/sugar/tutorius/tests/serializertests.py b/src/sugar/tutorius/tests/serializertests.py
index 64e467e..b138fa3 100644
--- a/src/sugar/tutorius/tests/serializertests.py
+++ b/src/sugar/tutorius/tests/serializertests.py
@@ -28,7 +28,7 @@ import unittest
import logging
import linecache
import os
-import cPickle as pickle
+import shutil
from sugar.tutorius import gtkutils, overlayer
from sugar.tutorius.core import Tutorial, State, FiniteStateMachine
@@ -63,10 +63,12 @@ class XMLSerializerTest(unittest.TestCase):
self.fsm.add_state(st2)
self.uuid = uuid1()
+
- def test_save(self):
+ def test_save(self, remove=True):
"""
Writes an FSM to disk, then compares the file to the expected results.
+ "Remove" boolean argument specify if the test data must be removed or not
"""
# Make the serializer believe the test is in a activity path
testpath = "/tmp/testdata/"
@@ -78,19 +80,18 @@ class XMLSerializerTest(unittest.TestCase):
#rpdb2.start_embedded_debugger('flakyPass')
xml_ser.save_fsm(self.fsm, "fsm.xml", os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)))
- # Compare the two files
-
+ #Remove test file and path
+ if remove == True:
+ os.remove(os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)) + "/fsm.xml")
+ if os.path.isdir(testpath):
+ shutil.rmtree(testpath)
- #Remove test file and path (commented for testing, to uncomment)
-## os.remove(testpath + os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)) + "/fsm.xml")
-## os.removedirs(testpath)
-
def test_save_and_load(self):
"""
Load up the written FSM and compare it with the object representation.
"""
- self.test_save()
-
+ self.test_save(False)
+ testpath = "/tmp/testdata/"
#rpdb2.start_embedded_debugger('flakyPass')
xml_ser = XMLSerializer()
@@ -101,12 +102,21 @@ class XMLSerializerTest(unittest.TestCase):
# Compare the two FSMs
assert loaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \
'FSM underlying dictionary differ from original to pickled/reformed one'
- assert loaded_fsm._states.get("LOST").name == self.fsm._states.get("Second").name, \
+ assert loaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \
'FSM underlying dictionary differ from original to pickled/reformed one'
+ assert loaded_fsm._states.get("INIT").get_action_list()[0].get_message() == \
+ self.fsm._states.get("INIT").get_action_list()[0].get_message(), \
+ 'FSM underlying State underlying Action differ from original to reformed one'
+
+ os.remove(os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)) + "/fsm.xml")
+ if os.path.isdir(testpath):
+ shutil.rmtree(testpath)
+
# Helper classes to help testing
class SerializerTest(unittest.TestCase):
"""
+
This class has to test the Serializer methods as well as the expected
functionality.
"""