diff options
author | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-19 20:05:01 (GMT) |
---|---|---|
committer | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-19 20:05:01 (GMT) |
commit | 8a0f8824e8296476f3ca9dd66c421dcbf960ba10 (patch) | |
tree | 90c130a774998e9f3f0e88c11c818edb66d2e145 | |
parent | 25c53a72e6d70ce7d6f2141e76955fcaaf424ee2 (diff) |
Made change after Vince code review. Some change are not done yet, usually for waiting after other module. See TODO comments in the code.
-rw-r--r-- | tests/vaulttests.py | 6 | ||||
-rw-r--r-- | tutorius/vault.py | 128 |
2 files changed, 65 insertions, 69 deletions
diff --git a/tests/vaulttests.py b/tests/vaulttests.py index a9a53ab..df79cea 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -158,7 +158,9 @@ class VaultInterfaceTest(unittest.TestCase): zout = zipfile.ZipFile(os.path.join(test_path, zfilename), "w") for fname in archive_list: - zout.write(fname) + fname_splitted = fname.rsplit('/') + file_only_name = fname_splitted[fname_splitted.__len__() - 1] + zout.write(fname, file_only_name) zout.close() # test if the file is a valid pkzip file @@ -255,7 +257,7 @@ class VaultInterfaceTest(unittest.TestCase): # Save the tutorial in the vault vault = Vault() tutorial = Tutorial('test', self.fsm) - vault.saveTutorial(tutorial, self.test_metadata_dict, None) + vault.saveTutorial(tutorial, self.test_metadata_dict) # Get the tutorial back reloaded_tuto = vault.loadTutorial(self.save_test_guid) diff --git a/tutorius/vault.py b/tutorius/vault.py index 2429813..2893852 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -23,16 +23,14 @@ This module contains all the data handling class of Tutorius import logging import os import shutil +import tempfile import uuid import xml.dom.minidom import zipfile from sugar.tutorius import addon from sugar.tutorius.core import Tutorial, State, FiniteStateMachine -# TODO : Import only revelent -from sugar.tutorius.filters import * -from sugar.tutorius.actions import * -from ConfigParser import * +from ConfigParser import SafeConfigParser logger = logging.getLogger("tutorius") @@ -64,15 +62,14 @@ NODE_COMPONENT = "Component" class Vault(object): ## Vault internal functions : - # TODO : Make vault static : add @staticmethod over and remove self from argument - def list_available_tutorials(self, activity_name = 'all', activity_vers = 0): - # TODO : remove extreme case if activity name is 'all' + @staticmethod + def list_available_tutorials(activity_name = None, activity_vers = 0): """ Generate the list of all tutorials present on disk for a given activity. - @param activity_name the name of the activity associated with this tutorial. 'all' means ALL activities - @param activity_vers the version number of the activity to find tutorail for. 0 means find for ANY version. Ifactivity_ame is 'all', version number is not used + @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. 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 @@ -99,11 +96,11 @@ class Vault(object): # break tutorials. We may lower this requirement when the # UAM gets less dependent on the widget order. # Also note property names are always stored lowercase. - if (activity_name.lower() in activities) and (activity_name != 'all'): + if (activity_name != None) and (activity_name.lower() in activities): version = parser.get(INI_ACTIVITY_SECTION, activity_name) if (activity_vers == version) or (activity_vers == 0): tutoGuidName[guid] = name - elif (activity_name == 'all'): + elif (activity_name == None): tutoGuidName[guid] = name except OSError: # the repository may not exist. Continue scanning @@ -112,8 +109,8 @@ class Vault(object): return tutoGuidName ## Vault interface functions : - - def installTutorials(self, path, zip_file_name, forceinstall=False): + @staticmethod + def installTutorials(path, zip_file_name, forceinstall=False): """ Extract the tutorial files in the ZIPPED tutorial archive at the specified path and add them inside the vault. This should remove any previous @@ -137,20 +134,8 @@ 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') - os.makedirs(temp_path) - # TODO : use extractall for extracting instead to reduce code base - for info in zfile.infolist(): - fname = info.filename - # decompress each file's data - data = zfile.read(fname) - # get only the filename from the full path name - fname_splitted = fname.rsplit('/') - filename = fname_splitted[fname_splitted.__len__() - 1] - # save the decompressed data to a new file in a temp folder - fout = open(os.path.join(temp_path, filename), 'w') - fout.write(data) - fout.close() + temp_path = tempfile.mkdtemp(dir=_get_store_root()) + zfile.extractall(temp_path) # get the tutorial file ini_file_path = os.path.join(temp_path, INI_FILENAME) @@ -187,8 +172,8 @@ class Vault(object): return 0 - - def query(self, keyword=[], relatedActivityNames=[], category=[]): + @staticmethod + def query(keyword=[], relatedActivityNames=[], category=[]): """ Returns the list of tutorials that corresponds to the specified parameters. @@ -208,11 +193,10 @@ class Vault(object): tuto_guid_list = [] ini_file = SafeConfigParser() if keyword == [] and relatedActivityNames == [] and category == []: - for activity_name in self.list_available_tutorials(): - # get all tutorials tuples (name:guid) for all activities and version - tuto_dict = self.list_available_tutorials() - for id in tuto_dict.keys(): - tuto_guid_list.append(id) + # get all tutorials tuples (name:guid) for all activities and version + tuto_dict = Vault.list_available_tutorials() + for id in tuto_dict.keys(): + tuto_guid_list.append(id) # Find .ini metadata files with the guid list @@ -250,8 +234,8 @@ class Vault(object): # Return tutorial list return tutorial_list - - def loadTutorial(self, Guid): + @staticmethod + def loadTutorial(Guid): """ Creates an executable version of a tutorial from its saved representation. @returns an executable representation of a tutorial @@ -270,7 +254,8 @@ class Vault(object): tuto = Tutorial(name, fsm) return tuto - def saveTutorial(self, tutorial, metadata_dict, ExternalResource): + @staticmethod + def saveTutorial(tutorial, metadata_dict): """ Creates a persistent version of a tutorial in the Vault. @returns true if the tutorial was saved correctly @@ -292,15 +277,14 @@ class Vault(object): 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(): - # TODO : change for key, value - if key_value[0] != 'activities': - parser.set(INI_METADATA_SECTION, key_value[0], key_value[1]) + for key, value in metadata_dict.items(): + if key != 'activities': + parser.set(INI_METADATA_SECTION, key, value) else: - related_activities_dict = key_value[1] + related_activities_dict = value parser.add_section(INI_ACTIVITY_SECTION) - for related_key_value in related_activities_dict.items(): - parser.set(INI_ACTIVITY_SECTION, related_key_value[0], related_key_value[1]) + for related_key, related_value in related_activities_dict.items(): + parser.set(INI_ACTIVITY_SECTION, related_key, related_value) # Write the file to disk with open(ini_file_path, 'wb') as configfile: @@ -311,22 +295,26 @@ class Vault(object): return False # TODO : wait for Ben input on how to unpublish tuto before coding this function -## def deleteTutorial(Tutorial): -## """ -## Removes the tutorial from the Vault. It will unpublish the tutorial if need be, -## and it will also wipe it from the persistent storage. -## @returns true is the tutorial was deleted from the Vault -## """ -## bundle = TutorialBundler(Guid) -## bundle_path = bundle.get_tutorial_path(Guid) -## -## # 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 + # For now, no unpublishing will occur. + + + @staticmethod + def deleteTutorial(Tutorial): + """ + Removes the tutorial from the Vault. It will unpublish the tutorial if need be, + and it will also wipe it from the persistent storage. + @returns true is the tutorial was deleted from the Vault + """ + bundle = TutorialBundler(Guid) + bundle_path = bundle.get_tutorial_path(Guid) + + # 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): @@ -643,14 +631,18 @@ class TutorialBundler(object): cfg.set(INI_METADATA_SECTION, INI_NAME_PROPERTY, tutorial.name) cfg.set(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY, TUTORIAL_FILENAME) cfg.add_section(INI_ACTIVITY_SECTION) - # TODO : Check behavior if environment varaibles dont exist - cfg.set(INI_ACTIVITY_SECTION, os.environ['SUGAR_BUNDLE_NAME'], + if os.environ['SUGAR_BUNDLE_NAME'] != None and os.environ['SUGAR_BUNDLE_VERSION'] != None: + cfg.set(INI_ACTIVITY_SECTION, os.environ['SUGAR_BUNDLE_NAME'], os.environ['SUGAR_BUNDLE_VERSION']) + else: + cfg.set(INI_ACTIVITY_SECTION, 'not_an_activity', '0') #Write the ini file cfg.write( file( os.path.join(self.Path, INI_FILENAME), 'w' ) ) - def get_tutorial_path(self, guid): + + @staticmethod + def get_tutorial_path(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 @@ -704,8 +696,10 @@ class TutorialBundler(object): # 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): + + + @staticmethod + def write_fsm(fsm): """ Save fsm to disk. If a GUID parameter is provided, the existing GUID is @@ -722,8 +716,8 @@ class TutorialBundler(object): xml_filename = config.get(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY) serializer.save_fsm(fsm, xml_filename, self.Path) - - def add_resources(self, typename, file): + @staticmethod + def add_resources(typename, file): """ Add ressources to metadata. """ |