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

Delta Between Two Patch Sets: tests/crowdin_mock_api.py

Issue 29693633: Noissue - Add crowdin api config support (Closed) Base URL: https://hg.adblockplus.org/cms
Left Patch Set: refactor abstractions for config and the wsgi app Created Feb. 13, 2018, 8:28 p.m.
Right Patch Set: Actually address PS6 comments Created March 12, 2018, 9:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | tests/test_translations.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 class CrowdinMock:
60 def __init__(self, locales_root):
61 self.info = {'files': [], 'languages': []}
62 self.languages = []
63 self.load(locales_root)
64
65 def load(self, filepath):
66 for root, locales, files in os.walk(filepath):
67 for locale in locales:
68 localenode = {'name': locale, 'node_type': 'directory',
69 'files': []}
70 self.info['languages'].append({'code': locale,
71 'can_approve': 1,
72 'can_translate': 1})
73 self.languages.append({'crowdin_code': locale})
74 for translations in os.listdir(os.path.join(root, locale)):
75 filenode = {'name': translations, 'node_type': 'file'}
76 localenode['files'].append(filenode)
77 self.info['files'].append(localenode)
78
79 def run(self):
80 app.request_log = []
81 app.config['info'] = self.info
82 app.config['languages'] = self.languages
83 self.app = app
LEFTRIGHT

Powered by Google App Engine
This is Rietveld