Left: | ||
Right: |
OLD | NEW |
---|---|
1 import os | |
2 | |
3 from collections import namedtuple | |
4 | |
1 from flask import Flask, send_from_directory, jsonify, request | 5 from flask import Flask, send_from_directory, jsonify, request |
2 | 6 |
3 app = Flask(__name__) | 7 app = Flask('mock_api') |
4 app.request_log = [] | |
5 | 8 |
6 | 9 |
7 @app.before_request | 10 @app.before_request |
8 def log_request_info(): | 11 def log_request_info(): |
9 log = (request.url, str(request.get_data())) | 12 app.request_log.append((request.url, str(request.get_data()))) |
10 app.request_log.append(log) | |
11 | 13 |
12 | 14 |
13 @app.route('/api/project/test/info', methods=['GET']) | 15 @app.route('/api/project/test/info', methods=['GET']) |
14 def info(): | 16 def info(): |
15 return jsonify( | 17 return jsonify(app.config['info']) |
16 { | |
17 'languages': [ | |
18 { | |
19 'name': 'German', | |
20 'code': 'de', | |
21 'can_translate': 1, | |
22 'can_approve': 1, | |
23 | 18 |
24 }, | |
25 { | |
26 'name': 'English', | |
27 'code': 'en', | |
28 'can_translate': 1, | |
29 'can_approve': 1, | |
30 | 19 |
31 }, | 20 @app.route('/api/project/test/edit-project', methods=['POST']) |
32 | 21 def edit(): |
33 ], | 22 return jsonify() |
34 'files': [ | |
35 { | |
36 'node_type': 'directory', | |
37 'name': 'en', | |
38 'files': [ | |
39 { | |
40 'node_type': 'file', | |
41 'name': 'translate.json', | |
42 'created': '2016-09-26 08:30:07', | |
43 'last_updated': '2016-09-26 08:30:08', | |
44 'last_accessed': None, | |
45 'last_revision': '1' | |
46 }, | |
47 ] | |
48 }, | |
49 { | |
50 'node_type': 'directory', | |
51 'name': 'de', | |
52 'files': [ | |
53 { | |
54 'node_type': 'file', | |
55 'name': 'translate.json', | |
56 'created': '2016-09-26 08:30:07', | |
57 'last_updated': '2016-09-26 08:30:08', | |
58 'last_accessed': None, | |
59 'last_revision': '1' | |
60 } | |
61 ] | |
62 } | |
63 ] | |
64 } | |
65 ) | |
66 | 23 |
67 | 24 |
68 @app.route('/api/project/test/supported-languages', methods=['GET']) | 25 @app.route('/api/project/test/supported-languages', methods=['GET']) |
69 def supported_langs(): | 26 def supported_langs(): |
70 return jsonify( | 27 return jsonify(app.config['languages']) |
71 [ | |
72 { | |
73 'name': 'German', | |
74 'crowdin_code': 'de', | |
75 'editor_code': 'de', | |
76 'iso_639_1': 'de', | |
77 'iso_639_3': 'deu', | |
78 'locale': 'de-DE' | |
79 }, | |
80 { | |
81 'name': 'English', | |
82 'crowdin_code': 'en', | |
83 'editor_code': 'en', | |
84 'iso_639_1': 'en', | |
85 'iso_639_3': 'eng', | |
86 'locale': 'en-US' | |
87 }, | |
88 ] | |
89 ) | |
90 | 28 |
91 | 29 |
92 @app.route('/api/project/test/add-file', methods=['POST']) | 30 @app.route('/api/project/test/add-file', methods=['POST']) |
93 def add_file(): | 31 def add_file(): |
94 app.string = request.get_data() | 32 app.string = request.get_data() |
95 return jsonify() | 33 return jsonify() |
96 | 34 |
97 | 35 |
98 @app.route('/api/project/test/upload-translation', methods=['POST']) | 36 @app.route('/api/project/test/upload-translation', methods=['POST']) |
99 def upload_translation(): | 37 def upload_translation(): |
(...skipping 11 matching lines...) Expand all Loading... | |
111 | 49 |
112 | 50 |
113 @app.route('/api/project/test/export', methods=['GET']) | 51 @app.route('/api/project/test/export', methods=['GET']) |
114 def export(): | 52 def export(): |
115 return jsonify({'success': {'status': 'skipped'}}) | 53 return jsonify({'success': {'status': 'skipped'}}) |
116 | 54 |
117 | 55 |
118 @app.route('/api/project/test/download/all.zip', methods=['GET']) | 56 @app.route('/api/project/test/download/all.zip', methods=['GET']) |
119 def get_zip(): | 57 def get_zip(): |
120 return send_from_directory('', 'all.zip') | 58 return send_from_directory('', 'all.zip') |
59 | |
60 | |
61 Locale = namedtuple('Locale', ['name', 'node_type', 'files']) | |
62 File = namedtuple('File', ['name', 'node_type']) | |
63 Language = namedtuple('Language', ['code', 'can_approve', 'can_translate']) | |
64 Supported = namedtuple('Supported', ['crowdin_code']) | |
Vasily Kuznetsov
2018/03/05 17:21:32
Maybe rename this to `SupportedLanguage`? Otherwis
| |
65 | |
66 | |
67 class CrowdinMock: | |
68 def __init__(self, locales_root): | |
69 self.info = {'files': [], 'languages': []} | |
70 self.languages = [] | |
71 self.load(locales_root) | |
72 | |
73 def load(self, filepath): | |
74 for root, locales, files in os.walk(filepath): | |
75 for locale in locales: | |
76 files = [] | |
77 self.info['languages'].append(Language(locale, 1, 1)) | |
78 self.languages.append(Supported(locale)) | |
79 for translations in os.listdir(os.path.join(root, locale)): | |
80 filenode = File(translations, 'file') | |
81 files.append(filenode._asdict()) | |
82 localenode = Locale(locale, 'directory', tuple(files)) | |
Vasily Kuznetsov
2018/03/05 17:21:32
Why are you converting it to tuple here? Seems lik
Jon Sonesen
2018/03/06 02:30:18
Acknowledged.
| |
83 self.info['files'].append(localenode) | |
84 | |
85 def run(self): | |
86 app.request_log = [] | |
87 self.info['files'] = [i._asdict() for i in self.info['files']] | |
Vasily Kuznetsov
2018/03/05 17:21:32
It seems like this conversion is basically part of
Jon Sonesen
2018/03/06 02:30:18
Acknowledged.
| |
88 self.info['languages'] = [i._asdict() for i in self.info['languages']] | |
89 app.config['info'] = self.info | |
90 app.config['languages'] = [i._asdict() for i in self.languages] | |
91 self.app = app | |
OLD | NEW |