From dba2da918dda60778a35e992ffc8f94989272977 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 22 Apr 2009 19:01:01 +0000 Subject: Merge branch 'jc' of ssh://mike@bobthebuilder.mine.nu:8080/home/git into jc_support Conflicts: source/external/source/sugar-toolkit/src/sugar/tutorius/bundler.py source/external/source/sugar-toolkit/src/sugar/tutorius/tests/serializertests.py --- (limited to 'src/sugar') diff --git a/src/sugar/tutorius/bundler.py b/src/sugar/tutorius/bundler.py index 37de0ac..c7456c4 100644 --- a/src/sugar/tutorius/bundler.py +++ b/src/sugar/tutorius/bundler.py @@ -119,7 +119,7 @@ class XMLSerializer(Serializer): used in the tutorials to/from a .xml file. Inherit from Serializer """ - def create_state_dict_node(self, state_dict, doc): + def _create_state_dict_node(self, state_dict, doc): """ Create and return a xml Node from a State dictionnary. """ @@ -128,11 +128,11 @@ class XMLSerializer(Serializer): stateNode = doc.createElement("State") statesList.appendChild(stateNode) stateNode.setAttribute("Name", state_name) - stateNode.appendChild(self.create_action_list_node(state.get_action_list(), doc)) - stateNode.appendChild(self.create_event_filters_node(state.get_event_filter_list(), doc)) + actionsList = stateNode.appendChild(self._create_action_list_node(state.get_action_list(), doc)) + eventfiltersList = stateNode.appendChild(self._create_event_filters_node(state.get_event_filter_list(), doc)) return statesList - def create_action_list_node(self, action_list, doc): + def _create_action_list_node(self, action_list, doc): """ Create and return a xml Node from a Action list. """ @@ -168,23 +168,23 @@ class XMLSerializer(Serializer): return actionsList - def create_event_filters_node(self, event_filters, doc): + def _create_event_filters_node(self, event_filters, doc): """ Create and return a xml Node from a event filters. """ - eventFiltersList = doc.createElement("EventFilters") + eventFiltersList = doc.createElement("EventFiltersList") for event_f in event_filters: eventFilterNode = eventFiltersList.appendChild("EventFilter") # TODO : elif for each type of event filters if type(event_f) is TimerEvent: # TODO - eventFilterNode.setAttribute("EventFilter:Class", str(type(event_f))) + eventFilterNode.setAttribute("Class", str(type(event_f))) elif type(event_f) is GtkWidgetEventFilter: # TODO - eventFilterNode.setAttribute("EventFilter:Class", str(type(event_f))) + eventFilterNode.setAttribute("Class", str(type(event_f))) elif type(event_f) is GtkWidgetTypeFilter: # TODO - eventFilterNode.setAttribute("EventFilter:Class", str(type(event_f))) + eventFilterNode.setAttribute("Class", str(type(event_f))) return eventFiltersList @@ -198,8 +198,8 @@ class XMLSerializer(Serializer): doc.appendChild(fsm_element) fsm_element.setAttribute("Name", fsm.name) fsm_element.setAttribute("StartStateName", fsm.start_state_name) - fsm_element.appendChild(self.create_state_dict_node(fsm._states, doc)) - fsm_element.appendChild(self.create_action_list_node(fsm.actions, doc)) + statesDict = fsm_element.appendChild(self._create_state_dict_node(fsm._states, doc)) + actionsList = fsm_element.appendChild(self._create_action_list_node(fsm.actions, doc)) file_object = open(os.path.join(path, xml_filename), "w") # Commented for DEBUG purposes @@ -275,17 +275,21 @@ class XMLSerializer(Serializer): @param filters_elem An XML Element representing a list of event filters """ - filter_object_list = [] + event_filters_list = [] - filter_node_list = filters_elem.getElementsByTagName("EventFilter") - - for filter_node in filter_node_list: - class_name = filter_node.getAttribute("Class") + for event_filter in actions_elem.getElementByTagName("EventFilter"): + # TODO : elif for each type of event filter + if event_filter.getAttribute("Class") is TimerEvent: + # TODO + pass + elif event_filter.getAttribute("Class") is GtkWidgetEventFilter: + # TODO + pass + elif event_filter.getAttribute("Class") is GtkWidgetTypeFilter: + # TODO + pass - #if class_name == "GtkWidgetEventFilter": - # event_name = - - return filter_object_list + return event_filters_list def _load_xml_actions(self, actions_elem): """ @@ -293,57 +297,53 @@ class XMLSerializer(Serializer): @param actions_elem An XML Element representing a list of Actions """ - action_object_list = [] - - action_node_list = actions_elem.getElementsByTagName("Action") - - for action_node in action_node_list: - class_name = action_node.getAttribute("Class") - action_object = None - # TODO : We will want an automatic way of registering new actions? - # I'm thinking of the case where we add an action to the core - # and we don't want to have to write this XML interop bit every - # time. - if class_name == "": - # Get the message, position and tail_position - message = action_node.getAttribute("Message") - position = action_node.getAttribute("Position") - tail_pos = action_node.getAttribute("Tail_Pos") - action_object = BubbleMessage(message, [0,0]) - # Elif for all the other actions... - - if action_object is not None: - action_object_list.append(action_object) + actions_list = [] - return action_object_list + for action in actions_elem.getElementByTagName("Action"): + # TODO : elif for each type of action + if action.getAttribute("Class") is DialogMessage: + message = action.getAttribute("Message") + position = action.getAttribute("Postion") + actions_list.append(DialogMessage(message,position)) + elif action.getAttribute("Class") is 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: + # TODO + pass + elif action.getAttribute("Class") is ChainAction: + # TODO + pass + elif action.getAttribute("Class") is DisableWidgetAction: + # TODO + pass + elif action.getAttribute("Class") is TypeTextAction: + # TODO + pass + elif action.getAttribute("Class") is ClickAction: + # TODO + pass + + return actions_list def _load_xml_states(self, states_elem): """ - Takes in a States element and fleshes out a complete list of State + Takes in a States element and fleshes out a complete dictionnary of State objects. @param states_elem An XML Element that represents a list of States """ - state_node_list = states_elem.getElementsByTagName("State") + states_dict = {} - state_object_list = [] - - for state_node in state_node_list: - # Get the state name - state_name = state_node.getAttribute("Name") - - actions_node_list = state_node.getElementsByTagName("Actions") - action_object_list = [] - if len(actions_node_list) > 0: - action_object_list = self._load_xml_actions(actions_node_list[0]) - - #filters_node_list = state_node.getElementsByTagName("EventFilters") - filter_object_list = self._load_xml_event_filters(state_node.getElementsByTagName("EventFilters")[0]) - state_object = State(state_name, action_list=action_object_list, event_filter_list = filter_object_list) - - state_object_list.append(state_object) + 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) - return state_object_list + return states_dict def _load_xml_fsm(self, fsm_elem): """ @@ -364,17 +364,17 @@ class XMLSerializer(Serializer): fsm = FiniteStateMachine(fsm_name, start_state_name=fsm_start_state_name) # Load the states - states = self._load_xml_states(fsm_elem.getElementsByTagName("States")[0]) + states = self._load_xml_states(fsm_elem.getElementsByTagName("States")) for state in states: fsm.add_state(state) # Load the actions on this FSM - actions = self._load_xml_actions(fsm_elem.getElementsByTagName("Actions")[0]) + actions = self._load_xml_actions(fsm_elem.getElementsByTagName("Actions")) for action in actions: fsm.add_action(action) # Load the event filters - events = self._load_xml_event_filters(fsm_elem.getElementsByTagName("EventFilters")[0]) + events = self._load_xml_event_filters(fsm_elem.getElementsByTagName("EventFiltersList")) for event in events: fsm.add_event_filter(event) @@ -539,10 +539,11 @@ class TutorialBundler: config = SafeConfigParser() if guid is not None: + serializer = XMLSerializer() path = get_tutorial_path() + "/meta.ini" config.read(path) xml_filename = config.get(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY) - save_fsm(fsm, xml_filename, store_root) + serializer.save_fsm(fsm, xml_filename, store_root) def add_resources(self, typename, file): diff --git a/src/sugar/tutorius/tests/run-tests.py b/src/sugar/tutorius/tests/run-tests.py index 0c9219b..6f22fee 100755 --- a/src/sugar/tutorius/tests/run-tests.py +++ b/src/sugar/tutorius/tests/run-tests.py @@ -48,5 +48,6 @@ if __name__=='__main__': from servicestests import * from gtkutilstests import * from overlaytests import * + from serializertests import * unittest.main() diff --git a/src/sugar/tutorius/tests/serializertests.py b/src/sugar/tutorius/tests/serializertests.py index 9a21d08..0b7fabd 100644 --- a/src/sugar/tutorius/tests/serializertests.py +++ b/src/sugar/tutorius/tests/serializertests.py @@ -67,15 +67,24 @@ class XMLSerializerTest(unittest.TestCase): def test_save(self): """ Writes an FSM to disk, then compares the file to the expected results. - """ + """ + # Make the serializer believe the test is in a activity path + testpath = "/tmp/testdata/" + os.environ["SUGAR_BUNDLE_PATH"] = testpath + os.environ["SUGAR_PREFIX"] = testpath +## os.mkdir(sugar.tutorius.bundler._get_store_root()) xml_ser = XMLSerializer() - os.mkdir(os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid))) + os.makedirs(os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid))) #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 (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. @@ -85,13 +94,15 @@ class XMLSerializerTest(unittest.TestCase): #rpdb2.start_embedded_debugger('flakyPass') xml_ser = XMLSerializer() - rpdb2.start_embedded_debugger('pass') # This interface needs to be redone... It's not clean because there is # a responsibility mixup between the XML reader and the bundler. loaded_fsm = xml_ser.load_fsm(str(self.uuid)) # 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, \ + 'FSM underlying dictionary differ from original to pickled/reformed one' # Helper classes to help testing class SerializerTest(unittest.TestCase): @@ -172,4 +183,4 @@ class SerializerTest(unittest.TestCase): if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() -- cgit v0.9.1