Skip to content

Commit

Permalink
support instantiation from a byte stream
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Mar 15, 2019
1 parent 1824a46 commit acc21db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion configurator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Config(ConfigNode):
parsers = load_parsers()

@classmethod
def from_text(cls, text, parser):
def from_text(cls, text, parser, encoding='ascii'):
if isinstance(text, bytes):
text = text.decode(encoding)
return cls.from_stream(StringIO(text), parser)

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_text_string_parser(self):
config = Config.from_text('{"foo": "bar"}', 'json')
compare(config.data, expected={'foo': 'bar'})

def test_bytes_string_parser(self):
config = Config.from_text(b'{"foo": "bar"}', 'json')
compare(config.data, expected={'foo': 'bar'})

def test_text_callable_parser(self):
config = Config.from_text("{'foo': 'bar'}", python_literal)
compare(config.data, expected={'foo': 'bar'})
Expand Down

0 comments on commit acc21db

Please sign in to comment.