Limited support for writing nbt files

This commit is contained in:
Nikita Kitaev 2016-10-06 21:56:45 -07:00
parent ef0572b351
commit 08da5a36c6

View File

@ -54,9 +54,13 @@ IF IS_PY2:
from cStringIO import StringIO from cStringIO import StringIO
from cpython cimport PyString_FromStringAndSize from cpython cimport PyString_FromStringAndSize
binary_type = str binary_type = str
cdef object iteritems(obj):
return obj.iteritems()
ELSE: ELSE:
from io import BytesIO as StringIO from io import BytesIO as StringIO
binary_type = bytes binary_type = bytes
cdef object iteritems(obj):
return obj.items()
import numpy import numpy
@ -71,8 +75,8 @@ IF IS_PY2:
cdef PycStringIO_CAPI *PycStringIO = <PycStringIO_CAPI *> PyCObject_Import("cStringIO", "cStringIO_CAPI") cdef PycStringIO_CAPI *PycStringIO = <PycStringIO_CAPI *> PyCObject_Import("cStringIO", "cStringIO_CAPI")
cdef PyTypeObject * StringO = PycStringIO.OutputType cdef PyTypeObject * StringO = PycStringIO.OutputType
ELSE: ELSE:
# The equivalent python3 code has not been written, so for now only reading # The equivalent python3 code has not been written, so for now we fall back
# is supported in python3 # on a codepath that might have poor performance.
pass pass
# Tag IDs # Tag IDs
@ -827,7 +831,7 @@ cdef void cwrite(obj, char *buf, size_t len):
IF IS_PY2: IF IS_PY2:
PycStringIO.cwrite(obj, buf, len) PycStringIO.cwrite(obj, buf, len)
ELSE: ELSE:
raise Exception("[nbt.pyx python3 port] cwrite was not ported to python3") obj.write(buf[:len])
cdef void save_tag_id(char tagID, object buf): cdef void save_tag_id(char tagID, object buf):
@ -940,7 +944,7 @@ def nested_string(tag, indent_string=" ", indent=0):
if tag.tagID == _ID_COMPOUND: if tag.tagID == _ID_COMPOUND:
result += 'TAG_Compound({\n' result += 'TAG_Compound({\n'
indent += 1 indent += 1
for key, value in tag.iteritems(): for key, value in iteritems(tag):
result += indent_string * indent + '"%s": %s,\n' % (key, nested_string(value, indent_string, indent)) result += indent_string * indent + '"%s": %s,\n' % (key, nested_string(value, indent_string, indent))
indent -= 1 indent -= 1
result += indent_string * indent + '})' result += indent_string * indent + '})'
@ -982,7 +986,7 @@ def walk(tag, path=None):
if path is None: if path is None:
path = [] path = []
if tag.isCompound(): if tag.isCompound():
for name, subtag in tag.iteritems(): for name, subtag in iteritems(tag):
yield (name, subtag, path) yield (name, subtag, path)
for result in walk(subtag, path + [name]): for result in walk(subtag, path + [name]):
yield result yield result