Mike Bayer avatar Mike Bayer committed b76e629

- Fixed -n option in yaml2nbt, previously was having
no effect and still gzipping output.

- not sure what this is about but there was an L for long ints in the test YAML samples that seems to
be not needed now. not sure when that was being produced.

- test improvements.

Comments (0)

Files changed (9)

 \.coverage
 \.DS_Store
 test.cfg
+.venv
+0.2.1
+=====
+Fixed -n option in yaml2nbt, previously was having
+no effect and still gzipping output.
+
+0.2.0
+=====
+Initial Release.
 def nbtedit():
     parser = argparse.ArgumentParser(description="Edit an nbt file in-place in yaml format.")
     parser.add_argument("filename", type=str, help="filename")
-    parser.add_argument("-n", "--no-gzip", 
-                        action="store_true", 
+    parser.add_argument("-n", "--no-gzip",
+                        action="store_true",
                         help="Don't use gzip"
                         )
 
     options = parser.parse_args()
 
     struct = parse_nbt(
-                    open(options.filename, 'rb'), 
+                    open(options.filename, 'rb'),
                     gzipped=not options.no_gzip)
     try:
         editor = os.environ['EDITOR']
 def nbt2yaml():
     parser = argparse.ArgumentParser(description="Dump an nbt file or stream to yaml.")
     parser.add_argument("filename", type=str, help="Filename.  Specify as '-' to read from stdin.")
-    parser.add_argument("-n", "--no-gzip", 
-                        action="store_true", 
+    parser.add_argument("-n", "--no-gzip",
+                        action="store_true",
                         help="Don't use gzip"
                         )
     options = parser.parse_args()
 def yaml2nbt():
     parser = argparse.ArgumentParser(description="Dump a yaml file or stream to nbt.")
     parser.add_argument("filename", type=str, help="Filename.  Specify as '-' to read from stdin.")
-    parser.add_argument("-n", "--no-gzip", 
-                        action="store_true", 
+    parser.add_argument("-n", "--no-gzip",
+                        action="store_true",
                         help="Don't use gzip"
                         )
     options = parser.parse_args()
         input_ = open(options.filename, 'rb')
 
     struct = parse_yaml(input_)
-    dump_nbt(struct, sys.stdout)
+    dump_nbt(struct, sys.stdout, gzipped=not options.no_gzip)

tests/__init__.py

 import os
+import gzip
 
-def datafile(name):
-    return open(os.path.join(os.path.dirname(__file__), 'files', name))
+def datafile(name, ungzip=False):
+    f = open(os.path.join(os.path.dirname(__file__), 'files', name))
+    if ungzip:
+        f = gzip.GzipFile(fileobj=f)
+    return f
 
 def eq_(a, b):
     assert a == b, "%r != %r" % (a, b)
Add a comment to this file

tests/files/spawner.nbt

Binary file added.

tests/files/spawner.yml

+Schematic:
+- Blocks: !byte_array "4"
+- Width: !short "1"
+- Height: !short "1"
+- Entities: !list_byte []
+- Length: !short "1"
+- Materials: Alpha
+- TileEntities: !list_compound
+  - - Delay: !short "120"
+    - y: 0
+    - x: 0
+    - EntityId: Skeleton
+    - z: 0
+    - id: MobSpawner
+- Data: !byte_array "\0"

tests/test_dump_nbt.py

 
     def test_large(self):
         self._assert_data("bigtest.nbt")
+
+    def test_spawner(self):
+        self._assert_data("spawner.nbt")

tests/test_dump_yaml.py

 """? ''
 : - Data:
     - thundering: !byte "0"
-    - LastPlayed: !long "1315921966180L"
+    - LastPlayed: !long "1315921966180"
     - Player:
       - Motion: !list_double
         - !double "9.166176096485612e-17"
       - Inventory: !list_byte []
 """)
 
+    def test_spawner(self):
+        data = parse_nbt(datafile("spawner.nbt"))
+        eq_(dump_yaml(data), datafile("spawner.yml").read())
+
     def test_large(self):
         data = parse_nbt(datafile("bigtest.nbt"))
         eq_(dump_yaml(data),
 r"""Level:
-- longTest: !long "9223372036854775807L"
+- longTest: !long "9223372036854775807"
 - shortTest: !short "32767"
 - stringTest: !!python/str "HELLO WORLD THIS IS A TEST STRING \xC5\xC4\xD6!"
 - floatTest: 0.4982314705848694
   - !long "15"
 - listTest (compound): !list_compound
   - - name: 'Compound tag #0'
-    - created-on: !long "1264099775885L"
+    - created-on: !long "1264099775885"
   - - name: 'Compound tag #1'
-    - created-on: !long "1264099775885L"
+    - created-on: !long "1264099775885"
 - byteTest: !byte "127"
 - byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...)): !byte_array "\0\
     >\"\x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1C\
     $N\x1E\\@.&(4J\x060"
 - doubleTest: !double "0.4931287132182315"
 """)
+

tests/test_parse_yaml.py

 from nbt2yaml import parse_nbt, dump_yaml, parse_yaml, dump_nbt
 from tests import datafile, eq_
 
+# TODO: this is to avoid ascii decode issues, is a huge hack.  need a
+# real binary buffer object
+import cStringIO as StringIO
+
 class FromYamlTest(unittest.TestCase):
     def test_basic(self):
         data = parse_yaml(datafile("test.yml"))
 
-        eq_(data, parse_nbt(datafile("test.nbt")))
+        self._assert(data, "test.nbt")
 
     def test_large(self):
         data = parse_yaml(datafile("bigtest.yml"))
+        self._assert(data, "bigtest.nbt")
 
-        eq_(data, parse_nbt(datafile("bigtest.nbt")))
 
     def test_lists(self):
         data = parse_yaml(datafile("list.yml"))
+        self._assert(data, "list.nbt")
 
-        eq_(data, parse_nbt(datafile("list.nbt")))
+
+    def test_spawner(self):
+        data = parse_yaml(datafile("spawner.yml"))
+        self._assert(data, "spawner.nbt")
+
+    def _assert(self, data, nbt_filename):
+        eq_(data, parse_nbt(datafile(nbt_filename)))
+
+        out = StringIO.StringIO()
+        dump_nbt(data, out, gzipped=False)
+
+        eq_(out.getvalue(), datafile(nbt_filename, ungzip=True).read())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.