From 266d6f4c97e1bd1bbb1c49ed4a8809429110aa0b Mon Sep 17 00:00:00 2001 From: aothms Date: Thu, 13 Aug 2015 14:26:14 +0200 Subject: [PATCH] Cache the output of the parsed express schema as a Python pickle stream for speedier code generation --- src/ifcexpressparser/bootstrap.py | 38 ++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/ifcexpressparser/bootstrap.py b/src/ifcexpressparser/bootstrap.py index bb9e4a58e6..c994bd4515 100644 --- a/src/ifcexpressparser/bootstrap.py +++ b/src/ifcexpressparser/bootstrap.py @@ -158,30 +158,42 @@ for id in to_emit: stmt = "Suppress%s" % stmt statements.append("%s << %s" % (id, stmt)) -print ("""import sys -from pyparsing import * -from nodes import * +print ("""import os +import sys +import pickle -%s +cache_file = sys.argv[1] + ".cache.dat" +if os.path.exists(cache_file): + with open(cache_file, "rb") as f: + mapping = pickle.load(f) +else: + from pyparsing import * + from nodes import * + + import schema + import mapping + + %s + + syntax.ignore(Regex(r"\((?:\*(?:[^*]*\*+)+?\))")) + ast = syntax.parseFile(sys.argv[1]) + schema = schema.Schema(ast) + mapping = mapping.Mapping(schema) -import schema -import mapping + with open(cache_file, "wb") as f: + pickle.dump(mapping, f, protocol=0) import header import enum_header import implementation import latebound_header import latebound_implementation - -syntax.ignore("--" + restOfLine) -syntax.ignore(Regex(r"\((?:\*(?:[^*]*\*+)+?\))")) -ast = syntax.parseFile(sys.argv[1]) -schema = schema.Schema(ast) -mapping = mapping.Mapping(schema) +import schema_class header.Header(mapping).emit() enum_header.EnumHeader(mapping).emit() implementation.Implementation(mapping).emit() latebound_header.LateBoundHeader(mapping).emit() latebound_implementation.LateBoundImplementation(mapping).emit() -"""%('\n'.join(statements))) +schema_class.SchemaClass(mapping).emit() +"""%('\n '.join(statements)))