Commit 91da2165 authored by Alvin Noe Ladines's avatar Alvin Noe Ladines Committed by Markus Scheidgen
Browse files

Refactored eels mpes aptfim parsers

parent 9330ee6d
......@@ -9,6 +9,8 @@ from nomad import parsing
from nomad import normalizing
from nomad import datamodel
import nomadcore
from nomad.metainfo import MSection
from nomad.parsing.legacy import Backend
from .cli import cli
......@@ -44,6 +46,11 @@ def parse(
parser_backend = parser.run(mainfile_path, logger=logger)
if isinstance(parser_backend, MSection):
backend = Backend(parser._metainfo_env, parser.domain)
backend.resource.add(parser_backend)
parser_backend = backend
if not parser_backend.status[0] == 'ParseSuccess':
logger.error('parsing was not successful', status=parser_backend.status)
......
......@@ -80,6 +80,9 @@ from nomad.parsing.legacy import (
from nomad.parsing.parser import Parser, BrokenParser, MissingParser, MatchingParser
from nomad.parsing.artificial import (
TemplateParser, GenerateRandomParser, ChaosParser, EmptyParser)
from eelsparser import EelsParser
from mpesparser import MPESParser
from aptfimparser import APTFIMParser
try:
# these packages are not available without parsing extra, which is ok, if the
......@@ -418,26 +421,9 @@ parsers = [
mainfile_mime_re=r'(application/json)|(text/.*)',
mainfile_contents_re=(r'skeleton experimental metadata format')
),
LegacyParser(
name='parsers/mpes', code_name='mpes', code_homepage='https://github.com/mpes-kit/mpes', domain='ems',
parser_class_name='mpesparser.MPESParserInterface',
mainfile_mime_re=r'(application/json)|(text/.*)',
mainfile_name_re=(r'.*.meta'),
mainfile_contents_re=(r'"data_repository_name": "zenodo.org"')
),
LegacyParser(
name='parsers/aptfim', code_name='mpes', code_homepage='https://github.com/mpes-kit/mpes', domain='ems',
parser_class_name='aptfimparser.APTFIMParserInterface',
mainfile_mime_re=r'(application/json)|(text/.*)',
mainfile_name_re=(r'.*.aptfim')
),
LegacyParser(
name='parsers/eels', code_name='eels', code_homepage='https://eelsdb.eu/', domain='ems',
parser_class_name='eelsparser.EelsParserInterface',
mainfile_mime_re=r'text/.*',
mainfile_name_re=(r'.*.txt'),
mainfile_contents_re=(r'api_permalink = https://api\.eelsdb\.eu')
),
MPESParser(),
APTFIMParser(),
EelsParser(),
LegacyParser(
name='parsers/qbox', code_name='qbox', code_homepage='http://qboxcode.org/', domain='dft',
parser_class_name='qboxparser.QboxParser',
......
......@@ -15,6 +15,7 @@
from typing import List
from abc import ABCMeta, abstractmethod
import re
import importlib
from nomad.metainfo import Environment
......@@ -153,6 +154,32 @@ class MatchingParser(Parser):
return self.name
class FairdiParser(MatchingParser):
def __init__(self, parser_class_name: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parser_class_name = parser_class_name
module_name = self.parser_class_name.split('.')[:-1]
parser_class_name = self.parser_class_name.split('.')[-1]
self.__parser_impl = module_name, parser_class_name
self.__parser_class = None
@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):
parser = self.parser_class() # pylint: disable=not-callable
root_section = parser.parse(mainfile, logger)
return root_section
class MissingParser(MatchingParser):
'''
A parser implementation that just fails and is used to match mainfiles with known
......
......@@ -45,6 +45,7 @@ from nomad.archive import query_archive
from nomad.datamodel.encyclopedia import (
EncyclopediaMetadata,
)
from nomad.metainfo import MSection
import phonopyparser
......@@ -383,6 +384,12 @@ class Calc(Proc):
try:
self._parser_backend = parser.run(
self.upload_files.raw_file_object(self.mainfile).os_path, logger=logger)
if isinstance(self._parser_backend, MSection):
backend = Backend(parser._metainfo_env, parser.domain)
backend.resource.add(self._parser_backend)
self._parser_backend = backend
except Exception as e:
self.fail('parser failed with exception', exc_info=e, error=str(e), **context)
return
......
......@@ -22,7 +22,7 @@ from shutil import copyfile
from nomad import utils, files, datamodel
from nomad.parsing import parser_dict, match_parser, BrokenParser, BadContextUri, Backend
from nomad.app import dump_json
from nomad.metainfo import MSection
parser_examples = [
('parsers/random', 'test/data/parsers/random_0'),
......@@ -305,6 +305,10 @@ def assert_parser_dir_unchanged(previous_wd, current_wd):
def run_parser(parser_name, mainfile):
parser = parser_dict[parser_name]
result = parser.run(mainfile, logger=utils.get_logger(__name__))
if isinstance(result, MSection):
backend = Backend(parser._metainfo_env, parser.domain)
backend.resource.add(result)
result = backend
result.domain = parser.domain
return add_calculation_info(result, parser_name=parser_name)
......
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