Skip to content

Commit 4c2eb07

Browse files
fix: fix minor errors detected by ruff in _fix_malformed_dates function
1 parent d2a5b28 commit 4c2eb07

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

ontograph/config/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from importlib.metadata import version
21
from pathlib import Path
2+
from importlib.metadata import version
33

44
from appdirs import user_cache_dir
55

ontograph/loader.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
formats and integrates with downloader and catalog utilities.
66
"""
77

8+
import re
89
from abc import ABC, abstractmethod
910
from typing import Any
1011
import logging
11-
import re
12-
import tempfile
1312
from pathlib import Path
13+
import tempfile
1414
from functools import cached_property
1515

1616
import 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

Comments
 (0)