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

Unified Diff: cms/bin/xtm_translations/translate_xtm_cloud.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/translate_xtm_cloud.py
diff --git a/cms/bin/xtm_translations/translate_xtm_cloud.py b/cms/bin/xtm_translations/translate_xtm_cloud.py
new file mode 100644
index 0000000000000000000000000000000000000000..93e78462b94f13b56e44e77437b97fe175260dd2
--- /dev/null
+++ b/cms/bin/xtm_translations/translate_xtm_cloud.py
@@ -0,0 +1,158 @@
+# 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/>.
+"""Script handling interaction with the XTM Cloud REST API."""
Vasily Kuznetsov 2018/09/24 16:32:11 This part would be more readable if we separate th
Tudor Avram 2018/09/25 12:26:25 Done.
+import argparse
+import logging
+import sys
+import getpass
+import os
+
+from cms.bin.xtm_translations.xtm_api import (
+ XTMCloudException, get_token,
+)
+import cms.bin.xtm_translations.constants as cnts
+from cms.bin.xtm_translations.projects_handler import (
+ main as handle_projects,
+ create_project, upload_files, download_files,
+)
+
+input = cnts.input_fn
Vasily Kuznetsov 2018/09/24 16:32:11 Again, we can just call `cnts.input_fn` (or maybe
Tudor Avram 2018/09/25 12:26:25 Done.
+
+
+def generate_token(args):
+ """Generate an API token from username and password."""
+ username = input('Username: ')
+ user_id = input('User ID: ')
+ password = getpass.getpass(prompt='Pasword: ')
+
+ logging.info(cnts.InfoMessages.GENERATING_TOKEN.format(username, user_id))
+ try:
+ token = get_token(username, password, int(user_id))
+ logging.info(cnts.InfoMessages.TOKEN_GENERATED.format(token))
+
+ cmd = cnts.TOKEN['save_cmd'].format(cnts.TOKEN['env_var'], token)
+ sys.stdout.write(cnts.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd))
+ except XTMCloudException as err:
+ sys.exit(err)
+ except Exception as err:
+ sys.exit(err)
+
+
+def parse_args():
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+
+ # Universal arguments
+ parser.add_argument('-v', '--verbose', action='store_true',
+ help=cnts.ARGUMENTS_HELP['verbose'])
+
+ # Subparser for generating token
+ token_parser = subparsers.add_parser('login',
+ help=cnts.ARGUMENTS_HELP['login'])
+ token_parser.set_defaults(func=generate_token)
+
+ # Subparser for project creation.
+ project_create_parser = subparsers.add_parser(
+ 'create',
+ help=cnts.ARGUMENTS_HELP['project_create']['main'],
+ )
+ project_create_parser.set_defaults(func=handle_projects)
+ project_create_parser.set_defaults(projects_func=create_project)
+
+ project_create_parser.add_argument(
+ 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'],
+ default=os.getcwd(), nargs='?',
+ )
+
+ project_create_parser.add_argument(
+ '--name', required=True,
+ help=cnts.ARGUMENTS_HELP['project_create']['name'],
+ )
+ project_create_parser.add_argument(
+ '--desc', required=True,
+ help=cnts.ARGUMENTS_HELP['project_create']['desc'],
+ )
+ # TODO: Decide what the description field means right now. It doesn't
+ # TODO: make sense to still have it as the URL to the GitLab issue, as
+ # TODO: a project is associated to a website, not an issue. Maybe url to
+ # TODO: website repo?
+ project_create_parser.add_argument(
+ '--client-id', required=True, type=int,
+ help=cnts.ARGUMENTS_HELP['project_create']['client_id'],
+ )
+ # TODO: Decide what the clientId would signify on our end (if anything)
+ project_create_parser.add_argument(
+ '--ref-id', required=True,
+ help=cnts.ARGUMENTS_HELP['project_create']['ref_id'],
+ )
+ # TODO: Decide what the referenceId would mean on our end
+ project_create_parser.add_argument(
+ '--workflow-id', required=True, type=int,
+ help=cnts.ARGUMENTS_HELP['project_create']['workflow_id'],
+ )
+ # TODO: Decide what the workflowId would mean on our end
+ project_create_parser.add_argument(
+ '--source-lang', default='en_US',
+ help=cnts.ARGUMENTS_HELP['project_create']['source'],
+ )
+ project_create_parser.add_argument(
+ '--save-id', action='store_true', default=False,
+ help=cnts.ARGUMENTS_HELP['project_create']['save-id'],
+ )
+
+ # Subparser for uploading files to project
+ project_upload_parser = subparsers.add_parser(
+ 'upload', help=cnts.ARGUMENTS_HELP['project_upload']['main'],
+ )
+ project_upload_parser.set_defaults(func=handle_projects)
+ project_upload_parser.set_defaults(projects_func=upload_files)
+
+ project_upload_parser.add_argument(
+ 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'],
+ default=os.getcwd(), nargs='?',
+ )
+
+ project_upload_parser.add_argument(
+ '--no-overwrite', action='store_true', default=False,
+ help=cnts.ARGUMENTS_HELP['project_upload']['no-overwrite'],
+ )
+
+ # Subparser for downloading files from project
+ download_parser = subparsers.add_parser(
+ 'download', help=cnts.ARGUMENTS_HELP['project_download'],
+ )
+ download_parser.set_defaults(func=handle_projects)
+ download_parser.set_defaults(projects_func=download_files)
+
+ download_parser.add_argument(
+ 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'],
+ default=os.getcwd(), nargs='?',
+ )
+
+ return parser.parse_args()
+
+
+def main():
+ """Run XTM integration script."""
+ args = parse_args()
+
+ if args.verbose:
+ logging.basicConfig(level=logging.INFO)
+
+ args.func(args)
+
+
+if __name__ == '__main__':
+ main()

Powered by Google App Engine
This is Rietveld