Skip to content

Commit 558530c

Browse files
committed
Move python-fsutil and requests to [io] optional dependencies.
1 parent 1148f3b commit 558530c

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

benedict/dicts/io/io_util.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
except ModuleNotFoundError:
1616
s3_installed = False
1717

18-
import fsutil
18+
try:
19+
import fsutil
20+
21+
fsutil_installed = True
22+
except ModuleNotFoundError:
23+
fsutil_installed = False
1924

20-
from benedict.extras import require_s3
25+
from benedict.extras import require_fsutil, require_s3
2126
from 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

9095
def 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

149154
def 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

156163
def 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(
171179
def 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

182192
def 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

189199
def 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

193204
def 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()

benedict/extras.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from benedict.exceptions import ExtrasRequireModuleNotFoundError
22

33
__all__ = [
4+
"require_fsutil",
45
"require_html",
56
"require_parse",
67
"require_s3",
@@ -20,6 +21,10 @@ def require_html(*, installed: bool) -> None:
2021
_require_optional_dependencies(target="html", installed=installed)
2122

2223

24+
def require_fsutil(*, installed: bool) -> None:
25+
_require_optional_dependencies(target="io", installed=installed)
26+
27+
2328
def require_parse(*, installed: bool) -> None:
2429
_require_optional_dependencies(target="parse", installed=installed)
2530

benedict/serializers/xls.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from __future__ import annotations
22

3-
import fsutil
3+
try:
4+
import fsutil
5+
6+
fsutil_installed = True
7+
except ModuleNotFoundError:
8+
fsutil_installed = False
49

510
try:
611
from openpyxl import load_workbook
@@ -15,7 +20,7 @@
1520

1621
from slugify import slugify
1722

18-
from benedict.extras import require_xls
23+
from benedict.extras import require_fsutil, require_xls
1924
from benedict.serializers.abstract import AbstractSerializer
2025

2126

@@ -175,6 +180,7 @@ def _decode(self, s: str, **kwargs: Any) -> list[dict[str, Any]]:
175180

176181
def decode(self, s: str, **kwargs: Any) -> list[dict[str, Any]]:
177182
require_xls(installed=xls_installed)
183+
require_fsutil(installed=fsutil_installed)
178184
extension = fsutil.get_file_extension(s)
179185
if extension in ["xlsx", "xlsm"]:
180186
return self._decode(s, **kwargs)

pyproject.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ classifiers = [
9090
"Typing :: Typed",
9191
]
9292
dependencies = [
93-
"python-fsutil >= 0.16.0, < 1.0.0",
9493
"python-slugify >= 7.0.0, < 9.0.0",
95-
"requests >= 2.33.0, < 3.0.0",
9694
"typing_extensions >= 4.13.2, < 4.16.0",
9795
]
9896
dynamic = ["version"]
@@ -120,11 +118,9 @@ Twitter = "https://twitter.com/fabiocaccamo"
120118
all = [
121119
"python-benedict[io,parse,s3]",
122120
]
123-
html = [
124-
"beautifulsoup4 >= 4.12.0, < 5.0.0",
125-
"python-benedict[xml]",
126-
]
127121
io = [
122+
"python-fsutil >= 0.16.1, < 1.0.0",
123+
"requests >= 2.33.0, < 3.0.0",
128124
"python-benedict[html,toml,xls,xml,yaml]",
129125
]
130126
parse = [
@@ -135,12 +131,14 @@ parse = [
135131
]
136132
s3 = [
137133
"boto3 >= 1.24.89, < 2.0.0",
134+
"python-fsutil >= 0.16.1, < 1.0.0",
138135
]
139136
toml = [
140137
"toml >= 0.10.2, < 1.0.0",
141138
]
142139
xls = [
143140
"openpyxl >= 3.0.0, < 4.0.0",
141+
"python-fsutil >= 0.16.1, < 1.0.0",
144142
"xlrd >= 2.0.0, < 3.0.0",
145143
]
146144
xml = [

0 commit comments

Comments
 (0)