Skip to content

Commit 2937787

Browse files
committed
Added toml io support.
1 parent 66c8f43 commit 2937787

File tree

7 files changed

+200
-11
lines changed

7 files changed

+200
-11
lines changed

benedict/dicts/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def fromkeys(cls, sequence, value=None):
5555
def from_json(s, **kwargs):
5656
return IODict.from_json(s, **kwargs)
5757

58+
@staticmethod
59+
@benediction
60+
def from_toml(s, **kwargs):
61+
return IODict.from_toml(s, **kwargs)
62+
5863
@staticmethod
5964
@benediction
6065
def from_yaml(s, **kwargs):

benedict/dicts/io.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ def _from_any_string(s, **kwargs):
6161
d = IODict.from_json(s, **kwargs)
6262
except ValueError:
6363
try:
64-
d = IODict.from_yaml(s, **kwargs)
64+
d = IODict.from_toml(s, **kwargs)
6565
except ValueError:
66-
d = None
66+
try:
67+
d = IODict.from_yaml(s, **kwargs)
68+
except ValueError:
69+
d = None
6770
return d
6871

6972
@staticmethod
7073
def from_json(s, **kwargs):
7174
return IODict._load_and_decode(s,
7275
io_util.decode_json, **kwargs)
7376

77+
@staticmethod
78+
def from_toml(s, **kwargs):
79+
return IODict._load_and_decode(s,
80+
io_util.decode_toml, **kwargs)
81+
7482
@staticmethod
7583
def from_yaml(s, **kwargs):
7684
return IODict._load_and_decode(s,
@@ -81,6 +89,11 @@ def to_json(self, filepath=None, **kwargs):
8189
encoder=io_util.encode_json,
8290
filepath=filepath, **kwargs)
8391

