diff options
author | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-11 21:10:45 (GMT) |
---|---|---|
committer | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-11 21:10:45 (GMT) |
commit | 4128281863fff3b86c4b06f04b13af75bfbe1e67 (patch) | |
tree | 763ac2adb58a12bb7af322df4af896c294615171 | |
parent | e74348be28a9e5101e0213397844bb74d6bc7717 (diff) |
Finished code of Vault, NOT TESTED, tests in next commit
-rw-r--r-- | tutorius/vault.py | 120 |
1 files changed, 82 insertions, 38 deletions
diff --git a/tutorius/vault.py b/tutorius/vault.py index 1b39b99..8c29d67 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -22,6 +22,7 @@ This module contains all the data handling class of Tutorius import logging import os +import shutil import uuid import xml.dom.minidom import zipfile @@ -45,9 +46,10 @@ def _get_bundle_root(): INI_ACTIVITY_SECTION = "RELATED_ACTIVITIES" INI_METADATA_SECTION = "GENERAL_METADATA" -INI_GUID_PROPERTY = "GUID" -INI_NAME_PROPERTY = "NAME" -INI_XML_FSM_PROPERTY = "FSM_FILENAME" +INI_GUID_PROPERTY = "guid" +INI_NAME_PROPERTY = "name" +INI_XML_FSM_PROPERTY = "fsm_filename" +INI_VERSION_PROPERTY = 'version' INI_FILENAME = "meta.ini" TUTORIAL_FILENAME = "tutorial.xml" NODE_COMPONENT = "Component" @@ -122,25 +124,48 @@ class Vault(object): # unpack the zip archive zfile = zipfile.ZipFile(os.path.join(path, zip_file_name), "r" ) + temp_path = os.path.join(_get_store_root(),'temp_extracted_tuto') + for info in zfile.infolist(): fname = info.filename # decompress each file's data data = zfile.read(fname) - # testing --> display file's contents if a text file - # save the decompressed data to a new file - filename = 'unzipped_' + fname - fout = open(filename, "w") + # save the decompressed data to a new file in a temp folder + fout = open(os.path.join(temp_path, fname), 'w') fout.write(data) fout.close() # get the tutorial file - tutorial_file = os.path.join(path, TUTORIAL_FILENAME) + ini_file_path = os.path.join(temp_path, INI_FILENAME) + ini_file = SafeConfigParser() + ini_file.read(ini_file_path) - xml_dom = xml.dom.minidom.parse(tutorial_file) + # get the tutorial guid + guid = ini_file.get(INI_METADATA_SECTION, INI_GUID_PROPERTY) - fsm_elem = xml_dom.getElementsByTagName("FSM")[0] + # Check if tutorial already exist + tutorial_path = os.path.join(_get_store_root(), guid) + if os.path.isdir(tutorial_path) == False: + # Copy the tutorial in the Vault + shutil.copytree(temp_path, tutorial_path) - return self._load_xml_fsm(fsm_elem) + else: + # Check the version of the existing tutorial + existing_version = ini_file.get(INI_METADATA_SECTION, INI_VERSION_PROPERTY) + # Check the version of the new tutorial + new_ini_file = SafeConfigParser() + new_ini_file.read(os.path.join(tutorial_path, INI_FILENAME)) + new_version = new_ini_file.get(INI_METADATA_SECTION, INI_VERSION_PROPERTY) + + if new_version < existing_version and forceinstall == False: + # Version of new tutorial is older and forceinstall is false, return exception + return 1 + else : + # New tutorial is newer or forceinstall flag is set, can overwrite the existing tutorial + shutil.rmtree(tutorial_path) + shutil.copytree(temp_path, tutorial_path) + + return 0 def query(self, keyword=[], relatedActivityNames=[], category=[]): @@ -218,12 +243,44 @@ class Vault(object): tuto = Tutorial(name, fsm) return tuto - def saveTutorial(Tutorial, Metadata, ExternalResource): + def saveTutorial(tutorial_fsm, metadata_dict, ExternalResource): """ Creates a persistent version of a tutorial in the Vault. @returns true if the tutorial was saved correctly """ - pass + + # Get the tutorial guid from metadata dictionnary + guid = metadata_dict[INI_GUID_PROPERTY] + + # Check if tutorial already exist + tutorial_path = os.path.join(_get_store_root(), guid) + if os.path.isdir(tutorial_path) == False: + + # Serialize the tutorial and write it to disk + xml_ser = XMLSerializer() + os.makedirs(tutorial_path) + xml_ser.save_fsm(tutorial_fsm, TUTORIAL_FILENAME, tutorial_path) + + # 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: + if key_value[0] != 'activities': + parser.set(INI_METADATA_SECTION, key_value[0], key_value[1]) + else: + related_activities_dict = key_value[1] + parser.add_section(INI_ACTIVITY_SECTION) + for related_key_value in related_activities_dict: + parser.set(INI_ACTIVITY_SECTION, related_key_value[0], related_key_value[1]) + + # Write the file to disk + with open(ini_file_path, 'wb') as configfile: + parser.write(configfile) + + else: + # Error, tutorial already exist + return False ## # 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 @@ -240,30 +297,17 @@ class Vault(object): and it will also wipe it from the persistent storage. @returns true is the tutorial was deleted from the Vault """ - pass - - -## def load_tutorial(self, Guid): -## """ -## Rebuilds a tutorial object from it's serialized state. -## Common storing paths will be scanned. -## -## @param Guid the generic identifier of the tutorial -## @returns a Tutorial object containing an FSM -## """ -## bundle = TutorialBundler(Guid) -## bundle_path = bundle.get_tutorial_path() -## config = SafeConfigParser() -## config.read(os.path.join(bundle_path, INI_FILENAME)) -## -## serializer = XMLSerializer() -## -## name = config.get(INI_METADATA_SECTION, INI_NAME_PROPERTY) -## fsm = serializer.load_fsm(Guid) -## -## tuto = Tutorial(name, fsm) -## return tuto - + bundle = TutorialBundler(Guid) + bundle_path = bundle.get_tutorial_path() + + # TODO : Need also to unpublish tutorial, need to interact with webservice module + + shutil.rmtree(bundle_path) + if os.path.isdir(bundle_path) == False: + return True + else: + return False + class Serializer(object): """ @@ -529,7 +573,7 @@ class XMLSerializer(Serializer): return reformed_state_list - def _load_xml_fsm(self, fsm_elem): + def load_xml_fsm(self, fsm_elem): """ Takes in an XML element representing an FSM and returns the fully crafted FSM. |