Defined a new function to handle recursive file includes

This commit is contained in:
Vitesh Shah 2018-06-25 15:10:54 +02:00
parent 47a6e6b292
commit aad3611bb4
1 changed files with 16 additions and 55 deletions

71
lib/damask/config/material.py Executable file → Normal file
View File

@ -174,67 +174,28 @@ class Material():
else: # plain key else: # plain key
self.data[part][name_section][items[0]] = items[1:] self.data[part][name_section][items[0]] = items[1:]
c = []
def myRead(c,sub_file):
re_include = re.compile(r'^{(.+)}$') # pattern for include
with open(sub_file,'r') as f:
for entry in f:
if not entry.strip():
continue
match = re_include.match(entry.split()[0])
if match:
if match.group(1).startswith('/'):
sub_file = match.group(1)
else:
sub_file = os.path.normpath(os.path.dirname(sub_file).join(match.group(1)))
with open(sub_file,'r') as f1:
for l in f1.readlines():
if l not in c:
c.append(l)
else:
continue
myRead(c,sub_file)
else:
if entry not in c:
c.append(entry)
else:
continue
return c,sub_file
def read(self,file=None): def read(self,file=None):
fileNames = [None] * 10
fileNames[0] = file def readInclude(filename):
re_include = re.compile(r'^{(.+)}$') # pattern for include result = []
c = [] re_include = re.compile(r'^{(.+)}$')
i = 0 with open(filename) as f: myData = f.readlines()
for files in fileNames: for line in myData:
if files is not None: match = re_include.match(line.split()[0]) if line.strip() else False
with open(file,'r') as f: if match:
for entry in f: result += readInclude(match.group(1) if match.group(1).startswith('/') else
if not entry.strip(): os.path.normpath(os.path.join(os.path.dirname(filename),match.group(1))))
continue else:
match = re_include.match(entry.split()[0]) result.append(line)
if match: return result
if match.group(1).startswith('/'):
sub_file = match.group(1) c = readInclude(file)
else:
sub_file = os.path.normpath(os.path.dirname(file).join(match.group(1)))
i = i + 1
fileNames[i] = sub_file
myRead(c,sub_file)
else:
if entry not in c:
c.append(entry)
else:
continue
for l in c:
print(l)
for p in self.parts: for p in self.parts:
self.parse_data(part=p, content=c) self.parse_data(part=p, content=c)
def write(self,file='material.config', overwrite=False): def write(self,file='material.config', overwrite=False):
import os
i = 0 i = 0
saveFile = file saveFile = file
while not overwrite and os.path.exists(saveFile): while not overwrite and os.path.exists(saveFile):