block.py: use logging to report issues

This way diagnostics are printed to stderr and can easilly be redirected to a
file. Additionally we can filter information and don#t need to comment print
statements.
This commit is contained in:
Stefan Sauer 2016-08-18 10:40:15 +02:00
parent 37f3f33bf2
commit acd7eaa444
4 changed files with 30 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import zlib
import nbt import nbt
import random import random
import time import time
import logging
from io import BytesIO from io import BytesIO
import sqlite3 import sqlite3
from serialize import * from serialize import *
@ -11,6 +12,7 @@ from itemstack import *
from tile_entities import te_convert from tile_entities import te_convert
from entities import e_convert from entities import e_convert
logger = logging.getLogger('block')
class MCMap: class MCMap:
"""A MC map""" """A MC map"""
@ -229,7 +231,7 @@ class MTBlock:
self.pos = (0, 0, 0) self.pos = (0, 0, 0)
def fromMCBlock(self, mcblock, conversion_table): def fromMCBlock(self, mcblock, conversion_table):
# print('\n***fromMCBlock: Starting New Block***') logger.debug('***fromMCBlock: Starting New Block***')
self.timers = [] self.timers = []
self.pos = (mcblock.pos[0], mcblock.pos[1]-4, mcblock.pos[2]) self.pos = (mcblock.pos[0], mcblock.pos[1]-4, mcblock.pos[2])
@ -273,9 +275,9 @@ class MTBlock:
elif isdoor(blocks[i]) and data[i] < 8: elif isdoor(blocks[i]) and data[i] < 8:
above = i + 256 above = i + 256
if (above >= 4096): if (above >= 4096):
print('Unable to fix door - top part is across block boundary!') logger.warning('Unable to fix door - top part is across block boundary! (%d >= 4096)' % above)
elif isdoor(blocks[above]) and data[above] < 7: elif isdoor(blocks[above]) and data[above] < 7:
print('Unable to fix door - bottom part on top of bottom part!') logger.warning('Unable to fix door - bottom part on top of bottom part!')
else: else:
d_right = data[above] & 1 # 0 - left, 1 - right d_right = data[above] & 1 # 0 - left, 1 - right
d_open = data[i] & 4 # 0 - closed, 1 - open d_open = data[i] & 4 # 0 - closed, 1 - open
@ -289,9 +291,9 @@ class MTBlock:
elif isdoor(blocks[i]) and data[i] >= 8: elif isdoor(blocks[i]) and data[i] >= 8:
below = i - 256 below = i - 256
if (below < 0): if (below < 0):
print('Unable to fix door - bottom part is across block boundary!') logger.warning('Unable to fix door - bottom part is across block boundary! (%d < 0)' % below)
elif isdoor(blocks[below]) and data[below] >= 8: elif isdoor(blocks[below]) and data[below] >= 8:
print('Unable to fix door - top part below top part!') logger.warning('Unable to fix door - top part below top part!')
else: else:
d_right = data[i] & 1 # 0 - left, 1 - right d_right = data[i] & 1 # 0 - left, 1 - right
d_open = data[below] & 4 # 0 - closed, 1 - open d_open = data[below] & 4 # 0 - closed, 1 - open
@ -304,7 +306,7 @@ class MTBlock:
self.metadata[(i & 0xf, (i>>8) & 0xf, (i>>4) & 0xf)] = ({ "right": "1" }, {}) self.metadata[(i & 0xf, (i>>8) & 0xf, (i>>4) & 0xf)] = ({ "right": "1" }, {})
elif content[i]==0 and param2[i]==0 and not (blocks[i]==0): elif content[i]==0 and param2[i]==0 and not (blocks[i]==0):
print('Unknown Minecraft Block:' + str(mcblockidentifier[i])) # This is the minecraft ID#/data as listed in map_content.txt logger.warning('Unknown Minecraft Block:' + str(mcblockidentifier[i])) # This is the minecraft ID#/data as listed in map_content.txt
for te in mcblock.tile_entities: for te in mcblock.tile_entities:
id = te["id"] id = te["id"]
@ -312,8 +314,8 @@ class MTBlock:
index = ((y&0xf)<<8)|((z&0xf)<<4)|(x&0xf) index = ((y&0xf)<<8)|((z&0xf)<<4)|(x&0xf)
f = te_convert.get(id.lower(), lambda arg: (None, None, None)) # Do nothing if not found f = te_convert.get(id.lower(), lambda arg: (None, None, None)) # Do nothing if not found
block, p2, meta = f(te) block, p2, meta = f(te)
# print('\nEntityInfoPre: ' +str(te)) # EntityInfo: if you want to print pre-conversion entity information then uncomment this line logger.debug('EntityInfoPre: ' +str(te))
# print('EntityInfoPost: ' +' y='+str(y)+' z='+str(z)+' x='+str(x)+' Meta:'+str(meta)) # EntityInfo: if you want to print post-conversion entity information then uncomment this line logger.debug('EntityInfoPost: ' +' y='+str(y)+' z='+str(z)+' x='+str(x)+' Meta:'+str(meta))
# NB block and p2 never seems to be returned, but if this is important, then just change the above 'meta' to 'f(te)' # NB block and p2 never seems to be returned, but if this is important, then just change the above 'meta' to 'f(te)'
if block != None: if block != None:
@ -330,7 +332,7 @@ class MTBlock:
else: else:
content[above], param2[above] = conversion_table[940][p] content[above], param2[above] = conversion_table[940][p]
else: else:
print("can't pot plant in pot across block border, or not air") logger.warning("can't pot plant in pot across block border, or not air")
except: except:
self.metadata[(x&0xf, y&0xf, z&0xf)] = meta self.metadata[(x&0xf, y&0xf, z&0xf)] = meta
@ -429,8 +431,8 @@ class MTBlock:
# Node timer # Node timer
writeU8(os, 2+4+4) # Timer data len writeU8(os, 2+4+4) # Timer data len
writeU16(os, len(self.timers)) # Number of timers writeU16(os, len(self.timers)) # Number of timers
#if len(self.timers) > 0: if len(self.timers) > 0:
# print('wrote ' + str(len(self.timers)) + ' node timers') logger.info('wrote ' + str(len(self.timers)) + ' node timers')
for i in range(len(self.timers)): for i in range(len(self.timers)):
writeU16(os, self.timers[i][0]) writeU16(os, self.timers[i][0])
writeU32(os, self.timers[i][1]) writeU32(os, self.timers[i][1])

