Пример #1
0
        def process_results(raw_results: str) -> QueryResult:
            def on_parsing_error(message: str):
                raise ProbableBugError(
                    f'Error parsing XML database response JSON: {message}')

            results_json = json.loads(raw_results)
            if not isinstance(results_json, list):
                on_parsing_error('Results are not a list')

            for result in results_json:
                for i, word in enumerate(result["words"]):
                    result["words"][i] = {
                        key: word[key]
                        for key in word if word[key] is not None
                    }

            num_results = len(results_json)
            total_pages = math.ceil(num_results / app_constants.page_size) or 1
            if query.page > total_pages:
                raise ProbableBugError(
                    f'Requested page {query.page} is out of bounds for results with {total_pages} total pages'
                )
            page_start = (query.page - 1) * app_constants.page_size
            page_end = page_start + app_constants.page_size
            results_for_page = results_json[page_start:page_end]

            return QueryResult(results_for_page, query.page, total_pages,
                               on_parsing_error)
Пример #2
0
 def _check_attributes(self, query: TextQuery) -> None:
     for sequence in query.sequences:
         for word_query in sequence.word_queries:
             for attribute in word_query.attributes:
                 if attribute not in allowed_attributes:
                     raise ProbableBugError(
                         f'Attribute \'{attribute}\' not allowed')
Пример #3
0
    def attribute_query(self, attribute_name: str) -> List[str]:
        if attribute_name not in allowed_attributes:
            raise ProbableBugError(
                f'Attribute \'{attribute_name}\' not allowed')

        if attribute_name == 'lemma' and self.cache.get('lemma'):
            return self.cache.get('lemma')
        if attribute_name == 'normalized' and self.cache.get('normalized'):
            return self.cache.get('normalized')

        query_string = f"""
            json:serialize(
              array {{
                sort(distinct-values(//w/@{attribute_name}))
              }}
            )
        """

        def process_results(raw_results: str) -> List[str]:
            results = json.loads(raw_results)
            if not isinstance(results, list):
                raise ProbableBugError(
                    f'Error parsing XML database response JSON: not a list')
            return results

        results = self._execute_query_and_process_results(
            query_string, process_results)
        if attribute_name == 'lemma':
            self.cache['lemma'] = results
        if attribute_name == 'normalized':
            self.cache['normalized'] = results
        return results
Пример #4
0
def _json_to_text_query(json: Union[Dict[Any, Any], None]) -> TextQuery:
    if json is None:
        raise ProbableBugError('Request does not contain a JSON body', 400)

    def on_parsing_error(message: str):
        raise ProbableBugError(f'Error parsing JSON: {message}', 400)

    return TextQuery(json, on_parsing_error)
Пример #5
0
    def _execute_query_and_process_results(self, query_string: str,
                                           process_results: Callable):
        try:
            raw_results = self._execute_query(query_string)
        except Exception as err:
            raise ServerOverwhelmedError(
                f'Error executing XML database query: {type(err).__name__}')

        try:
            return process_results(raw_results)
        except AnoixoError:
            raise
        except Exception as err:
            raise ProbableBugError(
                f'Error processing query results: {type(err).__name__}')
Пример #6
0
 def process_results(raw_results: str) -> List[str]:
     results = json.loads(raw_results)
     if not isinstance(results, list):
         raise ProbableBugError(
             f'Error parsing XML database response JSON: not a list')
     return results
Пример #7
0
 def on_parsing_error(message: str):
     raise ProbableBugError(
         f'Error parsing XML database response JSON: {message}')
Пример #8
0
def _get_text_provider(text_id: str) -> TextProvider:
    if text_id not in text_providers:
        raise ProbableBugError(
            f'Text provider with id \'{text_id}\' was not found. '
            f'Available texts: {" ".join(text_providers.keys())}', 404)
    return text_providers[text_id]
Пример #9
0
 def on_parsing_error(message: str):
     raise ProbableBugError(f'Error parsing JSON: {message}', 400)