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

Side by Side Diff: cms/translations/xtm/cli.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Addressed comments from patch set #3 Created Oct. 5, 2018, 12:30 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.translations.xtm.xtm_api import (
25 XTMCloudException, get_token, XTMCloudAPI,
26 )
27 import cms.translations.xtm.constants as const
28 from cms.translations.xtm.projects_handler import (
29 create_project, upload_files, download_files,
30 )
31 from cms.translations.xtm.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
98 project_create_parser.add_argument(
99 '--client-id', required=True, type=int,
100 help=const.ArgumentsHelp.ProjectCreate.CLIENT_ID,
101 )
102 project_create_parser.add_argument(
103 '--ref-id', required=True,
104 help=const.ArgumentsHelp.ProjectCreate.REF_ID,
105 )
106 project_create_parser.add_argument(
107 '--workflow-id', required=True, type=int,
108 help=const.ArgumentsHelp.ProjectCreate.WORKFLOW_ID,
109 )
110 project_create_parser.add_argument(
111 '--source-lang', default='en_US',
112 help=const.ArgumentsHelp.ProjectCreate.SOURCE,
113 )
114 project_create_parser.add_argument(
115 '--save-id', action='store_true', default=False,
116 help=const.ArgumentsHelp.ProjectCreate.SAVE_ID,
117 )
118
119 # Subparser for uploading files to project
120 project_upload_parser = subparsers.add_parser(
121 'upload', help=const.ArgumentsHelp.ProjectUpload.MAIN,
122 )
123 project_upload_parser.set_defaults(func=handle_projects)
124 project_upload_parser.set_defaults(projects_func=upload_files)
125
126 project_upload_parser.add_argument(
127 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR,
128 default=os.getcwd(), nargs='?',
129 )
130
131 project_upload_parser.add_argument(
132 '--no-overwrite', action='store_true', default=False,
133 help=const.ArgumentsHelp.ProjectUpload.NO_OVERWRITE,
134 )
135
136 # Subparser for downloading files from project
137 download_parser = subparsers.add_parser(
138 'download', help=const.ArgumentsHelp.PROJECT_DOWNLOAD,
139 )
140 download_parser.set_defaults(func=handle_projects)
141 download_parser.set_defaults(projects_func=download_files)
142
143 download_parser.add_argument(
144 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR,
145 default=os.getcwd(), nargs='?',
146 )
147
148 return parser.parse_args()
149
150
151 def main():
152 """Run XTM integration script."""
153 args = parse_args()
154
155 if args.verbose:
156 logging.basicConfig(level=logging.INFO)
157
158 args.func(args)
159
160
161 if __name__ == '__main__':
162 main()
OLDNEW

Powered by Google App Engine
This is Rietveld