Skip to content

Commit 4a01243

Browse files
fix: Make pydantic validation optional for backward compatibility
1 parent 2a52814 commit 4a01243

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

scripts/convert.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
if parent_dir not in sys.path:
2525
sys.path.insert(0, parent_dir)
2626

27-
from scripts.card_models import CardYAML, MappingYAML, ValidationError as PydanticValidationError
27+
try:
28+
from scripts.card_models import CardYAML, MappingYAML, ValidationError as PydanticValidationError
29+
PYDANTIC_AVAILABLE = True
30+
except ImportError:
31+
PYDANTIC_AVAILABLE = False
32+
PydanticValidationError = Exception # Fallback
2833

2934

3035
class ConvertVars:
@@ -644,13 +649,14 @@ def get_mapping_data_for_edition(
644649
with open(mappingfile, "r", encoding="utf-8") as f:
645650
try:
646651
data = yaml.safe_load(f)
647-
# Validate structure with Pydantic
648-
try:
649-
validated = MappingYAML(**data)
650-
data = validated.model_dump(exclude_none=True) # Convert back to dict, exclude None values
651-
except PydanticValidationError as ve:
652-
logging.warning(f"Mapping file validation warning for {mappingfile}: {ve}")
653-
# Continue with unvalidated data for backward compatibility
652+
# Validate structure with Pydantic if available
653+
if PYDANTIC_AVAILABLE:
654+
try:
655+
validated = MappingYAML(**data)
656+
data = validated.model_dump(exclude_none=True) # Convert back to dict, exclude None values
657+
except PydanticValidationError as ve:
658+
logging.warning(f"Mapping file validation warning for {mappingfile}: {ve}")
659+
# Continue with unvalidated data for backward compatibility
654660
except yaml.YAMLError as e:
655661
logging.info(f"Error loading yaml file: {mappingfile}. Error = {e}")
656662
data = {}
@@ -752,14 +758,15 @@ def get_language_data(
752758
with open(language_file, "r", encoding="utf-8") as f:
753759
try:
754760
data = yaml.safe_load(f)
755-
# Validate structure with Pydantic
756-
try:
757-
validated = CardYAML(**data)
758-
data = validated.model_dump(exclude_none=True) # Convert back to dict, exclude None values
759-
except PydanticValidationError as ve:
760-
logging.error(f"Card file validation failed for {language_file}: {ve}")
761-
# For card files, validation failure is more critical
762-
data = {}
761+
# Validate structure with Pydantic if available
762+
if PYDANTIC_AVAILABLE:
763+
try:
764+
validated = CardYAML(**data)
765+
data = validated.model_dump(exclude_none=True) # Convert back to dict, exclude None values
766+
except PydanticValidationError as ve:
767+
logging.error(f"Card file validation failed for {language_file}: {ve}")
768+
# For card files, validation failure is more critical
769+
data = {}
763770
except yaml.YAMLError as e:
764771
logging.error(f"Error loading yaml file: {language_file}. Error = {e}")
765772
data = {}

0 commit comments

Comments
 (0)