introduced material.config parsing and self representation, i.e. printing of class object.

This commit is contained in:
Philip Eisenlohr 2011-11-18 19:59:20 +00:00
parent dc6c29a910
commit 3c7ebc7a30
1 changed files with 92 additions and 38 deletions

View File

@ -1,5 +1,7 @@
import os,string,re
class DAMASK_TOOLS():
import os,string
def check_env(self):
import os
if os.getenv('DAMASK_ROOT') is None:
@ -10,10 +12,73 @@ class DAMASK_TOOLS():
class MATERIAL_CONFIG():
import os,sys
__slots__ = ['data']
def __init__(self):
homogenization=[]
self.data = {\
'homogenization': {'__order__': []},
'microstructure': {'__order__': []},
'crystallite': {'__order__': []},
'phase': {'__order__': []},
'texture': {'__order__': []},
}
def __repr__(self):
me = []
for part in [
'homogenization',
'microstructure',
'crystallite',
'phase',
'texture',
]:
me += ['','#-----------------------------#','<%s>'%part,'#-----------------------------#',]
for section in self.data[part]['__order__']:
me += ['','[%s]\t\t#-----------------------'%section,'',]
for key in self.data[part][section]['__order__']:
if key.startswith('(') and key.endswith(')'): # multiple (key)
me += ['%s\t%s'%(key,' '.join(values)) for values in self.data[part][section][key]]
else: # plain key
me += ['%s\t%s'%(key,' '.join(self.data[part][section][key]))]
return '\n'.join(me)
def parse_data(self, part=None, sections=[], content=None):
re_part = re.compile(r'^<(.+)>$') # pattern for part
re_sec = re.compile(r'^\[(.+)\]$') # pattern for section
name_section = ''
idx_section = 0
active = False
for line in content:
line = line.split('#')[0].strip() # kill comments and extra whitespace
if line: # content survives...
match_part = re_part.match(line)
if match_part: # found <part> separator
active = (match_part.group(1) == part) # only active in <part>
continue
if active:
match_sec = re_sec.match(line)
if match_sec: # found [section]
name_section = match_sec.group(1) # remember name ...
if '__order__' not in self.data[part]: self.data[part]['__order__'] = []
self.data[part]['__order__'].append(name_section) # ... and position
self.data[part][name_section] = {'__order__':[]}
continue
if sections == [] or name_section in sections: # respect subset
items = line.split()
if items[0] not in self.data[part][name_section]: # first encounter of key?
self.data[part][name_section][items[0]] = [] # create item
self.data[part][name_section]['__order__'].append(items[0])
if items[0].startswith('(') and items[0].endswith(')'): # multiple "(key)"
self.data[part][name_section][items[0]].append(items[1:])
else: # plain key
self.data[part][name_section][items[0]] = items[1:]
def add_homogenization(self, name=None, type=None):
@ -28,19 +93,8 @@ class MATERIAL_CONFIG():
pass
def add_phase(self, name=None):
pass
def add_microstructure(self, name=None, phase=None, texture=None):
pass
def add_microstructure(self, name=None, phases=None, textures=None, fractions=None):
self.data['microstructure'][name] = {} # kill existing entry (should warn?)
def write_file(self,label=None):
fname='material.config'
if label is not None: fname+='_%s'%label
#f=open(fname)
#f.write()
#f.close()
class HOMOGENIZATION():
type=None