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

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

Issue 29968558: Issue 7037 - [XTM Integration] Make REST API url customizable
Patch Set: Addressed comments Created Sept. 17, 2019, 12:22 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
« no previous file with comments | « no previous file | cms/translations/xtm/constants.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 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 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 10 matching lines...) Expand all
21 import getpass 21 import getpass
22 import os 22 import os
23 23
24 from cms.translations.xtm.xtm_api import ( 24 from cms.translations.xtm.xtm_api import (
25 XTMCloudException, get_token, XTMCloudAPI, 25 XTMCloudException, get_token, XTMCloudAPI,
26 ) 26 )
27 import cms.translations.xtm.constants as const 27 import cms.translations.xtm.constants as const
28 from cms.translations.xtm.projects_handler import ( 28 from cms.translations.xtm.projects_handler import (
29 create_project, upload_files, download_files, 29 create_project, upload_files, download_files,
30 ) 30 )
31 from cms.translations.xtm.utils import input_fn, read_token 31 from cms.translations.xtm.utils import input_fn, read_token, get_api_url
32 from cms.sources import create_source 32 from cms.sources import create_source
33 33
34 34
35 def handle_projects(args): 35 def handle_projects(args):
36 try: 36 try:
37 api = XTMCloudAPI(read_token()) 37 token = read_token()
38 with create_source(args.source_dir, cached=True) as fs:
39 api = XTMCloudAPI(token, get_api_url(args, fs))
40 args.projects_func(args, api, fs)
38 except Exception as err: 41 except Exception as err:
39 sys.exit(err) 42 sys.exit(err)
40 with create_source(args.source_dir, cached=True) as fs:
41 args.projects_func(args, api, fs)
42 43
43 44
44 def generate_token(args): 45 def generate_token(args):
45 """Generate an API token from username and password.""" 46 """Generate an API token from username and password."""
46 username = input_fn('Username: ') 47 username = input_fn('Username: ')
47 user_id = input_fn('User ID: ') 48 user_id = input_fn('User ID: ')
48 password = getpass.getpass(prompt='Pasword: ') 49 password = getpass.getpass(prompt='Password: ')
49 50
50 logging.info(const.InfoMessages.GENERATING_TOKEN.format(username, user_id)) 51 logging.info(const.InfoMessages.GENERATING_TOKEN.format(username, user_id))
51 try: 52 try:
52 token = get_token(username, password, int(user_id)) 53 token = get_token(username, password, int(user_id),
54 get_api_url(args, None))
53 logging.info(const.InfoMessages.TOKEN_GENERATED.format(token)) 55 logging.info(const.InfoMessages.TOKEN_GENERATED.format(token))
54 56
55 cmd = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, token) 57 cmd = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, token)
56 sys.stdout.write(const.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd)) 58 sys.stdout.write(const.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd))
57 except XTMCloudException as err: 59 except XTMCloudException as err:
58 sys.exit(err) 60 sys.exit(err)
59 except Exception as err: 61 except Exception as err:
60 sys.exit(err) 62 sys.exit(err)
61 63
62 64
63 def parse_args(): 65 def parse_args():
64 parser = argparse.ArgumentParser() 66 parser = argparse.ArgumentParser()
65 subparsers = parser.add_subparsers() 67 subparsers = parser.add_subparsers()
66 68
67 # Universal arguments 69 # Universal arguments
68 parser.add_argument('-v', '--verbose', action='store_true', 70 parser.add_argument('-v', '--verbose', action='store_true',
69 help=const.ArgumentsHelp.VERBOSE) 71 help=const.ArgumentsHelp.VERBOSE)
72 parser.add_argument('--api-url', help=const.ArgumentsHelp.API_URL)
70 73
71 # Subparser for generating token 74 # Subparser for generating token
72 token_parser = subparsers.add_parser('login', 75 token_parser = subparsers.add_parser('login',
73 help=const.ArgumentsHelp.LOGIN) 76 help=const.ArgumentsHelp.LOGIN)
74 token_parser.set_defaults(func=generate_token) 77 token_parser.set_defaults(func=generate_token)
75 78
76 # Subparser for project creation. 79 # Subparser for project creation.
77 project_create_parser = subparsers.add_parser( 80 project_create_parser = subparsers.add_parser(
78 'create', 81 'create',
79 help=const.ArgumentsHelp.ProjectCreate.MAIN, 82 help=const.ArgumentsHelp.ProjectCreate.MAIN,
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 ) 153 )
151 154
152 return parser.parse_args() 155 return parser.parse_args()
153 156
154 157
155 def main(): 158 def main():
156 """Run XTM integration script.""" 159 """Run XTM integration script."""
157 args = parse_args() 160 args = parse_args()
158 161
159 if args.verbose: 162 if args.verbose:
160 logging.basicConfig(level=logging.INFO) 163 logging.basicConfig(level=logging.INFO, stream=sys.stderr)
161 164
162 args.func(args) 165 args.func(args)
163 166
164 167
165 if __name__ == '__main__': 168 if __name__ == '__main__':
166 main() 169 main()
OLDNEW
« no previous file with comments | « no previous file | cms/translations/xtm/constants.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld