Skip to content
Snippets Groups Projects
Commit 2d5181f5 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

adding onOpen to make getting hold of gIndex simpler (no adHoc)

parent 5e6c5337
No related branches found
No related tags found
No related merge requests found
...@@ -132,12 +132,13 @@ class CachingSection(object): ...@@ -132,12 +132,13 @@ class CachingSection(object):
class CachingSectionManager(object): class CachingSectionManager(object):
def __init__(self, metaInfo, parentSectionNames, lastSectionGIndex = -1, openSections = {}, onClose = [], storeInSuper = False, forwardOpenClose = True): def __init__(self, metaInfo, parentSectionNames, lastSectionGIndex = -1, openSections = {}, onClose = [], onOpen = [], storeInSuper = False, forwardOpenClose = True):
self.metaInfo = metaInfo self.metaInfo = metaInfo
self.parentSectionNames = parentSectionNames self.parentSectionNames = parentSectionNames
self.lastSectionGIndex = lastSectionGIndex self.lastSectionGIndex = lastSectionGIndex
self.openSections = dict(openSections) self.openSections = dict(openSections)
self.onClose = list(onClose) self.onClose = list(onClose)
self.onOpen = list(onOpen)
self.storeInSuper = storeInSuper self.storeInSuper = storeInSuper
self.forwardOpenClose = forwardOpenClose self.forwardOpenClose = forwardOpenClose
...@@ -158,7 +159,10 @@ class CachingSectionManager(object): ...@@ -158,7 +159,10 @@ class CachingSectionManager(object):
references.append(pSect.lastSectionGIndex) references.append(pSect.lastSectionGIndex)
else: else:
references.append(-1) references.append(-1)
self.openSections[gIndex] = CachingSection(gIndex, references, storeInSuper = self.storeInSuper, activeBackend=backend) opened = CachingSection(gIndex, references, storeInSuper = self.storeInSuper, activeBackend=backend)
self.openSections[gIndex] = opened
for onOpenF in self.onOpen:
onOpenF(backend, gIndex, opened)
def closeSection(self, backend, gIndex): def closeSection(self, backend, gIndex):
toClose = self.openSections[gIndex] toClose = self.openSections[gIndex]
...@@ -236,13 +240,19 @@ class ActiveBackend(object): ...@@ -236,13 +240,19 @@ class ActiveBackend(object):
@classmethod @classmethod
def activeBackend(cls, metaInfoEnv, cachingLevelForMetaName = {}, defaultDataCachingLevel = CachingLevel.ForwardAndCache, defaultSectionCachingLevel = CachingLevel.Forward, superBackend = None, def activeBackend(cls, metaInfoEnv, cachingLevelForMetaName = {}, defaultDataCachingLevel = CachingLevel.ForwardAndCache, defaultSectionCachingLevel = CachingLevel.Forward, superBackend = None,
onClose = {}, propagateStartFinishParsing = True, default_units=None, metainfo_units=None): onClose = {}, onOpen = {}, propagateStartFinishParsing = True, default_units=None, metainfo_units=None):
for sectionName in onClose.keys(): for sectionName in onClose.keys():
if not sectionName in metaInfoEnv: if not sectionName in metaInfoEnv:
raise Exception("Found trigger for non existing section %s" % sectionName) raise Exception("Found trigger for non existing section %s" % sectionName)
elif metaInfoEnv.infoKinds[sectionName].kindStr != "type_section": elif metaInfoEnv.infoKinds[sectionName].kindStr != "type_section":
raise Exception("Found trigger for %s which is not a section but %s" % raise Exception("Found trigger for %s which is not a section but %s" %
(sectionName, json.dumps(metaInfoEnv.infoKinds[sectionName].toDict(), indent=2))) (sectionName, json.dumps(metaInfoEnv.infoKinds[sectionName].toDict(), indent=2)))
for sectionName in onOpen.keys():
if not sectionName in metaInfoEnv:
raise Exception("Found trigger for non existing section %s" % sectionName)
elif metaInfoEnv.infoKinds[sectionName].kindStr != "type_section":
raise Exception("Found trigger for %s which is not a section but %s" %
(sectionName, json.dumps(metaInfoEnv.infoKinds[sectionName].toDict(), indent=2)))
sectionManagers = {} sectionManagers = {}
for ikNames, ik in metaInfoEnv.infoKinds.items(): for ikNames, ik in metaInfoEnv.infoKinds.items():
if ik.kindStr == "type_section": if ik.kindStr == "type_section":
...@@ -254,7 +264,8 @@ class ActiveBackend(object): ...@@ -254,7 +264,8 @@ class ActiveBackend(object):
parentSectionNames = parentS, parentSectionNames = parentS,
storeInSuper = (cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache), storeInSuper = (cachingLevel == CachingLevel.ForwardAndCache or cachingLevel == CachingLevel.Cache),
forwardOpenClose = (cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache), forwardOpenClose = (cachingLevel == CachingLevel.Forward or cachingLevel == CachingLevel.ForwardAndCache),
onClose = onClose.get(ik.name, [])) onClose = onClose.get(ik.name, []),
onOpen = onOpen.get(ik.name, []))
dataManagers = {} dataManagers = {}
for ikNames, ik in metaInfoEnv.infoKinds.items(): for ikNames, ik in metaInfoEnv.infoKinds.items():
if ik.kindStr == "type_document_content" or ik.kindStr == "type_dimension": if ik.kindStr == "type_document_content" or ik.kindStr == "type_dimension":
...@@ -271,6 +282,9 @@ class ActiveBackend(object): ...@@ -271,6 +282,9 @@ class ActiveBackend(object):
def appendOnClose(self, sectionName, onClose): def appendOnClose(self, sectionName, onClose):
self.sectionManagers.onClose.append(onClose) self.sectionManagers.onClose.append(onClose)
def appendOnOpen(self, sectionName, onOpen):
self.sectionManagers.onOpen.append(onOpen)
def storeToClosedSuper(self, superName, superGIndex, metaInfo, toClose): def storeToClosedSuper(self, superName, superGIndex, metaInfo, toClose):
logging.getLogger("nomadcore.caching_backend").warn( logging.getLogger("nomadcore.caching_backend").warn(
"Dropping section {name} gIndex {gIndex}, as it cannot be added to closed super section {superName} gIndex: {superGIndex}".format( "Dropping section {name} gIndex {gIndex}, as it cannot be added to closed super section {superName} gIndex: {superGIndex}".format(
......
...@@ -672,6 +672,19 @@ def extractOnCloseTriggers(obj): ...@@ -672,6 +672,19 @@ def extractOnCloseTriggers(obj):
triggers[name] = getattr(obj, attr) triggers[name] = getattr(obj, attr)
return triggers return triggers
def extractOnOpenTriggers(obj):
"""extracts all triggers from the object obj
triggers should start with onOpen_ and then have a valid section name.
They will be called with backend, gIndex and the section that is being closed
(of type CachingSection)"""
triggers = {}
for attr in dir(obj):
if attr.startswith("onOpen_"):
name = attr[7:]
triggers[name] = getattr(obj, attr)
return triggers
class SimpleParserBuilder(object): class SimpleParserBuilder(object):
def __init__(self, rootMatcher, metaInfoEnv, metaInfoToKeep=None, default_units=None, metainfo_units=None): def __init__(self, rootMatcher, metaInfoEnv, metaInfoToKeep=None, default_units=None, metainfo_units=None):
""" """
...@@ -1068,6 +1081,7 @@ def mainFunction(mainFileDescription, ...@@ -1068,6 +1081,7 @@ def mainFunction(mainFileDescription,
defaultSectionCachingLevel = CachingLevel.Forward, defaultSectionCachingLevel = CachingLevel.Forward,
superContext = None, superContext = None,
onClose = {}, onClose = {},
onOpen = {},
default_units=None, default_units=None,
metainfo_units=None, metainfo_units=None,
superBackend=None, superBackend=None,
...@@ -1177,12 +1191,20 @@ def mainFunction(mainFileDescription, ...@@ -1177,12 +1191,20 @@ def mainFunction(mainFileDescription,
oldCallbacks.append(callback) oldCallbacks.append(callback)
else: else:
onClose[attr] = [callback] 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( backend = ActiveBackend.activeBackend(
metaInfoEnv = metaInfoEnv, metaInfoEnv = metaInfoEnv,
cachingLevelForMetaName = cachingLevelForMetaName, cachingLevelForMetaName = cachingLevelForMetaName,
defaultDataCachingLevel = defaultDataCachingLevel, defaultDataCachingLevel = defaultDataCachingLevel,
defaultSectionCachingLevel = defaultSectionCachingLevel, defaultSectionCachingLevel = defaultSectionCachingLevel,
onClose = onClose, onClose = onClose,
onOpen = onOpen,
superBackend = jsonBackend, superBackend = jsonBackend,
default_units=default_units, default_units=default_units,
metainfo_units=metainfo_units) metainfo_units=metainfo_units)
...@@ -1240,11 +1262,15 @@ class AncillaryParser(object): ...@@ -1240,11 +1262,15 @@ class AncillaryParser(object):
onClose = {} onClose = {}
for attr, callback in extractOnCloseTriggers(superContext).items(): for attr, callback in extractOnCloseTriggers(superContext).items():
onClose[attr] = [callback] onClose[attr] = [callback]
onOpen = {}
for attr, callback in extractOnOpenTriggers(superContext).items():
onOpen[attr] = [callback]
# create backend for parser # create backend for parser
self.backend = ActiveBackend.activeBackend( self.backend = ActiveBackend.activeBackend(
metaInfoEnv = parser.parserBuilder.metaInfoEnv, metaInfoEnv = parser.parserBuilder.metaInfoEnv,
cachingLevelForMetaName = cachingLevelForMetaName, cachingLevelForMetaName = cachingLevelForMetaName,
onClose = onClose, onClose = onClose,
onOpen = onOpen,
superBackend = parser.backend.superBackend, superBackend = parser.backend.superBackend,
propagateStartFinishParsing = False, # write no parser info upon start and end of parsing propagateStartFinishParsing = False, # write no parser info upon start and end of parsing
default_units=default_units, default_units=default_units,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment