Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #206 #230

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apertium_apy/apy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
GenerateHandler,
IdentifyLangHandler,
ListHandler,
ListPairHandler,
ListLanguageNamesHandler,
PerWordHandler,
PipeDebugHandler,
Expand Down Expand Up @@ -265,7 +266,7 @@ def setup_application(args):
handlers = [
(r'/', RootHandler),
(r'/list', ListHandler),
(r'/listPairs', ListHandler),
(r'/listPairs', ListPairHandler),
(r'/stats', StatsHandler),
(r'/pairprefs', PairPrefsHandler),
(r'/translate', TranslateHandler),
Expand Down
1 change: 1 addition & 0 deletions apertium_apy/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apertium_apy.handlers.identify_lang import IdentifyLangHandler # noqa: F401
from apertium_apy.handlers.list_language_names import ListLanguageNamesHandler # noqa: F401
from apertium_apy.handlers.list_modes import ListHandler # noqa: F401
from apertium_apy.handlers.list_modes import ListPairHandler # noqa: F401
from apertium_apy.handlers.per_word import PerWordHandler # noqa: F401
from apertium_apy.handlers.pipe_debug import PipeDebugHandler # noqa: F401
from apertium_apy.handlers.speller import SpellerHandler # noqa: F401
Expand Down
43 changes: 40 additions & 3 deletions apertium_apy/handlers/list_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,31 @@
class ListHandler(BaseHandler):
@tornado.gen.coroutine
def get(self):
query = self.get_argument('q', default='pairs')
query = self.get_argument('q', default='all')

if query == 'pairs':
if query == 'all':
src = self.get_argument('src', default=None)
pairs_data = []
if src:
pairs = [(src, trg) for trg in self.paths[src]]
else:
pairs = [(p[0], p[1]) for par in self.pairs for p in [par.split('-')]]
for (l1, l2) in pairs:
pairs_data.append({'sourceLanguage': l1, 'targetLanguage': l2})
if self.get_arguments('include_deprecated_codes'):
pairs_data.append({'sourceLanguage': to_alpha2_code(l1), 'targetLanguage': to_alpha2_code(l2)})
response = {
'responseData': {
'pairsData': pairs_data,
'generatorsData': {pair: modename for (pair, (path, modename)) in self.generators.items()},
'taggersData': {pair: modename for (pair, (path, modename)) in self.taggers.items()},
'spellersData': {lang_src: modename for (lang_src, (path, modename)) in self.spellers.items()}
},
'responseDetails': None,
'responseStatus': 200
}
self.send_response(response)
elif query == 'pairs':
src = self.get_argument('src', default=None)
response_data = []
if src:
Expand All @@ -30,4 +52,19 @@ def get(self):
elif query == 'spellers':
self.send_response({lang_src: modename for (lang_src, (path, modename)) in self.spellers.items()})
else:
self.send_error(400, explanation='Expecting q argument to be one of analysers, generators, spellers, disambiguators, or pairs')
self.send_error(400, explanation='Expecting q argument to be one of analysers, generators, spellers, disambiguators, pairs or all')

class ListPairHandler(BaseHandler):
@tornado.gen.coroutine
def get(self):
src = self.get_argument('src', default=None)
response_data = []
if src:
pairs = [(src, trg) for trg in self.paths[src]]
else:
pairs = [(p[0], p[1]) for par in self.pairs for p in [par.split('-')]]
for (l1, l2) in pairs:
response_data.append({'sourceLanguage': l1, 'targetLanguage': l2})
if self.get_arguments('include_deprecated_codes'):
response_data.append({'sourceLanguage': to_alpha2_code(l1), 'targetLanguage': to_alpha2_code(l2)})
self.send_response({'responseData': response_data, 'responseDetails': None, 'responseStatus': 200})
27 changes: 27 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ def test_home_page(self):


class TestListHandler(BaseTestCase):
def test_list_all(self):
response = self.fetch_json('/list', {'q': 'all'})
self.assertIsNone(response['responseDetails'])
self.assertEqual(response['responseStatus'], 200)

expected_pairs = set(map(lambda x: frozenset(x.items()), [
{'sourceLanguage': 'sme', 'targetLanguage': 'nob'},
{'sourceLanguage': 'eng', 'targetLanguage': 'spa'},
{'sourceLanguage': 'spa', 'targetLanguage': 'eng_US'},
{'sourceLanguage': 'spa', 'targetLanguage': 'eng'},
]))
expected_generators = {'nno': 'nno-gener'}
expected_taggers = {'nno': 'nno-tagger'}
expected_spellers = {'nno': 'nno-speller'}

response_pairs = set(map(lambda x: frozenset(x.items()), response['responseData']['pairsData']))
self.assertTrue(response_pairs >= expected_pairs, '{} is missing one of {}'.format(response_pairs, expected_pairs))

self.assertTrue(response['responseData']['generatorsData'].items() >= expected_generators.items(),
'{} is missing {}'.format(response['responseData']['generatorsData'], expected_generators))

self.assertTrue(response['responseData']['taggersData'].items() >= expected_taggers.items(),
'{} is missing {}'.format(response['responseData']['taggersData'], expected_taggers))

self.assertTrue(response['responseData']['spellersData'].items() >= expected_spellers.items(),
'{} is missing {}'.format(response['responseData']['spellersData'], expected_spellers))

def test_list_pairs(self):
response = self.fetch_json('/list', {'q': 'pairs'})
self.assertIsNone(response['responseDetails'])
Expand Down