From 29338d21201d35d815719d5efadde70411bdda54 Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Wed, 04 Nov 2009 16:34:45 +0000 Subject: vault ressssources, pending review --- diff --git a/tests/ressources/icon.svg b/tests/ressources/icon.svg new file mode 100644 index 0000000..bb28f04 --- /dev/null +++ b/tests/ressources/icon.svg @@ -0,0 +1,21 @@ + + +]> + + + + + + + + image/svg+xml + + + + + + + + + \ No newline at end of file diff --git a/tests/vaulttests.py b/tests/vaulttests.py index 02c34e8..d6787ef 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -277,7 +277,46 @@ class VaultInterfaceTest(unittest.TestCase): # TODO : Compare the initial and reloaded metadata when vault.Query() will accept specifc argument # (so we can specifiy that we want only the metadata for this particular tutorial - + + def test_add_delete_get_path_ressource(self): + """ + This test verify that the vault interface function add_ressource succesfully add ressource in the vault + and return the new ressource id. It also test the deletion of the ressource. + """ + # Path of an image file in the test folder + image_path = os.path.join(os.getcwd(), 'tests', 'ressources', 'icon.svg') + assert os.path.isfile(image_path), 'cannot find the test image file' + + # Create and save a tutorial + tutorial = Tutorial('test', self.fsm) + Vault.saveTutorial(tutorial, self.test_metadata_dict) + + bundler = TutorialBundler(self.save_test_guid) + tuto_path = bundler.get_tutorial_path(self.save_test_guid) + + # add the ressource to the tutorial + ressource_id = Vault.add_ressource(self.save_test_guid, image_path) + + # Check that the image file is now in the vault + assert os.path.isfile(os.path.join(tuto_path, 'ressources', ressource_id)), 'image file not found in vault' + + # Check if get_ressource_path Vault interface function is working + vault_path = Vault.get_ressource_path(self.save_test_guid, ressource_id) + + assert os.path.isfile(vault_path), 'path returned is not a file' + basename, extension = os.path.splitext(vault_path) + assert extension == '.svg', 'wrong file path have been returned' + + + # Delete the ressource + Vault.delete_ressource(self.save_test_guid, ressource_id) + + # Check that the ressource is not in the vault anymore + assert os.path.isfile(os.path.join(tuto_path, 'ressources', ressource_id)) == False, 'image file found in vault when it should have been deleted.' + + + + def tearDown(self): folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); @@ -362,6 +401,9 @@ class XMLSerializerTest(unittest.TestCase): shutil.rmtree(file_path) def create_test_metadata(self, ini_file_path, guid): + """ + Create a basinc .ini file for testing purpose. + """ ini_file = open(ini_file_path, 'wt') ini_file.write("[GENERAL_METADATA]\n") ini_file.write('guid=' + str(guid) + '\n') @@ -513,4 +555,4 @@ class TutorialBundlerTests(unittest.TestCase): if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file diff --git a/tutorius/vault.py b/tutorius/vault.py index b455a52..9cda2e9 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -28,10 +28,10 @@ import uuid import xml.dom.minidom from xml.dom import NotFoundErr import zipfile -from ConfigParser import SafeConfigParser from . import addon from .core import Tutorial, State, FiniteStateMachine +from ConfigParser import SafeConfigParser logger = logging.getLogger("tutorius") @@ -62,6 +62,7 @@ NODE_COMPONENT = "Component" NODE_SUBCOMPONENT = "property" NODE_SUBCOMPONENTLIST = "listproperty" NEXT_STATE_ATTR = "next_state" +RESSOURCES_FOLDER = 'ressources' class Vault(object): @@ -73,7 +74,7 @@ class Vault(object): given activity. @param activity_name the name of the activity associated with this tutorial. None means ALL activities - @param activity_vers the version number of the activity to find tutorail for. 0 means find for ANY version. If activity_name is None, version number is not used + @param activity_vers the version number of the activity to find tutorail for. 0 means find for ANY version. Ifactivity_ame is None, version number is not used @returns a map of tutorial {names : GUID}. """ # check both under the activity data and user installed folders @@ -303,7 +304,7 @@ class Vault(object): @staticmethod - def deleteTutorial(Tutorial): + def deleteTutorial(Guid): """ Removes the tutorial from the Vault. It will unpublish the tutorial if need be, and it will also wipe it from the persistent storage. @@ -320,6 +321,74 @@ class Vault(object): else: return False + + @staticmethod + def add_ressource(tutorial_guid, file_path): + """ + Returns a unique name for this resource composed from the original name of the file + and a suffix to make it unique ( ex: name_1.jpg ) and add it to the resources for the tutorial. + @param tutorial_guid The guid of the tutorial + @param file_path the file path of the ressource to add + @returns the ressource_id of the ressource + """ + + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Get the file name + fname_splitted = file_path.rsplit('/') + file_name = fname_splitted[fname_splitted.__len__() - 1] + base_name, extension = os.path.splitext(file_name) + # Append unique name to file name + file_name_appended = base_name + '_' + str(uuid.uuid1()) + extension + # Check if the ressource file already exists + new_file_path = os.path.join(tutorial_path, RESSOURCES_FOLDER, file_name_appended) + if os.path.isfile(new_file_path) == False: + # Copy the ressource file in the vault + if os.path.isdir(os.path.join(tutorial_path, RESSOURCES_FOLDER)) == False: + os.makedirs(os.path.join(tutorial_path, RESSOURCES_FOLDER)) + assert os.path.isfile(file_path) + shutil.copyfile(file_path, new_file_path) + + return file_name_appended + + + @staticmethod + def delete_ressource(tutorial_guid, ressource_id): + """ + Delete the resource from the resources of the tutorial. + @param tutorial_guid the guid of the tutorial + @param ressource_id the ressource id of the ressource to delete + """ + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Check if the ressource file exists + file_path = os.path.join(tutorial_path, RESSOURCES_FOLDER, ressource_id) + if os.path.isfile(file_path): + # Delete the ressource + os.remove(file_path) + else: + print('File not found, no delete took place') + + @staticmethod + def get_ressource_path(tutorial_guid, ressource_id): + """ + Returns the absolute file path to the resourceID + @param tutorial_guid the guid of the tutorial + @param ressource_id the ressource id of the ressource to find the path for + @returns the absolute path of the ressource file + """ + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Check if the ressource file exists + file_path = os.path.join(tutorial_path, RESSOURCES_FOLDER, ressource_id) + if os.path.isfile(file_path): + return file_path + else: + return None + class Serializer(object): """ -- cgit v0.9.1