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

Unified Diff: tests/crowdin_mock_api.py

Issue 29693633: Noissue - Add crowdin api config support (Closed) Base URL: https://hg.adblockplus.org/cms
Patch Set: implement named tuples Created Feb. 18, 2018, 10:20 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/test_translations.py » ('j') | tests/test_translations.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/crowdin_mock_api.py
===================================================================
--- a/tests/crowdin_mock_api.py
+++ b/tests/crowdin_mock_api.py
@@ -1,97 +1,35 @@
+import os
+
+from collections import namedtuple
+
from flask import Flask, send_from_directory, jsonify, request
-app = Flask(__name__)
-app.request_log = []
+app = Flask('mock_api')
@app.before_request
def log_request_info():
- log = (request.url, str(request.get_data()))
- app.request_log.append(log)
+ app.request_log.append((request.url, str(request.get_data())))
@app.route('/api/project/test/info', methods=['GET'])
def info():
- return jsonify(
- {
- 'languages': [
- {
- 'name': 'German',
- 'code': 'de',
- 'can_translate': 1,
- 'can_approve': 1,
+ return jsonify(app.config['info'])
- },
- {
- 'name': 'English',
- 'code': 'en',
- 'can_translate': 1,
- 'can_approve': 1,
-
- },
- ],
- 'files': [
- {
- 'node_type': 'directory',
- 'name': 'en',
- 'files': [
- {
- 'node_type': 'file',
- 'name': 'translate.json',
- 'created': '2016-09-26 08:30:07',
- 'last_updated': '2016-09-26 08:30:08',
- 'last_accessed': None,
- 'last_revision': '1'
- },
- ]
- },
- {
- 'node_type': 'directory',
- 'name': 'de',
- 'files': [
- {
- 'node_type': 'file',
- 'name': 'translate.json',
- 'created': '2016-09-26 08:30:07',
- 'last_updated': '2016-09-26 08:30:08',
- 'last_accessed': None,
- 'last_revision': '1'
- }
- ]
- }
- ]
- }
- )
+@app.route('/api/project/test/edit-project', methods=['POST'])
+def edit():
+ return jsonify()
@app.route('/api/project/test/supported-languages', methods=['GET'])
def supported_langs():
- return jsonify(
- [
- {
- 'name': 'German',
- 'crowdin_code': 'de',
- 'editor_code': 'de',
- 'iso_639_1': 'de',
- 'iso_639_3': 'deu',
- 'locale': 'de-DE'
- },
- {
- 'name': 'English',
- 'crowdin_code': 'en',
- 'editor_code': 'en',
- 'iso_639_1': 'en',
- 'iso_639_3': 'eng',
- 'locale': 'en-US'
- },
- ]
- )
+ return jsonify(app.config['languages'])
@app.route('/api/project/test/add-file', methods=['POST'])
def add_file():
app.string = request.get_data()
return jsonify()
@@ -113,8 +51,41 @@
@app.route('/api/project/test/export', methods=['GET'])
def export():
return jsonify({'success': {'status': 'skipped'}})
@app.route('/api/project/test/download/all.zip', methods=['GET'])
def get_zip():
return send_from_directory('', 'all.zip')
+
+
+Locale = namedtuple('Locale', ['name', 'node_type', 'files'])
+File = namedtuple('File', ['name', 'node_type'])
+Language = namedtuple('Language', ['code', 'can_approve', 'can_translate'])
+Supported = namedtuple('Supported', ['crowdin_code'])
Vasily Kuznetsov 2018/03/05 17:21:32 Maybe rename this to `SupportedLanguage`? Otherwis
+
+
+class CrowdinMock:
+ def __init__(self, locales_root):
+ self.info = {'files': [], 'languages': []}
+ self.languages = []
+ self.load(locales_root)
+
+ def load(self, filepath):
+ for root, locales, files in os.walk(filepath):
+ for locale in locales:
+ files = []
+ self.info['languages'].append(Language(locale, 1, 1))
+ self.languages.append(Supported(locale))
+ for translations in os.listdir(os.path.join(root, locale)):
+ filenode = File(translations, 'file')
+ files.append(filenode._asdict())
+ 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.
+ self.info['files'].append(localenode)
+
+ def run(self):
+ app.request_log = []
+ 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.
+ self.info['languages'] = [i._asdict() for i in self.info['languages']]
+ app.config['info'] = self.info
+ app.config['languages'] = [i._asdict() for i in self.languages]
+ self.app = app
« no previous file with comments | « no previous file | tests/test_translations.py » ('j') | tests/test_translations.py » ('J')

Powered by Google App Engine
This is Rietveld