55formats and integrates with downloader and catalog utilities.
66"""
77
8+ import re
89from abc import ABC , abstractmethod
910from typing import Any
1011import logging
11- import re
12- import tempfile
1312from pathlib import Path
13+ import tempfile
1414from functools import cached_property
1515
1616import pronto
@@ -189,56 +189,51 @@ def _fix_malformed_dates(self, path_file: Path) -> Path:
189189 """
190190 try :
191191 encoding = self .find_file_encoding (path_file )
192- with open (path_file , 'r' , encoding = encoding ) as f :
192+ with open (path_file , encoding = encoding ) as f :
193193 content = f .read ()
194194
195195 # Check if the file has malformed header dates (DD:MM:YYYY format)
196196 malformed_date_pattern = r'^date: \d{2}:\d{2}:\d{4}.*\n'
197- match = re .search (malformed_date_pattern , content , flags = re .MULTILINE )
197+ match = re .search (
198+ malformed_date_pattern , content , flags = re .MULTILINE
199+ )
198200
199201 if not match :
200202 # No malformed dates, return original file
201203 return path_file
202204
203205 logger .warning (
204- f" Detected malformed date format in { path_file } , fixing..."
206+ f' Detected malformed date format in { path_file } , fixing...'
205207 )
206208
207209 # Remove the malformed header date line
208210 fixed_content = re .sub (
209- malformed_date_pattern ,
210- '' ,
211- content ,
212- flags = re .MULTILINE
211+ malformed_date_pattern , '' , content , flags = re .MULTILINE
213212 )
214213
215214 # Also remove all creation_date fields from terms
216215 # This is necessary because the malformed header date corrupts
217216 # fastobo's date parser, causing it to fail on creation_date fields
218217 fixed_content = re .sub (
219- r'^creation_date:.*\n' ,
220- '' ,
221- fixed_content ,
222- flags = re .MULTILINE
218+ r'^creation_date:.*\n' , '' , fixed_content , flags = re .MULTILINE
223219 )
224220
225221 # Write to temporary file
226222 temp_file = tempfile .NamedTemporaryFile (
227- mode = 'w' ,
228- suffix = '.obo' ,
229- delete = False ,
230- encoding = encoding
223+ mode = 'w' , suffix = '.obo' , delete = False , encoding = encoding
231224 )
232225 temp_file .write (fixed_content )
233226 temp_file .close ()
234227
235228 logger .info (
236- f" Fixed malformed dates in { path_file } , using temporary file: { temp_file .name } "
229+ f' Fixed malformed dates in { path_file } , using temporary file: { temp_file .name } '
237230 )
238231 return Path (temp_file .name )
239232
240- except Exception as e :
241- logger .warning (f"Failed to fix malformed dates: { e } , using original file" )
233+ except (OSError , UnicodeError ) as e :
234+ logger .warning (
235+ f'Failed to fix malformed dates: { e } , using original file'
236+ )
242237 return path_file
243238
244239 def _load_ontology (
@@ -277,7 +272,7 @@ def _load_ontology(
277272 if fixed_path != path_file :
278273 try :
279274 fixed_path .unlink ()
280- except Exception :
275+ except OSError :
281276 pass
282277 raise ValueError (error_msg ) from e
283278
@@ -290,8 +285,10 @@ def _load_ontology(
290285 try :
291286 fixed_path .unlink ()
292287 logger .debug (f'Cleaned up temporary file: { fixed_path } ' )
293- except Exception as e :
294- logger .warning (f'Failed to clean up temporary file { fixed_path } : { e } ' )
288+ except OSError as e :
289+ logger .warning (
290+ f'Failed to clean up temporary file { fixed_path } : { e } '
291+ )
295292
296293 return ontology , ontology_id
297294
0 commit comments