Commit 133e089c authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Forward infrastructure/proc logger to parsers.

parent 97045efc
Pipeline #37122 failed with stages
in 5 minutes and 42 seconds
......@@ -66,7 +66,7 @@ parsers = [
TemplateParser(),
LegacyParser(
python_git=dependencies['parsers/vasp'],
parser_class_name='vaspparser.VASPParser',
parser_class_name='vaspparser.VASPRunParserInterface',
main_file_re=r'^.*\.xml(\.[^\.]*)?$',
main_contents_re=(
r'^\s*<\?xml version="1\.0" encoding="ISO-8859-1"\?>\s*'
......
......@@ -93,7 +93,7 @@ class TemplateParser(ArtificalParser):
self.backend.closeSection(name, index)
def run(self, mainfile: str) -> LocalBackend:
def run(self, mainfile: str, logger=None) -> LocalBackend:
self.init_backend()
template_json = json.load(open(mainfile, 'r'))
section = template_json['section_run'][0]
......@@ -162,7 +162,7 @@ class GenerateRandomParser(TemplateParser):
else:
return value
def run(self, mainfile: str) -> LocalBackend:
def run(self, mainfile: str, logger=None) -> LocalBackend:
self.init_backend()
seed = int(os.path.basename(mainfile).split('_')[1])
random.seed(seed)
......
......@@ -43,13 +43,14 @@ class Parser(metaclass=ABCMeta):
pass
@abstractmethod
def run(self, mainfile: str) -> LocalBackend:
def run(self, mainfile: str, logger=None) -> LocalBackend:
"""
Runs the parser on the given mainfile. It uses :class:`LocalBackend` as
a backend. The meta-info access is handled by the underlying NOMAD-coe parser.
Args:
mainfile: A path to a mainfile that this parser can parse.
logger: A optional logger
Returns:
The used :class:`LocalBackend` with status information and result data.
......@@ -93,7 +94,11 @@ class LegacyParser(Parser):
return False
def run(self, mainfile: str) -> LocalBackend:
def run(self, mainfile: str, logger=None) -> LocalBackend:
# TODO we need a homogeneous interface to parsers, but we dont have it right now.
# There are some hacks to distringuish between ParserInterface parser and simple_parser
# using hasattr, kwargs, etc.
def create_backend(meta_info):
return LocalBackend(meta_info, debug=False)
......@@ -110,11 +115,15 @@ class LegacyParser(Parser):
kwargs = {key: value for key, value in kwargs.items() if key in init_signature.args}
parser = Parser(**kwargs)
if logger is not None:
if hasattr(parser, 'setup_logger'):
parser.setup_logger(logger.bind(parser=self.name))
else:
logger.warning('could not setup parser logger')
with patch.object(sys, 'argv', []):
backend = parser.parse(mainfile)
# TODO we need a homogeneous interface to parsers, but we dont have it right now
# thats a hack to distringuish between ParserInterface parser and simple_parser
if backend is None or not hasattr(backend, 'status'):
backend = parser.parser_context.super_backend
......
......@@ -139,7 +139,8 @@ class Calc(Proc):
@task
def parsing(self):
self._parser_backend = parser_dict[self.parser].run(self.mainfile_tmp_path)
parser = parser_dict[self.parser]
self._parser_backend = parser.run(self.mainfile_tmp_path, logger=self.get_logger())
if self._parser_backend.status[0] != 'ParseSuccess':
error = self._parser_backend.status[1]
self.fail(error, level=logging.DEBUG)
......
......@@ -113,9 +113,8 @@ if not _logging_is_configured:
structlog.configure(processors=log_processors, logger_factory=logger_factory)
# configure logging in general
logging.basicConfig(level=logging.WARNING, stream=sys.stdout)
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
root = logging.getLogger()
root.setLevel(config.logstash.level)
for handler in root.handlers:
handler.setLevel(logging.WARNING if 'pytest' not in sys.modules else logging.CRITICAL)
......
import pytest
from mongoengine import connect
from mongoengine.connection import disconnect
import logging
from nomad import config
......@@ -99,6 +100,7 @@ def mocksearch(monkeypatch):
@pytest.fixture(scope='function')
def no_warn(caplog):
# TODO there is a bug in pytest, and the caplog is always empty
yield caplog
for record in caplog.records:
if record.levelname in ['WARNING', 'ERROR', 'CRITICAL']:
......@@ -107,10 +109,13 @@ def no_warn(caplog):
@pytest.fixture(scope='function')
def one_error(caplog):
# TODO there is a bug in pytest, and the caplog is always empty
yield caplog
count = 0
for record in caplog.records:
if record.levelname in ['ERROR', 'CRITICAL']:
count += 1
if count > 1:
assert False, "oo many errors"
assert False, "too many errors"
# assert count == 1
......@@ -20,6 +20,7 @@ import pytest
from nomadcore.local_meta_info import loadJsonFile
import nomad_meta_info
from nomad import utils
from nomad.parsing import JSONStreamWriter, parser_dict
from nomad.parsing import LocalBackend, BadContextURI
......@@ -196,7 +197,7 @@ def assert_parser_result(backend):
def run_parser(parser_name, mainfile):
parser = parser_dict[parser_name]
return parser.run(mainfile)
return parser.run(mainfile, logger=utils.get_logger(__name__))
@pytest.fixture
......
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