Commit dc77c8b5 authored by Martin Claus's avatar Martin Claus

Added `OrderedDict` container for index assignment variables

parent 890ca023
......@@ -19,7 +19,7 @@ valueBool = re.compile(r"(\.(true|false|t|f)\.)", re.I)
quote = re.compile(r"([\']{1}[^\']*[\']{1}|[\"]{1}[^\"]*[\"]{1})",
re.MULTILINE)
namelistname = re.compile(r"&(" + varname + r")")
paramname = re.compile(r"^(" + varname + r"(\([0-9]*\))?" + r")$")
paramname = re.compile(r"^(" + varname + r")" + r"(\(([0-9]*)\))?" + r"$")
namlistend = re.compile(r'^(&(end)?|/)$', re.I)
comment = re.compile(r"!.*$", re.MULTILINE)
equalsign = re.compile(r"^=$")
......@@ -151,53 +151,84 @@ def parse_namelist_string(in_string):
"""
retlist = []
content = _tokenize(in_string)
index = 0
for item in content:
match = re.match(namelistname, item)
if match:
nmlname = match.group(1)
nml = Namelist(nmlname)
retlist.append(nml)
continue
match = re.match(paramname, item)
if match:
pname = match.group(1)
nml[pname] = []
index = match.group(3)
if index is not None: # index assignment
if pname not in nml:
nml[pname] = DictClass()
else:
nml[pname] = []
continue
if re.match(namlistend, item):
continue
if re.match(equalsign, item):
continue
match = re.match(valueBool, item)
if match:
nml[pname].append(match.group(1)[1].lower() == "t")
_add_value(nml[pname], match.group(1)[1].lower() == "t", index)
continue
match = re.match(quote, item)
if match:
nml[pname].append(match.group(1)[1:-1])
_add_value(nml[pname], match.group(1)[1:-1], index)
continue
try:
nml[pname].append(int(item))
i = int(item)
_add_value(nml[pname], i, index)
except ValueError:
pass
else:
continue # pragma: no cover
try:
nml[pname].append(float(item))
f = float(item)
_add_value(nml[pname], f, index)
except ValueError:
pass
else:
continue # pragma: no cover
match = re.match(computation, item)
if match:
nml[pname].append(eval(item))
_add_value(nml[pname], eval(item), index)
for nml in retlist:
for k, v in nml.items():
if len(v) == 1:
nml[k] = v[0]
try:
nml[k] = v[0]
except KeyError:
pass
ret_dict = DictClass(zip([nml.name for nml in retlist], retlist))
return ret_dict
def _add_value(container, value, key):
try:
# test if container is a dict like object
container.keys()
container[key] = value
except: # container is a list
container.append(value)
def _tokenize(text):
"""Extract syntax tokens."""
fs = "$$$FS$$$"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment