22import hmac
33import io
44import json
5+ import os
56from pathlib import Path
67from typing import Any , BinaryIO , Dict , Union
78
@@ -15,18 +16,31 @@ class LocalResponse:
1516 """File object of the local response."""
1617
1718 def __init__ (self , input_file : Union [BinaryIO , str , Path , bytes ]):
18- if isinstance (input_file , BinaryIO ):
19- self ._file = input_file
19+ if isinstance (input_file , (BinaryIO , io .BufferedReader )):
20+ str_stripped = (
21+ input_file .read ().decode ("utf-8" ).replace ("\r " , "" ).replace ("\n " , "" )
22+ )
23+ self ._file = io .BytesIO (str_stripped .encode ("utf-8" ))
2024 self ._file .seek (0 )
21- elif isinstance (input_file , (str , Path )):
25+ elif isinstance (input_file , Path ) or (
26+ isinstance (input_file , str ) and os .path .exists (input_file )
27+ ):
2228 with open (input_file , "r" , encoding = "utf-8" ) as file :
2329 self ._file = io .BytesIO (
2430 file .read ().replace ("\r " , "" ).replace ("\n " , "" ).encode ()
2531 )
32+ elif isinstance (input_file , str ):
33+ self ._file = io .BytesIO (
34+ input_file .replace ("\r " , "" ).replace ("\n " , "" ).encode ("utf-8" )
35+ )
2636 elif isinstance (input_file , bytes ):
27- self ._file = io .BytesIO (input_file )
37+ str_stripped = (
38+ input_file .decode ("utf-8" ).replace ("\r " , "" ).replace ("\n " , "" )
39+ )
40+ self ._file = io .BytesIO (str_stripped .encode ("utf-8" ))
41+ self ._file .seek (0 )
2842 else :
29- raise MindeeError ("Incompatible type for input." )
43+ raise MindeeError (f "Incompatible type for input ' { type ( input_file ) } ' ." )
3044
3145 @property
3246 def as_dict (self ) -> Dict [str , Any ]:
0 commit comments