Merge branch 'master' of git://github.com/carrus85/pymclevel.git

This commit is contained in:
David Vierra 2010-11-16 16:41:26 -10:00
commit 92d45e781c
2 changed files with 23 additions and 29 deletions

View File

@ -388,11 +388,8 @@ class MCLevel(object):
self.packChunkData();
buf = StringIO.StringIO()
gzipper = gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=2)
self.root_tag.save(buf=gzipper)
gzipper.close();
with closing(gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=2)) as gzipper:
self.root_tag.save(buf=gzipper)
self.compressedTag = buf.getvalue()
@ -406,15 +403,14 @@ class MCLevel(object):
else:
return;
gzipper = gzip.GzipFile(fileobj=StringIO.StringIO(self.compressedTag))
try:
data = gzipper.read();
if data == None: return;
except Exception, e:
error( u"Error reading compressed data, assuming uncompressed: {0}".format(e) )
data = self.compressedTag
with closing(gzip.GzipFile(fileobj=StringIO.StringIO(self.compressedTag))) as gzipper:
try:
data = gzipper.read();
if data == None: return;
except Exception, e:
error( u"Error reading compressed data, assuming uncompressed: {0}".format(e) )
data = self.compressedTag
gzipper.close();
try:
self.root_tag = nbt.load(buf=fromstring(data, dtype='uint8'));
@ -1288,9 +1284,9 @@ class MCSchematic (MCLevel):
#root_tag[Materials] = nbt.TAG_String(materialNames[self.materials])
#self.packChunkData();
self.compress();
chunkfh = file(filename, 'wb')
chunkfh.write(self.compressedTag)
chunkfh.close()
with open(filename, 'wb') as chunkfh:
chunkfh.write(self.compressedTag)
#self.root_tag.saveGzipped(filename);
#self.unpackChunkData();
@ -2021,9 +2017,8 @@ class MCInfdevOldLevel(MCLevel):
#intended to be called on a second thread.
#as a side effect, the operating system will have the given chunk files in the file cache.
for c in chunks:
chunkfh = file(self._presentChunks[c].filename, 'rb')
self.compressedTags[c] = chunkfh.read();
chunkfh.close();
with open(self._presentChunks[c].filename, 'rb') as chunkfh:
self.compressedTags[c] = chunkfh.read();
def compressAllChunks(self):
for ch in self._presentChunks.itervalues():
@ -3480,12 +3475,10 @@ class MCJavaLevel(MCLevel):
pass;
try:
f = file(self.filename, 'wb')
f.write(s.getvalue());
with open(self.filename, 'wb') as f:
f.write(s.getvalue());
except Exception, e:
info( u"Error while saving java level in place: {0}".format( e ) )
f.close()
try:os.remove(self.filename);
except: pass
os.rename(self.filename + ".old", self.filename);
@ -3494,7 +3487,6 @@ class MCJavaLevel(MCLevel):
os.remove(self.filename + ".old");
except Exception,e:
pass;
f.close()
###xxxxx CHECK RESULTS
def testJavaLevels():

12
nbt.py
View File

@ -24,6 +24,7 @@ import struct
import gzip
import StringIO;
import os;
from contextlib import closing
from numpy import array, zeros, uint8, fromstring
TAGfmt = ">b"
@ -88,13 +89,14 @@ class TAG_Value(object):
#print "Atomic Save: No existing file to rename"
pass
outputGz = gzip.GzipFile(fileobj=sio, mode="wb", compresslevel=compresslevel)
self.save(buf=outputGz);
outputGz.flush();
outputGz.close();
with closing(gzip.GzipFile(fileobj=sio, mode="wb", compresslevel=compresslevel)) as outputGz:
self.save(buf=outputGz);
outputGz.flush();
#print len(sio.getvalue());
try:
file(filename, "wb").write(sio.getvalue());
with open(filename, 'wb') as f:
f.write(sio.getvalue());
except:
try:
os.rename(filename + ".old", filename, );