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