77import logging
88import os
99import shutil
10+ import json
1011from typing import Optional
1112
1213from rocrate_validator import services
2223 find_validation_object_on_minio
2324)
2425from app .utils .webhook_utils import send_webhook_notification
25- from app .utils .file_utils import build_metadata_only_rocrate
2626
2727logger = logging .getLogger (__name__ )
2828
2929
3030@celery .task
3131def process_validation_task_by_id (
32- minio_config : dict , crate_id : str , root_path : str , profile_name : str | None , webhook_url : str | None
32+ minio_config : dict , crate_id : str , root_path : str , profile_name : str | None ,
33+ webhook_url : str | None , profiles_path : str | None
3334) -> None :
3435 """
3536 Background task to process the RO-Crate validation by ID.
@@ -56,7 +57,7 @@ def process_validation_task_by_id(
5657 logging .info (f"Processing validation task for { file_path } " )
5758
5859 # Perform validation:
59- validation_result = perform_ro_crate_validation (file_path , profile_name )
60+ validation_result = perform_ro_crate_validation (file_path , profile_name , profiles_path = profiles_path )
6061
6162 if isinstance (validation_result , str ):
6263 logging .error (f"Validation failed: { validation_result } " )
@@ -97,32 +98,27 @@ def process_validation_task_by_id(
9798
9899@celery .task
99100def process_validation_task_by_metadata (
100- crate_json : str , profile_name : str | None , webhook_url : str | None
101+ crate_json : str , profile_name : str | None , webhook_url : str | None , profiles_path : Optional [ str ] = None
101102) -> ValidationResult | str :
102103 """
103104 Background task to process the RO-Crate validation for a given json metadata string.
104105
105106 :param crate_json: A string containing the RO-Crate JSON metadata to validate.
106107 :param profile_name: The name of the validation profile to use. Defaults to None.
107108 :param webhook_url: The webhook URL to send notifications to. Defaults to None.
109+ :param profiles_path: The path to the profiles definition directory. Defaults to None.
108110 :raises Exception: If an error occurs during the validation process.
109111
110112 :todo: Replace the Crate ID with a more comprehensive system, and replace profile name with URI.
111113 """
112114
113- skip_checks_list = ['ro-crate-1.1_12.1' ]
114- file_path = None
115-
116115 try :
117- # Fetch the RO-Crate from MinIO using the provided ID:
118- file_path = build_metadata_only_rocrate (crate_json )
119-
120- logging .info (f"Processing validation task for { file_path } " )
116+ logging .info ("Processing validation task for provided metadata string" )
121117
122118 # Perform validation:
123- validation_result = perform_ro_crate_validation ( file_path ,
119+ validation_result = perform_metadata_validation ( crate_json ,
124120 profile_name ,
125- skip_checks_list
121+ profiles_path
126122 )
127123
128124 if isinstance (validation_result , str ):
@@ -131,9 +127,9 @@ def process_validation_task_by_metadata(
131127 raise Exception (f"Validation failed: { validation_result } " )
132128
133129 if not validation_result .has_issues ():
134- logging .info (f "RO Crate { file_path } is valid." )
130+ logging .info ("RO Crate metadata is valid." )
135131 else :
136- logging .info (f "RO Crate { file_path } is invalid." )
132+ logging .info ("RO Crate metadata is invalid." )
137133
138134 if webhook_url :
139135 send_webhook_notification (webhook_url , validation_result .to_json ())
@@ -147,25 +143,22 @@ def process_validation_task_by_metadata(
147143 send_webhook_notification (webhook_url , error_data )
148144
149145 finally :
150- # Clean up the temporary file if it was created:
151- if file_path and os .path .exists (file_path ):
152- shutil .rmtree (file_path )
153-
154146 if isinstance (validation_result , str ):
155147 return validation_result
156148 else :
157149 return validation_result .to_json ()
158150
159151
160152def perform_ro_crate_validation (
161- file_path : str , profile_name : str | None , skip_checks_list : Optional [list ] = None
153+ file_path : str , profile_name : str | None , skip_checks_list : Optional [list ] = None , profiles_path : Optional [ str ] = None
162154) -> ValidationResult | str :
163155 """
164156 Validates an RO-Crate using the provided file path and profile name.
165157
166158 :param file_path: The path to the RO-Crate file to validate
167159 :param profile_name: The name of the validation profile to use. Defaults to None. If None, the CRS4 validator will
168160 attempt to determine the profile.
161+ :param profiles_path: The path to the profiles definition directory
169162 :param skip_checks_list: A list of checks to skip, if needed
170163 :return: The validation result.
171164 :raises Exception: If an error occurs during the validation process.
@@ -183,7 +176,41 @@ def perform_ro_crate_validation(
183176 settings = services .ValidationSettings (
184177 rocrate_uri = full_file_path ,
185178 ** ({"profile_identifier" : profile_name } if profile_name else {}),
186- ** ({"skip_checks" : skip_checks_list } if skip_checks_list else {})
179+ ** ({"skip_checks" : skip_checks_list } if skip_checks_list else {}),
180+ ** ({"profiles_path" : profiles_path } if profiles_path else {})
181+ )
182+
183+ return services .validate (settings )
184+
185+ except Exception as e :
186+ logging .error (f"Unexpected error during validation: { e } " )
187+ return str (e )
188+
189+
190+ def perform_metadata_validation (
191+ crate_json : str , profile_name : str | None , skip_checks_list : Optional [list ] = None , profiles_path : Optional [str ] = None
192+ ) -> ValidationResult | str :
193+ """
194+ Validates only RO-Crate metadata provided as a json string.
195+
196+ :param crate_json: The JSON string containing the metadata
197+ :param profile_name: The name of the validation profile to use. Defaults to None. If None, the CRS4 validator will
198+ attempt to determine the profile.
199+ :param profiles_path: The path to the profiles definition directory
200+ :param skip_checks_list: A list of checks to skip, if needed
201+ :return: The validation result.
202+ :raises Exception: If an error occurs during the validation process.
203+ """
204+
205+ try :
206+ logging .info (f"Validating ro-crate metadata with profile { profile_name } " )
207+
208+ settings = services .ValidationSettings (
209+ ** ({"metadata_only" : True }),
210+ ** ({"metadata_dict" : json .loads (crate_json )}),
211+ ** ({"profile_identifier" : profile_name } if profile_name else {}),
212+ ** ({"skip_checks" : skip_checks_list } if skip_checks_list else {}),
213+ ** ({"profiles_path" : profiles_path } if profiles_path else {})
187214 )
188215
189216 return services .validate (settings )
0 commit comments