Skip to content
Snippets Groups Projects
Commit 10c3c196 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added a separate regex service, added a common base class for objects that...

Added a separate regex service, added a common base class for objects that hold commonly used SimpleMatcher elements.
parent 981ba106
Branches
Tags
No related merge requests found
......@@ -14,6 +14,7 @@ from nomadcore.simple_parser import mainFunction
from nomadcore.local_backend import LocalBackend
from nomadcore.local_meta_info import load_metainfo
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import extractOnCloseTriggers
import nomadcore.ActivateLogging
from future.utils import with_metaclass
logger = logging.getLogger("nomad")
......@@ -252,6 +253,48 @@ class FileService(object):
return path
#===============================================================================
class RegexService(object):
"""
Stores basic regex definitions that can be reused on multiple parsers.
"""
def __init__(self):
self.regex_f = "-?\d+\.\d+(?:E(?:\+|-)\d+)?" # Regex for a floating point value
self.regex_i = "-?\d+" # Regex for an integer
self.regex_word = "[\S]+" # Regex for a single word. Can contain anything else but whitespace
self.regex_letter = "[^\W\d_]" # Regex for a single alphabetical letter
self.regex_eol = "[^\n]+" # Regex for a single alphabetical letter
#===============================================================================
class CommonMatcher(object):
"""
This class is used as a base class for objects that store and instantiate
common parts of the hierarchical SimpleMatcher structure. The object can be
shared for many HierarchicalParsers.
"""
def __init__(self, parser_context):
# Repeating regex definitions
self.parser_context = parser_context
self.backend = parser_context.caching_backend
self.file_service = parser_context.file_service
self.cache_service = parser_context.cache_service
self.caching_levels = {}
self.regexs = RegexService()
def getOnCloseTriggers(self):
"""
Returns:
A dictionary containing a section name as a key, and a list of
trigger functions associated with closing that section.
"""
onClose = {}
for attr, callback in extractOnCloseTriggers(self).items():
onClose[attr] = [callback]
return onClose
#===============================================================================
class BasicParser(with_metaclass(ABCMeta, object)):
"""A base class for all objects that parse contents from files.
......@@ -295,36 +338,45 @@ class HierarchicalParser(BasicParser):
Attributes:
root_matcher: The SimpleMatcher object at the root of this parser.
caching_level_for_metaname: A dicionary containing the caching options
caching_levels: A dicionary containing the caching options
that the ActiveBackend will use for individual metanames.
Example:
self.caching_level_for_metaname = {
self.caching_levels = {
'section_XC_functionals': CachingLevel.ForwardAndCache,
}
caching_levels: Dictionary that stores the caching levels for
individual metanames.
default_data_caching_level: A default caching level for data, i.e.
metainfo with kindStr=type_document_content or type_dimension
default_section_caching_level: A default caching level for sections.
onClose: a dictionary of onClose triggers. These are added to the
triggers that are already present in the parser context object.
cm: An optional CommonMatcher object that is used to store common
onClose triggers, SimpleMatchers, caching levels, etc.
"""
def __init__(self, file_path, parser_context):
super(HierarchicalParser, self).__init__(file_path, parser_context)
self.root_matcher = None
self.caching_level_for_metaname = {}
self.caching_levels = {}
self.default_data_caching_level = CachingLevel.ForwardAndCache
self.default_section_caching_level = CachingLevel.Forward
self.onClose = {}
self.cm = None
self.regexs = RegexService()
def setup_common_matcher(self, common_matcher):
""" Used to setup the CommonMatcher object. This object will contain
SimpleMatchers, onClose functions, caching levels, etc. that are common
for many different HierarchicalParsers.
Args:
common_matcher: A CommonMatcher object from which to exctract stuff.
"""
self.cm = common_matcher
self.onClose.update(common_matcher.getOnCloseTriggers())
self.caching_level_for_metaname.update(common_matcher.caching_levels)
self.caching_levels.update(common_matcher.caching_levels)
#===============================================================================
......@@ -352,7 +404,7 @@ class MainHierarchicalParser(HierarchicalParser):
metaInfoEnv=self.parser_context.metainfo_env,
parserInfo=self.parser_context.parser_info,
outF=self.parser_context.super_backend.fileOut,
cachingLevelForMetaName=self.caching_level_for_metaname,
cachingLevelForMetaName=self.caching_levels,
superContext=self,
onClose=self.onClose,
default_units=self.parser_context.default_units,
......@@ -374,7 +426,8 @@ class MainHierarchicalParser(HierarchicalParser):
self.parser_context.super_backend = parser.backend.superBackend
self.backend = self.parser_context.caching_backend
self.super_backend = self.parser_context.super_backend
self.cm.backend = parser.backend
if self.cm is not None:
self.cm.backend = parser.backend
#===============================================================================
......@@ -605,7 +658,7 @@ class CacheService(object):
# # Setup the backend that caches ond handles triggers
# self.caching_backend = ActiveBackend.activeBackend(
# metaInfoEnv=self.backend.metaInfoEnv(),
# cachingLevelForMetaName=self.caching_level_for_metaname,
# cachingLevelForMetaName=self.caching_levels,
# defaultDataCachingLevel=self.default_data_caching_level,
# defaultSectionCachingLevel=self.default_section_caching_level,
# onClose=onClose,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment