diff options
Diffstat (limited to 'tutorius/vault.py')
-rw-r--r-- | tutorius/vault.py | 90 |
1 files changed, 65 insertions, 25 deletions
diff --git a/tutorius/vault.py b/tutorius/vault.py index 2b9c5b9..15c7e17 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -32,6 +32,7 @@ from ConfigParser import SafeConfigParser from . import addon from .tutorial import Tutorial, State, AutomaticTransitionEvent +from localization import LocalizationHelper logger = logging.getLogger("tutorius") @@ -60,6 +61,7 @@ INI_CATEGORY_PROPERTY = 'category' INI_FILENAME = "meta.ini" TUTORIAL_FILENAME = "tutorial.xml" RESOURCES_FOLDER = 'resources' +LOCALIZATION_FOLDER = 'localization' ###################################################################### # XML Tag names and attributes @@ -327,35 +329,41 @@ class Vault(object): # 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) - with open(os.path.join(tutorial_path, TUTORIAL_FILENAME), 'w') as fsmfile: - xml_ser.save_tutorial(tutorial, fsmfile) - - # 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) + # Serialize the tutorial and write it to disk + xml_ser = XMLSerializer() - # Write the file to disk - with open(ini_file_path, 'wb') as configfile: - parser.write(configfile) + with open(os.path.join(tutorial_path, TUTORIAL_FILENAME), 'w') as fsmfile: + xml_ser.save_tutorial(tutorial, fsmfile) + + # 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) + + # Write the file to disk + with open(ini_file_path, 'wb') as configfile: + parser.write(configfile) + l10n_path = os.path.join(tutorial_path, LOCALIZATION_FOLDER) + try: + os.mkdir(l10n_path) + except: + # FIXME : Ignore error as we suppose it is + # the directory already exists + pass + # Write the localization template (.pot) file + with open(os.path.join(l10n_path, 'tutorial_text.pot'), "wb") as l10n_file: + LocalizationHelper.write_translation_file(tutorial, l10n_file) - else: - # Error, tutorial already exist - return False - @staticmethod def deleteTutorial(Guid): @@ -430,15 +438,28 @@ class Vault(object): for root, dirs, files in os.walk(os.path.join(bundle_path, RESOURCES_FOLDER)): for name in files: archive_list.append(os.path.join(bundle_path, RESOURCES_FOLDER, name)) + + logger.debug("Vault :: Looking for translation .mo files...") + + for root, dirs, files in os.walk(os.path.join(bundle_path, LOCALIZATION_FOLDER)): + logger.debug("Vault :: Inspecting files %s at root %s", str(files), root) + for name in files: + if name == "tutorial_text.mo": + fname_splitted = root.rsplit('/') + archive_list.append(os.path.join(bundle_path, LOCALIZATION_FOLDER, fname_splitted[-2], fname_splitted[-1], name)) zfilename = str(guid) + ".zip" zout = zipfile.ZipFile(os.path.join(bundle_path, zfilename), "w") for fname in archive_list: + logger.debug("Vault :: zipping file %s"%fname) fname_splitted = fname.rsplit('/') if fname_splitted[-2] == RESOURCES_FOLDER: ressource_path_and_file = os.path.join(fname_splitted[-2], fname_splitted[-1]) zout.write(fname, ressource_path_and_file) + elif len(fname_splitted) >= 4 and fname_splitted[-4] == LOCALIZATION_FOLDER: + translation_path_and_file = os.path.join(*fname_splitted[-4:]) + zout.write(fname, translation_path_and_file) else: file_only_name = fname_splitted[-1] zout.write(fname, file_only_name) @@ -528,6 +549,25 @@ class Vault(object): else: return None + @staticmethod + def get_localization_dir(tutorial_guid): + """ + Returns the base folder under which all the <language>/LC_MESSAGES/tutorial_text.mo + are stored. These files are used for runtime translations by the Resource + Translator. + + @param tutorial_guid the guid of the tutorial + @returns the directory that stores the translation objects + """ + # Get the tutorial path + bundler = TutorialBundler(tutorial_guid) + tutorial_path = bundler.get_tutorial_path(tutorial_guid) + # Check if the localization directory exists + l10n_dir = os.path.join(tutorial_path, LOCALIZATION_FOLDER) + if os.path.isdir(l10n_dir): + return l10n_dir + else: + return None class Serializer(object): """ |