Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ladinesa committed Sep 3, 2024
1 parent 57c9797 commit 81a4dbf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
67 changes: 34 additions & 33 deletions electronicparsers/cp2k/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,39 +380,40 @@ def override(name, data):
self._variables = dict()
line = True
sections = [InpValue('tree')]
while line:
line = self.mainfile_obj.readline()
# comments
strip = line.strip()
if not strip or strip[0] in ('#', '!'):
continue
variable = self._re_variable.search(line)
if variable:
self._variables['${%s}' % variable.group(1)] = variable.group(
2
).strip()
continue
close_section = self._re_close.search(line)
if close_section:
sections.pop(-1)
continue
open_section = self._re_open.search(line)
if open_section:
section = InpValue(open_section.group(1))
sections[-1].add(open_section.group(1), section)
sections.append(section)
if open_section.group(2):
sections[-1].add('VALUE', open_section.group(2))
continue
key_value = self._re_key_value.search(line)
if key_value:
key, val = key_value.group(1), key_value.group(2)
val = val.strip()
if val in self._variables:
val = self._variables[val]
key, val = override(sections[-1].name, [key, val])
sections[-1].add(key, val)
continue
with self.mainfile_obj as mainfile_obj:
while line:
line = mainfile_obj.readline()
# comments
strip = line.strip()
if not strip or strip[0] in ('#', '!'):
continue
variable = self._re_variable.search(line)
if variable:
self._variables['${%s}' % variable.group(1)] = variable.group(
2
).strip()
continue
close_section = self._re_close.search(line)
if close_section:
sections.pop(-1)
continue
open_section = self._re_open.search(line)
if open_section:
section = InpValue(open_section.group(1))
sections[-1].add(open_section.group(1), section)
sections.append(section)
if open_section.group(2):
sections[-1].add('VALUE', open_section.group(2))
continue
key_value = self._re_key_value.search(line)
if key_value:
key, val = key_value.group(1), key_value.group(2)
val = val.strip()
if val in self._variables:
val = self._variables[val]
key, val = override(sections[-1].name, [key, val])
sections[-1].add(key, val)
continue
self._file_handler = sections[0]
return self._file_handler

Expand Down
3 changes: 2 additions & 1 deletion electronicparsers/vasp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,8 @@ def parse(self):
content_handler = RunXmlContentHandler()
parser.setContentHandler(content_handler)
try:
parser.parse(self.mainfile_obj)
with self.mainfile_obj as f:
parser.parse(f)
except Exception as e:
# support broken XML structure
if self.logger:
Expand Down

0 comments on commit 81a4dbf

Please sign in to comment.