Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 import os | 1 import os |
2 | |
3 from collections import namedtuple | |
4 | 2 |
5 from flask import Flask, send_from_directory, jsonify, request | 3 from flask import Flask, send_from_directory, jsonify, request |
6 | 4 |
7 app = Flask('mock_api') | 5 app = Flask('mock_api') |
6 | |
7 | |
8 def load(rootdir, zipdir): | |
9 app.request_log = [] | |
10 app.config['zipdir'] = zipdir | |
11 app.config['info'] = {'files': [], 'languages': []} | |
12 app.config['supported_languages'] = [] | |
13 | |
14 for root, locales, files in os.walk(rootdir): | |
15 for locale in locales: | |
16 files = [] | |
17 app.config['supported_languages'].append({'crowdin_code': locale}) | |
18 app.config['info']['languages'].append({ | |
19 'code': locale, | |
20 'can_translate': 1, | |
21 'can_approve': 1, | |
22 }) | |
23 | |
24 for translations in os.listdir(os.path.join(root, locale)): | |
25 files.append({'name': translations, 'node_type': 'file'}) | |
26 | |
27 app.config['info']['files'].append({ | |
28 'name': locale, | |
29 'files': files, | |
30 'node_type': 'directory', | |
31 }) | |
32 | |
33 return app | |
8 | 34 |
9 | 35 |
10 @app.before_request | 36 @app.before_request |
11 def log_request_info(): | 37 def log_request_info(): |
12 app.request_log.append((request.url, str(request.get_data()))) | 38 app.request_log.append((request.url, str(request.get_data()))) |
13 | 39 |
14 | 40 |
15 @app.route('/api/project/test/info', methods=['GET']) | 41 @app.route('/api/project/test/info', methods=['GET']) |
16 def info(): | 42 def info(): |
17 return jsonify(app.config['info']) | 43 return jsonify(app.config['info']) |
18 | 44 |
19 | 45 |
20 @app.route('/api/project/test/edit-project', methods=['POST']) | 46 @app.route('/api/project/test/edit-project', methods=['POST']) |
21 def edit(): | 47 def edit(): |
22 return jsonify() | 48 return jsonify() |
23 | 49 |
24 | 50 |
25 @app.route('/api/project/test/supported-languages', methods=['GET']) | 51 @app.route('/api/project/test/supported-languages', methods=['GET']) |
26 def supported_langs(): | 52 def supported_langs(): |
27 return jsonify(app.config['languages']) | 53 return jsonify(app.config['supported_languages']) |
28 | 54 |
29 | 55 |
30 @app.route('/api/project/test/add-file', methods=['POST']) | 56 @app.route('/api/project/test/add-file', methods=['POST']) |
31 def add_file(): | 57 def add_file(): |
32 app.string = request.get_data() | 58 app.string = request.get_data() |
33 return jsonify() | 59 return jsonify() |
34 | 60 |
35 | 61 |
36 @app.route('/api/project/test/upload-translation', methods=['POST']) | 62 @app.route('/api/project/test/upload-translation', methods=['POST']) |
37 def upload_translation(): | 63 def upload_translation(): |
(...skipping 10 matching lines...) Expand all Loading... | |
48 return jsonify() | 74 return jsonify() |
49 | 75 |
50 | 76 |
51 @app.route('/api/project/test/export', methods=['GET']) | 77 @app.route('/api/project/test/export', methods=['GET']) |
52 def export(): | 78 def export(): |
53 return jsonify({'success': {'status': 'skipped'}}) | 79 return jsonify({'success': {'status': 'skipped'}}) |
54 | 80 |
55 | 81 |
56 @app.route('/api/project/test/download/all.zip', methods=['GET']) | 82 @app.route('/api/project/test/download/all.zip', methods=['GET']) |
57 def get_zip(): | 83 def get_zip(): |
58 return send_from_directory('', 'all.zip') | 84 return send_from_directory(app.config['zipdir'], '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 | |
LEFT | RIGHT |