Skip to content

Commit 9ba6850

Browse files
committed
refactor(logs): more informative log/error messages
1 parent 238f802 commit 9ba6850

5 files changed

Lines changed: 109 additions & 74 deletions

File tree

src/ontoweaver/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,11 @@ def make_edge_class(self, name, source_t, target_t, properties={}, base=Edge):
550550

551551
# Compare the properties with the existing class fields
552552
if properties == cls_fields:
553-
logger.info(
554-
f"\t\tEdge class `{name}` (prop: `{cls_fields}`) already exists with the same properties, I will not create another one.")
553+
logger.debug(
554+
f"\t\tEdge class `{name}` already exists with the same properties,"
555+
" I will not create another one."
556+
)
557+
logger.debug(f"\t\t\tProperties: `{cls_fields}`")
555558
return cls
556559

557560
logger.warning(f"\t\tEdge class `{name}` already exists, but properties do not match.")

src/ontoweaver/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
which is used by the ``ontoweave`` command as an error return code.
66
77
The exceptions taxonomy is::
8-
8+
99
OntoWeaverError
1010
├─ AutoSchemaError
1111
├─ ConfigError

src/ontoweaver/iterative.py

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -543,82 +543,86 @@ def process_row(row_data):
543543
local_transformations += 1
544544
logger.debug(f"\tCalling the {j}th transformer: {transformer}...")
545545
k = 0
546-
for target_id, target_edge, target_node, reverse_relation in transformer(row, i):
547-
logger.debug(f"\t\t{k}th element yielded by transformer")
548-
k += 1
549-
target_node_id = self._make_target_node_id(
550-
row,
551-
i,
552-
transformer,
553-
j,
554-
target_id,
555-
target_edge,
556-
target_node,
557-
local_nodes,
558-
local_errors
559-
)
546+
try:
547+
for target_id, target_edge, target_node, reverse_relation in transformer(row, i):
548+
logger.debug(f"\t\t{k}th element yielded by transformer")
549+
k += 1
550+
target_node_id = self._make_target_node_id(
551+
row,
552+
i,
553+
transformer,
554+
j,
555+
target_id,
556+
target_edge,
557+
target_node,
558+
local_nodes,
559+
local_errors
560+
)
560561

