diff options
Diffstat (limited to 'tutorius/vault.py')
-rw-r--r-- | tutorius/vault.py | 78 |
1 files changed, 75 insertions, 3 deletions
diff --git a/tutorius/vault.py b/tutorius/vault.py index d6b4720..728cf64 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -58,6 +58,7 @@ INI_XML_FSM_PROPERTY = "fsm_filename" INI_VERSION_PROPERTY = 'version' INI_FILENAME = "meta.ini" TUTORIAL_FILENAME = "tutorial.xml" +RESOURCES_FOLDER = 'resources' ###################################################################### # XML Tag names and attributes @@ -85,7 +86,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 tutorial for. 0 means find for ANY version. If activity_name is None, version number is not used @returns a map of tutorial {names : GUID}. """ # check both under the activity data and user installed folders @@ -249,6 +250,7 @@ class Vault(object): # Return tutorial list return tutorial_list + @staticmethod def loadTutorial(Guid): @@ -323,7 +325,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. @@ -341,6 +343,76 @@ class Vault(object): return False + @staticmethod + def add_resource(tutorial_guid, file_path): + """ + Add given resource file in the vault and 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 ). + @param tutorial_guid The guid of the tutorial + @param file_path the file path of the resource to add + @returns the resource_id of the resource + """ + + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Get the file name + file_name = os.path.basename(file_path) + #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 resource file already exists + new_file_path = os.path.join(tutorial_path, RESOURCES_FOLDER, file_name_appended) + if os.path.isfile(new_file_path) == False: + # Copy the resource file in the vault + if os.path.isdir(os.path.join(tutorial_path, RESOURCES_FOLDER)) == False: + os.makedirs(os.path.join(tutorial_path, RESOURCES_FOLDER)) + assert os.path.isfile(file_path) + shutil.copyfile(file_path, new_file_path) + + return file_name_appended + + + @staticmethod + def delete_resource(tutorial_guid, resource_id): + """ + Delete the resource from the resources of the tutorial. + @param tutorial_guid the guid of the tutorial + @param resource_id the resource id of the resource to delete + """ + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Check if the resource file exists + file_path = os.path.join(tutorial_path, RESOURCES_FOLDER, resource_id) + if os.path.isfile(file_path): + # Delete the resource + os.remove(file_path) + else: + print('File not found, no delete took place') + + @staticmethod + def get_resource_path(tutorial_guid, resource_id): + """ + Returns the absolute file path to the resourceID + @param tutorial_guid the guid of the tutorial + @param resource_id the resource id of the resource to find the path for + @returns the absolute path of the resource file + """ + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Check if the resource file exists + file_path = os.path.join(tutorial_path, RESOURCES_FOLDER, resource_id) + if os.path.isfile(file_path): + return file_path + else: + return None + + class Serializer(object): """ Interface that provide serializing and deserializing of the FSM @@ -914,6 +986,6 @@ class TutorialBundler(object): @staticmethod def add_resources(typename, file): """ - Add ressources to metadata. + Add resources to metadata. """ raise NotImplementedError("add_resources not implemented") |