1212 import boto3
1313
1414 s3_installed = True
15- except ModuleNotFoundError :
15+ except ModuleNotFoundError : # pragma: no cover
1616 s3_installed = False
1717
18- import fsutil
18+ try :
19+ import fsutil
20+
21+ fsutil_installed = True
22+ except ModuleNotFoundError : # pragma: no cover
23+ fsutil_installed = False
1924
20- from benedict .extras import require_s3
25+ from benedict .extras import require_fsutil , require_s3
2126from benedict .serializers import (
2227 get_format_by_path ,
2328 get_serializer_by_format ,
@@ -88,7 +93,7 @@ def is_data(s: str | bytes) -> bool:
8893
8994
9095def is_filepath (s : Path | str ) -> bool :
91- if fsutil .is_file (s ):
96+ if fsutil_installed and fsutil .is_file (s ):
9297 return True
9398 return bool (
9499 get_format_by_path (s )
@@ -147,15 +152,18 @@ def read_content(
147152
148153
149154def read_content_from_file (filepath : str , format : str | None = None ) -> str :
155+ require_fsutil (installed = fsutil_installed )
150156 binary_format = is_binary_format (format )
151157 if binary_format :
152158 return filepath
153- return fsutil .read_file (filepath ) # type: ignore[no-any-return]
159+ content = fsutil .read_file (filepath )
160+ return str (content )
154161
155162
156163def read_content_from_s3 (
157164 url : str , s3_options : Mapping [str , Any ], format : str | None = None
158165) -> str :
166+ require_fsutil (installed = fsutil_installed )
159167 require_s3 (installed = s3_installed )
160168 s3_url = parse_s3_url (url )
161169 dirpath = tempfile .gettempdir ()
@@ -171,12 +179,14 @@ def read_content_from_s3(
171179def read_content_from_url (
172180 url : str , requests_options : Mapping [str , Any ], format : str | None = None
173181) -> str :
182+ require_fsutil (installed = fsutil_installed )
174183 binary_format = is_binary_format (format )
175184 if binary_format :
176185 dirpath = tempfile .gettempdir ()
177186 filepath = fsutil .download_file (url , dirpath = dirpath , ** requests_options )
178- return filepath # type: ignore[no-any-return]
179- return fsutil .read_file_from_url (url , ** requests_options ) # type: ignore[no-any-return]
187+ return str (filepath )
188+ content = fsutil .read_file_from_url (url , ** requests_options )
189+ return str (content )
180190
181191
182192def write_content (filepath : str , content : str , ** options : Any ) -> None :
@@ -187,12 +197,14 @@ def write_content(filepath: str, content: str, **options: Any) -> None:
187197
188198
189199def write_content_to_file (filepath : str , content : str , ** options : Any ) -> None :
200+ require_fsutil (installed = fsutil_installed )
190201 fsutil .write_file (filepath , content )
191202
192203
193204def write_content_to_s3 (
194205 url : str , content : str , s3_options : Mapping [str , Any ], ** options : Any
195206) -> None :
207+ require_fsutil (installed = fsutil_installed )
196208 require_s3 (installed = s3_installed )
197209 s3_url = parse_s3_url (url )
198210 dirpath = tempfile .gettempdir ()
0 commit comments