561-
#If no valid target node id was created, an error is logged in the `_make_target_node_id` function,
562-
#and we move to the next iteration of the loop.
563-
if target_node_id is None:
564-
continue
565-
else:
566-
local_nb_nodes += 1
567-
568-
# If a `from_subject` attribute is present in the transformer, loop over the transformer
569-
# list to find the transformer instance mapping to the correct type, and then label_maker new
570-
# subject id.
571-
572-
# FIXME add hook functions to be overloaded.
573-
574-
# FIXME: Make from_subject reference a list of subjects instead of using the add_edge function.
575-
576-
if hasattr(transformer, "from_subject"):
577-
578-
self._make_alternative_source_node_id(
579-
row,
580-
i,
581-
transformer,
582-
j,
583-
target_node_id,
584-
target_edge,
585-
local_edges,
586-
local_errors
587-
)
588-
589-
else: # no attribute `from_subject` in `transformer`
590-
logger.debug(f"\t\tMake edge {target_edge.__name__} from {source_node_id} toward {target_node_id}")
591-
local_edges.append(
592-
self.make_edge(
593-
edge_t=target_edge,
594-
id_target=target_node_id,
595-
id_source=source_node_id,
596-
properties=self.properties(
597-
target_edge.fields(),
598-
row,
599-
i,
600-
target_edge,
601-
target_node
602-
)
562+
#If no valid target node id was created, an error is logged in the `_make_target_node_id` function,
563+
#and we move to the next iteration of the loop.
564+
if target_node_id is None:
565+
continue
566+
else:
567+
local_nb_nodes += 1
568+
569+
# If a `from_subject` attribute is present in the transformer, loop over the transformer
570+
# list to find the transformer instance mapping to the correct type, and then label_maker new
571+
# subject id.
572+
573+
# FIXME add hook functions to be overloaded.
574+
575+
# FIXME: Make from_subject reference a list of subjects instead of using the add_edge function.
576+
577+
if hasattr(transformer, "from_subject"):
578+
579+
self._make_alternative_source_node_id(
580+
row,
581+
i,
582+
transformer,
583+
j,
584+
target_node_id,
585+
target_edge,
586+
local_edges,
587+
local_errors
603588
)
604-
)
605589

606-
if reverse_relation:
607-
logger.info(f"\t\t\tMake reverse edge {reverse_relation.__name__} from {target_node_id} to {source_node_id}")
590+
else: # no attribute `from_subject` in `transformer`
591+
logger.debug(f"\t\tMake edge {target_edge.__name__} from {source_node_id} toward {target_node_id}")
608592
local_edges.append(
609593
self.make_edge(
610-
edge_t=reverse_relation,
611-
id_target=source_node_id,
612-
id_source=target_node_id,
594+
edge_t=target_edge,
595+
id_target=target_node_id,
596+
id_source=source_node_id,
613597
properties=self.properties(
614-
reverse_relation.fields(),
598+
target_edge.fields(),
615599
row,
616600
i,
617-
reverse_relation,
618-
source_node_id.__class__
601+
target_edge,
602+
target_node
619603
)
620604
)
621605
)
606+
607+
if reverse_relation:
608+
logger.info(f"\t\t\tMake reverse edge {reverse_relation.__name__} from {target_node_id} to {source_node_id}")
609+
local_edges.append(
610+
self.make_edge(
611+
edge_t=reverse_relation,
612+
id_target=source_node_id,
613+
id_source=target_node_id,
614+
properties=self.properties(
615+
reverse_relation.fields(),
616+
row,
617+
i,
618+
reverse_relation,
619+
source_node_id.__class__
620+
)
621+
)
622+
)
623+
except Exception as err:
624+
logger.error(f"Error while calling the {j}th transformer on the {i}th row, after having yielded {k} items.")
625+
raise err
622626
# assert hasattr(local_nodes, "__iter__")
623627
# assert hasattr(local_edges, "__iter__")
624628
return local_nodes, local_edges, local_errors, local_rows, local_transformations, local_nb_nodes

src/ontoweaver/loader.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ def load(self, filenames, **kwargs):
288288
logger.debug(f"Additional arguments passed to the `{pathlib.Path(filename).suffix}` load function: {kw}")
289289
data.append( loadfunc(filename, **kw) )
290290

291+
if not data:
292+
# self.error(f"I failed to load data from files: {', '.join()}", section = "data loading", exception = exceptions.InputDataError)
293+
msg = f"I failed to load data from files: {', '.join(filenames)}"
294+
raise exceptions.InputDataError(msg)
295+
291296
out = pd.concat(data)
292297
return out
293298

src/ontoweaver/transformer.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,15 @@ def __init__(self, raise_errors: bool = True, format_string: str = None):
326326

327327
def __call__(self, columns, row, i):
328328

329-
formatted_string = self.format_string.format_map(row)
329+
try:
330+
formatted_string = self.format_string.format_map(row)
331+
except KeyError as err:
332+
self.error(f"{err}, available keys: {row}",
333+
exception = exceptions.TransformerConfigError,
334+
index = i,
335+
section = "cat_format"
336+
)
337+
330338
yield formatted_string
331339

332340
def __init__(self,
@@ -687,19 +695,30 @@ def __init__(self, translate, translate_from, translate_to, raise_errors: bool =
687695
self.translate = translate
688696
self.translate_from = translate_from
689697
self.translate_to = translate_to
698+
self.warn_lines = {}
690699
super().__init__(raise_errors)
691700

692701
def __call__(self, columns, row, i):
693702

694703
for key in columns:
695704
if key not in row:
696-
self.error(f"Column '{key}' not found in data", section="translate",
705+
self.error(f"Column '{key}' not found in data", section="translate",
697706
exception = exceptions.TransformerDataError)
698707
cell = row[key]
699708
if cell in self.translate:
700709
yield self.translate[cell]
701710
else:
702-
logger.warning(f"Row {i} does not contain something to be translated from `{self.translate_from}` to `{self.translate_to}` at column `{key}`.")
711+
msg = f"Some rows does not contain something to be translated"
712+
f" from `{self.translate_from}` to `{self.translate_to}`"
713+
f" at column `{key}`"
714+
self.warn_lines.get(msg, []).append(i)
715+
716+
def __del__(self): # FIXME make warnings management a common interface
717+
if len(self.warn_lines) > 0:
718+
logging.warning(f"There was warnings in a translate transformer, on {len(self.warn_lines)} lines:")
719+
for w in self.warn_lines:
720+
logging.warning(f"{w}: {', '.join(self.warn_lines[w])}")
721+
703722

704723
def __init__(self,
705724
properties_of,
@@ -785,7 +804,11 @@ def __init__(self,
785804
for with_loader in [lpf, lpd, lrf, lrg]:
786805
if with_loader.allows([self.translations_file]):
787806
logger.debug(f"\t\t\tUsing loader: {type(with_loader).__name__}")
788-
self.df = with_loader.load([self.translations_file], **more_args)
807+
try:
808+
self.df = with_loader.load([self.translations_file], **more_args)
809+
except exceptions.InputDataError as err:
810+
logging.error(f"I cannot load the translations_file: `{self.translations_file}`")
811+
raise err
789812
break
790813

791814
if self.df.empty:
@@ -834,7 +857,7 @@ def __init__(self,
834857
raise_errors=raise_errors,
835858
**kwargs
836859
)
837-
860+
838861
def __call__(self, row, i):
839862
"""
840863
Process a row and yield cell values as node IDs.
@@ -1260,4 +1283,4 @@ def __call__(self, row, i):
12601283
pseudorow = {"replace_column": value}
12611284
for val in self.replace.value_maker(["replace_column"], pseudorow, i):
12621285
value, edge_type, node_type, reverse_edge = self.create(val, row)
1263-
yield value, edge_type, node_type, reverse_edge
1286+
yield value, edge_type, node_type, reverse_edge

0 commit comments

Comments
 (0)