From 4653b74fbf4cdcc75b16d96c4ec5dba63a56a58e Mon Sep 17 00:00:00 2001
From: Arvid Ihrig <ihrig@fhi-berlin.mpg.de>
Date: Fri, 17 Nov 2017 14:57:19 +0100
Subject: [PATCH] parsers can now mark files as skipped by raising
 SkipFileException

-at present, it's the parser's responsibility to ensure that all open
 sections are closed before raising the exception
---
 common/python/nomadcore/simple_parser.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/common/python/nomadcore/simple_parser.py b/common/python/nomadcore/simple_parser.py
index 48f4dcf..5787ac5 100644
--- a/common/python/nomadcore/simple_parser.py
+++ b/common/python/nomadcore/simple_parser.py
@@ -24,6 +24,9 @@ import io
 logger = logging.getLogger("nomadcore.simple_parser")
 annotate = False
 
+class SkipFileException(Exception):
+    pass
+
 class PushbackLineFile(object):
     """a file interface where it is possible to put back read lines"""
 
@@ -1284,10 +1287,16 @@ def defaultParseFile(parserInfo):
     def parseF(parserBuilder, uri, path, backend, superContext):
         with open(path, "r") as fIn:
             backend.startedParsingSession(uri, parserInfo)
-            parsingStats = runParser(parserBuilder, backend, superContext, fIn)
-            backend.finishedParsingSession(
-                "ParseSuccess", None,
-                parsingStats=parsingStats)
+            try:
+                parsingStats = runParser(parserBuilder, backend, superContext, fIn)
+                backend.finishedParsingSession(
+                    "ParseSuccess", None,
+                    parsingStats=parsingStats)
+            except SkipFileException:
+                # FIXME: close all open sections here instead of relying on the caller doing it
+                backend.finishedParsingSession(
+                    "ParseSkipped", None,
+                    parsingStats={})
     return parseF
 
 def mainFunction(mainFileDescription,
-- 
GitLab