Start implementation of the configuration interface
- Create the class Configuration to handle .mt/.conf configuration files
This commit is contained in:
parent
de2f75aee5
commit
9bf45cf1b6
72
src/configuration.py
Executable file
72
src/configuration.py
Executable file
@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- encoding: utf-8 -*-
|
||||
######################################
|
||||
## Configuration for Python-Minetest
|
||||
##
|
||||
#
|
||||
|
||||
import os
|
||||
|
||||
#from errors import ConfigurationFormatError
|
||||
|
||||
class Configuration:
|
||||
def __init__(self, file):
|
||||
self.data = dict()
|
||||
self.read(file)
|
||||
self.file = file
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self.data
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.data.get(key)
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.data[key]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
self.data[key] = val
|
||||
|
||||
|
||||
def read(self, file=None):
|
||||
buffer = open(file or self.file)
|
||||
for line in buffer.readlines():
|
||||
line = line.strip("\r\n")
|
||||
k = line.split("=")
|
||||
|
||||
if len(k) < 2:
|
||||
continue
|
||||
|
||||
data = " = ".join(k[1:]).strip()
|
||||
|
||||
if data == "true" or data == "1":
|
||||
data = True
|
||||
|
||||
elif data == "false" or data == "0":
|
||||
data = False
|
||||
|
||||
self.data[k[0].strip()] = data
|
||||
|
||||
def write(self, file=None):
|
||||
try:
|
||||
f = open(file or self.file, "w")
|
||||
except Exception as err:
|
||||
return None
|
||||
|
||||
for key in self.data:
|
||||
f.write("{0} = {1}\n".format(key, self.data[key]))
|
||||
|
||||
f.close()
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def open_world(cls, dir):
|
||||
try:
|
||||
open(dir + "/world.mt")
|
||||
except Exception as err:
|
||||
return None
|
||||
|
||||
return cls(dir + "/world.mt")
|
@ -38,7 +38,7 @@ if __name__ == "__main__":
|
||||
db = minetest.map.MapInterface(dbfile)
|
||||
|
||||
if mode.lower() == "import":
|
||||
db.set_maxcachesize(40)
|
||||
db.set_maxcachesize(100)
|
||||
print("Reading schematic...")
|
||||
schem = minetest.schematics.Schematic(schfile)
|
||||
if not schem:
|
||||
@ -46,7 +46,7 @@ if __name__ == "__main__":
|
||||
|
||||
print("Importing schematic..")
|
||||
try:
|
||||
db.import_schematic(pos_import, schem, stage_save=0)
|
||||
db.import_schematic(pos_import, schem, stage_save=5)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
@ -15,6 +15,7 @@ import inventory
|
||||
import errors
|
||||
import metadata
|
||||
import nodes
|
||||
import configuration as config
|
||||
|
||||
|
||||
__version__ = "00.00.01"
|
||||
__version__ = "00.00.02"
|
||||
|
26
src/test.py
26
src/test.py
@ -7,6 +7,7 @@
|
||||
import minetest
|
||||
import random
|
||||
import time
|
||||
import os
|
||||
|
||||
from io import BytesIO
|
||||
from schematics import Schematic
|
||||
@ -159,8 +160,27 @@ def testMapBlockInit():
|
||||
print(" --> Test successful")
|
||||
|
||||
|
||||
def testConfiguration():
|
||||
# Open the file
|
||||
dir = os.environ["HOME"] + "/.minetest/worlds/world"
|
||||
print("-> Trying to open {0}".format(dir))
|
||||
conf = minetest.config.Configuration.open_world(dir)
|
||||
if not conf:
|
||||
print("=> No conf found")
|
||||
return
|
||||
|
||||
print("-> The world's backend is {0}".format(conf["backend"]))
|
||||
print("-> There are {0} configuration keys in the file".format(len(conf)))
|
||||
if "load_mod_mesecon" in conf and conf["load_mod_mesecon"]:
|
||||
print("-> You have mesecon installed and set to be loaded")
|
||||
else:
|
||||
print("-> You do not have mesecon installed, or it is disabled")
|
||||
|
||||
|
||||
def main():
|
||||
print("=> MapBlockLoad Test")
|
||||
print("=> Configuration Test")
|
||||
testConfiguration()
|
||||
""" print("=> MapBlockLoad Test")
|
||||
s = time.time()
|
||||
testMapBlockLoad()
|
||||
print(" => Test took {0:.10f}s".format(time.time()-s))
|
||||
@ -188,12 +208,12 @@ def main():
|
||||
print("=> schematic manipulation (WIP)")
|
||||
s = time.time()
|
||||
testSchematics()
|
||||
print(" => Test took {0:.10f}s".format(time.time()-s))"""
|
||||
print(" => Test took {0:.10f}s".format(time.time()-s))
|
||||
|
||||
print("=> MapBlock init")
|
||||
s = time.time()
|
||||
testMapBlockInit()
|
||||
print(" => Test took {0:.10f}s".format(time.time()-s))
|
||||
print(" => Test took {0:.10f}s".format(time.time()-s))"""
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user