Index: compile_rules.py ================================================================== --- compile_rules.py +++ compile_rules.py @@ -1,13 +1,15 @@ """ Grammalecte: compile rules """ import re +import os import traceback import json import colorsys +import time import compile_rules_js_convert as jsconv import compile_rules_graph as crg @@ -417,11 +419,11 @@ dOptPriority = {} for sLine in lOptionLines: sLine = sLine.strip() if sLine.startswith("OPTGROUP/"): m = re.match("OPTGROUP/([a-z0-9]+):(.+)$", sLine) - lStructOpt.append( (m.group(1), list(map(str.split, m.group(2).split(",")))) ) + lStructOpt.append( [m.group(1), list(map(str.split, m.group(2).split(",")))] ) elif sLine.startswith("OPTSOFTWARE:"): lOpt = [ [s, {}] for s in sLine[12:].strip().split() ] # don’t use tuples (s, {}), because unknown to JS elif sLine.startswith("OPT/"): m = re.match("OPT/([a-z0-9]+):(.+)$", sLine) for i, sOpt in enumerate(m.group(2).split()): @@ -463,13 +465,22 @@ def printBookmark (nLevel, sComment, nLine): "print bookmark within the rules file" print(" {:>6}: {}".format(nLine, " " * nLevel + sComment)) -def make (spLang, sLang, bJavaScript): +def make (spLang, sLang, bUseCache=False): "compile rules, returns a dictionary of values" # for clarity purpose, don’t create any file here + + if bUseCache and os.path.isfile("_build/data_cache.json"): + print("> don’t rebuild rules, use cache...") + sJSON = open("_build/data_cache.json", "r", encoding="utf-8").read() + dCacheVars = json.loads(sJSON) + print(" build made at: " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(dCacheVars.get("fBuildTime", 0)))) + return dCacheVars + + fBuildTime = time.time() print("> read rules file...") try: lRules = open(spLang + "/rules.grx", 'r', encoding="utf-8").readlines() except: @@ -610,20 +621,25 @@ displayStats(lParagraphRules, lSentenceRules) print("Unnamed rules: " + str(nRULEWITHOUTNAME)) - dVars = { "callables": sPyCallables, - "callablesJS": sJSCallables, - "gctests": sGCTests, - "gctestsJS": sGCTestsJS, - "paragraph_rules": mergeRulesByOption(lParagraphRules), - "sentence_rules": mergeRulesByOption(lSentenceRules), - "paragraph_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lParagraphRulesJS)), - "sentence_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lSentenceRulesJS)) } + dVars = { + "fBuildTime": fBuildTime, + "callables": sPyCallables, + "callablesJS": sJSCallables, + "gctests": sGCTests, + "gctestsJS": sGCTestsJS, + "paragraph_rules": mergeRulesByOption(lParagraphRules), + "sentence_rules": mergeRulesByOption(lSentenceRules), + "paragraph_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lParagraphRulesJS)), + "sentence_rules_JS": jsconv.writeRulesToJSArray(mergeRulesByOption(lSentenceRulesJS)) + } dVars.update(dOptions) # compile graph rules - dVars2 = crg.make(lGraphRule, dDEF, sLang, dOptPriority, bJavaScript) + dVars2 = crg.make(lGraphRule, dDEF, sLang, dOptPriority) dVars.update(dVars2) + with open("_build/data_cache.json", "w", encoding="utf-8") as hDst: + hDst.write(json.dumps(dVars, ensure_ascii=False)) return dVars Index: compile_rules_graph.py ================================================================== --- compile_rules_graph.py +++ compile_rules_graph.py @@ -316,11 +316,11 @@ else: print(" # Unknown action.", sActionId) return None -def make (lRule, dDef, sLang, dOptPriority, bJavaScript): +def make (lRule, dDef, sLang, dOptPriority): "compile rules, returns a dictionary of values" # for clarity purpose, don’t create any file here # removing comments, zeroing empty lines, creating definitions, storing tests, merging rule lines print(" parsing rules...") @@ -452,10 +452,10 @@ # Result return { "graph_callables": sPyCallables, "graph_callablesJS": sJSCallables, - "rules_graphs": dAllGraph, + "rules_graphs": str(dAllGraph), "rules_graphsJS": str(dAllGraph).replace("True", "true").replace("False", "false"), - "rules_actions": dACTIONS, + "rules_actions": str(dACTIONS), "rules_actionsJS": str(dACTIONS).replace("True", "true").replace("False", "false") } Index: make.py ================================================================== --- make.py +++ make.py @@ -184,11 +184,11 @@ for sf in os.listdir(spLangPack): if not os.path.isdir(spLangPack+"/"+sf): hZip.write(spLangPack+"/"+sf, sAddPath+spLangPack+"/"+sf) -def create (sLang, xConfig, bInstallOXT, bJavaScript): +def create (sLang, xConfig, bInstallOXT, bJavaScript, bUseCache): "make Grammalecte for project " oNow = datetime.datetime.now() print("============== MAKE GRAMMALECTE [{0}] at {1.hour:>2} h {1.minute:>2} min {1.second:>2} s ==============".format(sLang, oNow)) #### READ CONFIGURATION @@ -198,11 +198,11 @@ dVars = xConfig._sections['args'] dVars['locales'] = dVars["locales"].replace("_", "-") dVars['loc'] = str(dict([ [s, [s[0:2], s[3:5], ""]] for s in dVars["locales"].split(" ") ])) ## COMPILE RULES - dResult = compile_rules.make(spLang, dVars['lang'], bJavaScript) + dResult = compile_rules.make(spLang, dVars['lang'], bUseCache) dVars.update(dResult) ## READ GRAMMAR CHECKER PLUGINS print("PYTHON:") print("+ Plugins: ", end="") @@ -373,10 +373,11 @@ def main (): "build Grammalecte with requested options" print("Python: " + sys.version) xParser = argparse.ArgumentParser() xParser.add_argument("lang", type=str, nargs='+', help="lang project to generate (name of folder in /lang)") + xParser.add_argument("-uc", "--use_cache", help="use data cache instead of rebuilding rules", action="store_true") xParser.add_argument("-b", "--build_data", help="launch build_data.py (part 1 and 2)", action="store_true") xParser.add_argument("-bb", "--build_data_before", help="launch build_data.py (only part 1: before dictionary building)", action="store_true") xParser.add_argument("-ba", "--build_data_after", help="launch build_data.py (only part 2: before dictionary building)", action="store_true") xParser.add_argument("-d", "--dict", help="generate FSA dictionary", action="store_true") xParser.add_argument("-t", "--tests", help="run unit tests", action="store_true") @@ -440,11 +441,11 @@ # copy dictionaries from Graphspell copyGraphspellDictionaries(dVars, xArgs.javascript, xArgs.add_extended_dictionary, xArgs.add_community_dictionary, xArgs.add_personal_dictionary) # make - sVersion = create(sLang, xConfig, xArgs.install, xArgs.javascript, ) + sVersion = create(sLang, xConfig, xArgs.install, xArgs.javascript, xArgs.use_cache) # tests if xArgs.tests or xArgs.perf or xArgs.perf_memo: print("> Running tests") try: