Skip to content

Commit ef4d382

Browse files
committed
fix get_base_schema and implement method to get diff between base and new schema
1 parent 0b994dd commit ef4d382

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

relecov_tools/build_schema.py

+76-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pandas as pd
55
import os
66
import sys
7+
import json
8+
import difflib
79

810
import relecov_tools.utils
911
import relecov_tools.assets.schema_utils.jsonschema_draft
@@ -103,20 +105,27 @@ def get_base_schema(self):
103105
"""
104106
try:
105107
conf = ConfigJson()
106-
schemas = conf.get_configuration("json_schemas")
107-
108-
if "relecov_schema" in schemas:
109-
base_schema = schemas["relecov_schema"]
110-
stderr.print("[green]RELECOV schema found in the configuration.")
111-
return base_schema
108+
relecov_schema_conf = conf.get_topic_data("json_schemas", "relecov_schema")
109+
110+
if relecov_schema_conf:
111+
try:
112+
base_schema_path = os.path.join(
113+
os.path.dirname(os.path.realpath(__file__)),
114+
"schema",
115+
relecov_schema_conf,
116+
)
117+
os.path.isfile(base_schema_path)
118+
stderr.print("[green]RELECOV schema found in the configuration.")
119+
base_schema_json = relecov_tools.utils.read_json_file(
120+
base_schema_path
121+
)
122+
return base_schema_json
123+
except FileNotFoundError as fnf_error:
124+
stderr.print(f"[red]Configuration file not found: {fnf_error}")
125+
return None
112126
else:
113127
stderr.print("[orange]RELECOV schema not found in the configuration.")
114128
return None
115-
116-
except FileNotFoundError as fnf_error:
117-
stderr.print(f"[red]Configuration file not found: {fnf_error}")
118-
return None
119-
120129
except KeyError as key_error:
121130
stderr.print(f"[orange]Configuration key error: {key_error}")
122131
return None
@@ -207,6 +216,53 @@ def verify_schema(self, schema):
207216
)
208217
# TODO: specification version should be added to input params and self.
209218

219+
def print_schema_diff(self, base_schema, new_schema):
220+
"""
221+
Print the differences between the base version of schema_input.json
222+
and the updated version.
223+
"""
224+
# Set diff input
225+
base_schema_lines = json.dumps(base_schema, indent=4).splitlines()
226+
new_schema_lines = json.dumps(new_schema, indent=4).splitlines()
227+
228+
# Get diff lines
229+
diff_lines = list(
230+
difflib.unified_diff(
231+
base_schema_lines,
232+
new_schema_lines,
233+
fromfile="base_schema.json",
234+
tofile="new_schema.json",
235+
)
236+
)
237+
238+
if not diff_lines:
239+
stderr.print(
240+
"[orange]No differencess were found between already installed and new generated schema. Exiting. No changes made"
241+
)
242+
return None
243+
else:
244+
stderr.print(
245+
"Differences found between the existing schema and the newly generated schema."
246+
)
247+
# Set user's choices
248+
choices = ["Print to sandard output (stdout)", "Save to file", "Both"]
249+
diff_output_choice = relecov_tools.utils.prompt_selection(
250+
"How would you like to print the diff between schemes?:", choices
251+
)
252+
if diff_output_choice in ["Print to sandard output (stdout)", "Both"]:
253+
for line in diff_lines:
254+
print(line)
255+
return True
256+
if diff_output_choice in ["Save to file", "Both"]:
257+
stderr.print(self.out_dir)
258+
diff_filepath = os.path.join(
259+
os.path.realpath(self.out_dir) + "/build_schema_diff.txt"
260+
)
261+
with open(diff_filepath, "w") as diff_file:
262+
diff_file.write("\n".join(diff_lines))
263+
stderr.print(f"[green]Schema differences saved to {diff_filepath}")
264+
return True
265+
210266
def update_schema(self):
211267
"""
212268
Update the schema_input.json based on the definitions in the Excel file.
@@ -217,24 +273,16 @@ def update_schema(self):
217273

218274
# TODO: Update the schema_input.json with the new definitions.
219275

220-
def print_schema_diff(self):
221-
"""
222-
Print the differences between the base version of schema_input.json
223-
and the updated version after calling update_schema().
224-
"""
225-
# TODO: Load the base schema_input.json.
226-
227-
# TODO: Load the updated schema_input.json after calling update_schema().
228-
229-
# TODO: Compare the two versions and print/save the differences.
230-
231276
def handle_build_schema(self):
232277
# Load xlsx database and convert into json format
233278
database_dic = self.read_database_definition()
234279

235280
# Verify current schema used by relecov-tools:
236-
self.get_base_schema()
237-
281+
base_schema = self.get_base_schema()
282+
if not base_schema:
283+
stderr.print("[red]Couldn't find relecov base schema. Exiting...)")
284+
sys.exit()
285+
238286
# TODO: if schema not found do something
239287
# if not base_schema:
240288

@@ -248,5 +296,9 @@ def handle_build_schema(self):
248296
self.verify_schema(new_schema)
249297

250298
# TODO: Compare base vs new schema
299+
schema_diff = self.print_schema_diff(base_schema, new_schema)
300+
if schema_diff:
301+
# TODO: If diff ask user to update schema (allow a preview). Else exit. ¿bump version?
302+
self.update_schema()
251303

252304
# TODO: add method to add new schema via input file instead of building new (encompases validation checks).

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ ena-upload-cli
1212
bio==1.4.0
1313
xlsxwriter
1414
bs4==0.0.2
15-
tabulate
15+
tabulate
16+
cdifflib==1.2.6

0 commit comments

Comments
 (0)