Split the 'clean unknown' demo and tests

- Demo : create a file called demo_clean_unknown with all the code to clean maps from unknown nodes extracted from tests.py
 - Tests : all tests are reactivated and the unknown nodes cleaning code is moved to a demo file
 - Maps : MapVessels can now retrieve a list of all available mapblock positions, and the isEmpty method is now called is_empty to be consistent with naming convention in the class
This commit is contained in:
LeMagnesium 2016-02-22 17:40:09 +01:00
parent 44b64d8780
commit f44128f175
3 changed files with 91 additions and 62 deletions

70
src/demo_clean_unknown.py Executable file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env python3.4
# -*- encoding: utf-8 -*-
############################
## Demo of Python-Minetest :
## Erease all unknown nodes from a map.sqlite file
##
## args :
## ./demo_clean_unknowns.py <path to sqlite file> <path to list of known nodes>
##
#
import minetest
def removeUnknowns():
import sys
if len(sys.argv) < 2:
print("I need a map.sqlite file!")
return
elif len(sys.argv) < 3:
print("I need a known nodes file!")
return
try:
knodes = open(sys.argv[2])
except Exception as err:
print("Couldn't open know nodes file {0} : {1}".format(sys.argv[1], err))
return
print("Know nodes file opened")
nodes = [node[:-1] for node in knodes.readlines()] # Remove the \n
print("{0} nodes known".format(len(nodes)))
u = minetest.map.MapVessel(sys.argv[1])
ids = u.get_all_mapblock_ids()
nids = len(ids)
print("{0} mapblocks to inspect".format(nids))
for i in ids:
k = u.load(i)
absi = minetest.utils.posFromInt(i, 4096)
pct = ids.index(i)/nids * 100
print("[{0:3.2f}%] Checking mapblock {1} ({2})".format(pct, i, absi), end = " \r")
unknowns = []
for id in k.name_id_mappings:
node = k.name_id_mappings[id]
if node != "air":
if not node in nodes:
print("Unknown node in {0} : {1}".format(i, node))
unknowns.append(node)
if len(unknowns) > 0:
for x in range(16):
for y in range(16):
for z in range(16):
noderef = k.get_node(x + y * 16 + z * 16 * 16)
if noderef.get_name() in unknowns:
print("Removed node in {0} : {1}".format(noderef.get_pos(), noderef.get_name()))
k.remove_node(x + y * 16 + z * 16 * 16)
print("Saving mapblock {0}".format(absi))
u.store(i, k.implode())
u.write(i)
u.uncache(i)
u.commit()
if __name__ == "__main__":
removeUnknowns()

View File

@ -441,12 +441,23 @@ class MapVessel:
self.open(mapfile, backend)
def __str__(self):
if self.isEmpty():
if self.is_empty():
return "empty mapfile vessel"
else:
return "mapfile vessel for {0}".format(self.mapfile)
def isEmpty(self):
def get_all_mapblock_ids(self):
if self.is_empty():
raise EmptyMapVesselError()
try:
self.cur.execute("SELECT \"pos\" from \"blocks\"")
except _sql.OperationalError as err:
raise MapError("Error retrieving all mapblock pos : {0}".format(err))
return [id[0] for id in self.cur.fetchall()]
def is_empty(self):
return self.mapfile == None
def open(self, mapfile, backend = "sqlite3"):
@ -463,7 +474,7 @@ class MapVessel:
self.mapfile = None
def read(self, blockID):
if self.isEmpty():
if self.is_empty():
raise EmptyMapVesselError()
if self.cache.get(blockID):
@ -482,7 +493,7 @@ class MapVessel:
return False, "notfound"
def uncache(self, blockID):
if self.isEmpty():
if self.is_empty():
raise EmptyMapVesselError()
self.cache[blockID] = None
@ -490,7 +501,7 @@ class MapVessel:
return True, "ok"
def write(self, blockID):
if self.isEmpty():
if self.is_empty():
raise EmptyMapVesselError()
try:
@ -504,7 +515,7 @@ class MapVessel:
self.conn.commit()
def load(self, blockID):
if self.isEmpty():
if self.is_empty():
raise EmptyMapVesselError()
if not self.cache.get(blockID):
@ -517,7 +528,7 @@ class MapVessel:
return MapBlock(self.cache[blockID], abspos = blockID)
def store(self, blockID, mapblockData):
if self.isEmpty():
if self.is_empty():
raise EmptyMapVesselError()
if not self.cache.get(blockID):

View File

@ -79,66 +79,14 @@ def testSchematics():
# Get node
print(schem.get_node(Pos({"x": 1, "y": 1, "z": 0})))
def removeUnknowns():
import sys
if len(sys.argv) < 2:
print("I need a map.sqlite file!")
return
elif len(sys.argv) < 3:
print("I need a known nodes file!")
return
try:
knodes = open(sys.argv[2])
except Exception as err:
print("Couldn't open know nodes file {0} : {1}".format(sys.argv[1], err))
return
print("Know nodes file opened")
nodes = [node[:-1] for node in knodes.readlines()] # Remove the \n
print("{0} nodes known".format(len(nodes)))
u = minetest.map.MapVessel(sys.argv[1])
ma = 4096 + 4096 * 4096# + 4096 * 4096 * 4096
for i in range(-ma, ma):
k = u.load(i)
absi = minetest.utils.posFromInt(i, 4096)
print("Testing mapblock {0} ({1}) ".format(i, absi), end = '\r')
if k:
print("Checking mapblock {0} ({1})".format(i, absi), end = " \r")
unknowns = []
for id in k.name_id_mappings:
node = k.name_id_mappings[id]
if node != "air":
if not node in nodes:
print("Unknown node in {0} : {1}".format(i, node))
unknowns.append(node)
if len(unknowns) > 0:
for x in range(16):
for y in range(16):
for z in range(16):
noderef = k.get_node(x + y * 16 + z * 16 * 16)
if noderef.get_name() in unknowns:
print("Removed node in {0} : {1}".format(noderef.get_pos(), noderef.get_name()))
k.remove_node(x + y * 16 + z * 16 * 16)
print("Saving mapblock {0}".format(absi))
u.store(i, k.implode())
u.write(i)
u.uncache(i)
u.commit()
if __name__ == "__main__":
#findTheShelves()
print("=> MapBlockLoad")
#testMapBlockLoad()
removeUnknowns()
"""print("=> signed endians")
testMapBlockLoad()
print("=> signed endians")
testSignedEndians()
print("=> get_node")
testGetNode()
@ -147,4 +95,4 @@ if __name__ == "__main__":
print("=> inventory manipulation (WIP)")
invManip()
print("=> schematic manipulation (WIP)")
testSchematics()"""
testSchematics()