def validateSwitchout(self, tag): if not isblank(self.outBfrName): raise XLemException("Already opened switchout into the page:'" + self.outBfrName + "' !!!") self.outBfrName = trim(tag.getContent()) if isblank(self.outBfrName): raise XLemException( 'Missing buffer name into switchout statement!') pass
def validateExternal(self, tagName, tagContent): XLEM_FILE_NAME = "" dim_re = re.compile( "\s*src[\s]*[=][\s]*[\"](?P<XLEM_FILE_NAME>.+)[.]xlmh[\"]\s*", re.VERBOSE) for match in dim_re.finditer(tagContent): XLEM_FILE_NAME = match.group("XLEM_FILE_NAME") if XLEM_FILE_NAME == "": raise XLemException( "Invalid or missing module file name: <@external " + tagContent + "@>!") return XLEM_FILE_NAME
def validateForStatement(self, tag, shiftPrev): tag.setShiftPrev(shiftPrev) tag.setShift(1) dim_re = re.compile( "\s*(?P<XLEM_VAR_NAME>.+)[\s]*=[\s]*(?P<XLEM_VAR_FROM>.+)" + "[\s]*to[\s]*(?P<XLEM_VAR_TO>.+)\s*(?P<XLEM_VAR_STEP>step)\s*(?P<XLEM_VAR_STEP_VALUE>.+)\s*", re.VERBOSE) XLEM_VAR_NAME = "" XLEM_VAR_TO = "" XLEM_VAR_FROM = "" for match in dim_re.finditer(tag.getContent()): XLEM_VAR_NAME = match.group("XLEM_VAR_NAME") XLEM_VAR_FROM = match.group("XLEM_VAR_FROM") XLEM_VAR_TO = match.group("XLEM_VAR_TO") XLEM_VAR_STEP_VALUE = match.group("XLEM_VAR_STEP_VALUE") tag.setCompiled("for " + XLEM_VAR_NAME + " in range (" + XLEM_VAR_FROM + ", (" + XLEM_VAR_TO + ")+1, " + XLEM_VAR_STEP_VALUE + "):") return pass if XLEM_VAR_NAME == "" or XLEM_VAR_FROM == "" or XLEM_VAR_TO == "": dim_re = re.compile( "\s*(?P<XLEM_VAR_NAME>.+)[\s]*=[\s]*(?P<XLEM_VAR_FROM>.+)" + "[\s]*to[\s]*(?P<XLEM_VAR_TO>.+)\s*", re.VERBOSE) for match in dim_re.finditer(tag.getContent()): XLEM_VAR_NAME = match.group("XLEM_VAR_NAME") XLEM_VAR_FROM = match.group("XLEM_VAR_FROM") XLEM_VAR_TO = match.group("XLEM_VAR_TO") tag.setCompiled("for " + XLEM_VAR_NAME + " in range (" + XLEM_VAR_FROM + ", (" + XLEM_VAR_TO + ")+1):") return else: return if XLEM_VAR_NAME == "" or XLEM_VAR_FROM == "" or XLEM_VAR_TO == "": raise XLemException("Invalid for statement: " + repr(tag.getContent()))
def validateMethodStatement(self, tag, shiftPrev): tag.setShiftPrev(shiftPrev) tag.setShift(1) tag.writeThis = True dim_re = re.compile( "\s*(?P<XLEM_METHOD_NAME>\w+)[\s]*[(]\s*(?P<XLEM_METHOD_ARGUMENTS>.*)\s*[)]\s*", re.VERBOSE) for match in dim_re.finditer(tag.getContent()): XLEM_METHOD_NAME = match.group("XLEM_METHOD_NAME") XLEM_METHOD_ARGUMENTS = match.group("XLEM_METHOD_ARGUMENTS") if isblank(XLEM_METHOD_ARGUMENTS): XLEM_METHOD_ARGUMENTS = "self" else: XLEM_METHOD_ARGUMENTS = "self, " + XLEM_METHOD_ARGUMENTS tag.setCompiled("def " + XLEM_METHOD_NAME + " ( " + XLEM_METHOD_ARGUMENTS + " ) :") return pass raise XLemException("Invalid method definition: " + repr(tag.getContent()))
def xlem2py(self): XLEM_PAGE_TEMPLATE = """ ''' Created on $$CreatedDate$$ XLEM page generator V.$$ParserVersion$$ ''' $$XLEMImport$$ class $$XLEMClassName$$(XLEM_PAGE): def doXlemWebSocketGetMessage(self,request, sendmessage, message): application=request.getapplication() $$WSGetMessageBody$$ pass def doXlemWebSocketOpen(self,request, __currentWebSocketThread, sendmessage): application=request.getapplication() $$WSGetMessageOpen$$ pass def doXlemWebSocketClose(self,request, __currentWebSocketThread, sendmessage): application=request.getapplication() $$WSGetMessageClose$$ pass def doXlemWebSocketMain(self,request, __currentWebSocketThread, sendmessage): application=request.getapplication() isrunning=__currentWebSocketThread.isRunning $$WSBody$$ pass def doXLemService(self,request,response): querystring=request.getQueryString() session=$$session_value$$ application=request.getapplication() $$RunBody$$ def toBeReloaded(self): for d in self.__dependencies: try: if self.__dependencies.get(d)!=getmtime(d): #print('File',d,'has changed!' ) return True except Exception: pass pass return False def __init__(self): self.__dependencies={} $$Dependencies$$ #print("inizzio") pass #$$MainScript$$ # Custom Page functions... $$Functions$$ """ pyTxt = XLEM_PAGE_TEMPLATE pyTxt = pyTxt.replace("$$CreatedDate$$", __DATETIME__.now().isoformat()) pyTxt = pyTxt.replace("$$ParserVersion$$", self.XLEM_PARSER_VERSION) pyTxt = pyTxt.replace("$$XLEMClassName$$", self.do_GenerateClassName()) dpdcs = [] for dpdc in self.xLemEnv.dependencies: dpdcs.append(" self.__dependencies.update({'" + dpdc + "':" + str(self.xLemEnv.dependencies.get(dpdc)) + "})") pyTxt = pyTxt.replace("$$Dependencies$$", "\n".join(dpdcs)) if self.__useSession: pyTxt = pyTxt.replace( "$$session_value$$", "request.getsession(" + repr(self.__createSessionOnFail) + ")") else: pyTxt = pyTxt.replace("$$session_value$$", "None") lbrs = [] lbrs.append("from xlem.http.XLemPage import XLemPage as XLEM_PAGE") lbrs.append("from xlem.utils import *") lbrs.append("from xlem.data import *") lbrs.append("from xlem.data.dateAndTime import *") lbrs.append("from xlem.data.collections import *") lbrs.append("from os.path import getmtime") lbrs.append("from xlem.runtime.RDBMS import GenericDatabase") lbrs.append("from xlem.data.io import *") lbrs.append("from xlem.data.xml import *") for lb in self.xlemLibraries: lbrs.append(lb) pyTxt = pyTxt.replace("$$XLEMImport$$", "\n".join(lbrs)) shfts_run = 3 shfts_fun = 0 shfts_wsm = 3 shfts_wsb = 3 shfts_wso = 3 shfts_wsc = 3 currBfr = self.runBfr shfts = shfts_run retBfr = self.runBfr tNm = "" for tag in self.xlemTokens: tNm = tag.getName() if (tag.getCompiled() != ""): if (tag.isWritePass()): currBfr.append(self.generateLine(shfts, "pass")) shfts += tag.getShiftPrev() if tNm in ("function", "model"): retBfr = currBfr if currBfr == self.wsmBfr: shfts_wsm = shfts elif currBfr == self.wsbBfr: shfts_wsb = shfts elif currBfr == self.wsoBfr: shfts_wso = shfts elif currBfr == self.wscBfr: shfts_wsc = shfts else: shfts_run = shfts shfts = shfts_fun currBfr = self.funBfr elif tNm in ("/function", "endfunction", "/model", "endmodel"): currBfr = retBfr #self.runBfr shfts_fun = shfts if currBfr == self.wsmBfr: shfts = shfts_wsm elif currBfr == self.wsbBfr: shfts = shfts_wsb elif currBfr == self.wsoBfr: shfts = shfts_wso elif currBfr == self.wscBfr: shfts = shfts_wsc else: shfts = shfts_run elif tNm == "websocket:onmessage": retBfr = currBfr if currBfr == self.funBfr: shfts_fun = shfts elif currBfr == self.wsbBfr: shfts_wsb = shfts elif currBfr == self.wsoBfr: shfts_wso = shfts elif currBfr == self.wscBfr: shfts_wsc = shfts else: shfts_run = shfts shfts = shfts_wsm currBfr = self.wsmBfr elif tNm == "/websocket:onmessage": currBfr = retBfr #self.runBfr shfts_wsm = shfts if currBfr == self.funBfr: shfts = shfts_fun elif currBfr == self.wsbBfr: shfts = shfts_wsb elif currBfr == self.wsoBfr: shfts = shfts_wso elif currBfr == self.wscBfr: shfts = shfts_wsc else: shfts = shfts_run elif tNm == "websocket": retBfr = currBfr if currBfr == self.funBfr: shfts_fun = shfts elif currBfr == self.wsmBfr: shfts_wsm = shfts elif currBfr == self.wsoBfr: shfts_wso = shfts elif currBfr == self.wscBfr: shfts_wsc = shfts else: shfts_run = shfts shfts = shfts_wsb currBfr = self.wsbBfr elif tNm == "/websocket": currBfr = retBfr #self.runBfr shfts_wsb = shfts if currBfr == self.funBfr: shfts = shfts_fun elif currBfr == self.wsmBfr: shfts = shfts_wsm elif currBfr == self.wsoBfr: shfts = shfts_wso elif currBfr == self.wscBfr: shfts = shfts_wsc else: shfts = shfts_run elif tNm == "websocket:open": retBfr = currBfr if currBfr == self.funBfr: shfts_fun = shfts elif currBfr == self.wsmBfr: shfts_wsm = shfts elif currBfr == self.wsbBfr: shfts_wsb = shfts elif currBfr == self.wscBfr: shfts_wsc = shfts else: shfts_run = shfts shfts = shfts_wso currBfr = self.wsoBfr elif tNm == "/websocket:open": currBfr = retBfr #self.runBfr shfts_wso = shfts if currBfr == self.funBfr: shfts = shfts_fun elif currBfr == self.wsmBfr: shfts = shfts_wsm elif currBfr == self.wsbBfr: shfts = shfts_wsb elif currBfr == self.wscBfr: shfts = shfts_wsc else: shfts = shfts_run elif tNm == "websocket:close": retBfr = currBfr if currBfr == self.funBfr: shfts_fun = shfts elif currBfr == self.wsmBfr: shfts_wsm = shfts elif currBfr == self.wsbBfr: shfts_wsb = shfts elif currBfr == self.wsoBfr: shfts_wso = shfts else: shfts_run = shfts shfts = shfts_wso currBfr = self.wscBfr elif tNm == "/websocket:close": currBfr = retBfr #self.runBfr shfts_wsc = shfts if currBfr == self.funBfr: shfts = shfts_fun elif currBfr == self.wsmBfr: shfts = shfts_wsm elif currBfr == self.wsbBfr: shfts = shfts_wsb elif currBfr == self.wsoBfr: shfts = shfts_wso else: shfts = shfts_run if (tag.getCompiled() != ""): #shfts+=tag.getShiftPrev() currBfr.append(self.generateLine(shfts, tag.getCompiled())) shfts += tag.getShift() if (tag.isWriteThis()): currBfr.append(self.generateLine(shfts, "this=self")) pyTxt = pyTxt.replace("$$RunBody$$", "\n".join(self.runBfr)) pyTxt = pyTxt.replace("$$Functions$$", "\n".join(self.funBfr)) pyTxt = pyTxt.replace("$$WSGetMessageBody$$", "\n".join(self.wsmBfr)) pyTxt = pyTxt.replace("$$WSBody$$", "\n".join(self.wsbBfr)) pyTxt = pyTxt.replace("$$WSGetMessageOpen$$", "\n".join(self.wsoBfr)) pyTxt = pyTxt.replace("$$WSGetMessageClose$$", "\n".join(self.wscBfr)) #$$WSGetMessageOpen$$ out_file = None #self.xLemEnv.destPath="/Users/mgshow/Documents/workspace/XLEM/work/" #self.xLemEnv.destPath="/Users/mgshow/Documents/workspace/XLEM/work/" self.xLemEnv.destFile = self.do_GenerateClassName() + ".py" try: #className=self.do_GenerateClassName()+"."+self.do_GenerateClassName() out_file = codecs.open( self.xLemEnv.destPath + self.xLemEnv.destFile, "w", "utf-8") out_file.write(pyTxt) out_file.close() out_file = None btCode = py_compile.compile(self.xLemEnv.destPath + self.xLemEnv.destFile, doraise=True) # Store byte code into Server.... return btCode #struct=__import__(self.do_GenerateClassName(),globals={}, locals={}, fromlist=[''],level=1) ''' print("Carico modulo!") modl=imp.load_compiled(self.do_GenerateClassName(),btCode) print("modulo caricato!") clss=modl.__getattribute__(self.do_GenerateClassName()) inst=clss() inst.doXLemService(None,None) ''' except (IOError, OSError) as err: print("IO/OS ERROR 500 in parsing ", err) raise XLemException(err) except (Exception) as err: print("Exception/ERROR 500 in parsing", err) raise XLemException() finally: if out_file is not None: out_file.close() return pyTxt
def validateCloseSwitchout(self, tag): if isblank(self.outBfrName): raise XLemException('No switchout is currently opened!') self.outBfrName = ""
def xlemValidate(self): #print ("\r\n\r\nValidation processing...") tn = "" for tag in self.xlemTokens: if not self.existsTag(tag.getName()): raise XLemException("Invalid tag: " + tag.getName()) #analize tags... tn = tag.getName() tag.writePass = False tag.writeThis = False if tn == '$TEXT': self.validateText(tag) elif tn == '$EXPR': self.validateExpr(tag) elif tn == 'dim': self.validateVarDeclaration(tag) tag.writePass = False elif tn == 'field': self.validateFieldDeclaration(tag) tag.writePass = False elif tn == 'set': self.validateSetVariable(tag) elif tn in ('if', 'while', 'forall'): self.validateConditionalStatement(tag, 0, False) elif tn in ('try'): self.validateConditionalStatement(tag, 0, True) elif tn == 'for': self.validateForStatement(tag, 0) elif tn == 'else': tag.writePass = True self.validateConditionalStatement(tag, -1, True) elif tn in ('elseif', 'catch', 'finally'): self.validateConditionalStatement(tag, -1, False) tag.writePass = True elif tn in ('endif', 'wend', '/if', '/while', 'endwhile', 'wend', '/for', 'endfor', 'next', 'endforall', '/forall', '/try', 'endtry'): tag.writePass = True #elif tn=='endif' or tn=='wend' or tn=='/if' or tn=='/while': self.validateCloseConditionalStatement(tag) elif tn == 'call': self.validateCall(tag) elif tn == 'uselibrary': self.validateUseLibrary(tag) elif tn == 'content': self.validateContent(tag) elif tn == 'page': self.validatePage(tag) elif tn == 'function': self.validateFunction(tag, 0, False) elif tn == 'model': self.validateModel(tag, 0, False) elif tn == 'fields': self.validateFields(tag, 0, False) elif tn == 'method': self.validateMethodStatement(tag, 0) elif tn == 'return': self.validateReturn(tag) elif tn == '/function' or tn == 'endfunction': self.validateCloseFunction(tag) elif tn == '/model' or tn == 'endmodel': self.validateCloseModel(tag) elif tn == '/fields' or tn == 'endfields': self.validateCloseFields(tag) elif tn in ('/model', 'endmodel'): self.validateCloseModel(tag) elif tn in ('/method', 'endmethod'): self.validateCloseMethod(tag) elif tn == 'switchout': self.validateSwitchout(tag) elif tn == '/switchout': self.validateCloseSwitchout(tag) elif tn.startswith("ui:") or tn.startswith("/ui:"): self.validateUI(tag) '''
def xlemParse(self): # Reset Buffer self.runBfr = [] self.funBfr = [] self.wsmBfr = [] self.wsbBfr = [] self.wsoBfr = [] self.wscBfr = [] self.outBfrName = "" #f = open(self.xLemEnv.sourcePath + self.xLemEnv.sourceFile,"r") #f=codecs.getreader("utf-8")(open(self.xLemEnv.sourcePath + self.xLemEnv.sourceFile,"r")) fName = self.xLemEnv.sourcePath + self.xLemEnv.sourceFile f = codecs.open(fName, "r", "utf-8") ST_TEXT_READING = 1 ST_TAG_READING = 2 ST_TAG_CONTENT_READING = 6 self.xLemEnv.dependencies.update({fName: getmtime(fName)}) ''' start parsing ''' buffer = [] buffer.append(f.read()) f.close() buffer.append("<@// End of file@>") s = "".join(buffer) status = ST_TEXT_READING currTagName = "$TEXT" currTagContent = "" MAX_SOURCE_LENGTH = len(s) #s += " " i = 0 while i < MAX_SOURCE_LENGTH: ch = s[i] if ch == "<": if status == ST_TEXT_READING and s[i + 1] == '@': self.addTag(currTagName, currTagContent) currTagContent = "" currTagName = "" status = ST_TAG_READING i += 1 else: currTagContent += ch elif ch == "@": if status == ST_TAG_CONTENT_READING and s[i + 1] == '>': status = ST_TEXT_READING self.addTag(currTagName, currTagContent) if currTagName == "external": XLEM_FILE_NAME = self.validateExternal( currTagName, currTagContent) subEnv = XLemParserEnvironment() subEnv.sourcePath = self.xLemEnv.sourcePath subEnv.sourceFile = XLEM_FILE_NAME + ".xlmh" subEnv.dependencies = self.xLemEnv.dependencies sub = XLemParser(subEnv, None) subTags = sub.xlemParse() for subTag in subTags: self.addTag(subTag.getName(), subTag.getContent()) currTagContent = "" currTagName = "" i += 1 elif status == ST_TAG_READING and s[i + 1] == '>': self.addTag(currTagName, currTagContent) currTagContent = "" currTagName = "" status = ST_TEXT_READING i += 1 else: currTagContent += ch elif status == ST_TAG_READING: if ch == " ": status = ST_TAG_CONTENT_READING else: currTagName += ch if currTagName == "=": status = ST_TAG_CONTENT_READING elif currTagName == "--": endCommentPos = s.find("--@>", i) if endCommentPos > 0: i = endCommentPos status = ST_TAG_CONTENT_READING else: raise XLemException("Comment <@-- Not closed! ") pass elif status == ST_TAG_CONTENT_READING: currTagContent += ch else: currTagContent += ch i += 1 return self.xlemTokens