From 1ebaff8c43b3949a956b7c2de245b6441fde0b57 Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Sat, 17 Oct 2009 21:58:34 +0000 Subject: Interface function OpenTutorial is now also functional and tested in vaulttest.py --- diff --git a/tests/vaulttests.py b/tests/vaulttests.py index c6f4534..04a473a 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -56,15 +56,15 @@ class VaultInterfaceTest(unittest.TestCase): ini_file.write('rating=3.5\n') ini_file.write('category=Test\n') ini_file.write('publish_state=false\n') - ini_file.write("fsm_filename=TestTutorial1.xml\n") ini_file.write('[RELATED_ACTIVITIES]\n') ini_file.write('org.laptop.TutoriusActivity = 1\n') ini_file.write('org.laptop.Writus = 1\n') ini_file.close() + def setUp(self): os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path') - path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path') + path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path', 'data', 'tutorius', 'data') if os.path.isdir(path) != True: os.makedirs(path) @@ -94,12 +94,27 @@ class VaultInterfaceTest(unittest.TestCase): ini_file2.write('rating=4\n') ini_file2.write('category=Test2\n') ini_file2.write('publish_state=false\n') - ini_file2.write("fsm_filename=TestTutorial2.xml\n") ini_file2.write('[RELATED_ACTIVITIES]\n') ini_file2.write('org.laptop.TutoriusActivity = 2\n') ini_file2.write('org.laptop.Writus = 1\n') ini_file2.write('org.laptop.Testus = 1\n') ini_file2.close() + + # Create a dummy fsm + self.fsm = FiniteStateMachine("testingMachine") + # Add a few states + act1 = addon.create('BubbleMessage', message="Hi", pos=[300, 450]) + ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked", "Second") + act2 = addon.create('BubbleMessage', message="Second message", pos=[250, 150], tailpos=[1,2]) + st1 = State("INIT") + st1.add_action(act1) + st1.add_event_filter(ev1) + st2 = State("Second") + st2.add_action(act2) + self.fsm.add_state(st1) + self.fsm.add_state(st2) + self.tuto_guid = uuid1() + def test_installTutorials(self): # create dummy tutorial @@ -114,28 +129,13 @@ class VaultInterfaceTest(unittest.TestCase): # Creat a dummy tutorial .xml file serializer = XMLSerializer() - # Create the sample FSM - fsm = FiniteStateMachine("testingMachine") - # Add a few states - act1 = addon.create('BubbleMessage', message="Hi", pos=[300, 450]) - ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked", "Second") - act2 = addon.create('BubbleMessage', message="Second message", pos=[250, 150], tailpos=[1,2]) - st1 = State("INIT") - st1.add_action(act1) - st1.add_event_filter(ev1) - st2 = State("Second") - st2.add_action(act2) - fsm.add_state(st1) - fsm.add_state(st2) - guid = uuid1() - - serializer.save_fsm(fsm, 'TestTutorial1.xml', test_path) + serializer.save_fsm(self.fsm, 'tutorial.xml', test_path) # Create a dummy tutorial metadata file - self.create_test_metadata_file(os.path.join(test_path, 'meta.ini'), guid) + self.create_test_metadata_file(os.path.join(test_path, 'meta.ini'), self.tuto_guid) #Zip these tutorials files in the pkzip file format - archive_list = [os.path.join(test_path, 'meta.ini'), os.path.join(test_path, 'TestTutorial1.xml')] + archive_list = [os.path.join(test_path, 'meta.ini'), os.path.join(test_path, 'tutorial.xml')] zfilename = "TestTutorial.zip" @@ -145,8 +145,7 @@ class VaultInterfaceTest(unittest.TestCase): zout.close() # test if the file is a valid pkzip file - if zipfile.is_zipfile(zfilename) != True: - fail("The zipping of the tutorial files failed.") + assert zipfile.is_zipfile(os.path.join(test_path, zfilename)) == True, "The zipping of the tutorial files failed." # test installTutorials function vault = Vault() @@ -156,9 +155,10 @@ class VaultInterfaceTest(unittest.TestCase): # check if the tutorial is now in the vault try : - serializer.find_tutorial_dir_with_guid(guid) + bundler = TutorialBundler(self.tuto_guid) + bundler.get_tutorial_path(self.tuto_guid) except IOError: - fail("Cannot find the specified tutorial's GUID in the vault") + print("Cannot find the specified tutorial's GUID in the vault") def test_query(self): @@ -185,7 +185,6 @@ class VaultInterfaceTest(unittest.TestCase): assert tuto_dictionnary['rating'] == '3.5' assert tuto_dictionnary['category'] == 'Test' assert tuto_dictionnary['publish_state'] == 'false' - assert tuto_dictionnary['fsm_filename'] == 'TestTutorial1.xml' assert related.has_key('org.laptop.tutoriusactivity') assert related.has_key('org.laptop.writus') @@ -196,16 +195,40 @@ class VaultInterfaceTest(unittest.TestCase): assert tuto_dictionnary['rating'] == '4' assert tuto_dictionnary['category'] == 'Test2' assert tuto_dictionnary['publish_state'] == 'false' - assert tuto_dictionnary['fsm_filename'] == 'TestTutorial2.xml' assert related.has_key('org.laptop.tutoriusactivity') assert related.has_key('org.laptop.writus') assert related.has_key('org.laptop.testus') else: assert False, 'list is empty or name property is wrong' + + + def test_openTutorial(self): + + # call test_installTutorials to be sure that the tuto is now in the Vault + self.test_installTutorials() + bundler = TutorialBundler(self.tuto_guid) + test = bundler.get_tutorial_path(self.tuto_guid) + print('Test : ' + test) + # load tutorial created in the test_installTutorial function + vault = Vault() + reloaded_tuto = vault.openTutorial(self.tuto_guid) + + # Compare the two FSMs + reloaded_fsm = reloaded_tuto.state_machine + + assert reloaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \ + 'FSM underlying dictionary differ from original to reformed one' + assert reloaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \ + 'FSM underlying dictionary differ from original to reformed one' + assert reloaded_fsm._states.get("INIT").get_action_list()[0].message == \ + self.fsm._states.get("INIT").get_action_list()[0].message, \ + 'FSM underlying State underlying Action differ from original to reformed one' + assert len(reloaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself" + def tearDown(self): - + pass folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); for file in os.listdir(folder): file_path = os.path.join(folder, file) @@ -213,11 +236,13 @@ class VaultInterfaceTest(unittest.TestCase): if (os.path.isdir(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp'))): shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp')) - + + class SerializerInterfaceTest(unittest.TestCase): """ For completeness' sake. """ + def test_save(self): ser = Serializer() @@ -240,6 +265,7 @@ class XMLSerializerTest(unittest.TestCase): """ Tests the transformation of XML to FSM, then back. """ + def setUp(self): os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path') @@ -276,6 +302,7 @@ class XMLSerializerTest(unittest.TestCase): """ Removes the created files, if need be. """ + pass if self.remove == True: shutil.rmtree(os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')) @@ -283,6 +310,21 @@ class XMLSerializerTest(unittest.TestCase): for file in os.listdir(folder): file_path = os.path.join(folder, file) shutil.rmtree(file_path) + + def create_test_metadata(self, ini_file_path, guid): + ini_file = open(ini_file_path, 'wt') + ini_file.write("[GENERAL_METADATA]\n") + ini_file.write('guid=' + str(guid) + '\n') + ini_file.write('name=TestTutorial1\n') + ini_file.write('version=1\n') + ini_file.write('description=This is a test tutorial 1\n') + ini_file.write('rating=3.5\n') + ini_file.write('category=Test\n') + ini_file.write('publish_state=false\n') + ini_file.write('[RELATED_ACTIVITIES]\n') + ini_file.write('org.laptop.TutoriusActivity = 1\n') + ini_file.write('org.laptop.Writus = 1\n') + ini_file.close() def test_save(self): """ @@ -291,9 +333,10 @@ class XMLSerializerTest(unittest.TestCase): """ xml_ser = XMLSerializer() os.makedirs(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid))) - #rpdb2.start_embedded_debugger('flakyPass') xml_ser.save_fsm(self.fsm, sugar.tutorius.vault.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid))) - + self.create_test_metadata(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid), 'meta.ini'), self.uuid) + + def test_save_and_load(self): """ Load up the written FSM and compare it with the object representation. @@ -301,8 +344,6 @@ class XMLSerializerTest(unittest.TestCase): self.test_save() xml_ser = XMLSerializer() - # 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 @@ -403,20 +444,20 @@ class TutorialBundlerTests(unittest.TestCase): ini_file.write('rating=3.5\n') ini_file.write('category=Test\n') ini_file.write('publish_state=false\n') - ini_file.write('fsm_filename=TestTutorial.xml\n') ini_file.write('[RELATED_ACTIVITES]\n') ini_file.write('org.laptop.TutoriusActivity = 1\n') ini_file.write('org.laptop.Writus = 1\n') ini_file.close() def tearDown(self): - os.remove(self.ini_file) - os.rmdir(self.guid_path) - - folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); - for file in os.listdir(folder): - file_path = os.path.join(folder, file) - shutil.rmtree(file_path) + pass +## os.remove(self.ini_file) +## os.rmdir(self.guid_path) +## +## folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); +## for file in os.listdir(folder): +## file_path = os.path.join(folder, file) +## shutil.rmtree(file_path) ## def test_add_ressource(self): ## bund = bundler.TutorialBundler(str(self.test_guid)) diff --git a/tutorius/vault.py b/tutorius/vault.py index cc277c8..7770fb4 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -206,7 +206,7 @@ class Vault(object): # Create a TutorialBundler object from the guid bundler = TutorialBundler(guid) # Find the .ini file path for this guid - ini_file_path = bundler.get_tutorial_path() + ini_file_path = bundler.get_tutorial_path(guid) # Read the .ini file ini_file.read(os.path.join(ini_file_path, 'meta.ini')) @@ -231,14 +231,14 @@ class Vault(object): return tutorial_list - def openTutorial(Guid): + def openTutorial(self, Guid): """ Creates an executable version of a tutorial from its saved representation. @returns an executable representation of a tutorial """ bundle = TutorialBundler(Guid) - bundle_path = bundle.get_tutorial_path() + bundle_path = bundle.get_tutorial_path(Guid) config = SafeConfigParser() config.read(os.path.join(bundle_path, INI_FILENAME)) @@ -305,7 +305,7 @@ class Vault(object): @returns true is the tutorial was deleted from the Vault """ bundle = TutorialBundler(Guid) - bundle_path = bundle.get_tutorial_path() + bundle_path = bundle.get_tutorial_path(Guid) # TODO : Need also to unpublish tutorial, need to interact with webservice module @@ -437,58 +437,60 @@ class XMLSerializer(Serializer): file_object.close() - def find_tutorial_dir_with_guid(self, guid): - """ - Finds the tutorial with the associated GUID. If it is found, return - the path to the tutorial's directory. If it doesn't exist, raise an - IOError. - - A note : if there are two tutorials with this GUID in the folders, - they will both be inspected and the one with the highest version - number will be returned. If they have the same version number, the one - from the global store will be returned. - - @param guid The GUID of the tutorial that is to be loaded. - """ - # Attempt to find the tutorial's directory in the global directory - global_dir = os.path.join(_get_store_root(),str(guid)) - # Then in the activty's bundle path - activity_dir = os.path.join(_get_bundle_root(), str(guid)) - - # If they both exist - if os.path.isdir(global_dir) and os.path.isdir(activity_dir): - # Inspect both metadata files - global_meta = os.path.join(global_dir, "meta.ini") - activity_meta = os.path.join(activity_dir, "meta.ini") - - # Open both config files - global_parser = SafeConfigParser() - global_parser.read(global_meta) - - activity_parser = SafeConfigParser() - activity_parser.read(activity_meta) - - # Get the version number for each tutorial - global_version = global_parser.get(INI_METADATA_SECTION, "version") - activity_version = activity_parser.get(INI_METADATA_SECTION, "version") - - # If the global version is higher or equal, we'll take it - if global_version >= activity_version: - return global_dir - else: - return activity_dir - - # Do we just have the global directory? - if os.path.isdir(global_dir): - return global_dir - - # Or just the activity's bundle directory? - if os.path.isdir(activity_dir): - return activity_dir - - # Error : none of these directories contain the tutorial - raise IOError(2, "Neither the global nor the bundle directory contained the tutorial with GUID %s"%guid) - +## def find_tutorial_dir_with_guid(self, guid): +## """ +## Finds the tutorial with the associated GUID. If it is found, return +## the path to the tutorial's directory. If it doesn't exist, raise an +## IOError. +## +## A note : if there are two tutorials with this GUID in the folders, +## they will both be inspected and the one with the highest version +## number will be returned. If they have the same version number, the one +## from the global store will be returned. +## +## @param guid The GUID of the tutorial that is to be loaded. +## """ +## # Attempt to find the tutorial's directory in the global directory +## global_dir = os.path.join(_get_store_root(),str(guid)) +## # Then in the activty's bundle path +## activity_dir = os.path.join(_get_bundle_root(), str(guid)) +## +## # If they both exist +## if os.path.isdir(global_dir) and os.path.isdir(activity_dir): +## # Inspect both metadata files +## global_meta = os.path.join(global_dir, "meta.ini") +## activity_meta = os.path.join(activity_dir, "meta.ini") +## +## # Open both config files +## global_parser = SafeConfigParser() +## global_parser.read(global_meta) +## +## activity_parser = SafeConfigParser() +## activity_parser.read(activity_meta) +## +## # Get the version number for each tutorial +## global_version = global_parser.get(INI_METADATA_SECTION, "version") +## activity_version = activity_parser.get(INI_METADATA_SECTION, "version") +## +## # If the global version is higher or equal, we'll take it +## if global_version >= activity_version: +## return global_dir +## else: +## return activity_dir +## +## # Do we just have the global directory? +## if os.path.isdir(global_dir): +## return global_dir +## +## # Or just the activity's bundle directory? +## if os.path.isdir(activity_dir): +## return activity_dir +## +## # Error : none of these directories contain the tutorial +## raise IOError(2, "Neither the global nor the bundle directory contained the tutorial with GUID %s"%guid) +## + + def _load_xml_properties(self, properties_elem): """ Changes a list of properties into fully instanciated properties. @@ -621,7 +623,8 @@ class XMLSerializer(Serializer): Load fsm from xml file whose .ini file guid match argument guid. """ # Fetch the directory (if any) - tutorial_dir = self.find_tutorial_dir_with_guid(guid) + bundler = TutorialBundler(guid) + tutorial_dir = bundler.get_tutorial_path(guid) # Open the XML file tutorial_file = os.path.join(tutorial_dir, TUTORIAL_FILENAME) @@ -651,12 +654,12 @@ class TutorialBundler(object): #Look for the file in the path if a uid is supplied if generated_guid: #General store - store_path = os.path.join(_get_store_root(), generated_guid, INI_FILENAME) + store_path = os.path.join(_get_store_root(), str(generated_guid), INI_FILENAME) if os.path.isfile(store_path): self.Path = os.path.dirname(store_path) else: #Bundle store - bundle_path = os.path.join(_get_bundle_root(), generated_guid, INI_FILENAME) + bundle_path = os.path.join(_get_bundle_root(), str(generated_guid), INI_FILENAME) if os.path.isfile(bundle_path): self.Path = os.path.dirname(bundle_path) else: @@ -686,59 +689,57 @@ class TutorialBundler(object): #Write the ini file cfg.write( file( os.path.join(self.Path, INI_FILENAME), 'w' ) ) - def get_tutorial_path(self): - """ - Return the path of the .ini file associated with the given guid set in - the Guid property of the Tutorial_Bundler. If the guid is present in - more than one path, the store_root is given priority. + def get_tutorial_path(self, guid): """ + Finds the tutorial with the associated GUID. If it is found, return + the path to the tutorial's directory. If it doesn't exist, raise an + IOError. - store_root = _get_store_root() - bundle_root = _get_bundle_root() + A note : if there are two tutorials with this GUID in the folders, + they will both be inspected and the one with the highest version + number will be returned. If they have the same version number, the one + from the global store will be returned. - config = SafeConfigParser() - path = None + @param guid The GUID of the tutorial that is to be loaded. + """ + # Attempt to find the tutorial's directory in the global directory + global_dir = os.path.join(_get_store_root(),str(guid)) + # Then in the activty's bundle path + activity_dir = os.path.join(_get_bundle_root(), str(guid)) - logging.debug("************ Path of store_root folder of activity : " \ - + store_root) - - # iterate in each GUID subfolder - for dir in os.listdir(store_root): + # If they both exist + if os.path.isdir(global_dir) and os.path.isdir(activity_dir): + # Inspect both metadata files + global_meta = os.path.join(global_dir, "meta.ini") + activity_meta = os.path.join(activity_dir, "meta.ini") - # iterate for each .ini file in the store_root folder - - for file_name in os.listdir(os.path.join(store_root, dir)): - if file_name.endswith(".ini"): - logging.debug("******************* Found .ini file : " \ - + file_name) - config.read(os.path.join(store_root, dir, file_name)) - if config.get(INI_METADATA_SECTION, INI_GUID_PROPERTY) == self.Guid: - xml_filename = config.get(INI_METADATA_SECTION, - INI_XML_FSM_PROPERTY) - - path = os.path.join(store_root, dir) - return path + # Open both config files + global_parser = SafeConfigParser() + global_parser.read(global_meta) - logging.debug("************ Path of bundle_root folder of activity : " \ - + bundle_root) + activity_parser = SafeConfigParser() + activity_parser.read(activity_meta) - - # iterate in each GUID subfolder - for dir in os.listdir(bundle_root): + # Get the version number for each tutorial + global_version = global_parser.get(INI_METADATA_SECTION, "version") + activity_version = activity_parser.get(INI_METADATA_SECTION, "version") - # iterate for each .ini file in the bundle_root folder - for file_name in os.listdir(os.path.join(bundle_root, dir)): - if file_name.endswith(".ini"): - logging.debug("******************* Found .ini file : " \ - + file_name) - config.read(os.path.join(bundle_root, dir, file_name)) - if config.get(INI_METADATA_SECTION, INI_GUID_PROPERTY) == self.Guid: - path = os.path.join(bundle_root, self.Guid) - return path - - if path is None: - logging.debug("**************** Error : GUID not found") - raise KeyError + # If the global version is higher or equal, we'll take it + if global_version >= activity_version: + return global_dir + else: + return activity_dir + + # Do we just have the global directory? + if os.path.isdir(global_dir): + return global_dir + + # Or just the activity's bundle directory? + if os.path.isdir(activity_dir): + return activity_dir + + # Error : none of these directories contain the tutorial + raise IOError(2, "Neither the global nor the bundle directory contained the tutorial with GUID %s"%guid) def write_fsm(self, fsm): -- cgit v0.9.1