From 7b3801ec411154cb05e6c1b3c802138e12752124 Mon Sep 17 00:00:00 2001 From: Kenneth Hoste Date: Sun, 8 Nov 2020 19:55:32 +0100 Subject: [PATCH] significantly speed up parsing of easyconfig files by only extracting comments from an easyconfig file when they're actually needed --- easybuild/framework/easyconfig/format/format.py | 7 ++++++- easybuild/framework/easyconfig/format/one.py | 15 +++++++++++++-- easybuild/framework/easyconfig/parser.py | 2 -- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/easybuild/framework/easyconfig/format/format.py b/easybuild/framework/easyconfig/format/format.py index 89bf9ecc3e..ff9acac3d6 100644 --- a/easybuild/framework/easyconfig/format/format.py +++ b/easybuild/framework/easyconfig/format/format.py @@ -612,12 +612,17 @@ def __init__(self): raise EasyBuildError('Invalid version number %s (incorrect length)', self.VERSION) self.rawtext = None # text version of the easyconfig - self.comments = {} # comments in easyconfig file + self._comments = {} # comments in easyconfig file self.header = None # easyconfig header (e.g., format version, license, ...) self.docstring = None # easyconfig docstring (e.g., author, maintainer, ...) self.specs = {} + @property + def comments(self): + """Return comments in easyconfig file""" + return self._comments + def set_specifications(self, specs): """Set specifications.""" self.log.debug('Set copy of specs %s' % specs) diff --git a/easybuild/framework/easyconfig/format/one.py b/easybuild/framework/easyconfig/format/one.py index e28b94bd50..6d789a4c6a 100644 --- a/easybuild/framework/easyconfig/format/one.py +++ b/easybuild/framework/easyconfig/format/one.py @@ -135,7 +135,8 @@ def parse(self, txt): """ Pre-process txt to extract header, docstring and pyheader, with non-indented section markers enforced. """ - super(FormatOneZero, self).parse(txt, strict_section_markers=True) + self.rawcontent = txt + super(FormatOneZero, self).parse(self.rawcontent, strict_section_markers=True) def _reformat_line(self, param_name, param_val, outer=False, addlen=0): """ @@ -356,6 +357,16 @@ def dump(self, ecfg, default_values, templ_const, templ_val, toolchain_hierarchy return '\n'.join(dump) + @property + def comments(self): + """ + Return comments (and extract them first if needed). + """ + if not self._comments: + self.extract_comments(self.rawcontent) + + return self._comments + def extract_comments(self, rawtxt): """ Extract comments from raw content. @@ -363,7 +374,7 @@ def extract_comments(self, rawtxt): Discriminates between comment header, comments above a line (parameter definition), and inline comments. Inline comments on items of iterable values are also extracted. """ - self.comments = { + self._comments = { 'above': {}, # comments above a parameter definition 'header': [], # header comment lines 'inline': {}, # inline comments diff --git a/easybuild/framework/easyconfig/parser.py b/easybuild/framework/easyconfig/parser.py index e1294cf545..e291a2d356 100644 --- a/easybuild/framework/easyconfig/parser.py +++ b/easybuild/framework/easyconfig/parser.py @@ -110,8 +110,6 @@ def __init__(self, filename=None, format_version=None, rawcontent=None, else: raise EasyBuildError("Neither filename nor rawcontent provided to EasyConfigParser") - self._formatter.extract_comments(self.rawcontent) - def process(self, filename=None): """Create an instance""" self._read(filename=filename)