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

Side by Side Diff: cms/bin/xtm_translations/translate_xtm_cloud.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Addressed comments on patch set #2 Created Oct. 3, 2018, 2:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 """Script handling interaction with the XTM Cloud REST API."""
17
18 import argparse
19 import logging
20 import sys
21 import getpass
22 import os
23
24 from cms.bin.xtm_translations.xtm_api import (
25 XTMCloudException, get_token, XTMCloudAPI,
26 )
27 import cms.bin.xtm_translations.constants as const
28 from cms.bin.xtm_translations.projects_handler import (
29 create_project, upload_files, download_files,
30 )
31 from cms.bin.xtm_translations.utils import input_fn, read_token
32 from cms.sources import create_source
33
34
35 def handle_projects(args):
36 try:
37 api = XTMCloudAPI(read_token())
38 except Exception as err:
39 sys.exit(err)
40 with create_source(args.source_dir, cached=True) as fs:
41 args.projects_func(args, api, fs)
42
43
44 def generate_token(args):
45 """Generate an API token from username and password."""
46 username = input_fn('Username: ')
47 user_id = input_fn('User ID: ')
48 password = getpass.getpass(prompt='Pasword: ')
49
50 logging.info(const.InfoMessages.GENERATING_TOKEN.format(username, user_id))
51 try:
52 token = get_token(username, password, int(user_id))
53 logging.info(const.InfoMessages.TOKEN_GENERATED.format(token))
54
55 cmd = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, token)
56 sys.stdout.write(const.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd))
57 except XTMCloudException as err:
58 sys.exit(err)
59 except Exception as err:
60 sys.exit(err)
61
62
63 def parse_args():
64 parser = argparse.ArgumentParser()
65 subparsers = parser.add_subparsers()
66
67 # Universal arguments
68 parser.add_argument('-v', '--verbose', action='store_true',
69 help=const.ArgumentsHelp.VERBOSE)
70
71 # Subparser for generating token
72 token_parser = subparsers.add_parser('login',
73 help=const.ArgumentsHelp.LOGIN)
74 token_parser.set_defaults(func=generate_token)
75
76 # Subparser for project creation.
77 project_create_parser = subparsers.add_parser(
78 'create',
79 help=const.ArgumentsHelp.ProjectCreate.MAIN,
80 )
81 project_create_parser.set_defaults(func=handle_projects)
82 project_create_parser.set_defaults(projects_func=create_project)
83
84 project_create_parser.add_argument(
85 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR,
86 default=os.getcwd(), nargs='?',
87 )
88
89 project_create_parser.add_argument(
90 '--name', required=True,
91 help=const.ArgumentsHelp.ProjectCreate.NAME,
92 )
93 project_create_parser.add_argument(
94 '--desc', required=True,
95 help=const.ArgumentsHelp.ProjectCreate.DESC,
96 )
97 # TODO: Decide what the description field means right now. It doesn't
98 # TODO: make sense to still have it as the URL to the GitLab issue, as
99 # TODO: a project is associated to a website, not an issue. Maybe url to
100 # TODO: website repo?
101 project_create_parser.add_argument(
102 '--client-id', required=True, type=int,
103 help=const.ArgumentsHelp.ProjectCreate.CLIENT_ID,
104 )
105 # TODO: Decide what the clientId would signify on our end (if anything)
106 project_create_parser.add_argument(
107 '--ref-id', required=True,
108 help=const.ArgumentsHelp.ProjectCreate.REF_ID,
109 )
110 # TODO: Decide what the referenceId would mean on our end
111 project_create_parser.add_argument(
112 '--workflow-id', required=True, type=int,
113 help=const.ArgumentsHelp.ProjectCreate.WORKFLOW_ID,
114 )
115 # TODO: Decide what the workflowId would mean on our end
116 project_create_parser.add_argument(
117 '--source-lang', default='en_US',
118 help=const.ArgumentsHelp.ProjectCreate.SOURCE,
119 )
120 project_create_parser.add_argument(
121 '--save-id', action='store_true', default=False,
122 help=const.ArgumentsHelp.ProjectCreate.SAVE_ID,
123 )
124
125 # Subparser for uploading files to project
126 project_upload_parser = subparsers.add_parser(
127 'upload', help=const.ArgumentsHelp.ProjectUpload.MAIN,
128 )
129 project_upload_parser.set_defaults(func=handle_projects)
130 project_upload_parser.set_defaults(projects_func=upload_files)
131
132 project_upload_parser.add_argument(
133 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR,
134 default=os.getcwd(), nargs='?',
135 )
136
137 project_upload_parser.add_argument(
138 '--no-overwrite', action='store_true', default=False,
139 help=const.ArgumentsHelp.ProjectUpload.NO_OVERWRITE,
140 )
141
142 # Subparser for downloading files from project
143 download_parser = subparsers.add_parser(
144 'download', help=const.ArgumentsHelp.PROJECT_DOWNLOAD,
145 )
146 download_parser.set_defaults(func=handle_projects)
147 download_parser.set_defaults(projects_func=download_files)
148
149 download_parser.add_argument(
150 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR,
151 default=os.getcwd(), nargs='?',
152 )
153
154 return parser.parse_args()
155
156
157 def main():
158 """Run XTM integration script."""
159 args = parse_args()
160
161 if args.verbose:
162 logging.basicConfig(level=logging.INFO)
163
164 args.func(args)
165
166
167 if __name__ == '__main__':
168 main()
OLDNEW

Powered by Google App Engine
This is Rietveld