From bdc9138a6c7ac38a282568c7ea9b00da1801fb7b Mon Sep 17 00:00:00 2001 From: Scott Donaldson Date: Sun, 29 Sep 2024 16:10:22 +0100 Subject: [PATCH] Fix for commnet lines inside blocks --- castep_outputs/test/test_cell_param_parser.py | 2 ++ castep_outputs/utilities/castep_res.py | 5 +++++ castep_outputs/utilities/filewrapper.py | 5 +++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/castep_outputs/test/test_cell_param_parser.py b/castep_outputs/test/test_cell_param_parser.py index 9a843f2..4cc62de 100644 --- a/castep_outputs/test/test_cell_param_parser.py +++ b/castep_outputs/test/test_cell_param_parser.py @@ -212,10 +212,12 @@ def test_parse_supercell_matrix(self): def test_parse_symops(self): test_text = io.StringIO(""" %BLOCK SYMMETRY_OPS + # SYM OP 1 -1.0000000000 0.0000000000 0.0000000000 0.0000000000 -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.5000000000 0.0000000000 0.5000000000 + # SYM OP 2 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 diff --git a/castep_outputs/utilities/castep_res.py b/castep_outputs/utilities/castep_res.py index 3a5503c..c127e1f 100644 --- a/castep_outputs/utilities/castep_res.py +++ b/castep_outputs/utilities/castep_res.py @@ -250,6 +250,11 @@ def get_atom_parts(spec: str) -> dict[str, str]: #: :meta hide-value: EMPTY = r"^\s*$" +#: Comment line RegEx +#: +#: :meta hide-value: +COMMENT_LINE_RE = re.compile(r"^\s*#") + #: CASTEP Atom RegEx with optional index. #: #: :meta hide-value: diff --git a/castep_outputs/utilities/filewrapper.py b/castep_outputs/utilities/filewrapper.py index d3cde85..1812369 100644 --- a/castep_outputs/utilities/filewrapper.py +++ b/castep_outputs/utilities/filewrapper.py @@ -4,10 +4,11 @@ from __future__ import annotations import re +from itertools import filterfalse from io import StringIO from typing import NoReturn, TextIO -from .castep_res import Pattern +from .castep_res import Pattern, COMMENT_LINE_RE class FileWrapper: @@ -203,7 +204,7 @@ def from_re( data.append(init_line) found = 0 - for line in in_file: + for line in filterfalse(COMMENT_LINE_RE.match,in_file): data.append(line) if re.search(end, line): found += 1