Skip to content

Commit 0bdd999

Browse files
authored
Fix s3_options option forwarded to json decoder. (#204)
1 parent 8753f07 commit 0bdd999

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

benedict/dicts/io/io_util.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def decode(s, format, **kwargs):
2222
options = kwargs.copy()
2323
if format in ["b64", "base64"]:
2424
options.setdefault("subformat", "json")
25-
content = read_content(s, format, **options)
25+
content = read_content(s, format, options)
2626
data = serializer.decode(content, **options)
2727
return data
2828

@@ -76,43 +76,45 @@ def parse_s3_url(url):
7676
}
7777

7878

79-
def read_content(s, format=None, **options):
79+
def read_content(s, format=None, options=None):
8080
# s -> filepath or url or data
81-
options.setdefault("format", format)
81+
# options.setdefault("format", format)
82+
options = options or {}
8283
s = s.strip()
8384
if is_data(s):
8485
return s
8586
elif is_url(s):
86-
return read_content_from_url(s, **options)
87+
requests_options = options.pop("requests_options", None) or {}
88+
return read_content_from_url(s, requests_options, format)
8789
elif is_s3(s):
88-
return read_content_from_s3(s, **options)
90+
s3_options = options.pop("s3_options", None) or {}
91+
return read_content_from_s3(s, s3_options, format)
8992
elif is_filepath(s):
90-
return read_content_from_file(s, **options)
93+
return read_content_from_file(s, format)
9194
# one-line data?!
9295
return s
9396

9497

95-
def read_content_from_file(filepath, format=None, **options):
98+
def read_content_from_file(filepath, format=None):
9699
binary_format = is_binary_format(format)
97100
if binary_format:
98101
return filepath
99102
return fsutil.read_file(filepath)
100103

101104

102-
def read_content_from_s3(url, s3_options, format=None, **options):
105+
def read_content_from_s3(url, s3_options, format=None):
103106
s3_url = parse_s3_url(url)
104107
dirpath = tempfile.gettempdir()
105108
filename = fsutil.get_filename(s3_url["key"])
106109
filepath = fsutil.join_path(dirpath, filename)
107110
s3 = boto3.client("s3", **s3_options)
108111
s3.download_file(s3_url["bucket"], s3_url["key"], filepath)
109112
s3.close()
110-
content = read_content_from_file(filepath, format, **options)
113+
content = read_content_from_file(filepath, format)
111114
return content
112115

113116

114-
def read_content_from_url(url, requests_options=None, format=None, **options):
115-
requests_options = requests_options or {}
117+
def read_content_from_url(url, requests_options, format=None):
116118
binary_format = is_binary_format(format)
117119
if binary_format:
118120
dirpath = tempfile.gettempdir()

tests/github/test_issue_0198.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import unittest
2+
3+
from decouple import config
4+
5+
from benedict import benedict
6+
7+
8+
class github_issue_0198_test_case(unittest.TestCase):
9+
"""
10+
This class describes a github issue 0198 test case.
11+
https://github.com/fabiocaccamo/python-benedict/issues/198
12+
13+
To run this specific test:
14+
- Run python -m unittest tests.github.test_issue_0198
15+
"""
16+
17+
def test_constructor_with_s3_url_and_s3_options_with_file_json(self):
18+
aws_access_key_id = config("AWS_ACCESS_KEY_ID", default=None)
19+
aws_secret_access_key = config("AWS_SECRET_ACCESS_KEY", default=None)
20+
if not all([aws_access_key_id, aws_secret_access_key]):
21+
# don't use s3 on GH CI
22+
return
23+
s3_options = {
24+
"aws_access_key_id": aws_access_key_id,
25+
"aws_secret_access_key": aws_secret_access_key,
26+
}
27+
d = benedict(
28+
"s3://python-benedict/valid-content.json",
29+
s3_options=s3_options,
30+
)
31+
expected_dict = {"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}
32+
self.assertEqual(d, expected_dict)
33+
34+
def test_constructor_with_s3_url_and_s3_options_with_file_xlsx(self):
35+
aws_access_key_id = config("AWS_ACCESS_KEY_ID", default=None)
36+
aws_secret_access_key = config("AWS_SECRET_ACCESS_KEY", default=None)
37+
if not all([aws_access_key_id, aws_secret_access_key]):
38+
# don't use s3 on GH CI
39+
return
40+
s3_options = {
41+
"aws_access_key_id": aws_access_key_id,
42+
"aws_secret_access_key": aws_secret_access_key,
43+
}
44+
d = benedict(
45+
"s3://python-benedict/valid-content.xlsx",
46+
s3_options=s3_options,
47+
)
48+
expected_dict = {
49+
"values": [
50+
{
51+
"mon": 10,
52+
"tue": 11,
53+
"wed": 12,
54+
"thu": 13,
55+
"fri": 14,
56+
"sat": 15,
57+
"sun": 16,
58+
},
59+
{
60+
"mon": 20,
61+
"tue": 21,
62+
"wed": 22,
63+
"thu": 23,
64+
"fri": 24,
65+
"sat": 25,
66+
"sun": 26,
67+
},
68+
{
69+
"mon": 30,
70+
"tue": 31,
71+
"wed": 32,
72+
"thu": 33,
73+
"fri": 34,
74+
"sat": 35,
75+
"sun": 36,
76+
},
77+
]
78+
}
79+
self.assertEqual(d, expected_dict)

0 commit comments

Comments
 (0)