Left: | ||
Right: |
OLD | NEW |
---|---|
(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 """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.
| |
16 import argparse | |
17 import logging | |
18 import sys | |
19 import getpass | |
20 import os | |
21 | |
22 from cms.bin.xtm_translations.xtm_api import ( | |
23 XTMCloudException, get_token, | |
24 ) | |
25 import cms.bin.xtm_translations.constants as cnts | |
26 from cms.bin.xtm_translations.projects_handler import ( | |
27 main as handle_projects, | |
28 create_project, upload_files, download_files, | |
29 ) | |
30 | |
31 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.
| |
32 | |
33 | |
34 def generate_token(args): | |
35 """Generate an API token from username and password.""" | |
36 username = input('Username: ') | |
37 user_id = input('User ID: ') | |
38 password = getpass.getpass(prompt='Pasword: ') | |
39 | |
40 logging.info(cnts.InfoMessages.GENERATING_TOKEN.format(username, user_id)) | |
41 try: | |
42 token = get_token(username, password, int(user_id)) | |
43 logging.info(cnts.InfoMessages.TOKEN_GENERATED.format(token)) | |
44 | |
45 cmd = cnts.TOKEN['save_cmd'].format(cnts.TOKEN['env_var'], token) | |
46 sys.stdout.write(cnts.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd)) | |
47 except XTMCloudException as err: | |
48 sys.exit(err) | |
49 except Exception as err: | |
50 sys.exit(err) | |
51 | |
52 | |
53 def parse_args(): | |
54 parser = argparse.ArgumentParser() | |
55 subparsers = parser.add_subparsers() | |
56 | |
57 # Universal arguments | |
58 parser.add_argument('-v', '--verbose', action='store_true', | |
59 help=cnts.ARGUMENTS_HELP['verbose']) | |
60 | |
61 # Subparser for generating token | |
62 token_parser = subparsers.add_parser('login', | |
63 help=cnts.ARGUMENTS_HELP['login']) | |
64 token_parser.set_defaults(func=generate_token) | |
65 | |
66 # Subparser for project creation. | |
67 project_create_parser = subparsers.add_parser( | |
68 'create', | |
69 help=cnts.ARGUMENTS_HELP['project_create']['main'], | |
70 ) | |
71 project_create_parser.set_defaults(func=handle_projects) | |
72 project_create_parser.set_defaults(projects_func=create_project) | |
73 | |
74 project_create_parser.add_argument( | |
75 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'], | |
76 default=os.getcwd(), nargs='?', | |
77 ) | |
78 | |
79 project_create_parser.add_argument( | |
80 '--name', required=True, | |
81 help=cnts.ARGUMENTS_HELP['project_create']['name'], | |
82 ) | |
83 project_create_parser.add_argument( | |
84 '--desc', required=True, | |
85 help=cnts.ARGUMENTS_HELP['project_create']['desc'], | |
86 ) | |
87 # TODO: Decide what the description field means right now. It doesn't | |
88 # TODO: make sense to still have it as the URL to the GitLab issue, as | |
89 # TODO: a project is associated to a website, not an issue. Maybe url to | |
90 # TODO: website repo? | |
91 project_create_parser.add_argument( | |
92 '--client-id', required=True, type=int, | |
93 help=cnts.ARGUMENTS_HELP['project_create']['client_id'], | |
94 ) | |
95 # TODO: Decide what the clientId would signify on our end (if anything) | |
96 project_create_parser.add_argument( | |
97 '--ref-id', required=True, | |
98 help=cnts.ARGUMENTS_HELP['project_create']['ref_id'], | |
99 ) | |
100 # TODO: Decide what the referenceId would mean on our end | |
101 project_create_parser.add_argument( | |
102 '--workflow-id', required=True, type=int, | |
103 help=cnts.ARGUMENTS_HELP['project_create']['workflow_id'], | |
104 ) | |
105 # TODO: Decide what the workflowId would mean on our end | |
106 project_create_parser.add_argument( | |
107 '--source-lang', default='en_US', | |
108 help=cnts.ARGUMENTS_HELP['project_create']['source'], | |
109 ) | |
110 project_create_parser.add_argument( | |
111 '--save-id', action='store_true', default=False, | |
112 help=cnts.ARGUMENTS_HELP['project_create']['save-id'], | |
113 ) | |
114 | |
115 # Subparser for uploading files to project | |
116 project_upload_parser = subparsers.add_parser( | |
117 'upload', help=cnts.ARGUMENTS_HELP['project_upload']['main'], | |
118 ) | |
119 project_upload_parser.set_defaults(func=handle_projects) | |
120 project_upload_parser.set_defaults(projects_func=upload_files) | |
121 | |
122 project_upload_parser.add_argument( | |
123 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'], | |
124 default=os.getcwd(), nargs='?', | |
125 ) | |
126 | |
127 project_upload_parser.add_argument( | |
128 '--no-overwrite', action='store_true', default=False, | |
129 help=cnts.ARGUMENTS_HELP['project_upload']['no-overwrite'], | |
130 ) | |
131 | |
132 # Subparser for downloading files from project | |
133 download_parser = subparsers.add_parser( | |
134 'download', help=cnts.ARGUMENTS_HELP['project_download'], | |
135 ) | |
136 download_parser.set_defaults(func=handle_projects) | |
137 download_parser.set_defaults(projects_func=download_files) | |
138 | |
139 download_parser.add_argument( | |
140 'source_dir', help=cnts.ARGUMENTS_HELP['project_source-dir'], | |
141 default=os.getcwd(), nargs='?', | |
142 ) | |
143 | |
144 return parser.parse_args() | |
145 | |
146 | |
147 def main(): | |
148 """Run XTM integration script.""" | |
149 args = parse_args() | |
150 | |
151 if args.verbose: | |
152 logging.basicConfig(level=logging.INFO) | |
153 | |
154 args.func(args) | |
155 | |
156 | |
157 if __name__ == '__main__': | |
158 main() | |
OLD | NEW |