Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: cms/bin/xtm_translations/projects_handler.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Created Sept. 20, 2018, 4:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: cms/bin/xtm_translations/projects_handler.py
diff --git a/cms/bin/xtm_translations/projects_handler.py b/cms/bin/xtm_translations/projects_handler.py
new file mode 100644
index 0000000000000000000000000000000000000000..18d65f81e41e1e61f3dfedaa3414eb462849f346
--- /dev/null
+++ b/cms/bin/xtm_translations/projects_handler.py
@@ -0,0 +1,200 @@
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-present eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+import sys
+import logging
+import ConfigParser
+import zipfile
+from io import BytesIO
+
+import cms.bin.xtm_translations.constants as cnts
Vasily Kuznetsov 2018/09/24 16:32:11 What do you think about using "cnst" instead of "c
Tudor Avram 2018/09/25 12:26:24 Used `const`. And replaced it in all other files D
+from cms.bin.xtm_translations.xtm_api import XTMCloudAPI, XTMCloudException
+from cms.sources import FileSource
+from cms.bin.xtm_translations import utils
+
+__all__ = [
+ 'create_project', 'upload_files', 'download_files', 'main',
+]
+
+input = cnts.input_fn
Vasily Kuznetsov 2018/09/24 16:32:11 Since this is not used this much, we could just ca
Tudor Avram 2018/09/25 12:26:25 Oh, actually it isn't anymore. It was used initial
+
+
+def create_project(args, api, source):
+ """Create a project.
+
+ Parameters
+ ----------
+ args: argparse.Namespace
+ The arguments parsed by the main script.
+ api: XTMCloudAPI
+ Used for interacting with the project.
+ source: FileSource
+ Representing the website the project is created for.
+
+ """
+ config = source.read_config()
+
+ try:
+ project_id = config.get(cnts.CONFIG['XTM_section'],
+ cnts.CONFIG['project_option'])
+ sys.exit(cnts.ErrorMessages.EXISTENT_PROJECT.format(project_id))
+ except ConfigParser.NoOptionError, ConfigParser.NoSectionError:
+ pass
+
+ target_langs = utils.map_locales(source)
+
+ page_strings = utils.extract_strings(source)
+ files = utils.get_local_files(page_strings)
+ files_to_upload = utils.get_files_to_upload(files, page_strings)
+
+ try:
+ name = utils.resolve_naming_conflicts(args.name)
+ logging.info(cnts.InfoMessages.PROJECT_NAME_CREATING.format(name))
+ logging.info(cnts.InfoMessages.UPLOADING_FILES.format(len(files)))
+
+ project_id, resulting_jobs = utils.run_and_wait(
+ api.create_project,
+ XTMCloudException,
+ cnts.UNDER_ANALYSIS_MESSAGE,
+ cnts.InfoMessages.WAITING_FOR_PROJECT,
+ name=name,
+ description=args.desc,
+ reference_id=args.ref_id,
+ target_languages=target_langs,
+ customer_id=args.client_id,
+ workflow_id=args.workflow_id,
+ source_language=args.source_lang,
+ files=files_to_upload,
+ )
+ except XTMCloudException as err:
+ sys.exit(err)
+
+ logging.info(cnts.InfoMessages.PROJECT_CREATED.format(project_id))
+ utils.log_resulting_jobs(resulting_jobs)
+
+ if args.save_id:
+ source.write_to_config(
+ cnts.CONFIG['XTM_section'],
+ cnts.CONFIG['project_option'],
+ str(project_id),
+ )
+ logging.info(cnts.InfoMessages.SAVED_PROJECT_ID)
+
+
+def upload_files(args, api, source):
+ """Upload files to project.
+
+ Parameters
+ ----------
+ args: argparse.Namespace
+ The arguments parsed by the main script.
+ api: XTMCloudAPI
+ Used for interacting with the project.
+ source: cms.sources.FileSource
+ Representing the website the project is created for.
+
+ """
+ config = source.read_config()
+ try:
+ project_id = config.get(cnts.CONFIG['XTM_section'],
+ cnts.CONFIG['project_option'])
+ except ConfigParser.NoOptionError, ConfigParser.NoSectionError:
+ sys.exit(cnts.ErrorMessages.NO_PROJECT.format(source.get_path('')))
+
+ utils.resolve_locales(api, source)
+
+ page_strings = utils.extract_strings(source)
+ files = utils.get_local_files(page_strings)
+ files_to_upload = utils.get_files_to_upload(files, page_strings)
+ logging.info(cnts.InfoMessages.UPLOADING_FILES.format(len(files)))
+
+ new_jobs = utils.run_and_wait(
+ api.upload_files,
+ XTMCloudException,
+ cnts.UNDER_ANALYSIS_MESSAGE,
+ cnts.InfoMessages.WAITING_FOR_PROJECT,
+ files=files_to_upload,
+ project_id=project_id,
+ overwrite=not args.no_overwrite,
+ )
+
+ logging.info(cnts.InfoMessages.FILES_UPLOADED)
+ utils.log_resulting_jobs(new_jobs)
+
+
+def download_files(args, api, source):
+ """Download the translation files and save them as locales.
+
+ Parameters
+ ----------
+ args: argparse.Namespace
+ The arguments provided when running the script
+ api: XTMCloudAPI
+ Used for interacting with the project.
+ source: FileSource
+ Used to write the downloaded translation files locally.
+
+ """
+ try:
+ raw_bytes = api.download_files(
+ source.read_config().get(
+ cnts.CONFIG['XTM_section'], cnts.CONFIG['project_option'],
+ ),
+ )
+ except ConfigParser.NoOptionError, ConfigParser.NoSectionError:
+ sys.exit(cnts.ErrorMessages.NO_PROJECT.format(source.get_path('')))
+ except XTMCloudException as err:
+ sys.exit(err)
+ except Exception as err:
+ sys.exit(err)
+
+ try:
+ with zipfile.ZipFile(BytesIO(raw_bytes)) as zf:
+ zip_contents = zf.namelist()
+ if len(zip_contents) == 0:
+ sys.exit(
+ cnts.InfoMessages.NO_FILES_FOUND.format(args.project_id),
+ )
+
+ logging.info(cnts.InfoMessages.FILES_DOWNLOADED)
+ locales_path = source.get_path('locales')
+ default_locale = source.read_config().get(
+ cnts.CONFIG['main_section'],
+ cnts.CONFIG['default_locale_option'],
+ )
+ valid_locales = utils.get_locales(locales_path, default_locale)
+ utils.clear_files(locales_path, valid_locales)
+
+ for name in zip_contents:
+ path = utils.resolve_remote_filename(name, locales_path,
+ valid_locales)
+ utils.write_to_file(zf.read(name), path)
+ logging.info(cnts.InfoMessages.FILE_SAVED.format(
+ path,
+ zf.getinfo(name).file_size,
+ ))
+ logging.info(cnts.InfoMessages.GREETINGS)
+ except zipfile.BadZipfile:
+ sys.exit(cnts.ErrorMessages.NO_TARGET_FILES_FOUND)
+ except IOError:
+ sys.exit(cnts.ErrorMessages.COULD_NOT_SAVE_FILES)
+
+
+def main(args):
+ try:
+ api = XTMCloudAPI(utils.read_token())
+ except Exception as err:
+ sys.exit(err)
+ with FileSource(args.source_dir) as fs:
+ args.projects_func(args, api, fs)

Powered by Google App Engine
This is Rietveld