4
4
import pandas as pd
5
5
import os
6
6
import sys
7
+ import json
8
+ import difflib
7
9
8
10
import relecov_tools .utils
9
11
import relecov_tools .assets .schema_utils .jsonschema_draft
@@ -103,20 +105,27 @@ def get_base_schema(self):
103
105
"""
104
106
try :
105
107
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
112
126
else :
113
127
stderr .print ("[orange]RELECOV schema not found in the configuration." )
114
128
return None
115
-
116
- except FileNotFoundError as fnf_error :
117
- stderr .print (f"[red]Configuration file not found: { fnf_error } " )
118
- return None
119
-
120
129
except KeyError as key_error :
121
130
stderr .print (f"[orange]Configuration key error: { key_error } " )
122
131
return None
@@ -207,6 +216,53 @@ def verify_schema(self, schema):
207
216
)
208
217
# TODO: specification version should be added to input params and self.
209
218
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
+
210
266
def update_schema (self ):
211
267
"""
212
268
Update the schema_input.json based on the definitions in the Excel file.
@@ -217,24 +273,16 @@ def update_schema(self):
217
273
218
274
# TODO: Update the schema_input.json with the new definitions.
219
275
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
-
231
276
def handle_build_schema (self ):
232
277
# Load xlsx database and convert into json format
233
278
database_dic = self .read_database_definition ()
234
279
235
280
# 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
+
238
286
# TODO: if schema not found do something
239
287
# if not base_schema:
240
288
@@ -248,5 +296,9 @@ def handle_build_schema(self):
248
296
self .verify_schema (new_schema )
249
297
250
298
# 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 ()
251
303
252
304
# TODO: add method to add new schema via input file instead of building new (encompases validation checks).
0 commit comments