Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 170a48d2 authored by Bruno Guillaume's avatar Bruno Guillaume
Browse files

improve error reporting

parent cb867f2d
Branches
No related tags found
No related merge requests found
......@@ -36,33 +36,35 @@ def graph(data=None):
or another graph
:return: a graph
"""
if not data:
return dict()
if isinstance(data, list):
# builds a flat ordered (using list order) graph
return {float2id(float(i)): (data[i], []) for i in range(len(data))}
elif isinstance(data, str):
# build from a JSON string
try:
return json.loads(data)
except json.decoder.JSONDecodeError:
if os.path.isfile(data):
req = { "command": "load_graph", "filename": data }
reply = network.send_and_receive(req)
else:
with tempfile.NamedTemporaryFile(mode="w", delete=True, suffix=".gr") as f:
f.write(data)
f.seek(0) # to be read by others
req = { "command": "load_graph", "filename": f.name }
try:
if not data:
return dict()
if isinstance(data, list):
# builds a flat ordered (using list order) graph
return {float2id(float(i)): (data[i], []) for i in range(len(data))}
elif isinstance(data, str):
# build from a JSON string
try:
return json.loads(data)
except json.decoder.JSONDecodeError:
if os.path.isfile(data):
req = { "command": "load_graph", "filename": data }
reply = network.send_and_receive(req)
return (reply)
elif isinstance(data,dict):
# copy an existing graph
return copy.deepcopy(data)
else:
raise GrewError('Library call error')
else:
with tempfile.NamedTemporaryFile(mode="w", delete=True, suffix=".gr") as f:
f.write(data)
f.seek(0) # to be read by others
req = { "command": "load_graph", "filename": f.name }
reply = network.send_and_receive(req)
return (reply)
elif isinstance(data,dict):
# copy an existing graph
return copy.deepcopy(data)
else:
raise GrewError('Library call error')
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.graph", "data": data, "message":e.value})
def save(gr, filename):
req = { "command": "save_graph", "graph": json.dumps(gr), "filename": filename }
......@@ -84,8 +86,10 @@ def add_edge(gr, source, label, target):
:param target: the target node id
:return:
"""
if source not in gr or target not in gr:
raise KeyError("Add_edge")
if source not in gr:
raise utils.GrewError({"function": "grew.add_edge", "src": source, "message":"KeyError"})
elif target not in gr:
raise utils.GrewError({"function": "grew.add_edge", "tar": target, "message":"KeyError"})
else:
succs = gr[source][1]
if not (label, target) in succs:
......
......@@ -38,8 +38,8 @@ def grs(data):
req = { "command": "load_grs", "filename": f.name }
reply = network.send_and_receive(req)
return reply["index"]
except:
raise utils.GrewError("[grew.grs] Could not build a GRS with: %s" % data)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.grs", "data":data, "message":e.value})
def run(grs_data, graph_data, strat="main"):
"""
......@@ -49,19 +49,22 @@ def run(grs_data, graph_data, strat="main"):
:param strat: the strategy (by default "main")
:return: the list of rewritten graphs
"""
if isinstance(grs_data, int):
grs_index = grs_data
else:
grs_index = grs(grs_data)
try:
if isinstance(grs_data, int):
grs_index = grs_data
else:
grs_index = grs(grs_data)
req = {
"command": "run",
"graph": json.dumps(graph_data),
"grs_index": grs_index,
"strat": strat
}
reply = network.send_and_receive(req)
return utils.rm_dups(reply)
req = {
"command": "run",
"graph": json.dumps(graph_data),
"grs_index": grs_index,
"strat": strat
}
reply = network.send_and_receive(req)
return utils.rm_dups(reply)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.run", "strat": strat, "message":e.value})
def corpus(data):
"""Load a corpus from a file of a string
......@@ -83,8 +86,8 @@ def corpus(data):
req = { "command": "load_corpus", "files": [f.name] }
reply = network.send_and_receive(req)
return reply["index"]
except:
raise utils.GrewError("Could not build a corpus with: %s" % data)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus", "data": data, "message":e.value})
def search(pattern, gr):
"""
......@@ -93,13 +96,17 @@ def search(pattern, gr):
:param gr: the graph
:return: the list of matching of [pattern] into [gr]
"""
req = {
"command": "search",
"graph": json.dumps(gr),
"pattern": pattern
}
reply = network.send_and_receive(req)
return reply
try:
req = {
"command": "search",
"graph": json.dumps(gr),
"pattern": pattern
}
reply = network.send_and_receive(req)
return reply
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.search", "message":e.value})
def corpus_search(pattern, corpus_index):
"""
......@@ -108,12 +115,15 @@ def corpus_search(pattern, corpus_index):
:param corpus_index: an integer given by the [corpus] function
:return: the list of matching of [pattern] into the corpus
"""
req = {
"command": "corpus_search",
"corpus_index": corpus_index,
"pattern": pattern,
}
return network.send_and_receive(req)
try:
req = {
"command": "corpus_search",
"corpus_index": corpus_index,
"pattern": pattern,
}
return network.send_and_receive(req)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus_search", "message":e.value})
def corpus_count(pattern, corpus_index):
"""
......@@ -122,12 +132,15 @@ def corpus_count(pattern, corpus_index):
:param corpus_index: an integer given by the [corpus] function
:return: the number of matching of [pattern] into the corpus
"""
req = {
"command": "corpus_count",
"corpus_index": corpus_index,
"pattern": pattern,
}
return network.send_and_receive(req)
try:
req = {
"command": "corpus_count",
"corpus_index": corpus_index,
"pattern": pattern,
}
return network.send_and_receive(req)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus_count", "message":e.value})
def corpus_get(data, corpus_index):
"""
......@@ -149,8 +162,11 @@ def corpus_get(data, corpus_index):
"sent_id": data,
}
else:
raise utils.GrewError("Data error in 'corpus_get': %s" % data)
return network.send_and_receive(req)
raise utils.GrewError({"function": "grew.corpus_get", "message":"unexpected data, should be int or str"})
try:
return network.send_and_receive(req)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus_get", "message":e.value})
def corpus_size(corpus_index):
"""
......@@ -159,7 +175,10 @@ def corpus_size(corpus_index):
:return: a integer
"""
req = { "command": "corpus_size", "corpus_index": corpus_index}
return network.send_and_receive(req)
try:
return network.send_and_receive(req)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus_size", "message":e.value})
def corpus_sent_ids(corpus_index):
"""
......@@ -167,11 +186,11 @@ def corpus_sent_ids(corpus_index):
:param corpus_index: an integer given by the [corpus] function
:return: a list of strings
"""
req = {
"command": "corpus_sent_ids",
"corpus_index": corpus_index,
}
return network.send_and_receive(req)
req = { "command": "corpus_sent_ids", "corpus_index": corpus_index }
try:
return network.send_and_receive(req)
except utils.GrewError as e:
raise utils.GrewError({"function": "grew.corpus_sent_ids", "message":e.value})
def graph_svg(graph):
req = {
......
import re
import math
import json
'''
Identifier tools
......@@ -51,7 +52,10 @@ class GrewError(Exception):
def __init__(self, message):
self.value = message
def __str__(self):
return repr(self.value)
if isinstance(self.value, dict):
return ("\n".join (("", "-"*80, json.dumps(self.value, indent=2), "-"*80)))
else:
return ("\n".join (("", "="*80, str (self.value), "="*80)))
GrewError.__doc__ = "A wrapper for grew-related errors"
......
......@@ -21,7 +21,6 @@ end
let json_error msg =
Yojson.Basic.to_string (`Assoc [("status", `String "ERROR"); ("message", `String msg)])
exception Error of string
(* ==================================================================================================== *)
let run_command request =
......@@ -79,11 +78,14 @@ let run_command request =
| None -> files
| Some dir -> List.map (fun file -> Filename.concat dir file) files in
let conll_corpus = Conllx_corpus.load_list ~config complete_files in
try
let conll_corpus = Conllx_corpus.load_list ~quiet:true ~config complete_files in
let index = Global.corpus_add conll_corpus in
let data = `Assoc [("index", `Int index)] in
Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)])
let index = Global.corpus_add conll_corpus in
let data = `Assoc [("index", `Int index)] in
Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)])
with Conllx_error js ->
Yojson.Basic.to_string (`Assoc [("status", `String "ERROR"); ("message", js)])
end
(* ======================= corpus_get ======================= *)
......@@ -204,7 +206,7 @@ let run_command request =
) with
| Some graph, Some grs_index, Some strat ->
let gr = Graph.of_json_python ~config (Yojson.Basic.from_string graph) in
let grs = Global.grs_get grs_index in
let grs = try Global.grs_get grs_index with Not_found -> raise (Error "Reference to an undefined GRS") in
let graph_list = Rewrite.simple_rewrite ~config gr grs strat in
Yojson.Basic.to_string
(`Assoc [
......@@ -219,7 +221,7 @@ let run_command request =
begin
match json |> member "grs_index" |> to_int_option with
| Some grs_index ->
let grs = Global.grs_get grs_index in
let grs = try Global.grs_get grs_index with Not_found -> raise (Error "Reference to an undefined GRS") in
Yojson.Basic.to_string
(`Assoc [
("status", `String "OK");
......
......@@ -7,6 +7,8 @@ module Int_map = Map.Make (struct type t=int let compare=Stdlib.compare end)
let config = Conllx_config.build "ud"
exception Error of string
(* ==================================================================================================== *)
module Utils = struct
let dot_to_png dot =
......@@ -65,7 +67,9 @@ module Global = struct
corpus_map := Int_map.add !corpus_max corpus !corpus_map;
!corpus_max
let corpus_get index = Int_map.find index !corpus_map
let corpus_get index =
try Int_map.find index !corpus_map
with Not_found -> raise (Error "Reference to an undefined corpus")
end
(* ==================================================================================================== *)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment