Skip to content

Commit

Permalink
Add shorter way to write simple data types
Browse files Browse the repository at this point in the history
  • Loading branch information
randomdude999 committed May 1, 2017
1 parent 3a07f87 commit 950eb0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mcinfo/nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def pretty_format_nbt(nbt):

class NBTTemplate(object):
def __init__(self, data):
if isinstance(data, str):
new_data = {
'type': "TAG_" + data.split(" ")[0],
'desc': " ".join(data.split(" ")[1:])
}
data = new_data
if 'type' not in data:
raise ValueError("Missing data type")
if data['type'] in nbt_all_types:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@

class TestNBT(unittest.TestCase):

def test_init_with_string(self):
a = nbt.NBTTemplate("Int test")
b = nbt.NBTTemplate({'type': "TAG_Int", 'desc': "test"})
self.assertEqual(a, b)

def test_formatting(self):
nbt_a = nbt.NBTTemplate(testData)
self.assertEqual(nbt.pretty_format_nbt(nbt_a), expectedFormatting)
Expand Down

0 comments on commit 950eb0b

Please sign in to comment.