2
2
import hmac
3
3
import io
4
4
import json
5
+ import os
5
6
from pathlib import Path
6
7
from typing import Any , BinaryIO , Dict , Union
7
8
@@ -15,18 +16,31 @@ class LocalResponse:
15
16
"""File object of the local response."""
16
17
17
18
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" ))
20
24
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
+ ):
22
28
with open (input_file , "r" , encoding = "utf-8" ) as file :
23
29
self ._file = io .BytesIO (
24
30
file .read ().replace ("\r " , "" ).replace ("\n " , "" ).encode ()
25
31
)
32
+ elif isinstance (input_file , str ):
33
+ self ._file = io .BytesIO (
34
+ input_file .replace ("\r " , "" ).replace ("\n " , "" ).encode ("utf-8" )
35
+ )
26
36
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 )
28
42
else :
29
- raise MindeeError ("Incompatible type for input." )
43
+ raise MindeeError (f "Incompatible type for input ' { type ( input_file ) } ' ." )
30
44
31
45
@property
32
46
def as_dict (self ) -> Dict [str , Any ]:
0 commit comments