View File

@ -1,10 +1,15 @@
import logging
from itemstack import * from itemstack import *
#If you wish to add more entities, then... #If you wish to add more entities, then...
# To print out pre and post-conversion entity information uncomment line 237 (ish) in blocks.py (search for 'EntityInfo' to locate it) # To see pre and post-conversion entity, raise the log level to DEBUG and look
# at the log output on stderr in category 'blocks' (search for 'EntityInfo' to
# locate them)
logger = logging.getLogger('entities')
def convert_frame(e): def convert_frame(e):
from pprint import pprint from pprint import pformat
pprint(e) logger.debug(pformat(e))
# must read attribs and translate to entities we know from map_content.txt # must read attribs and translate to entities we know from map_content.txt
content = e.get("Item") content = e.get("Item")
x = e.get("TileX") x = e.get("TileX")
@ -12,11 +17,10 @@ def convert_frame(e):
z = e.get("TileZ") z = e.get("TileZ")
item = e.get("Item") item = e.get("Item")
if item: if item:
content = item.get("id") logger.debug(item.get("id"))
print(content)
return "xdecor:itemframe", None, (None, None) return "xdecor:itemframe", None, (None, None)
else: else:
print("empty item frame") logger.warning("empty item frame")
return "xdecor:itemframe", None, (None, None) return "xdecor:itemframe", None, (None, None)
e_convert = {"itemframe": convert_frame} e_convert = {"itemframe": convert_frame}

View File

@ -2,9 +2,12 @@
import os import os
import sys import sys
import logging
from block import * from block import *
import content import content
logging.basicConfig(level=logging.INFO)
if (sys.version_info < (3, 0)): if (sys.version_info < (3, 0)):
print("This script does not work with Python < 3.0, sorry.") print("This script does not work with Python < 3.0, sorry.")
exit(1) exit(1)

View File

@ -1,7 +1,10 @@
import logging
from itemstack import * from itemstack import *
#If you wish to add more entities, then... #If you wish to add more entities, then...
# To print out pre and post-conversion entity information uncomment line 237 (ish) in blocks.py (search for 'EntityInfo' to locate it) # To print out pre and post-conversion entity information uncomment line 237 (ish) in blocks.py (search for 'EntityInfo' to locate it)
logger = logging.getLogger('tile_entities')
def convert_chest(te): def convert_chest(te):
formspec = "size[8,9]"+\ formspec = "size[8,9]"+\
"list[current_name;main;0,0;8,4;]"+\ "list[current_name;main;0,0;8,4;]"+\
@ -101,7 +104,7 @@ def convert_pot(te):
fields = { "_plant": t[c] } fields = { "_plant": t[c] }
return None, None, (fields, {}) return None, None, (fields, {})
except: except:
print('Unknown flower pot type: '+c) logger.warning('Unknown flower pot type: ' + c)
return None, None, None return None, None, None
def convert_cmdblock(te): def convert_cmdblock(te):