|
| 1 | +""" |
| 2 | +Implementation of RosettaCode Adapter. |
| 3 | +
|
| 4 | +Exports: |
| 5 | +
|
| 6 | + Rosetta(Adapter) |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import os |
| 11 | +import glob |
| 12 | +import yaml |
| 13 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 14 | + |
| 15 | +# pylint: disable=wrong-import-position,wrong-import-order |
| 16 | +from globals import PATH_CHEAT_SHEETS, ROSETTA_PATH |
| 17 | +from adapter import Adapter # pylint: disable=relative-import |
| 18 | +# pylint: enable=wrong-import-position,wrong-import-order |
| 19 | + |
| 20 | +class Rosetta(Adapter): |
| 21 | + |
| 22 | + """ |
| 23 | + Adapter for RosettaCode |
| 24 | + """ |
| 25 | + |
| 26 | + __section_name = 'rosetta' |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def _load_rosetta_code_names(): |
| 30 | + answer = {} |
| 31 | + for filename in glob.glob(os.path.join(PATH_CHEAT_SHEETS, '*/_info.yaml')): |
| 32 | + text = open(filename, 'r').read() |
| 33 | + data = yaml.load(text) |
| 34 | + if data is None: |
| 35 | + continue |
| 36 | + lang = os.path.basename(os.path.dirname(filename)) |
| 37 | + if lang.startswith('_'): |
| 38 | + lang = lang[1:] |
| 39 | + if 'rosetta' in data: |
| 40 | + answer[lang] = data['rosetta'] |
| 41 | + return answer |
| 42 | + |
| 43 | + def _rosetta_get_list(self, query, task=None): |
| 44 | + if query not in self._rosetta_code_name: |
| 45 | + return [] |
| 46 | + |
| 47 | + lang = self._rosetta_code_name[query] |
| 48 | + answer = [] |
| 49 | + if task: |
| 50 | + glob_path = os.path.join(ROSETTA_PATH, 'Lang', lang, task, '*') |
| 51 | + else: |
| 52 | + glob_path = os.path.join(ROSETTA_PATH, 'Lang', lang, '*') |
| 53 | + for filename in glob.glob(glob_path): |
| 54 | + taskname = os.path.basename(filename) |
| 55 | + answer.append(taskname) |
| 56 | + |
| 57 | + answer = "".join("%s\n" % x for x in sorted(answer)) |
| 58 | + return answer |
| 59 | + |
| 60 | + def _get_task(self, lang, query): |
| 61 | + if lang not in self._rosetta_code_name: |
| 62 | + return "" |
| 63 | + |
| 64 | + if '/' in query: |
| 65 | + task, subquery = query.split('/', 1) |
| 66 | + else: |
| 67 | + task, subquery = query, None |
| 68 | + |
| 69 | + if task == ':list': |
| 70 | + return self._rosetta_get_list(lang) |
| 71 | + if subquery == ':list': |
| 72 | + return self._rosetta_get_list(lang, task=task) |
| 73 | + |
| 74 | + # if it is not a number or the number is too big, just ignore it |
| 75 | + index = 1 |
| 76 | + if subquery: |
| 77 | + try: |
| 78 | + index = int(subquery) |
| 79 | + except ValueError: |
| 80 | + pass |
| 81 | + |
| 82 | + lang_name = self._rosetta_code_name[lang] |
| 83 | + |
| 84 | + tasks = sorted(glob.glob(os.path.join(ROSETTA_PATH, 'Lang', lang_name, task, '*'))) |
| 85 | + if not tasks: |
| 86 | + return "" |
| 87 | + |
| 88 | + if len(tasks) < index or index < 1: |
| 89 | + index = 1 |
| 90 | + |
| 91 | + answer_filename = tasks[index-1] |
| 92 | + answer = open(answer_filename, 'r').read() |
| 93 | + |
| 94 | + return answer |
| 95 | + |
| 96 | + def _starting_page(self, query): |
| 97 | + number_of_pages = self._rosetta_get_list(query) |
| 98 | + answer = ( |
| 99 | + "# %s pages available\n" |
| 100 | + "# use /:list to list" |
| 101 | + ) % number_of_pages |
| 102 | + return answer |
| 103 | + |
| 104 | + def get_page(self, topic, request_options=None): |
| 105 | + |
| 106 | + if '/' not in topic: |
| 107 | + return self._rosetta_get_list(topic) |
| 108 | + |
| 109 | + lang, topic = topic.split('/', 1) |
| 110 | + |
| 111 | + # this part should be generalized |
| 112 | + # currently we just remove the name of the adapter from the path |
| 113 | + if topic == self.__section_name: |
| 114 | + return self._starting_page(topic) |
| 115 | + |
| 116 | + if topic.startswith(self.__section_name + '/'): |
| 117 | + topic = topic[len(self.__section_name + '/'):] |
| 118 | + |
| 119 | + return self._get_task(lang, topic) |
| 120 | + |
| 121 | + def _get_list(self): |
| 122 | + return [] |
| 123 | + |
| 124 | + def get_list(self): |
| 125 | + # return self._get_list() |
| 126 | + answer = [self.__section_name] |
| 127 | + for i in self._rosetta_code_name: |
| 128 | + answer.append('%s/%s/' % (i, self.__section_name)) |
| 129 | + return answer |
| 130 | + |
| 131 | + def is_found(self, _): |
| 132 | + return False |
| 133 | + |
| 134 | + def __init__(self): |
| 135 | + Adapter.__init__() |
| 136 | + self._rosetta_code_name = self._load_rosetta_code_names() |
0 commit comments