14.6.13. crate_anon.nlp_webserver.views

crate_anon/nlp_webserver/views.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Pyramid views making up the CRATE NLPRP web server.

class crate_anon.nlp_webserver.views.NlpWebViews(request: pyramid.request.Request)[source]

Class to provide HTTP views (via Pyramid) for our NLPRP server.

__init__(request: pyramid.request.Request) None[source]
Parameters

request – a pyramid.request.Request

check_token() bool[source]

Checks to see if the user has given the correct token for the current session connected to their username.

create_error_response(error: crate_anon.nlprp.errors.NlprpError) Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Returns an HTTP response for a given error and description of the error

create_response(status: int, extra_info: Optional[Dict[str, Union[str, int, float, bool, None, Dict, List]]] = None) Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Returns a JSON HTTP response with some standard information for a given HTTP status and extra information to add to the response.

Ensures the HTTP status matches the NLPRP JSON status.

delete_from_queue() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Deletes from the queue all entries specified by the client.

fetch_from_queue() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Fetches requests for all document-processor pairs for the queue_id supplied by the user (if complete).

handle_nlprp_request() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

The main function. Authenticates user and checks the request is not malformed, then calls the function relating to the command specified by the user.

index() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

The top-level “index” view. Passes all the work to handle_nlprp_request(), except for error handling.

list_processors() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Returns an HTTP response listing the available NLP processors.

parse_command(command: str) Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Parse the NLPRP command.

process_now(process_request: crate_anon.nlp_webserver.views.NlprpProcessRequest) Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Processes the text supplied by the user immediately, without putting it in the queue.

Parameters

process_request – a NlprpProcessRequest

put_in_queue(process_request: crate_anon.nlp_webserver.views.NlprpProcessRequest) Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Puts the document-processor pairs specified by the user into a celery queue to be processed.

Parameters

process_request – a NlprpProcessRequest

set_http_response_status(status: int) None[source]

Sets the HTTP status code for our response.

Parameters

status – HTTP status code

show_queue() Dict[str, Union[str, int, float, bool, None, Dict, List]][source]

Finds the queue entries associated with the client, optionally restricted to one client job id.

class crate_anon.nlp_webserver.views.NlprpProcessRequest(nlprp_request: Dict[str, Union[str, int, float, bool, None, Dict, List]])[source]

Represents an NLPRP process command. Takes the request JSON, and offers efficient views on it.

Uses the global crate_anon.nlp_server.procs.Processors class to find processors.

__init__(nlprp_request: Dict[str, Union[str, int, float, bool, None, Dict, List]]) None[source]
Parameters

nlprp_request – dictionary from the (entire) JSON NLPRP request

Raises

NlprpError

gen_text_metadataobj() Generator[Tuple[str, Union[str, int, float, bool, None, Dict, List]], None, None][source]

Generates text and metadata pairs from the request, with the metadata in JSON object (Python dictionary) format.

Yields

tuple(text, metadata), as above

gen_text_metadatastr() Generator[Tuple[str, str], None, None][source]

Generates text and metadata pairs from the request, with the metadata in string (serialized JSON) format.

Yields

tuple(text, metadata), as above

processor_ids() List[str][source]

Return the IDs of all processors.

processor_ids_jsonstr() str[source]

Returns the IDs of all processors as a string of JSON-encoded IDs.