예제 #1
0
def index(request, p):
    return Response("{} index page".format(request.request_uri))
예제 #2
0
    def do_POST(self):

        print(self.path.split('/?')[0])
        is_matched = self.path.split('/?')[0]
        if is_matched == '/activate':
            from urllib.parse import urlparse
            query = urlparse(self.path).query
            query_components = dict(qc.split("=") for qc in query.split("&"))
            # print(query_components, "query_components----->", query)

            if query_components['token']:
                self.path = '/activate'

        elif is_matched == '/reset':
            from urllib.parse import urlparse
            query = urlparse(self.path).query
            query_components = dict(qc.split("=") for qc in query.split("&"))
            # print(query_components, "query_components----->", query)

            if query_components['token']:
                self.path = '/reset'

        if self.path == '/register':
            response = User().register(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = UserService().register(user_data=user_data,
                                                  that=self)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/activate':
            response = UserService().activate(token=query_components['token'])
            return Response(self).jsonResponse(status=200, data=response)

        if self.path == '/login':
            response = User().login(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = UserService().login(user_data=user_data)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/forgot':
            response = User().forgot(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = UserService().forgot(user_data=user_data, that=self)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/reset':
            response = User().reset(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = UserService().reset(token=query_components['token'],
                                               user_data=user_data)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/create_note':
            response = Note().create_note(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = NoteService().create_note(user_data=user_data)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/upload_profile_pic':
            response = NoteService().upload_photo(that=self)
            Response(self).jsonResponse(status=200, data=response)

        if self.path == '/collaborator':
            response = Note().collaborator(that=self)
            if response['success']:
                user_data = response['data'][0]
                response = NoteService().collaborator(user_data=user_data)
            Response(self).jsonResponse(status=200, data=response)
예제 #3
0
 def get_annotations(self, key):
     url = urlparse.urljoin(self.endpoint.base_url,
                            'monitors/annotations/' + key)
     resp = self.endpoint.get(url)
     return Response(resp, self.endpoint)
예제 #4
0
    async def request(self, client, addr):
        request = (await self.loop.sock_recv(
            client, self.SOCKET_BUFFER_SIZE)).decode('utf-8')

        if not self.reqParser(request):
            response = Response(400, setHeaders())
            await self.loop.sock_sendall(client, str(response).encode('utf-8'))
            await self.loop.sock_sendall(client, None)
            client.close()
            return

        if self.reqParser.requestLine.method not in self.ALLOWED_METHODS:
            response = Response(405, setHeaders())
            await self.loop.sock_sendall(client, str(response).encode('utf-8'))
            await self.loop.sock_sendall(client, None)
            client.close()
            return

        pathToFile = os.path.abspath(
            os.path.join(self.document_root, self.reqParser.requestLine.path))
        pathToFile = unquote(pathToFile)

        incorrectFile = (not pathToFile.endswith('/')
                         ) and self.reqParser.requestLine.path.endswith('/')

        if self.document_root not in pathToFile:
            response = Response(403, setHeaders())
            await self.loop.sock_sendall(client, str(response).encode('utf-8'))
            await self.loop.sock_sendall(client, None)
            client.close()
            return

        isAdded = False

        if os.path.isdir(pathToFile):
            isAdded = True
            pathToFile = os.path.join(
                pathToFile, self.reqParser.requestLine.INDEX_FILE_NAME)

        if not os.path.exists(pathToFile):
            if isAdded:
                response = Response(403, setHeaders())
            else:
                response = Response(404, setHeaders())

            await self.loop.sock_sendall(client, str(response).encode('utf-8'))
            await self.loop.sock_sendall(client, None)
        else:
            if incorrectFile and not isAdded:
                response = Response(404, setHeaders())

                await self.loop.sock_sendall(client,
                                             str(response).encode('utf-8'))
                await self.loop.sock_sendall(client, None)
            else:

                headers = setHeaders()
                headers.append(
                    ('Content-Length', str(os.path.getsize(pathToFile))))
                mimeType, _ = mimetypes.guess_type(pathToFile)
                headers.append(('Content-Type', mimeType))

                response = Response(200, headers)

                if self.reqParser.requestLine.method == 'HEAD':
                    await self.loop.sock_sendall(client,
                                                 str(response).encode('utf-8'))
                    await self.loop.sock_sendall(client, None)
                else:
                    with open(pathToFile, 'rb') as fp:
                        await self.loop.sock_sendall(
                            client,
                            str(response).encode('utf-8'))
                        await self.loop.sock_sendall(
                            client, fp.read(self.SOCKET_BUFFER_SIZE))

        client.close()
예제 #5
0
def info():
    cpu_use = psutil.cpu_percent()
    mem_use = psutil.virtual_memory().percent
    response = Response(data={'cpu_use': cpu_use, 'mem_use': mem_use})
    return response.to_json()
예제 #6
0
    def add_qedge(self, message, input_parameters, describe=False):
        """
        Adds a new QEdge object to the QueryGraph inside the Message object
        :return: Response object with execution information
        :rtype: Response
        """

        # #### Internal documentation setup
        allowable_parameters = {
            'id': {
                'Any string that is unique among all QEdge id fields, with recommended format e00, e01, e02, etc.'
            },
            'source_id': {
                'id of the source QNode already present in the QueryGraph (e.g. n01, n02)'
            },
            'target_id': {
                'id of the target QNode already present in the QueryGraph (e.g. n01, n02)'
            },
            'type': {
                'Any valid Translator/BioLink relationship type (e.g. physically_interacts_with, participates_in)'
            },
        }
        if describe:
            #allowable_parameters['action'] = { 'None' }
            #allowable_parameters = dict()
            allowable_parameters[
                'dsl_command'] = '`add_qedge()`'  # can't get this name at run-time, need to manually put it in per https://www.python.org/dev/peps/pep-3130/
            allowable_parameters[
                'brief_description'] = """The `add_qedge` method adds an additional QEdge to the QueryGraph in the Message object. Currently
                source_id and target_id QNodes must already be present in the QueryGraph. The specified type is not currently checked that it is a
                valid Translator/BioLink relationship type, but it should be."""
            return allowable_parameters

        #### Define a default response
        response = Response()
        self.response = response
        self.message = message

        #### Basic checks on arguments
        if not isinstance(input_parameters, dict):
            response.error("Provided parameters is not a dict",
                           error_code="ParametersNotDict")
            return response

        #### Define a complete set of allowed parameters and their defaults
        parameters = {
            'id': None,
            'source_id': None,
            'target_id': None,
            'type': None,
        }

        #### Loop through the input_parameters and override the defaults and make sure they are allowed
        for key, value in input_parameters.items():
            if key not in parameters:
                response.error(f"Supplied parameter {key} is not permitted",
                               error_code="UnknownParameter")
            else:
                parameters[key] = value
        #### Return if any of the parameters generated an error (showing not just the first one)
        if response.status != 'OK':
            return response

        #### Store these final parameters for convenience
        response.data['parameters'] = parameters
        self.parameters = parameters

        #### Now apply the filters. Order of operations is probably quite important
        #### Scalar value filters probably come first like minimum_confidence, then complex logic filters
        #### based on edge or node properties, and then finally maximum_results
        response.info(
            f"Adding a QueryEdge to Message with parameters {parameters}")

        #### Make sure there's a query_graph already here
        if message.query_graph is None:
            message.query_graph = QueryGraph()
            message.query_graph.nodes = []
            message.query_graph.edges = []
        if message.query_graph.edges is None:
            message.query_graph.edges = []

        #### Create a QEdge
        qedge = QEdge()
        if parameters['id'] is not None:
            id = parameters['id']
        else:
            id = self.__get_next_free_edge_id()
        qedge.id = id

        #### Get the list of available node_ids
        qnodes = message.query_graph.nodes
        ids = {}
        for qnode in qnodes:
            id = qnode.id
            ids[id] = 1

        #### Add the source_id
        if parameters['source_id'] is not None:
            if parameters['source_id'] not in ids:
                response.error(
                    f"While trying to add QEdge, there is no QNode with id {parameters['source_id']}",
                    error_code="UnknownSourceId")
                return response
            qedge.source_id = parameters['source_id']
        else:
            response.error(
                f"While trying to add QEdge, source_id is a required parameter",
                error_code="MissingSourceId")
            return response

        #### Add the target_id
        if parameters['target_id'] is not None:
            if parameters['target_id'] not in ids:
                response.error(
                    f"While trying to add QEdge, there is no QNode with id {parameters['target_id']}",
                    error_code="UnknownTargetId")
                return response
            qedge.target_id = parameters['target_id']
        else:
            response.error(
                f"While trying to add QEdge, target_id is a required parameter",
                error_code="MissingTargetId")
            return response

        #### Add the type if any. Need to verify it's an allowed type. FIXME
        if parameters['type'] is not None:
            qedge.type = parameters['type']

        #### Add it to the query_graph edge list
        message.query_graph.edges.append(qedge)

        #### Return the response
        return response
예제 #7
0
def main():

    #### Create a response object
    response = Response()

    #### Create a default ARAX Message
    messenger = ARAXMessenger()
    result = messenger.create_message()
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response
    message = messenger.message

    #### Some qnode examples
    parameters_sets = [
        #        { 'curie': 'DOID:9281'},
        {
            'curie': 'Orphanet:673'
        },
        {
            'name': 'acetaminophen'
        },
        {
            'curie': 'NCIT:C198'
        },
        {
            'curie': 'CUI:C4710278'
        },
        {
            'type': 'protein',
            'id': 'n10'
        },
        {
            'curie': ['UniProtKB:P14136', 'UniProtKB:P35579']
        },
        {
            'curie': ['UniProtKB:P14136', 'UniProtKB:P35579'],
            'is_set': 'false'
        },
    ]

    for parameter in parameters_sets:
        #### Add a QNode
        result = messenger.add_qnode(message, parameter)
        response.merge(result)
        if result.status != 'OK':
            print(response.show(level=Response.DEBUG))
            return response

    #### Some qedge examples
    parameters_sets = [
        {
            'source_id': 'n00',
            'target_id': 'n01'
        },
        {
            'source_id': 'n01',
            'target_id': 'n10',
            'type': 'treats'
        },
    ]

    for parameter in parameters_sets:
        #### Add a QEdge
        result = messenger.add_qedge(message, parameter)
        response.merge(result)
        if result.status != 'OK':
            print(response.show(level=Response.DEBUG))
            return response

    if 0:
        result = messenger.reassign_curies(message, {
            'knowledge_provider': 'KG1',
            'mismap_result': 'WARNING'
        })
        response.merge(result)
        if result.status != 'OK':
            print(response.show(level=Response.DEBUG))
            return response

    #### Show the final result
    print(response.show(level=Response.DEBUG))
    print(json.dumps(ast.literal_eval(repr(message)), sort_keys=True,
                     indent=2))
예제 #8
0
def handle_discovery(request):
    response = Response(ResponseName.DISCOVER)
    response.add_payload({"endpoints": api.get_endpoints()})
    return response
예제 #9
0
def home_controller(request):
    body = home_view.render()
    return Response('200 OK', body)
예제 #10
0
def delete_user_impl(args):
    try:
        db.session.delete(user_by_user_id(args['user_id']))
        return Response(True, "User Deleted", None).output()
    except Exception as e:
        return Response(False, str(e), None).output()
예제 #11
0
def handle_authentication(request):
    return Response(ResponseName.ACCEPT_GRANT)
예제 #12
0
def app(request: Request) -> Response:
    return Response(content="Hello!")
예제 #13
0
 def auth_handler(request: Request) -> Response:
     authorization = request.headers.get("authorization", "")
     if authorization.startswith(
             "Bearer ") and authorization[len("Bearer "):] == "opensesame":
         return handler(request)
     return Response(status="403 Forbidden", content="Forbidden!")
예제 #14
0
 def get_response(self):
     if self.request.method in ['GET', 'HEAD']:
         response = self.make_response()
     else:
         response = Response(ResponseCode.NOT_ALLOWED)
     return response.create_response()
예제 #15
0
    def handle_one_request(self):
        response = None
        self.logger = get_logger()
        try:
            self.close_connection = False
            request_line_is_valid = self.get_request_line()

            if self.close_connection:
                return

            request_is_valid = self.parse_request()
            if not request_is_valid:
                #parse_request() actually sends its own error responses
                return

            self.server.rewriter.rewrite(self)

            request = Request(self)
            response = Response(self, request)

            if request.method == "CONNECT":
                self.handle_connect(response)
                return

            if not request_line_is_valid:
                response.set_error(414)
                response.write()
                return

            self.logger.debug("%s %s" % (request.method, request.request_path))
            handler = self.server.router.get_handler(request)

            # If the handler we used for the request had a non-default base path
            # set update the doc_root of the request to reflect this
            if hasattr(handler, "base_path") and handler.base_path:
                request.doc_root = handler.base_path
            if hasattr(handler, "url_base") and handler.url_base != "/":
                request.url_base = handler.url_base

            if self.server.latency is not None:
                if callable(self.server.latency):
                    latency = self.server.latency()
                else:
                    latency = self.server.latency
                self.logger.warning("Latency enabled. Sleeping %i ms" %
                                    latency)
                time.sleep(latency / 1000.)

            if handler is None:
                response.set_error(404)
            else:
                try:
                    handler(request, response)
                except HTTPException as e:
                    response.set_error(e.code, e.message)
                except Exception as e:
                    if e.message:
                        err = [e.message]
                    else:
                        err = []
                    err.append(traceback.format_exc())
                    response.set_error(500, "\n".join(err))
            self.logger.debug(
                "%i %s %s (%s) %i" %
                (response.status[0], request.method, request.request_path,
                 request.headers.get('Referer'), request.raw_input.length))

            if not response.writer.content_written:
                response.write()

            # If we want to remove this in the future, a solution is needed for
            # scripts that produce a non-string iterable of content, since these
            # can't set a Content-Length header. A notable example of this kind of
            # problem is with the trickle pipe i.e. foo.js?pipe=trickle(d1)
            if response.close_connection:
                self.close_connection = True

            if not self.close_connection:
                # Ensure that the whole request has been read from the socket
                request.raw_input.read()

        except socket.timeout, e:
            self.log_error("Request timed out: %r", e)
            self.close_connection = True
            return
예제 #16
0
def upload_controller(request):
    # TODO
    body = upload_view.render()
    return Response('200 OK', body)
예제 #17
0
    def add_qnode(self, message, input_parameters, describe=False):
        """
        Adds a new QNode object to the QueryGraph inside the Message object
        :return: Response object with execution information
        :rtype: Response
        """

        # #### Internal documentation setup
        allowable_parameters = {
            'id': {
                'Any string that is unique among all QNode id fields, with recommended format n00, n01, n02, etc.'
            },
            'curie': {
                'Any compact URI (CURIE) (e.g. DOID:9281) (May also be a list like [UniProtKB:P12345,UniProtKB:Q54321])'
            },
            'name': {
                'Any name of a bioentity that will be resolved into a CURIE if possible or result in an error if not (e.g. hypertension, insulin)'
            },
            'type': {
                'Any valid Translator bioentity type (e.g. protein, chemical_substance, disease)'
            },
            'is_set': {
                'If set to true, this QNode represents a set of nodes that are all in common between the two other linked QNodes'
            },
        }
        if describe:
            allowable_parameters[
                'dsl_command'] = '`add_qnode()`'  # can't get this name at run-time, need to manually put it in per https://www.python.org/dev/peps/pep-3130/
            allowable_parameters[
                'brief_description'] = """The `add_qnode` method adds an additional QNode to the QueryGraph in the Message object. Currently
                when a curie or name is specified, this method will only return success if a matching node is found in the KG1/KG2 KGNodeIndex."""
            return allowable_parameters

        #### Define a default response
        response = Response()
        self.response = response
        self.message = message

        #### Basic checks on arguments
        if not isinstance(input_parameters, dict):
            response.error("Provided parameters is not a dict",
                           error_code="ParametersNotDict")
            return response

        #### Define a complete set of allowed parameters and their defaults
        parameters = {
            'id': None,
            'curie': None,
            'name': None,
            'type': None,
            'is_set': None,
        }

        #### Loop through the input_parameters and override the defaults and make sure they are allowed
        for key, value in input_parameters.items():
            if key not in parameters:
                response.error(f"Supplied parameter {key} is not permitted",
                               error_code="UnknownParameter")
            else:
                parameters[key] = value
        #### Return if any of the parameters generated an error (showing not just the first one)
        if response.status != 'OK':
            return response

        #### Store these final parameters for convenience
        response.data['parameters'] = parameters
        self.parameters = parameters

        #### Now apply the filters. Order of operations is probably quite important
        #### Scalar value filters probably come first like minimum_confidence, then complex logic filters
        #### based on edge or node properties, and then finally maximum_results
        response.info(
            f"Adding a QueryNode to Message with parameters {parameters}")

        #### Make sure there's a query_graph already here
        if message.query_graph is None:
            message.query_graph = QueryGraph()
            message.query_graph.nodes = []
            message.query_graph.edges = []
        if message.query_graph.nodes is None:
            message.query_graph.nodes = []

        #### Set up the KGNodeIndex
        kgNodeIndex = KGNodeIndex()

        # Create the QNode and set the id
        qnode = QNode()
        if parameters['id'] is not None:
            id = parameters['id']
        else:
            id = self.__get_next_free_node_id()
        qnode.id = id

        # Set the is_set parameter to what the user selected
        if parameters['is_set'] is not None:
            qnode.is_set = (parameters['is_set'].lower() == 'true')

        #### If the CURIE is specified, try to find that
        if parameters['curie'] is not None:

            # If the curie is a scalar then treat it here as a list of one
            if isinstance(parameters['curie'], str):
                curie_list = [parameters['curie']]
                is_curie_a_list = False
                if parameters['is_set'] is not None and qnode.is_set is True:
                    response.error(
                        f"Specified CURIE '{parameters['curie']}' is a scalar, but is_set=true, which doesn't make sense",
                        error_code="CurieScalarButIsSetTrue")
                    return response

            # Or else set it up as a list
            elif isinstance(parameters['curie'], list):
                curie_list = parameters['curie']
                is_curie_a_list = True
                qnode.curie = []
                if parameters['is_set'] is None:
                    response.warning(
                        f"Specified CURIE '{parameters['curie']}' is a list, but is_set was not set to true. It must be true in this context, so automatically setting to true. Avoid this warning by explictly setting to true."
                    )
                    qnode.is_set = True
                else:
                    if qnode.is_set == False:
                        response.warning(
                            f"Specified CURIE '{parameters['curie']}' is a list, but is_set=false, which doesn't make sense, so automatically setting to true. Avoid this warning by explictly setting to true."
                        )
                        qnode.is_set = True

            # Or if it's neither a list or a string, then error out. This cannot be handled at present
            else:
                response.error(
                    f"Specified CURIE '{parameters['curie']}' is neither a string nor a list. This cannot to handled",
                    error_code="CurieNotListOrScalar")
                return response

            # Loop over the available curies and create the list
            for curie in curie_list:
                response.debug(f"Looking up CURIE {curie} in KgNodeIndex")
                nodes = kgNodeIndex.get_curies_and_types(curie, kg_name='KG2')

                # If nothing was found, we won't bail out, but rather just issue a warning
                if len(nodes) == 0:
                    response.warning(
                        f"A node with CURIE {curie} is not in our knowledge graph KG2, but will continue"
                    )
                    if is_curie_a_list:
                        qnode.curie.append(curie)
                    else:
                        qnode.curie = curie

                else:

                    # FIXME. This is just always taking the first result. This could cause problems for CURIEs with multiple types. Is that possible?
                    # In issue #623 on 2020-06-15 we concluded that we should not specify the type here
                    #qnode.type = nodes[0]['type']

                    # Either append or set the found curie
                    if is_curie_a_list:
                        qnode.curie.append(nodes[0]['curie'])
                    else:
                        qnode.curie = nodes[0]['curie']

                if 'type' in parameters and parameters['type'] is not None:
                    if isinstance(parameters['type'], str):
                        qnode.type = parameters['type']
                    else:
                        qnode.type = parameters['type'][0]

            message.query_graph.nodes.append(qnode)
            return response

        #### If the name is specified, try to find that
        if parameters['name'] is not None:
            response.debug(
                f"Looking up CURIE {parameters['name']} in KgNodeIndex")
            nodes = kgNodeIndex.get_curies_and_types(parameters['name'])
            if len(nodes) == 0:
                nodes = kgNodeIndex.get_curies_and_types(parameters['name'],
                                                         kg_name='KG2')
                if len(nodes) == 0:
                    response.error(
                        f"A node with name '{parameters['name']}'' is not in our knowledge graph",
                        error_code="UnknownCURIE")
                    return response
            qnode.curie = nodes[0]['curie']
            qnode.type = nodes[0]['type']
            message.query_graph.nodes.append(qnode)
            return response

        #### If the type is specified, just add that type. There should be checking that it is legal. FIXME
        if parameters['type'] is not None:
            qnode.type = parameters['type']
            if parameters['is_set'] is not None:
                qnode.is_set = (parameters['is_set'].lower() == 'true')
            message.query_graph.nodes.append(qnode)
            return response

        #### If we get here, it means that all three main parameters are null. Just a generic node with no type or anything. This is okay.
        message.query_graph.nodes.append(qnode)
        return response
예제 #18
0
class Request(object):
    """Class that represents a request to PiCloud.

    :param str url: A URL to be fetched.
    :param str method: Target HTTP method.
        ("GET" or "POST" or "PUT" or "DELETE" or "HEAD" or "OPTIONS")
    :param dict headers: (optional) HTTP headers to send.
    :param dict args: (optional) Additional arguments, that are directly passed
        to `requests <http://docs.python-requests.org>`_. Any arguments that
        supported in `requests <http://docs.python-requests.org>`_ are supported.
    :param function success_callback: (optional) A function called if the
        request is successfully completed.
        A :class:`Response <picrawler.response.Response>` instance is passed as
        the first argument.
    :param function error_callback: (optional) A function called if the request
        is failed. An :class:`ErrorResponse <picrawler.response.ErrorResponse>`
        instance is passed as the first argument.
    """
    def __init__(self,
                 url,
                 method='get',
                 headers={},
                 args={},
                 success_callback=None,
                 error_callback=None):
        global _success_callbacks, _error_callbacks

        self._id = uuid.uuid1().hex
        self._url = url
        self._method = method.lower()
        self._headers = headers
        self._args = args
        self._success_callback_id = None
        self._error_callback_id = None

        if not self._method in SUPPORTED_HTTP_METHODS:
            raise InvalidHTTPMethod('Unsupported HTTP method')

        # NOTE: because the callback function cannot be pickled,
        # it must be saved locally
        if success_callback:
            self._success_callback_id = uuid.uuid1().hex
            _success_callbacks[self._success_callback_id] = success_callback

        if error_callback:
            self._error_callback_id = uuid.uuid1().hex
            _error_callbacks[self._error_callback_id] = error_callback

    @property
    def id(self):
        return self._id

    @property
    def url(self):
        return self._url

    @property
    def method(self):
        return self._method

    @property
    def headers(self):
        return self._headers

    @property
    def args(self):
        return self._args

    @property
    def success_callback(self):
        global _success_callbacks

        if self._success_callback_id:
            return _success_callbacks.get(self._success_callback_id)
        else:
            return None

    @property
    def error_callback(self):
        global _error_callbacks

        if self._error_callback_id:
            return _error_callbacks.get(self._error_callback_id)
        else:
            return None

    def run_callback(self, response):
        global _success_callbacks, _error_callbacks

        if isinstance(response, Response):
            if self._success_callback_id:
                _success_callbacks[self._success_callback_id](response)
        elif isinstance(response, ErrorResponse):
            if self._error_callback_id:
                _error_callbacks[self._error_callback_id](response)
        else:
            raise InvalidResponse('Invalid response')

    def __call__(self):
        method_func = getattr(requests, self._method)

        try:
            ret = method_func(self._url, headers=self._headers, **self._args)
        except Exception, e:
            return ErrorResponse(self, e)

        return Response(self, ret.status_code, ret.content, ret.headers)
예제 #19
0
    def reassign_curies(self, message, input_parameters, describe=False):
        """
        Reassigns CURIEs to the target Knowledge Provider
        :param message: Translator standard Message object
        :type message: Message
        :param input_parameters: Dict of input parameters to control the method
        :type input_parameters: Message
        :return: Response object with execution information
        :rtype: Response
        """

        # #### Internal documentation setup
        allowable_parameters = {
            'knowledge_provider': {
                'Name of the Knowledge Provider CURIE space to map to. Default=KG1. Also currently supported KG2'
            },
            'mismap_result': {
                'Desired action when mapping fails: ERROR or WARNING. Default is ERROR'
            },
        }
        if describe:
            allowable_parameters[
                'dsl_command'] = '`reassign_curies()`'  # can't get this name at run-time, need to manually put it in per https://www.python.org/dev/peps/pep-3130/
            allowable_parameters[
                'brief_description'] = """The `reassign_curies` method reassigns all the CURIEs in the Message QueryGraph to the specified
                knowledge provider. Allowed values are KG1 or KG2. Default is KG1 if not specified."""
            return allowable_parameters

        #### Define a default response
        response = Response()
        self.response = response
        self.message = message

        #### Basic checks on arguments
        if not isinstance(input_parameters, dict):
            response.error("Provided parameters is not a dict",
                           error_code="ParametersNotDict")
            return response

        #### Define a complete set of allowed parameters and their defaults
        parameters = {
            'knowledge_provider': 'KG1',
            'mismap_result': 'ERROR',
        }

        #### Loop through the input_parameters and override the defaults and make sure they are allowed
        for key, value in input_parameters.items():
            if key not in parameters:
                response.error(f"Supplied parameter {key} is not permitted",
                               error_code="UnknownParameter")
            else:
                parameters[key] = value
        #### Return if any of the parameters generated an error (showing not just the first one)
        if response.status != 'OK':
            return response

        #### Store these final parameters for convenience
        response.data['parameters'] = parameters
        self.parameters = parameters

        # Check that the knowledge_provider is valid:
        if parameters['knowledge_provider'] != 'KG1' and parameters[
                'knowledge_provider'] != 'KG2':
            response.error(
                f"Specified knowledge provider must be 'KG1' or 'KG2', not '{parameters['knowledge_provider']}'",
                error_code="UnknownKP")
            return response

        #### Now try to assign the CURIEs
        response.info(
            f"Reassigning the CURIEs in QueryGraph to {parameters['knowledge_provider']} space"
        )

        #### Make sure there's a query_graph already here
        if message.query_graph is None:
            message.query_graph = QueryGraph()
            message.query_graph.nodes = []
            message.query_graph.edges = []
        if message.query_graph.nodes is None:
            message.query_graph.nodes = []

        #### Set up the KGNodeIndex
        kgNodeIndex = KGNodeIndex()

        # Loops through the QueryGraph nodes and adjust them
        for qnode in message.query_graph.nodes:

            # If the CURIE is None, then there's nothing to do
            curie = qnode.curie
            if curie is None:
                continue

            # Map the CURIE to the desired Knowledge Provider
            if parameters['knowledge_provider'] == 'KG1':
                if kgNodeIndex.is_curie_present(curie) is True:
                    mapped_curies = [curie]
                else:
                    mapped_curies = kgNodeIndex.get_KG1_curies(curie)
            elif parameters['knowledge_provider'] == 'KG2':
                if kgNodeIndex.is_curie_present(curie, kg_name='KG2'):
                    mapped_curies = [curie]
                else:
                    mapped_curies = kgNodeIndex.get_curies_and_types(
                        curie, kg_name='KG2')
            else:
                response.error(
                    f"Specified knowledge provider must be 'KG1' or 'KG2', not '{parameters['knowledge_provider']}'",
                    error_code="UnknownKP")
                return response

            # Try to find a new CURIE
            new_curie = None
            if len(mapped_curies) == 0:
                if parameters['mismap_result'] == 'WARNING':
                    response.warning(
                        f"Did not find a mapping for {curie} to KP '{parameters['knowledge_provider']}'. Leaving as is"
                    )
                else:
                    response.error(
                        f"Did not find a mapping for {curie} to KP '{parameters['knowledge_provider']}'. This is an error"
                    )
            elif len(mapped_curies) == 1:
                new_curie = mapped_curies[0]
            else:
                original_curie_is_fine = False
                for potential_curie in mapped_curies:
                    if potential_curie == curie:
                        original_curie_is_fine = True
                if original_curie_is_fine:
                    new_curie = curie
                else:
                    new_curie = mapped_curies[0]
                    response.warning(
                        f"There are multiple possible CURIEs in KP '{parameters['knowledge_provider']}'. Selecting the first one {new_curie}"
                    )

            # If there's no CURIE, then nothing to do
            if new_curie is None:
                pass
            # If it's the same
            elif new_curie == curie:
                response.debug(
                    f"CURIE {curie} is fine for KP '{parameters['knowledge_provider']}'"
                )
            else:
                response.info(
                    f"Remapping CURIE {curie} to {new_curie} for KP '{parameters['knowledge_provider']}'"
                )

        #### Return the response
        return response
예제 #20
0
def run(config, debug):
    global _settings, _logger
    programExecTime = time.time()
    outData = []
    _settings = Settings(config)

    if debug == True:
        _logger = sys.stdout.write
    elif type(debug) == str:
        try:
            logFile = open(debug, "a")
            _logger = logFile.write
        except Exception as e:
            print('\nCannot open log file. Debug flag ignored. Error: ' +
                  str(e))

    try:
        startTime = time.time()
        loginStatus = login()
        if not (loginStatus):
            loginResponse = Response('sasping login request',
                                     'fail',
                                     message='Failed to login.')
        else:
            loginResponse = Response('sasping login request', 'success')
    except requests.exceptions.Timeout as err:
        loginResponse = Response('sasping login request',
                                 'fail',
                                 message='Request timeout')
    loginResponse.setTime(startTime, time.time() - startTime)
    loginResponse.setProgramExecTime(programExecTime)
    if loginResponse.status == 'fail':
        return [dict(loginResponse)]

    # get request to do sas security check
    # this request is not logged to csv file
    try:
        _session.get(
            _settings.get('hostUrl') +
            Test(_settings.get('applications')[0]['tests'][0]).get('execPath'))
    except requests.exceptions.RequestException:
        raise RuntimeError('SAS security check request failed')
    finally:
        outData.append(dict(loginResponse.setProgramExecTime(programExecTime)))
        applications = _settings.get('applications')
        for app in applications:
            for test in [Test(testConfig) for testConfig in app['tests']]:
                try:
                    startTime = time.time()
                    response = call(test)
                except requests.exceptions.Timeout:
                    response = Response(test.get('id'),
                                        'fail',
                                        message='Request timeout')
                except requests.exceptions.ConnectionError:
                    response = Response(test.get('id'),
                                        'fail',
                                        message='Name or service not known')
                except requests.exceptions.RequestException as e:
                    response = Response(test.get('id'), 'fail', message=str(e))
                response.setTime(startTime, time.time() - startTime)
                response.setAppName(app['name'])
                outData.append(
                    dict(response.setProgramExecTime(programExecTime)))

                try:
                    logFile.close()
                finally:
                    return outData
예제 #21
0
def main():
    ### Note that most of this is just manually doing what ARAXQuery() would normally do for you

    #### Create a response object
    response = Response()

    #### Create an ActionsParser object
    from actions_parser import ActionsParser
    actions_parser = ActionsParser()

    #### Set a simple list of actions
    # actions_list = [
    #    "overlay(compute_confidence_scores=true)",
    #    "return(message=true,store=false)"
    # ]

    actions_list = [
        #"filter_kg(action=remove_edges_by_type, edge_type=physically_interacts_with, remove_connected_nodes=false)",
        #"filter_kg(action=remove_edges_by_type, edge_type=physically_interacts_with, remove_connected_nodes=something)",
        #"filter(action=remove_nodes_by_type, node_type=protein)",
        #"overlay(action=compute_ngd)",
        #"filter(action=remove_edges_by_attribute, edge_attribute=ngd, threshold=.63, direction=below, remove_connected_nodes=t)",
        #"filter(action=remove_edges_by_attribute, edge_attribute=ngd, threshold=.6, remove_connected_nodes=False)",
        "filter(action=remove_orphaned_nodes)",
        "return(message=true,store=false)"
    ]

    #### Parse the action_list and print the result
    result = actions_parser.parse(actions_list)
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response
    actions = result.data['actions']

    #### Read message #2 from the database. This should be the acetaminophen proteins query result message
    sys.path.append(
        os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback")
    from RTXFeedback import RTXFeedback
    araxdb = RTXFeedback()

    #message_dict = araxdb.getMessage(2)  # acetaminophen2proteins graph
    # message_dict = araxdb.getMessage(13)  # ibuprofen -> proteins -> disease # work computer
    # message_dict = araxdb.getMessage(14)  # pleuropneumonia -> phenotypic_feature # work computer
    # message_dict = araxdb.getMessage(16)  # atherosclerosis -> phenotypic_feature  # work computer
    # message_dict = araxdb.getMessage(5)  # atherosclerosis -> phenotypic_feature  # home computer
    # message_dict = araxdb.getMessage(10)
    message_dict = araxdb.getMessage(40)

    #### The stored message comes back as a dict. Transform it to objects
    from ARAX_messenger import ARAXMessenger
    message = ARAXMessenger().from_dict(message_dict)
    # print(json.dumps(ast.literal_eval(repr(message)),sort_keys=True,indent=2))

    #### Create an overlay object and use it to apply action[0] from the list
    #filterkg = ARAXFilterKG()
    #result = filterkg.apply(message, actions[0]['parameters'])
    #response.merge(result)

    # Apply overlay so you get an edge attribute to work with, then apply the filter
    #from ARAX_overlay import ARAXOverlay
    #overlay = ARAXOverlay()
    #result = overlay.apply(message, actions[0]['parameters'])
    #response.merge(result)
    # then apply the filter
    filterkg = ARAXFilterKG()
    result = filterkg.apply(message, actions[0]['parameters'])
    response.merge(result)

    # if result.status != 'OK':
    #    print(response.show(level=Response.DEBUG))
    #    return response
    # response.data = result.data

    #### If successful, show the result
    # print(response.show(level=Response.DEBUG))
    # response.data['message_stats'] = { 'n_results': message.n_results, 'id': message.id,
    #    'reasoner_id': message.reasoner_id, 'tool_version': message.tool_version }
    # response.data['message_stats']['confidence_scores'] = []
    # for result in message.results:
    #    response.data['message_stats']['confidence_scores'].append(result.confidence)

    # print(json.dumps(ast.literal_eval(repr(response.data['parameters'])),sort_keys=True,indent=2))
    # print(json.dumps(ast.literal_eval(repr(response.data['message_stats'])),sort_keys=True,indent=2))
    # a comment on the end so you can better see the network on github

    # look at the response
    # print(response.show(level=Response.DEBUG))
    # print(response.show())
    # print("Still executed")

    # look at the edges
    # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)),sort_keys=True,indent=2))
    # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.nodes)), sort_keys=True, indent=2))
    # print(json.dumps(ast.literal_eval(repr(message)), sort_keys=True, indent=2))
    # print(response.show(level=Response.DEBUG))

    # just print off the values
    # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)), sort_keys=True, indent=2))
    # for edge in message.knowledge_graph.edges:
    #    if hasattr(edge, 'edge_attributes') and edge.edge_attributes and len(edge.edge_attributes) >= 1:
    #        print(edge.edge_attributes.pop().value)
    print(
        json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)),
                   sort_keys=True,
                   indent=2))
    print(response.show(level=Response.DEBUG))
    vals = []
    for node in message.knowledge_graph.nodes:
        print(node.id)
    print(len(message.knowledge_graph.nodes))
    for edge in message.knowledge_graph.edges:
        if hasattr(edge, 'edge_attributes') and edge.edge_attributes and len(
                edge.edge_attributes) >= 1:
            vals.append(edge.edge_attributes.pop().value)
    print(sorted(vals))
예제 #22
0
def main():

    #### Create a response object
    response = Response()

    #### Read message #2 from the database. This should be the acetaminophen proteins query result message
    sys.path.append(
        os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback")
    from RTXFeedback import RTXFeedback
    araxdb = RTXFeedback()
    message_dict = araxdb.getMessage(2)

    #### The stored message comes back as a dict. Transform it to objects
    messenger = ARAXMessenger()
    message = messenger.from_dict(message_dict)

    #### Asses some information about the QueryGraph
    query_graph_info = QueryGraphInfo()
    result = query_graph_info.assess(message)
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response
    #print(json.dumps(ast.literal_eval(repr(query_graph_info.node_order)),sort_keys=True,indent=2))

    #### Try to add query_graph_ids to the KnowledgeGraph
    knowledge_graph_info = KnowledgeGraphInfo()
    result = knowledge_graph_info.add_query_graph_tags(message,
                                                       query_graph_info)
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response

    #### Reassess some information about the KnowledgeGraph
    result = knowledge_graph_info.check_for_query_graph_tags(
        message, query_graph_info)
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response

    #### Try to re-create the results list
    result = messenger.generate_results(message)
    response.merge(result)
    if result.status != 'OK':
        print(response.show(level=Response.DEBUG))
        return response

    print(response.show(level=Response.DEBUG))

    tmp = {
        'query_graph_id_node_status':
        knowledge_graph_info.query_graph_id_node_status,
        'query_graph_id_edge_status':
        knowledge_graph_info.query_graph_id_edge_status,
        'n_nodes': knowledge_graph_info.n_nodes,
        'n_edges': knowledge_graph_info.n_edges
    }
    #print(json.dumps(ast.literal_eval(repr(message)),sort_keys=True,indent=2))
    print(json.dumps(ast.literal_eval(repr(tmp)), sort_keys=True, indent=2))
예제 #23
0
    -1.703608E-02 + 1.234000E-02j, -1.703608E-02 + -1.234000E-02j,
    -1.778991e-02 + 0.000000e-00j, -5.605308e-02 + 0.000000e-00j,
    -3.918000E+01 + 4.912000E+01j, -3.918000E+01 + -4.912000E+01j
]

# this is from sensor test suite
zer3 = [0.000000E+00 + 0.000000E+00j, 0.000000E+00 + 0.000000E+00j]
pol3 = [
    -1.263000E-02 + 1.265000E-02j, -1.263000E-02 + -1.265000E-02j,
    -3.620107E+01 + 6.850121E+01j, -3.620107E+01 + -6.850121E+01j
]

instName = 'CTAO_00'
print("calculating info for a " + instName)

inst1 = Response(desc=instName, units='Radians')
inst1.zeros = zer1
inst1.poles = pol1
#norm_freq=0.02
norm_freq = 1.0
n1, f1 = inst1.check_normalization(freq=norm_freq, nfft=2**26, t_sample=0.001)
scale_fac = 1.0 / n1
print('The A0 norm factor is: ' + str(scale_fac) + ' for f=' + str(norm_freq))
#check the value
inst1.a0 = 1.0 / n1
A01 = inst1.a0
n, f = inst1.check_normalization(freq=norm_freq, nfft=2**26, t_sample=0.001)
print('This should be close to 1: ' + str(1.0 / n))
h1, f1 = paz_to_freq_resp(inst1.poles,
                          inst1.zeros,
                          scale_fac,
예제 #24
0
 def __init__(self):
     self.rsp = Response()
     self.av_queue = queue.Queue()
     self.max_threads = 5
예제 #25
0
class Server:
    BAD_REQUEST = Response(code=HTTPResponseCodes.BAD_REQUEST,
                           content_type=HTTPContentTypes.PLAIN,
                           data='Failure'.encode())
    UNAUTHORIZED = Response(code=HTTPResponseCodes.UNAUTHORIZED,
                            content_type=HTTPContentTypes.PLAIN,
                            data='Failure'.encode())
    METHOD_NOT_ALLOWED = Response(code=HTTPResponseCodes.UNAUTHORIZED,
                                  content_type=HTTPContentTypes.PLAIN,
                                  data='Failure'.encode())
    SUCCESS = Response(code=HTTPResponseCodes.OK,
                       content_type=HTTPContentTypes.PLAIN,
                       data='Success'.encode())

    def __init__(self, host_ip=None, host_name=None, port=None):
        if host_ip is None:
            if host_name is None:
                host_name = socket.gethostname()
            if host_ip is None:
                host_ip = "192.168.1.5"  #socket.gethostbyname(host_name)
        if port is None:
            port = 7654

        self.socket = socket.socket()
        self.host_ip = host_ip
        self.host_name = host_name
        self.port = port
        self.socket.bind((self.host_ip, self.port))
        self.socket.listen(5)

    def run(self):
        while True:
            client, address = self.socket.accept()
            message = client.recv(4096)
            self.communicate(client, address, message)

    def communicate(self, client, address, message):
        # PARSE REQUEST
        req = Request(msg=message)
        resp = None
        console.info(
            'Received {} request from {} at {} with {} parameters'.format(
                req.method.name, address[0], req.path,
                0 if req.params is None else len(req.params.keys())))
        # CHECK TO MAKE SURE POST REQUEST WAS RECEIVED
        if req.method == HTTPMethods.POST:
            # CHECK TO MAKE SURE DATA WAS RECEIVED
            if req.data is not None:
                # CHECK TO MAKE SURE JSON HAS CORRECT FIELDS
                if 'command' in req.data.keys() and 'key' in req.data.keys():
                    console.debug('Received command: {}'.format(
                        req.data['command']))
                    # AUTHENTICATE KEY
                    if req.data['key'] == settings.MASTER_KEY:
                        # CALL APPROPRIATE FUNCTION (OPEN/CLOSE)
                        if req.data['command'] == 0:
                            garage.open_g()
                            resp = self.SUCCESS
                        elif req.data['command'] == 1:
                            garage.close_g()
                            resp = self.SUCCESS
                        else:
                            resp = self.BAD_REQUEST
                    else:
                        resp = self.UNAUTHORIZED
                else:
                    resp = self.BAD_REQUEST
            else:
                resp = self.BAD_REQUEST
        else:
            resp = self.METHOD_NOT_ALLOWED

        # SEND RESPONSE
        client.send(resp.generate())

        # CLOSE CLIENT CONNECTION
        client.close()
예제 #26
0
    def _execute(self, goal):
        """Callback for serving Execute action requests.
        """
        action_id = None
        if goal.action_id != '':
            action_id = goal.action_id
        else:
            action_id = self._action_db.id_for_name(goal.name)
        self._interaction.switch_to_action_by_id(action_id)
        response_params = self._interaction._execute_action(
            preregistered_landmarks=goal.landmarks)
        if RobotSpeech.START_EXECUTION not in response_params[0]:
            result = ExecuteResult()
            result.error = response_params[0]
            self._server.set_aborted(result=result, text=result.error)
            response = Response(self._interaction._empty_response,
                                response_params)
            response.respond()
            return

        # Wait for it to start executing for at most 10 seconds
        rate = rospy.Rate(10)
        start = rospy.Time.now()
        timeout = rospy.Duration(10)
        while not self._interaction.arms.is_executing():
            elapsed_time = rospy.Time.now() - start
            if elapsed_time > timeout:
                rospy.logwarn('PbD action did not start after 10 seconds')
                result = ExecuteResult()
                result.error = 'PbD action did not start after 10 seconds'
                self._server.set_aborted(result=result, text=result.error)
                break
            rate.sleep()

        response = Response(self._interaction._empty_response, response_params)
        response.respond()
        rate = rospy.Rate(10)
        start = rospy.Time.now()
        timeout = rospy.Duration(60 * 5)
        while self._interaction.arms.is_executing():
            elapsed_time = rospy.Time.now() - start
            self._server.publish_feedback(ExecuteFeedback())
            if elapsed_time > timeout:
                rospy.logwarn('PbD action did not finish after 5 minutes')
                result = ExecuteResult()
                result.error = 'PbD action did not finish after 5 minutes'
                self._server.set_aborted(result=result, text=result.error)
                break
            rate.sleep()

        status = self._interaction.arms.last_status
        if status == ExecutionStatus.SUCCEEDED:
            rospy.loginfo('Execution succeeded')
            self._server.set_succeeded()
        elif status == ExecutionStatus.NOT_EXECUTING:
            rospy.loginfo('Execution state was NOT_EXECUTING - failed?')
            # Race condition? Assume this means failure.
            error = 'Unable to run the PbD action.'
            result = ExecuteResult()
            result.error = error
            rospy.logerr(error)
            self._server.set_aborted(result=result, text=error)
        elif status == ExecutionStatus.PREEMPTED:
            error = 'The PbD action was preempted.'
            result = ExecuteResult()
            result.error = error
            rospy.logerr(error)
            self._server.set_aborted(result=result, text=error)
        elif status == ExecutionStatus.NO_IK:
            error = 'The robot\'s arms couldn\'t reach some poses.'
            result = ExecuteResult()
            result.error = error
            rospy.logerr(error)
            self._server.set_aborted(result=result, text=error)
        else:
            error = 'Unknown error {} running the PbD action'.format(status)
            result = ExecuteResult()
            result.error = error
            rospy.logerr(error)
            self._server.set_aborted(result=result, text=error)
예제 #27
0
 def delete_rule(self, key):
     url = urlparse.urljoin(self.endpoint.base_url, 'monitors/' + key)
     resp = self.endpoint.delete(url)
     return Response(resp, self.endpoint)
예제 #28
0
import numpy as np
import matplotlib.pyplot as plt

from obspy.signal.invsim import paz_to_freq_resp
from response import Response

pol = [
    -0.01234 - 1j * 0.01234, -0.01234 + 1j * 0.01234, -39.18 - 1j * 49.12,
    -39.18 + 1j * 49.12
]
zer = [0, 0]

# use Austin's response class to build the resp from the poles and zeros
inst = Response(desc='STS-5/360', units='Radians')
inst.zeros = zer
inst.poles = pol
norm_freq = 1.0
n, f = inst.check_normalization(freq=norm_freq, nfft=2**26, t_sample=0.001)
scale_fac = 1.0 / n
print('The A0 norm factor is: ' + str(scale_fac))

#poles = [-4.440 + 4.440j, -4.440 - 4.440j, -1.083 + 0.0j]
#zeros = [0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j]
scale_fac = 0.4

h, f = paz_to_freq_resp(inst.poles,
                        inst.zeros,
                        scale_fac,
                        0.001,
                        2**26,
                        freq=True)
예제 #29
0
 def get_changelog(self, key):
     url = urlparse.urljoin(self.endpoint.base_url,
                            'monitors/%s/changes/' % key)
     resp = self.endpoint.get(url)
     return Response(resp, self.endpoint)
예제 #30
0
def about(request, p):
    return Response("{} about page".format(request.request_uri))