From 89bf7512697dbfec50fb029bd470108d65f177b9 Mon Sep 17 00:00:00 2001 From: bhorinrb Date: Thu, 19 Dec 2024 12:26:42 +0100 Subject: [PATCH] Fixed global variables issues --- src/yatter/__init__.py | 8 +++++--- src/yatter/constants.py | 2 -- src/yatter/source.py | 5 ++--- src/yatter/target.py | 10 +++++----- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/yatter/__init__.py b/src/yatter/__init__.py index f57b482..6d531e9 100644 --- a/src/yatter/__init__.py +++ b/src/yatter/__init__.py @@ -12,10 +12,12 @@ def translate(yarrrml_data, mapping_format=RML_URI): logger.info("Translating YARRRML mapping to [R2]RML") + added_sources = {} + added_targets = {} yarrrml_data = normalize(yarrrml_data, added_sources, added_targets) rml_mapping = [add_prefix(yarrrml_data)] - rml_mapping.extend(generate_database_connections(yarrrml_data)) - rml_mapping.extend(add_logical_targets(yarrrml_data)) + rml_mapping.extend(generate_database_connections(yarrrml_data, added_sources)) + rml_mapping.extend(add_logical_targets(yarrrml_data, added_targets)) rml_mapping.extend(add_functions(yarrrml_data)) try: @@ -24,7 +26,7 @@ def translate(yarrrml_data, mapping_format=RML_URI): if mapping_format == R2RML_URI: source_list = add_table(yarrrml_data, mapping) else: - source_list = add_source(yarrrml_data, mapping) + source_list = add_source(yarrrml_data, mapping, added_sources) subject_list = add_subject(yarrrml_data, mapping, mapping_format) pred = add_predicate_object_maps(yarrrml_data, mapping, mapping_format) it = 0 diff --git a/src/yatter/constants.py b/src/yatter/constants.py index a1d79e0..dc5fb40 100644 --- a/src/yatter/constants.py +++ b/src/yatter/constants.py @@ -214,5 +214,3 @@ logger = logging.getLogger(__name__) coloredlogs.install(level='DEBUG', fmt='%(asctime)s,%(msecs)03d | %(levelname)s: %(message)s') - -added_sources, added_targets = {}, {} \ No newline at end of file diff --git a/src/yatter/source.py b/src/yatter/source.py index 3b3f569..6fe77ba 100644 --- a/src/yatter/source.py +++ b/src/yatter/source.py @@ -3,12 +3,11 @@ import rdflib from .constants import * from ruamel.yaml import YAML -from .constants import added_sources, added_targets -def add_source(data, mapping): +def add_source(data, mapping, added_sources={}): source_template = "\t" + RML_LOGICAL_SOURCE + " [\n\t\ta " + RML_LOGICAL_SOURCE_CLASS + \ ";\n\t\t" + RML_SOURCE + " " final_list = [] @@ -142,7 +141,7 @@ def switch_in_reference_formulation(value, source_extension=None): return switcher -def generate_database_connections(data): +def generate_database_connections(data, added_sources): database = [] for mapping in data.get(YARRRML_MAPPINGS): sources = data.get(YARRRML_MAPPINGS).get(mapping).get(YARRRML_SOURCES) diff --git a/src/yatter/target.py b/src/yatter/target.py index 3549329..fe29efa 100644 --- a/src/yatter/target.py +++ b/src/yatter/target.py @@ -1,17 +1,17 @@ from .constants import * -def add_logical_targets(yarrrml_data): +def add_logical_targets(yarrrml_data, added_targets): logical_targets = [] for target in added_targets: logical_targets.extend(generate_added_logical_target(added_targets.get(target), target)) for mapping in yarrrml_data.get(YARRRML_MAPPINGS): mapping_data = yarrrml_data.get(YARRRML_MAPPINGS).get(mapping) - add_internal_logical_target(mapping, mapping_data, logical_targets) + add_internal_logical_target(mapping, mapping_data, logical_targets, added_targets) return logical_targets -def add_internal_logical_target(mapping_id, mapping_data, internal_targets, local_target_id=0): +def add_internal_logical_target(mapping_id, mapping_data, internal_targets, added_targets, local_target_id=0): keys = mapping_data.keys() for key in keys: if type(mapping_data[key]) is list: @@ -35,9 +35,9 @@ def add_internal_logical_target(mapping_id, mapping_data, internal_targets, loca if type(value) is list: for v in value: if type(v) is dict: - add_internal_logical_target(mapping_id, v, internal_targets, local_target_id) + add_internal_logical_target(mapping_id, v, internal_targets, added_targets,local_target_id) elif type(value) is dict: - add_internal_logical_target(mapping_id, value, internal_targets, local_target_id) + add_internal_logical_target(mapping_id, value, internal_targets, added_targets,local_target_id) elif YARRRML_TARGETS in mapping_data[key]: target_value = mapping_data[key][YARRRML_TARGETS] logical_target_id = "logical_target_" + mapping_id + "_" + str(local_target_id)