Commit fb2b3398 authored by Lauri Himanen's avatar Lauri Himanen

Added a simpler parsing function for classes implementing ParserInterface....

Added a simpler parsing function for classes implementing ParserInterface. This function does not expect command line arguments that can come from external programs running the parser.
parent 797171f7
......@@ -11,7 +11,7 @@ import numpy as np
import logging
from abc import ABCMeta, abstractmethod
from nomadcore.unit_conversion import unit_conversion
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import parse as mainparse
from nomadcore.local_backend import LocalBackend
from nomadcore.local_meta_info import load_metainfo
from nomadcore.caching_backend import CachingLevel
......@@ -415,7 +415,7 @@ class MainHierarchicalParser(AbstractBaseParser):
want to use something else or customize the process just override this
method in the subclass.
"""
mainFunction(
mainparse(
mainFileDescription=self.root_matcher,
metaInfoEnv=self.parser_context.metainfo_env,
parserInfo=self.parser_context.parser_info,
......
......@@ -1343,6 +1343,107 @@ def defaultParseFile(parserInfo):
parsingStats={})
return parseF
def parse(
mainFileDescription,
metaInfoEnv,
parserInfo,
outF=sys.stdout,
cachingLevelForMetaName=None,
defaultDataCachingLevel=CachingLevel.ForwardAndCache,
defaultSectionCachingLevel=CachingLevel.Forward,
superContext=None,
onClose=None,
onOpen=None,
default_units=None,
metainfo_units=None,
superBackend=None,
metaInfoToKeep=None,
mainFile=None,
strValueTransform=None):
"""Used to parse a file with the SimpleMatcher-class. This function is
similar to mainFunction, but does not take command line arguments and only
parses info to the backend. This function is called by the parsers that
inherit from nomadcore.baseclasses.ParserInterface and do not use command
line arguments that can cause problems if the parser is calld within some
other program using different commmand line options.
Args:
default_units: A list of default unit definitions.
E.g. ["eV", "Bohr"]. Used to override the default conversion to SI
units. Leave as 'None' if no overriding necessary.
metainfo_units: A mapping between metainfo names and unit definitions.
E.g. {"energy_total": "eV"}. Used to override the default conversion to
SI units. Leave as 'None' if no overriding necessary.
super_backend: Backend to use for the parsing. This backend will be
called by the ActiveBackend that handles the caching. If not
specified, the default JsonParseEventsWriterBackend is used.
mainFile: The path to the file that is parsed.
"""
parseFile = defaultParseFile(parserInfo)
fileToParse = mainFile
uri = "file://" + fileToParse
if cachingLevelForMetaName is None:
cachingLevelForMetaName = {}
if onClose is None:
onClose = {}
if onOpen is None:
onOpen = {}
if strValueTransform is None:
strValueTransform = {}
# Initialize the backend where the final results are printed after they are
# possibly filtered by the caching ActiveBackend.
jsonBackend = superBackend
if superContext:
strValueTransform = dict(strValueTransform)
for attr, callback in extractStrValueTransforms(superContext).items():
oldCallbacks = strValueTransform.get(attr, None)
if attr not in strValueTransform:
strValueTransform[attr] = callback
# Initialize the parser builder
parserBuilder = compileParser(
mainFileDescription,
metaInfoEnv,
metaInfoToKeep,
default_units,
metainfo_units,
strValueTransform
)
if superContext:
onClose = dict(onClose)
for attr, callback in extractOnCloseTriggers(superContext).items():
oldCallbacks = onClose.get(attr, None)
if oldCallbacks:
oldCallbacks.append(callback)
else:
onClose[attr] = [callback]
onOpen = dict(onOpen)
for attr, callback in extractOnOpenTriggers(superContext).items():
oldCallbacks = onOpen.get(attr, None)
if oldCallbacks:
oldCallbacks.append(callback)
else:
onOpen[attr] = [callback]
backend = ActiveBackend.activeBackend(
metaInfoEnv=metaInfoEnv,
cachingLevelForMetaName=cachingLevelForMetaName,
defaultDataCachingLevel=defaultDataCachingLevel,
defaultSectionCachingLevel=defaultSectionCachingLevel,
onClose=onClose,
onOpen=onOpen,
superBackend=jsonBackend,
default_units=default_units,
metainfo_units=metainfo_units
)
parseFile(parserBuilder, uri, fileToParse, backend, superContext)
def mainFunction(mainFileDescription,
metaInfoEnv,
parserInfo,
......
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