From e88a2a8e4d411a4c80739f44343ae49f44d7ef19 Mon Sep 17 00:00:00 2001 From: Gilles Mouchard Date: Fri, 8 Oct 2021 11:13:06 +0200 Subject: [PATCH 1/2] Fixed URL for putting annotations in PKM: {path} is no supported because it is already in the payload. Fixed `access` which was accidentally URI encoded in the payload. Added some checks when search for a JMES path failed (same checks as in SRL tool). --- api/pynerapi/v1/routes.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/api/pynerapi/v1/routes.py b/api/pynerapi/v1/routes.py index 66d64cc..7178d8f 100644 --- a/api/pynerapi/v1/routes.py +++ b/api/pynerapi/v1/routes.py @@ -249,12 +249,22 @@ def pkmner(): if not isinstance(texts, list): texts = [texts] else: + access = urllib.parse.unquote(access) if debug: - message = f"json path is: {urllib.parse.unquote(access)}" + message = f"json path is: {access}" app.logger.debug(message) log.message(message) - texts = jmespath.search(urllib.parse.unquote(access), doc) - + texts = jmespath.search(access, doc) + except jmespath.EmptyExpressionError as e: + message = f"JMESpath search failed on empty access {access}: {e}" + app.logger.debug(message) + log.message(message) + except Exception as e: + message = f"JMESpath search failed on {access}: {e}" + app.logger.debug(message) + log.message(message) + if texts is None: + texts = [] if debug: message = f"selected texts are: {texts}" app.logger.debug(message) @@ -283,20 +293,17 @@ def pkmner(): # TODO implement writing the srl at the right location in the annotations. if debug: - message = (f"writing back srl result of project {project_id} " - f"and path {urllib.parse.quote(path, safe='')}") + message = f"writing back srl result of project {project_id}" app.logger.debug(message) log.message(message) message = f"result to write is: {result}" app.logger.debug(message) log.message(message) status, res = pkm.call(method="PUT", - path=f"annotations/{project_id}/" - f"{urllib.parse.quote(path, safe='')}", + path=f"annotations/{project_id}", payload=[result]) if status != 0: - message = (f"Writing back srl result of project {project_id} and path {path} " - f"failed") + message = f"Writing back srl result of project {project_id} failed" app.logger.error(message) log.error(message) return send_result(message, 500) -- GitLab From 395afa47e5a37d53964ab0b76a21404f5322a473 Mon Sep 17 00:00:00 2001 From: Gilles Mouchard Date: Mon, 18 Oct 2021 07:36:15 +0200 Subject: [PATCH 2/2] Fixed missing `try`. --- api/pynerapi/v1/routes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/pynerapi/v1/routes.py b/api/pynerapi/v1/routes.py index 7178d8f..9e82a49 100644 --- a/api/pynerapi/v1/routes.py +++ b/api/pynerapi/v1/routes.py @@ -254,7 +254,8 @@ def pkmner(): message = f"json path is: {access}" app.logger.debug(message) log.message(message) - texts = jmespath.search(access, doc) + try: + texts = jmespath.search(access, doc) except jmespath.EmptyExpressionError as e: message = f"JMESpath search failed on empty access {access}: {e}" app.logger.debug(message) -- GitLab