Add support for multiline values in .conf files
This commit is contained in:
parent
7964f5979a
commit
e0b490fdc0
|
@ -1,10 +1,59 @@
|
||||||
def parse_conf(string):
|
def parse_conf(string):
|
||||||
retval = {}
|
retval = {}
|
||||||
for line in string.split("\n"):
|
lines = string.splitlines()
|
||||||
idx = line.find("=")
|
i = 0
|
||||||
if idx > 0:
|
|
||||||
key = line[:idx].strip()
|
def syntax_error(message):
|
||||||
value = line[idx+1:].strip()
|
raise SyntaxError("Line {}: {}".format(i + 1, message))
|
||||||
retval[key] = value
|
|
||||||
|
while i < len(lines):
|
||||||
|
line = lines[i].strip()
|
||||||
|
|
||||||
|
# Comments
|
||||||
|
if line.startswith("#") or line == "":
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
key_value = line.split("=", 2)
|
||||||
|
if len(key_value) < 2:
|
||||||
|
syntax_error("No value given")
|
||||||
|
|
||||||
|
key = key_value[0].strip()
|
||||||
|
if key == "":
|
||||||
|
syntax_error("Empty key")
|
||||||
|
|
||||||
|
value = key_value[1].strip()
|
||||||
|
if value == "":
|
||||||
|
syntax_error("Empty value")
|
||||||
|
|
||||||
|
if value.startswith('"""'):
|
||||||
|
value_lines = []
|
||||||
|
closed = False
|
||||||
|
i += 1
|
||||||
|
while i < len(lines):
|
||||||
|
value = lines[i]
|
||||||
|
if value == '"""':
|
||||||
|
closed = True
|
||||||
|
value = value[:-3]
|
||||||
|
break
|
||||||
|
|
||||||
|
value_lines.append(value)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if not closed:
|
||||||
|
i -= 1
|
||||||
|
syntax_error("Unclosed multiline value")
|
||||||
|
|
||||||
|
value_lines.append(value)
|
||||||
|
value = "\n".join(value_lines)
|
||||||
|
|
||||||
|
else:
|
||||||
|
value = value.rstrip()
|
||||||
|
|
||||||
|
if key in retval:
|
||||||
|
syntax_error("Duplicate key {}".format(key))
|
||||||
|
|
||||||
|
retval[key] = value
|
||||||
|
i += 1
|
||||||
|
|
||||||
return retval
|
return retval
|
||||||
|
|
|
@ -84,11 +84,14 @@ class PackageTreeNode:
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
# .conf file
|
# .conf file
|
||||||
|
meta_file_path = self.getMetaFilePath()
|
||||||
try:
|
try:
|
||||||
with open(self.getMetaFilePath() or "", "r") as myfile:
|
with open(meta_file_path or "", "r") as myfile:
|
||||||
conf = parse_conf(myfile.read())
|
conf = parse_conf(myfile.read())
|
||||||
for key, value in conf.items():
|
for key, value in conf.items():
|
||||||
result[key] = value
|
result[key] = value
|
||||||
|
except SyntaxError as e:
|
||||||
|
raise MinetestCheckError("Error while reading {}: {}".format(meta_file_path , e.msg))
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue