From e37daadaffd9324bd193d4218229b173a8fc0d5f Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Thu, 12 Dec 2019 14:49:16 -0500 Subject: [PATCH] restored environment functionality --- python/damask/__init__.py | 1 + python/damask/environment.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 python/damask/environment.py diff --git a/python/damask/__init__.py b/python/damask/__init__.py index 34a1bbbdd..b0966b45f 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -6,6 +6,7 @@ with open(os.path.join(os.path.dirname(__file__),'VERSION')) as f: version = f.readline().strip() # classes +from .environment import Environment # noqa from .table import Table # noqa from .asciitable import ASCIItable # noqa diff --git a/python/damask/environment.py b/python/damask/environment.py new file mode 100644 index 000000000..b00086b96 --- /dev/null +++ b/python/damask/environment.py @@ -0,0 +1,29 @@ +import os +import re + +class Environment(): + __slots__ = [ \ + 'options', + ] + + def __init__(self): + """Read and provide values of DAMASK configuration.""" + self.options = {} + self.get_options() + + def relPath(self,relative = '.'): + return os.path.join(self.rootDir(),relative) + + def rootDir(self): + return os.path.normpath(os.path.join(os.path.realpath(__file__),'../../../')) + + def get_options(self): + with open(self.relPath(self.rootDir()+'/CONFIG')) as configFile: + for line in configFile: + line = line.strip() + line = line[4:] if line.startswith('set ') else line # remove "set" (tcsh) when setting variables + if line and not line.startswith('#'): + items = re.split(r'\s*=\s*',line) + if len(items) == 2: + self.options[items[0].upper()] = \ + re.sub(r'${*DAMASK_ROOT}*',self.rootDir(),os.path.expandvars(items[1])) # expand all shell variables and DAMASK_ROOT