diff --git a/common/python/nomadcore/simple_parser.py b/common/python/nomadcore/simple_parser.py index 307475e89919e228eec40f4d3c6acc2ce9e673fe..5e81cb116ca634e7e05775265b51fbd3dc855ad1 100644 --- a/common/python/nomadcore/simple_parser.py +++ b/common/python/nomadcore/simple_parser.py @@ -27,7 +27,7 @@ annotate = False class PushbackLineFile(object): """a file interface where it is possible to put back read lines""" - def __init__(self, fIn, lines = []): + def __init__(self, fIn, lines=tuple()): self.fIn = fIn self.lines = list(lines) self.lineNr = 0 @@ -57,7 +57,7 @@ class SimpleMatcher(object): # if the matcher has weak == true, this matcher should nevel steal # the matcher can/should repeat immediately - def __init__(self, startReStr, endReStr = None, subMatchers = [], sections = [], + def __init__(self, startReStr, endReStr = None, subMatchers = tuple(), sections = tuple(), subFlags = SubFlags.Sequenced, weak = False, # this matcher should not "steal" the position repeats = False, # this matcher is expected to repeat @@ -67,10 +67,10 @@ class SimpleMatcher(object): forwardMatch = False, # if start match should not eat input, but be forwarded to adHoc and subMatchers name = "", adHoc = None, - otherMetaInfo = [], # The metainfos that are later manually added ot the backend + otherMetaInfo = tuple(), # The metainfos that are later manually added ot the backend fixedStartValues = None, fixedEndValues = None, - dependencies={}, + dependencies = None, defLine=0, defFile='', coverageIgnore=False, # mark line as ignored in coverage analysis @@ -82,8 +82,8 @@ class SimpleMatcher(object): self.index = -1 self.startReStr = startReStr self.endReStr = endReStr - self.subMatchers = subMatchers - self.sections = sections + self.subMatchers = list(subMatchers) + self.sections = list(sections) self.subFlags = subFlags self.weak = weak self.repeats = repeats @@ -92,7 +92,9 @@ class SimpleMatcher(object): self.forwardMatch = forwardMatch self.superMatcher = None self.adHoc = adHoc - self.otherMetaInfo = otherMetaInfo + self.otherMetaInfo = list(otherMetaInfo) + if dependencies is None: + dependencies = {} self.dependencies = dependencies self.name = name self.fixedStartValues = fixedStartValues @@ -239,9 +241,12 @@ class SimpleMatcher(object): groups.extend(extractGroupNames(self.endReStr)) return groups - def allMetaNames(self, metaNames=[]): + def allMetaNames(self, metaNames=None): """The names of the meta infos the could be matched by this matcher or its sub matchers. groups can be a """ + if metaNames is None: + metaNames = [] + metaNames.extend(self.directMetaNames()) for m in self.subMatchers: m.allMetaNames(metaNames) @@ -1281,20 +1286,20 @@ def defaultParseFile(parserInfo): def mainFunction(mainFileDescription, metaInfoEnv, parserInfo, - parseFile = None, - outF = sys.stdout, - cachingLevelForMetaName = {}, - defaultDataCachingLevel = CachingLevel.ForwardAndCache, - defaultSectionCachingLevel = CachingLevel.Forward, - superContext = None, - onClose = {}, - onOpen = {}, + parseFile=None, + 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={}): + strValueTransform=None): """ Args: default_units: A list of default unit definitions. @@ -1330,6 +1335,16 @@ def mainFunction(mainFileDescription, verbose = False fileToParse = None writeMatchTelemetry = False + + if cachingLevelForMetaName is None: + cachingLevelForMetaName = {} + if onClose is None: + onClose = {} + if onOpen is None: + onOpen = {} + if strValueTransform is None: + strValueTransform = {} + ii = 1 while ii < len(sys.argv): arg = sys.argv[ii] @@ -1531,7 +1546,7 @@ class ParserOptimizer(object): """For optimizing a hierarchy of SimpleMatchers based on a list of metainfo names that should be included in the parsing. """ - def optimizeParsingTree(self, rootMatcher, metaInfoToKeep=[]): + def optimizeParsingTree(self, rootMatcher, metaInfoToKeep=None): """This function will remove any parsing unnecessary parsing actions from the parsing tree based on the given list of metainfo names to keep and metainfo names to skip. @@ -1542,6 +1557,9 @@ class ParserOptimizer(object): metaInfoToKeep: List of metainfonames to keep in parsing """ # First identify the parts that should be kept + if metaInfoToKeep is None: + metaInfoToKeep = [] + if metaInfoToKeep: # Single values are put inside a list for convenience