Skip to content

Commit

Permalink
mtf2json: added decoder for mixed encodings in MTF files (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
juk0de committed Jul 18, 2024
1 parent 310304a commit f862c8e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mtf2json/mtf2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import json
import re
import codecs
from math import ceil
from pathlib import Path
from typing import Dict, Any, Tuple, Union, Optional, List, cast, TextIO
Expand Down Expand Up @@ -71,6 +72,14 @@ class ConversionError(Exception):
string_keys = ['model']


def mixed_decoder(error: UnicodeError) -> Tuple[str, int]:
bs: bytes = error.object[error.start: error.end]
return bs.decode("cp1252"), error.start + 1


codecs.register_error("mixed", mixed_decoder)


def __rename_keys(obj: Any) -> Any:
"""
Rename the keys in the given object according to
Expand Down Expand Up @@ -794,7 +803,7 @@ def read_mtf(path: Path) -> Dict[str, Any]:
mech_data: Dict[str, Any] = {}

current_section = None
with open(path, 'r') as file:
with open(path, 'r', encoding='utf8', errors='mixed') as file:
__check_compat(file)
for line in file:
line = line.strip()
Expand Down

0 comments on commit f862c8e

Please sign in to comment.