From 1c971f09cba10a265e207ee7915cc2675dd45290 Mon Sep 17 00:00:00 2001 From: charles Date: Thu, 16 Apr 2009 15:28:36 +0000 Subject: Merge branch 'jc' of ssh://charles@bobthebuilder.mine.nu:8080/home/git into jc Conflicts: source/external/source/sugar-toolkit/src/sugar/tutorius/bundler.py --- diff --git a/src/sugar/tutorius/bundler.py b/src/sugar/tutorius/bundler.py index d88c9e6..0db8711 100644 --- a/src/sugar/tutorius/bundler.py +++ b/src/sugar/tutorius/bundler.py @@ -29,7 +29,20 @@ from sugar.tutorius import gtkutils, overlayer from sugar.tutorius.core import Tutorial, State, FiniteStateMachine from sugar.tutorius.actions import DialogMessage, OnceWrapper, BubbleMessage from sugar.tutorius.filters import GtkWidgetEventFilter, TimerEvent -from ConfigParser import SaveConfigParser +from ConfigParser import SafeConfigParser + +def __get_store_root(): + return os.path.join(os.getenv("SUGAR_PREFIX"),"share","tutorius","data") +def __get_bundle_root(): + return os.path.join(os.getenv("SUGAR_BUNDLE_PATH"),"data","tutorius","data") + +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_FILENAME = "meta.ini" +TUTORIAL_FILENAME = "tutorial.xml" class TutorialStore: @@ -39,20 +52,20 @@ class TutorialStore: given activity. """ - TUT_STORE_ROOT = os.getenv("$SUGAR_PREFIX") + "/share/tutorius/Data" - TUT_BUNDLE_ROOT = os.getenv("$SUGAR_BUNDLE_PATH") + "/data/tutorius/Data" + store_root = __get_store_root() + bundle_root = __get_bundle_root() - logging.debug("*********** Path of TUT_STORE_ROOT : " + TUT_STORE_ROOT) + logging.debug("*********** Path of store_root : " + store_root) # Create /data/tutorius if no exists - if not os.path.exists(TUT_STORE_ROOT): - os.mkdir(TUT_STORE_ROOT) - logging.debug("************* Creating /data/tutorius folder") + if not os.path.exists(store_root): + os.mkdir(store_root) + logging.debug("************* Creating %s folder" % store_root) tutoGuidName = {} - # iterate for each ".ini" file in the activity TUT_STORE_ROOT folder - for file_name in os.listdir(TUT_STORE_ROOT): + # iterate for each ".ini" file in the activity store_root folder + for file_name in os.listdir(store_root): if file_name.endswith(".ini"): logging.debug("************** .ini file found : " + file_name) @@ -95,10 +108,10 @@ class Serializer: def save_fsm(self,fsm, guid = None): """ Save fsm to disk. If a GUID parameter is provided, the existing GUID is - located in the .ini files in TUT_STORRE_ROOT and TUT_BUNDLE_ROOT and + located in the .ini files in the store root and bundle root and the corresponding FSM is/are overwritten. If the GUId is not found, an exception occur. If no GUID is provided, FSM is written in a new file - in TUT_STORE_ROOT. + in the store root. """ NotImplementedError @@ -163,83 +176,100 @@ class TutorialBundler: editor. """ - INI_ACTIVITY_SECTION = "RELATED_ACTIVITIES" - INI_METADATA_SECTION = "GENERAL_METADATA" - INI_GUID_PROPERTY = "GUID" - INI_NAME_PROPERTY = "NAME" - INI_XML_FSM_PROPERTY = "FSM_FILENAME" def __init__(self,generated_guid = None): """ - TODO. Tutorial_bundler constructor. If a GUID is given in the parameter, the Tutorial_bundler object will be associated with it. If no GUID is given, a new GUID will be generated, """ + self.Guid = generated_guid or uuid.uuid1() + + #Look for the file in the path if a uid is supplied + if generated_guid: + #General store + store_path = os.path.join(__get_store_root(), generated_guid, INI_FILENAME) + if os.path.isfile(store_path): + self.Path = os.path.dirname(store_path) + else: + #Bundle store + bundle_path = os.path.join(__get_bundle_root(), generated_guid, INI_FILENAME) + if os.path.isfile(bundle_path): + self.Path = os.path.dirname(bundle_path) + else: + raise IOError(2,"Unable to locate metadata file for guid '%s'" % generated_guid) + + else: + #Create the folder, any failure will go through to the caller for now + store_path = os.path.join(__get_store_root(), generated_guid) + os.mkdir(store_path) + self.Path = store_path - def __SetGuid(self, value): - self.__guid = value - - def __GetGuid(self): - return self.__guid - - def __DelGuid(self): - del self.__guid - - def __SetPath(self, value): - self.__path = value - - def __GetPath(self): - return self.__path - - def __DelPath(self): - del self.__path - - Guid = property(fget=__SetGuid, - fset=__GetGuid, - fdel=__DelGuid, - doc="The guid associated with the Tutoria_Bundler") - - Path = property(fget=__SetPath, - fset=__GetPath, - fdel=__DelPath, - doc="The path associated with the Tutoria_Bundler") + def __SetGuid(self, value): + self.__guid = value - if generated_gui is not None: - self.Guid = generated_guid - else: - self.Guid = uuid.uuid1() + def __GetGuid(self): + return self.__guid + + def __DelGuid(self): + del self.__guid + + def __SetPath(self, value): + self.__path = value + + def __GetPath(self): + return self.__path + + def __DelPath(self): + del self.__path + + Guid = property(fget=__SetGuid, + fset=__GetGuid, + fdel=__DelGuid, + doc="The guid associated with the Tutorial_Bundler") + + Path = property(fget=__SetPath, + fset=__GetPath, + fdel=__DelPath, + doc="The path associated with the Tutorial_Bundler") - #TODO : GUID generation if no guid is given in entry to the constructor. - def write_metadata_file(self, data): + def write_metadata_file(self, tutorial): """ - Write metadata to a property file. If a GUID is provided, TutorialBundler - will try to find and overwrite the existing property file who contain the - given GUID, and will raise an exception if it cannot find it. + Write metadata to the property file. + @param tutorial Tutorial for which to write metadata """ - NotImplementedError - + #Create the Config Object and populate it + cfg = SafeConfigParser() + cfg.add_section(INI_METADATA_SECTION) + cfg.set(INI_METADATA_SECTION, INI_GUID_PROPERTY, self.Guid) + cfg.set(INI_METADATA_SECTION, INI_NAME_PROPERTY, tutorial.name) + cfg.set(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY, TUTORIAL_FILENAME) + + + #Write the ini file + cfg.write( file( os.path.join(self.Path, INI_FILENAME) ) ) + def get_tutorial_path(self): """ Return the path of the .ini file associated with the guiven guid set in the Guid property of the Tutorial_Bundler. If the guid is present in - more than one path, the TUT_STORE_ROOT is given priority. + more than one path, the store_root is given priority. """ - TUT_STORE_ROOT = os.getenv("$SUGAR_PREFIX") + "/share/tutorius/Data" - TUT_BUNDLE_ROOT = os.getenv("$SUGAR_BUNDLE_PATH") + "/data/tutorius/Data" + store_root = __get_store_root() + bundle_root = __get_bundle_root() config = SafeConfigParser() path = None - logging.debug("************ Path of TUT_STORE_ROOT folder of activity : " \ - + TUT_STORE_ROOT) + logging.debug("************ Path of store_root folder of activity : " \ + + store_root) - # iterate for each .ini file in the TUT_STORE_ROOT folder + # iterate for each .ini file in the store_root folder - for file_name in os.listdir(TUT_STORE_ROOT): + for file_name in os.listdir(store_root): if file_name.endswith(".ini"): logging.debug("******************* Found .ini file : " \ + file_name) @@ -248,20 +278,20 @@ class TutorialBundler: xml_filename = config.get(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY) - path = TUT_STORE_ROOT + "/" + self.Guid + path = os.path.join(store_root, self.Guid) return path - logging.debug("************ Path of TUT_BUNDLE_ROOT folder of activity : " \ - + TUT_BUNDLE_ROOT) + logging.debug("************ Path of bundle_root folder of activity : " \ + + bundle_root) - # iterate for each .ini file in the TUT_BUNDLE_ROOT folder - for file_name in os.listdir(TUT_BUNDLE_ROOT): + # iterate for each .ini file in the bundle_root folder + for file_name in os.listdir(bundle_root): if file_name.endswith(".ini"): logging.debug("******************* Found .ini file : " \ + file_name) config.read(file_name) if config.get(INI_METADATA_SECTION, INI_GUID_PROPERTY) == guid: - path = TUT_BUNDLE_ROOT + "/" + self.Guid + path = os.path.join(bundle_root, self.Guid) return path if path is None: @@ -272,7 +302,7 @@ class TutorialBundler: """ Save fsm to disk. If a GUID parameter is provided, the existing GUID is - located in the .ini files in TUT_STORRE_ROOT and TUT_BUNDLE_ROOT and + located in the .ini files in the store root and bundle root and the corresponding FSM is/are created or overwritten. If the GUID is not found, an exception occur. """ @@ -283,7 +313,7 @@ class TutorialBundler: path = get_tutorial_path() + "/meta.ini" config.read(path) xml_filename = config.get(INI_METADATA_SECTION, INI_XML_FSM_PROPERTY) - save_fsm(fsm, xml_filename, TUT_STORE_ROOT) + save_fsm(fsm, xml_filename, store_root) def add_resources(self, typename, file): -- cgit v0.9.1