Commit 4fa62c8e authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Lazy parser module loading.

parent ead23114
Pipeline #71453 passed with stages
in 13 minutes and 11 seconds
......@@ -184,7 +184,7 @@ class Mapping:
'--skip-files', is_flag=True, help='Will not copy/move/link any files.')
@click.option(
'--migration', type=str, default=None,
help='The name of a migration script used to transform the metadata.')
help='The name of a migration script used to transform the metadata.')
@click.option(
'--skip-es', is_flag=True, help='Do not add mirrored data to elastic search')
@click.option(
......
......@@ -28,7 +28,6 @@ import os
import signal
from nomad import metainfo
from nomad.datamodel.metainfo import m_env as metainfo_env
from .legacy import Backend
from .parser import Parser, MatchingParser
......@@ -69,7 +68,8 @@ class TemplateParser(ArtificalParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.metainfo_env = metainfo_env
from nomad.datamodel.metainfo import m_env as metainfo_env
self._metainfo_env = metainfo_env
def is_mainfile(
self, filename: str, mime: str, buffer: bytes, decoded_buffer: str,
......
......@@ -513,12 +513,27 @@ class LegacyParser(MatchingParser):
self.backend_factory = backend_factory
module_name = self.parser_class_name.split('.')[:-1]
parser_class = self.parser_class_name.split('.')[-1]
module = importlib.import_module('.'.join(module_name))
self.parser_class = getattr(module, parser_class)
parser_class_name = self.parser_class_name.split('.')[-1]
self.__parser_impl = module_name, parser_class_name
self.__parser_class = None
module = importlib.import_module('.'.join(module_name + ['metainfo']))
self.metainfo_env = getattr(module, 'm_env')
@property
def metainfo_env(self):
if self._metainfo_env is None:
module_name, _ = self.__parser_impl
module = importlib.import_module('.'.join(module_name + ['metainfo']))
self._metainfo_env = getattr(module, 'm_env')
return super().metainfo_env
@property
def parser_class(self):
if self.__parser_class is None:
module_name, parser_class_name = self.__parser_impl
module = importlib.import_module('.'.join(module_name))
self.__parser_class = getattr(module, parser_class_name)
return self.__parser_class
def run(self, mainfile: str, logger=None) -> Backend:
# TODO we need a homogeneous interface to parsers, but we dont have it right now.
......@@ -529,7 +544,7 @@ class LegacyParser(MatchingParser):
if issubclass(self.parser_class, CoEParser):
# TODO reuse parser
parser = self.parser_class()
parser = self.parser_class() # pylint: disable=not-callable
return parser.run(mainfile, logger)
def create_backend(meta_info):
......@@ -543,7 +558,7 @@ class LegacyParser(MatchingParser):
kwargs = {key: value for key, value in kwargs.items() if key in init_signature.args}
with utils.legacy_logger(logger):
self.parser = self.parser_class(**kwargs)
self.parser = self.parser_class(**kwargs) # pylint: disable=not-callable
with patch.object(sys, 'argv', []):
backend = self.parser.parse(mainfile)
......
......@@ -27,7 +27,11 @@ class Parser(metaclass=ABCMeta):
def __init__(self):
self.domain = 'dft'
self.metainfo_env: Environment = None
self._metainfo_env: Environment = None
@property
def metainfo_env(self):
return self._metainfo_env
@abstractmethod
def is_mainfile(
......
Supports Markdown
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