92+
def to_toml(self, filepath=None, **kwargs):
93+
return IODict._encode_and_save(self,
94+
encoder=io_util.encode_toml,
95+
filepath=filepath, **kwargs)
96+
8497
def to_yaml(self, filepath=None, **kwargs):
8598
return IODict._encode_and_save(self,
8699
encoder=io_util.encode_yaml,

benedict/utils/io_util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import os
66
import requests
7+
import toml
78
import yaml
89

910

@@ -12,6 +13,11 @@ def decode_json(s, **kwargs):
1213
return data
1314

1415

16+
def decode_toml(s, **kwargs):
17+
data = toml.loads(s, **kwargs)
18+
return data
19+
20+
1521
def decode_yaml(s, **kwargs):
1622
kwargs.setdefault('Loader', yaml.Loader)
1723
data = yaml.load(s, **kwargs)
@@ -23,6 +29,11 @@ def encode_json(d, **kwargs):
2329
return data
2430

2531

32+
def encode_toml(d, **kwargs):
33+
data = toml.dumps(d, **kwargs)
34+
return data
35+
36+
2637
def encode_yaml(d, **kwargs):
2738
data = yaml.dump(d, **kwargs)
2839
return data

tests/input/invalid-content.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Lorem ipsum consectetur sint id aute officia sed excepteur consectetur labore laboris dolore in labore consequat ut in eu ut deserunt.
2+
Elit aliqua velit aliquip voluptate consequat reprehenderit occaecat dolor ut esse aute laboris cillum fugiat esse est laborum.

tests/input/valid-content.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is a TOML document.
2+
# https://pypi.org/project/toml/
3+
4+
title = "TOML Example"
5+
6+
[owner]
7+
name = "Tom Preston-Werner"
8+
dob = 1979-05-27T07:32:00-08:00 # First class dates
9+
10+
[database]
11+
server = "192.168.1.1"
12+
ports = [ 8001, 8001, 8002 ]
13+
connection_max = 5000
14+
enabled = true
15+
16+
[servers]
17+
18+
# Indentation (tabs and/or spaces) is allowed but not required
19+
[servers.alpha]
20+
ip = "10.0.0.1"
21+
dc = "eqdc10"
22+
23+
[servers.beta]
24+
ip = "10.0.0.2"
25+
dc = "eqdc10"
26+
27+
[clients]
28+
data = [ ["gamma", "delta"], [1, 2] ]
29+
30+
# Line breaks are OK when inside arrays
31+
hosts = [
32+
"alpha",
33+
"omega"
34+
]

tests/test_benedict.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ def test_from_json(self):
230230
self.assertTrue(isinstance(d, benedict))
231231
self.assertEqual(d, { 'a': 1, 'b': 2, 'c': 3, })
232232

233+
def test_from_toml(self):
234+
j = """
235+
a = 1
236+
237+
[b]
238+
c = 3
239+
d = 4
240+
"""
241+
# static method
242+
d = benedict.from_toml(j)
243+
self.assertTrue(isinstance(d, dict))
244+
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
245+
# constructor
246+
d = benedict(j)
247+
self.assertTrue(isinstance(d, dict))
248+
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
249+
233250
def test_from_yaml(self):
234251
j = """
235252
a: 1

tests/test_io_dict.py

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_from_json_with_invalid_file(self):
7373
d = IODict(filepath)
7474

7575
def test_from_json_with_valid_url_valid_content(self):
76-
url = 'https://jsonplaceholder.typicode.com/users'
76+
url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.json'
7777
# static method
7878
d = IODict.from_json(url)
7979
self.assertTrue(isinstance(d, dict))
@@ -127,6 +127,113 @@ def test_to_json_file(self):
127127

128128
# YAML
129129

130+
def test_from_toml_with_valid_data(self):
131+
j = """
132+
a = 1
133+
134+
[b]
135+
c = 3
136+
d = 4
137+
"""
138+
# static method
139+
d = IODict.from_toml(j)
140+
self.assertTrue(isinstance(d, dict))
141+
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
142+
# constructor
143+
d = IODict(j)
144+
self.assertTrue(isinstance(d, dict))
145+
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
146+
147+
def test_from_toml_with_invalid_data(self):
148+
j = 'Lorem ipsum est in ea occaecat nisi officia.'
149+
# static method
150+
with self.assertRaises(ValueError):
151+
d = IODict.from_toml(j)
152+
# constructor
153+
with self.assertRaises(ValueError):
154+
d = IODict(j)
155+
156+
def test_from_toml_with_valid_file_valid_content(self):
157+
filepath = self.input_path('valid-content.toml')
158+
# static method
159+
d = IODict.from_toml(filepath)
160+
self.assertTrue(isinstance(d, dict))
161+
# constructor
162+
d = IODict(filepath)
163+
self.assertTrue(isinstance(d, dict))
164+
165+
def test_from_toml_with_valid_file_invalid_content(self):
166+
filepath = self.input_path('invalid-content.toml')
167+
# static method
168+
with self.assertRaises(ValueError):
169+
d = IODict.from_toml(filepath)
170+
# constructor
171+
with self.assertRaises(ValueError):
172+
d = IODict(filepath)
173+
174+
def test_from_toml_with_invalid_file(self):
175+
filepath = self.input_path('invalid-file.toml')
176+
# static method
177+
with self.assertRaises(ValueError):
178+
d = IODict.from_toml(filepath)
179+
# constructor
180+
with self.assertRaises(ValueError):
181+
d = IODict(filepath)
182+
183+
# def test_from_toml_with_valid_url_valid_content(self):
184+
# url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.toml'
185+
# # static method
186+
# d = IODict.from_toml(url)
187+
# self.assertTrue(isinstance(d, dict))
188+
# # constructor
189+
# d = IODict(url)
190+
# self.assertTrue(isinstance(d, dict))
191+
192+
def test_from_toml_with_valid_url_invalid_content(self):
193+
url = 'https://github.com/fabiocaccamo/python-benedict'
194+
# static method
195+
with self.assertRaises(ValueError):
196+
d = IODict.from_toml(url)
197+
# constructor
198+
with self.assertRaises(ValueError):
199+
d = IODict(url)
200+
201+
def test_from_toml_with_invalid_url(self):
202+
url = 'https://github.com/fabiocaccamo/python-benedict-invalid'
203+
# static method
204+
with self.assertRaises(ValueError):
205+
d = IODict.from_toml(url)
206+
# constructor
207+
with self.assertRaises(ValueError):
208+
d = IODict(url)
209+
210+
def test_to_toml(self):
211+
d = IODict({
212+
'x': 7,
213+
'y': 8,
214+
'z': 9,
215+
'a': 1,
216+
'b': 2,
217+
'c': 3,
218+
})
219+
s = d.to_toml()
220+
self.assertEqual(d, IODict.from_toml(s))
221+
222+
def test_to_toml_file(self):
223+
d = IODict({
224+
'x': 7,
225+
'y': 8,
226+
'z': 9,
227+
'a': 1,
228+
'b': 2,
229+
'c': 3,
230+
})
231+
filepath = self.output_path('test_to_toml_file.toml')
232+
s = d.to_toml(filepath=filepath)
233+
self.assertTrue(d, os.path.isfile(filepath))
234+
self.assertEqual(d, IODict.from_toml(filepath))
235+
236+
130237
def test_from_yaml_with_valid_data(self):
131238
j = """
132239
a: 1
@@ -179,14 +286,14 @@ def test_from_yaml_with_invalid_file(self):
179286
with self.assertRaises(ValueError):
180287
d = IODict(filepath)
181288

182-
# def test_from_yaml_with_valid_url_valid_content(self):
183-
# url = 'https://github.com/fabiocaccamo/python-benedict/tests/input/valid-content.yml'
184-
# # static method
185-
# d = IODict.from_yaml(url)
186-
# self.assertTrue(isinstance(d, dict))
187-
# # constructor
188-
# d = IODict(url)
189-
# self.assertTrue(isinstance(d, dict))
289+
def test_from_yaml_with_valid_url_valid_content(self):
290+
url = 'https://raw.githubusercontent.com/fabiocaccamo/python-benedict/master/tests/input/valid-content.yml'
291+
# static method
292+
d = IODict.from_yaml(url)
293+
self.assertTrue(isinstance(d, dict))
294+
# constructor
295+
d = IODict(url)
296+
self.assertTrue(isinstance(d, dict))
190297

191298
def test_from_yaml_with_valid_url_invalid_content(self):
192299
url = 'https://github.com/fabiocaccamo/python-benedict'

0 commit comments

Comments
 (0)