From d667a0a9b02f9768c8b80ce0717ddc05d1cd1e2b Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Thu, 05 Nov 2009 23:01:32 +0000 Subject: Middle of ciding update_metatdata --- diff --git a/tests/vaulttests.py b/tests/vaulttests.py index c6bd852..9c51f85 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -259,6 +259,38 @@ 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_update_metadata(self): + """ + This test verify that the vault interface funciton update_metadata succesfully update a tutorial metadata. + """ + pass + + + def test_get_tutorial_archive(self): + """ + This test verify that the vault interface function get_tutorial_archive zip a tutorial and + return the zipped archive object succesfully. + """ + # Create and save a tutorial + tutorial = Tutorial('test') + Vault.saveTutorial(tutorial, self.test_metadata_dict) + + bundler = TutorialBundler(self.save_test_guid) + + # Test get_tutorial_archive() + archive_object = Vault.get_tutorial_archive(self.save_test_guid) + + # Write the archive data as a new file + zip_path = str(self.save_test_guid) + '_test_.zip' + file = open(zip_path, 'wt') + file.write(archive_object) + file.close() + + # Test if new zip is valid + assert zipfile.is_zipfile(zip_path) + # Remove test file + os.remove(zip_path) + def test_add_delete_get_path_resource(self): """ diff --git a/tutorius/vault.py b/tutorius/vault.py index 7ec0a23..e0554f1 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -342,6 +342,80 @@ class Vault(object): else: return False + + @staticmethod + def update_metadata(guid, metadata_dict): + """ + Modify the metadata of given tutorial found whit his guid. + @params guid of the tutorial to update the metadata. + @params metadata_dict the new metadata to update from + """ + + bundle = TutorialBundler(guid) + bundle_path = bundle.get_tutorial_path(guid) + # Create the metadata file + ini_file_path = os.path.join(tutorial_path, "meta.ini") + parser = SafeConfigParser() + parser.add_section(INI_METADATA_SECTION) + for key, value in metadata_dict.items(): + if key != 'activities': + parser.set(INI_METADATA_SECTION, key, value) + else: + related_activities_dict = value + parser.add_section(INI_ACTIVITY_SECTION) + for related_key, related_value in related_activities_dict.items(): + parser.set(INI_ACTIVITY_SECTION, related_key, related_value) + + # Delete the old metadata file + if os.path.isfile(ini_file_path): + os.remove(ini_file_path) + + # Write the new metadata file to disk + with open(ini_file_path, 'wb') as configfile: + parser.write(configfile) + + if os.path.isfile(ini_file_path): + return True + else: + return False + + + @staticmethod + def get_tutorial_archive(guid): + """ + Zip the files of the tutorial indentified with is guid and returns it. + @params guid the guid of the tutorial to zip + @returns a zip archive containing the files of the tutorial + """ + bundle = TutorialBundler(guid) + bundle_path = bundle.get_tutorial_path(guid) + + #Zip the tutorials files in the pkzip file format in a temp file + archive_list = [os.path.join(bundle_path, 'meta.ini'), os.path.join(bundle_path, 'tutorial.xml')] #, os.path.join(bundle_path, RESOURCES_FOLDER)] + + zfilename = str(guid) + ".zip" + + zout = zipfile.ZipFile(os.path.join(bundle_path, zfilename), "w") + for fname in archive_list: + fname_splitted = fname.rsplit('/') + file_only_name = fname_splitted[fname_splitted.__len__() - 1] + zout.write(fname, file_only_name) + zout.close() + + # test if the temp file is a valid pkzip file + assert zipfile.is_zipfile(os.path.join(bundle_path, zfilename)) == True, "The zipping of the tutorial files failed." + + # open the temp archive file in read mode + zin = open(os.path.join(bundle_path, zfilename), "r") + archive_data = zin.read() + zin.close() + + # delete temp file + os.remove(os.path.join(bundle_path, zfilename)) + + # return zip object data + return archive_data + @staticmethod def add_resource(tutorial_guid, file_path): -- cgit v0.9.1