Skip to content
Snippets Groups Projects
Commit f7e8cfa1 authored by Himanen, Lauri (himanel1)'s avatar Himanen, Lauri (himanel1)
Browse files

Now every subclass of ParserInterface will use the same metainfo environment...

Now every subclass of ParserInterface will use the same metainfo environment object, and not always load a new instance. Speeds things up if multiple parsers are instantiated within one python run.
parent c36e87cf
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,7 @@ class ParserInterface(object):
subparsers.
"""
__metaclass__ = ABCMeta
metainfo_env = None
def __init__(self, main_file, metainfo_to_keep=None, backend=None, default_units=None, metainfo_units=None):
"""
......@@ -62,15 +63,22 @@ class ParserInterface(object):
if not os.path.isfile(main_file):
logger.error("Couldn't find the main file {}. Check that the path is valid and the file exists on this path.".format(main_file))
# Load metainfo environment
metainfo_env, warn = load_metainfo(self.get_metainfo_filename())
self.parser_context.metainfo_env = metainfo_env
# Setup the metainfo environment. All parsers that inherit from this
# class will have a static class attribute that will store the metainfo
# environment. This way every instance of a parser doesn't have to load
# the environment separately because it is identical for each instance.
if type(self).metainfo_env is None:
metainfo_env, warn = load_metainfo(self.get_metainfo_filename())
type(self).metainfo_env = metainfo_env
self.parser_context.metainfo_env = metainfo_env
else:
self.parser_context.metainfo_env = type(self).metainfo_env
# Initialize the backend. Use local backend if none given
if backend is not None:
self.parser_context.super_backend = backend(metainfo_env)
self.parser_context.super_backend = backend(type(self).metainfo_env)
else:
self.parser_context.super_backend = LocalBackend(metainfo_env)
self.parser_context.super_backend = LocalBackend(type(self).metainfo_env)
# Check the list of default units
default_unit_map = {}
......@@ -90,7 +98,7 @@ class ParserInterface(object):
unit_conversion.ureg(unit)
# Check that the metaname is OK
meta = metainfo_env.infoKinds.get(metaname)
meta = ParserInterface.metainfo_env.infoKinds.get(metaname)
if meta is None:
raise KeyError("The metainfo name '{}' could not be found. Check for typos or try updating the metainfo repository.".format(metaname))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment