def ordenar(l,c,roots,ncod,debug=debug): if "<" in l[c[0]] and ">:" in l[c[0]] and root not in l[c[0]]: if "@" in l[c[0]]: widget=l[c[0]][l[c[0]].find("<")+1:l[c[0]].find("@")] else: widget=l[c[0]][l[c[0]].find("<")+1:l[c[0]].find(">:")] tab=zu.getTab(l[c[0]]) tab2=l[c[0]] roots[widget]=l[c[0]][len(tab):]+"\n" c2=c[0] c[0]+=1 while c[0]<len(l) and (len(zu.getTab(l[c[0]]))>len(tab) or l[c[0]]==""): if "<" in l[c[0]] and ">:" in l[c[0]] and root not in l[c[0]]: ordenar(l,c,roots,ncod) else: if len(l[c[0]])>len(tab): roots[widget]+=l[c[0]][len(tab):]+"\n" c[0]+=1 if debug==True: f=open("logs/log6_"+str(c2)+"-"+str(c[0])+".kv","w") f.write("["+tab2+"]\n"+roots[widget]) f.close() c[0]-=1 else: #print "@@@@@@@" if len(l[c[0]])>len("#:include "): if "#:include " !=l[c[0]][:len("#:include ")]: ncod[-1]+=l[c[0]]+"\n" else: ncod[-1]+=l[c[0]]+"\n" c[0]+=1
def __init__(self, url, data={}, admin=True, isglobal=True, offline=False): widget = "" data = str(data) self.admin = admin self.isglobal = isglobal if offline == False: self.update(data) else: if isglobal: __pragma__( "js", "{}", """ $.ajax({url:config.base_url+settings.app+'/admin/Show/layout/global/widgets/'+url+'.html/action=componer/', async:false, success: function(respuesta){ widget=respuesta; }, error : function(objXMLHttpRequest) { console.log("error1",objXMLHttpRequest); } }) """) elif admin: __pragma__( "js", "{}", """ $.ajax({url:config.base_url+settings.app+'/admin/Show/layout/admin/widgets/'+url+'.html/action=componer/', async:false, success: function(respuesta){ widget=respuesta; }, error : function(objXMLHttpRequest) { console.log("error2",objXMLHttpRequest); } }) """) else: __pragma__( "js", "{}", """ $.ajax({url:config.base_url+settings.app+'/admin/Show/layout/user/widgets/'+url+'.html/action=componer/', async:false, success: function(respuesta){ widget=respuesta; }, error : function(objXMLHttpRequest) { console.log("error3",objXMLHttpRequest); } }) """) lineas = widget.split("\n") from zu import getTab AnteriorIdentacion = "" abierta = False for k, elem in enumerate(lineas): identacion = getTab(elem) if "#" in elem: if "=" in elem and (elem[elem.find("=") - 1] != "=" and elem[elem.find("=") - 1] != "<" and elem[elem.find("=") - 1] != ">" and elem[elem.find("=") - 1] != "!"): c = elem.find("=") while c > 0: if elem[c] == "\t" or elem[c] == " ": break c -= 1 lineas[k] = elem[:c] + "var " + elem[c:] lineas[k] = lineas[k].replace("#", ";//") elem = lineas[k] if "except Exception as e:" in elem: lineas[k] = elem.replace("except Exception as e:", "}catch(e){") if lineas[k].strip().endswith("}catch(e){"): abierta = True else: lineas[k] += ";}" elem = lineas[k] abierta = False elif "try:" in elem: lineas[k] = elem.replace("try:", "try{") elem = lineas[k] + ("" if lineas[k].endswith("try{") else ";") abierta = True elif "'''" in elem: lineas[k] = elem.replace("'''", "`") elem = lineas[k] + ";" elif '"""' in elem: lineas[k] = elem.replace('"""', "`") elem = lineas[k] + ";" elif "elif " in elem and ":" in elem: lineas[k] = elem.replace("elif ", "}else if(").replace(":", "){") elem = lineas[k] abierta = True elif "if " in elem and ":" in elem: lineas[k] = elem.replace("if ", "if(").replace(":", "){") elem = lineas[k] abierta = True elif "else" in elem and ":" in elem: lineas[k] = elem.replace("else", "}else").replace(":", "{") elem = lineas[k] abierta = True elif "=" in elem and (elem[elem.find("=") - 1] != "+" and elem[elem.find("=") - 1] != "=" and elem[elem.find("=") - 1] != "<" and elem[elem.find("=") - 1] != ">" and elem[elem.find("=") - 1] != "!" and "//" not in elem ) and elem[elem.find("=") + 1] != "=": c = elem.find("=") - 1 while c > 0: if elem[c] == "\t" or elem[c] == " ": lineas[k] = elem[:c + 1] + "var " + elem[c + 1:] + ";" break c -= 1 elif "while " in elem and ":" in elem: lineas[k] = elem.replace("while ", "while(").replace(":", "){") elem = lineas[k] elif "for " in elem and ":" in elem: des = "(\w+)\s*(?:,(\s*\w+))?" _iter = "((?:\w+)?(?:\(?\[[A-Za-z0-9_,\-'\"]+?\]\)?)?)" patron1 = re.compile("for\s+" + des + "\s*in\s+" + _iter + ":") descompresion = patron1.findall(lineas[k])[0] #lista #dict cond2 = "}else{" i = lineas[k].find("for ") print(descompresion) if len(descompresion) > 2 and descompresion[ 1] != "" and descompresion[1] != None: f = lineas[k].find(":", lineas[k].find(descompresion[2])) cond1 = "if (str(" + descompresion[ 2] + ").strip()[0]=='[' && str(" + descompresion[ 2] + ").strip().slice(-1)==']'){" iterable = "var " + descompresion[ 1] + "=" + descompresion[2] + "[" + descompresion[ 0] + "][1];" iterable2 = "var " + descompresion[ 1] + "= Object.keys(" + descompresion[ 2] + ")[" + descompresion[0] + "];" bucle = "for (var " + descompresion[ 0] + " = 0; " + descompresion[ 0] + " < Object.keys(" + descompresion[ 2] + ").length; " + descompresion[ 0] + "++) {" lineas[k] = lineas[ k][: i] + bucle + cond1 + iterable + cond2 + iterable2 + "}" + lineas[ k][f + 1:] else: f = lineas[k].find(":", lineas[k].find(descompresion[2])) cond1 = "if (str(" + descompresion[ 2] + ").strip()[0]=='[' && str(" + descompresion[ 2] + ").strip().slice(-1)==']'){" bucle = "for (var _k = 0; _k < Object.keys(" + descompresion[ 2] + ").length; _k++){/**/" iterable = "var " + descompresion[ 0] + "=" + descompresion[2] + "[_k];" iterable2 = "var " + descompresion[ 0] + "= Object.keys(" + descompresion[2] + ")[_k];" lineas[k] = lineas[ k][: i] + bucle + cond1 + iterable + cond2 + iterable2 + "}" + lineas[ k][f + 1:] elif "try:" in elem: lineas[k] = elem.replace("try:", "try{") elem = lineas[k] abierta = True elif "pass" in elem: lineas[k] = elem.replace("pass", "}//pass") elem = lineas[k] abierta = False elif lineas[k].strip() != "": lineas[k] += ";" elif len(AnteriorIdentacion) > len( identacion) and abierta == True: if "}" not in lineas[k]: lineas[k] += "}" if not lineas[k].strip().endswith("{") and not lineas[k].strip( ).endswith("}") and not lineas[k].strip().endswith( ";") and lineas[k].strip() != "": lineas[k] += ";" elem = lineas[k] if " if " in elem and " else " in elem: if elem.find("=") > elem.find(" if "): codigo = elem[len("str("):-1] valor1 = codigo[:codigo.find(" if ") - len(" if ")] condicion = codigo[codigo. find(" if "):codigo.find(" else ") - len(" else ")] valor2 = codigo[codigo.find(" else "):] lineas[ k] = variable + "str(" + condicion + "?" + valor1 + ":" + valor2 + ");" else: variable = elem[:elem.find("str(")] codigo = elem[elem.find("str(") + len("str("):] i = elem.rfind("(", elem.find(" if ")) f = elem.find(")", elem.find(" else ")) bloque = elem[i + 1:f] valor1 = bloque[:bloque.find(" if ")] condicion = bloque[bloque.find(" if ") + len(" if "):bloque.find(" else ")] valor2 = bloque[bloque.find(" else ") + len(" else "):] lineas[ k] = variable + "str((" + condicion + ")?" + valor1 + ":" + valor2 + ");" widget = "\n".join(lineas) self.widget = widget return widget
def Builder(file): f=open(file,"r") script=f.read() f.close() lineas=script.split("\n") inclusiones={} codigo="" lwidget=[] c4=0 for i,linea in enumerate(lineas): print "\n" print "linea:",[linea] if linea[:len("#:include ")]=="#:include ": if "force " in linea[len("#:include "):]: pass else: includes=linea[len("#:include "):].split(",") for elem in includes: f=open(elem,"r") inclusion=f.read().split("\n") f.close() inicio=True widget="" for incluir in inclusion: if incluir!="": if ">:" in incluir: lwidget.append(incluir.replace("<","").replace(">","")+"\n") widget=incluir.replace("<","").replace(">","") if inicio==False: inclusiones[widget]=temp lwidget=[] else: inicio=False else: lwidget.append(incluir.replace("<","").replace(">","")+"\n") else: lwidget.append(incluir+"\n") if lwidget!=[]: inclusiones[widget]=lwidget for elem in inclusiones: if elem in linea: c=i+1 #print c4 c4+=1 if c<len(lineas): tab=zu.getTab(lineas[c]) tab2=zu.getTab(lineas[i]) principio=0 inclusion="" while tab==zu.getTab(lineas[c]): if ":" in lineas[c] and ":" != lineas[c]: c2=1 v=lineas[c].split(":") vez=[] while c2<len(lwidget): lwidget=copy.copy(inclusiones[elem]) lwidget[c2]=tab2+lwidget[c2] if v[0]== lineas[c][:len(v[0])] and v[0]==lwidget[c2][:len(v[0])] and lineas[c][:len(v[0])] not in vez : #print [lineas[c][:len(v[0])]]," ",[lwidget[c2][:len(v[0])]] inclusion+="#"+lineas[c]+"\n" vez.append(tab2+lwidget[c2][:len(v[0])]) #print vez else: inclusion+="@"+lwidget[c2] """ if v[0]== lineas[c][:len(v[0])] and v[0]==lwidget[c2][:len(v[0])] and lineas[c][:len(v[0])] not in vez : print [lwidget[c2]]," ",[lineas[c]] inclusion+="@"+lineas[c] vez.append(lineas[c][:len(v[0])]) print inclusion else: inclusion+="@@"+lwidget[c2] """ c2+=1 c+=1 i=c lineas[i]=zu.getTab(linea)+elem+"\n"+inclusion codigo+=lineas[i]+"\n" return codigo
def convertir(t=None,file=None,d={},debug=False): try: import intervalor.intervalo as i import sys import zu def incluir(widget,data={},folder="widgets/",tab="",convertir=convertir): global __builtins__ open=__builtins__["open"] dir=__builtins__["dir"] str=__builtins__["str"] z=__builtins__["__import__"]("lib.ztec") zu=z.ztec.zu a=open(folder+widget+".zkv","r") #dentro de zkv s=a.read() a.close() c=0 return "#"+widget+":\n"+zu.tabular(convertir(s,file=widget,d=data),tab) if file!=None and t==None: f=open(t,"r") t=f.read() f.close() elif t==None and file==None: pass t+="\n" cpython=True ultimo=0 #nota si dice list index out range estar pendiente que el embebido este dentro de #etiquestas por ejemplo <div></<div> PYTHON,ex=i.getConjuntos(t,["{{","}}"]) HTML=i.borrarAll([0,len(t)],PYTHON) #"hola mundo" # 0123456789 e=[[17, 40], [41, 65]] con=[0,len(t)] #print "==========================" #print "conjuntos: ",conjuntos #print i.mostrarConjuntos(t,[conjuntos]) #print "---------------------------" #print "exclusiones: ",exclusiones #print i.mostrarConjuntos(t,exclusiones) l=[] #print "HTML ", HTML #print "python ",PYTHON iden="" if "{{" in t and "}}" in t: if type(HTML[0])==list: for html in HTML: giden="" if t[html[0]:html[1]][0]=='"' or t[html[0]:html[1]][-1]=='"': c0=-1 while c0>-len(t[html[0]:html[1]]) and (t[html[0]:html[1]][c0]=="\t" or t[html[0]:html[1]][c0]==" "): giden+=t[html[0]:html[1]][c0] c0-=1 codhtml="zkv+= '''"+t[html[0]:html[1]]+"'''"+"\n" elif t[html[0]:html[1]][0]=="'" or t[html[0]:html[1]][-1]=="'": c0=-1 while c0>-len(t[html[0]:html[1]]) and (t[html[0]:html[1]][c0]=="\t" or t[html[0]:html[1]][c0]==" "): giden+=t[html[0]:html[1]][c0] c0-=1 codhtml='zkv+= """'+t[html[0]:html[1]]+'"""'+"\n" else: c0=-1 while c0>-len(t[html[0]:html[1]]) and (t[html[0]:html[1]][c0]=="\t" or t[html[0]:html[1]][c0]==" "): giden+=t[html[0]:html[1]][c0] c0-=1 codhtml="zkv+= '''"+t[html[0]:html[1]]+"'''"+"\n" if codhtml!="zkv+= ''": l.append(iden+codhtml) for python in PYTHON: if html[1]==python[0]: codpython=t[python[0]:python[1]][2:-2]+"\n" tab=zu.getTab(codpython) lfor=len("for ") lwhile=len("while ") lif=len("if ") lelse=len("else:") ltry=len("try:") lexcept=len("except ") lelif=len("elif ") lpass=len("pass") lprint=len("print ") lreturn=len("return ") lincluir=len("incluir(") if tab=="": if codpython[0:lfor]=="for ": l.append(iden+codpython) iden+=" " elif codpython[0:lwhile]=="while ": l.append(iden+codpython) iden+=" " elif codpython[0:lif]=="if ": l.append(iden+codpython) iden+=" " elif "else:" in codpython[0:lelse]: iden=iden[:-2] l.append(iden+codpython) iden+=" " elif "try:" in codpython[0:ltry]: l.append(iden+codpython) iden+=" " elif "except" in codpython[0:lexcept]: iden=iden[:-2] l.append(iden+codpython) iden+=" " elif codpython[0:lelif]=="elif ": iden=iden[:-2] l.append(iden+codpython) iden+=" " elif codpython[0:lpass]=="pass" and codpython=="pass\n": l.append(iden+codpython) iden=iden[:-2] elif codpython[0:lreturn]=="return ": l.append(iden+codpython) iden=iden[:-2] elif codpython[0:lprint]=="print ": if "incluir(" in codpython: ini=codpython.find("incluir(") fin=codpython.find(")",ini) _c=ini o=0 d2="{}" while _c<fin: if codpython[_c]=="{": if o==0: dini=_c o+=1 elif codpython[_c]=="}": o-=1 if o==0: d2=codpython[dini:_c+1] _c+=1 l.insert(-1,iden+"data="+d2+"\n") codpython= codpython[:codpython.find(")",ini)]+",tab='"+giden+"'"+codpython[codpython.find(")",ini):] codpython[:codpython.find(")",ini)]+",tab='"+giden+"'"+codpython[codpython.find(")",ini):] l[-1]=l[-1][:-1]+"+"+iden+""+"str("+codpython.replace("print ","")[:-1]+")\n" #iden=iden[:-2] elif codpython[:1]=="=" and codpython[:2]!="=": l[-1]=l[-1][:-1]+"+"+iden+"str("+codpython.replace("print ","")[:-1]+")\n" #iden=iden[:-2] else: l.append(iden+codpython) else: liden=len(iden) if codpython[liden:liden+lfor]=="for ": l.append(iden+codpython) iden+=" " elif codpython[liden:liden+lwhile]=="while ": l.append(iden+codpython) iden+=" " elif codpython[liden:liden+lif]=="if ": l.append(iden+codpython) iden+=" " elif "else:" in codpython[liden-2:liden+lelse]: iden=iden[:-2] l.append(iden+codpython) iden+=" " elif "try:" in codpython[liden:liden+ltry]: l.append(iden+codpython) iden+=" " elif "except" in codpython[liden-2:liden+lexcept]: iden=iden[:-2] l.append(iden+codpython) iden+=" " elif codpython[liden-2:liden+lelif]=="elif ": iden=iden[:-2] l.append(iden+codpython) iden+=" " """ elif codpython[liden-2:liden+lprint]=="print ": print "siiii" #iden=iden[:-2] l.append(iden+codpython) #iden+=" " """ elif codpython[:lpass]=="pass" and codpython=="pass\n": l.append(iden+codpython) iden=iden[:-2] elif codpython[liden:lreturn]=="return ": l.append(iden+codpython) iden=iden[:-2] else: l.append(iden+codpython) else: l.append(t[HTML[0]:HTML[1]]) #aqui esta la verdadera data txt="\nfile='"+str(file)+"'\ndata="+str(d)+"\nzkv=''\n" for elem in l: txt+=elem f2=open("log4.kv","w") f2.write(txt) f2.close() exec(txt) l=zkv.split("\n") c2=0 ncod="" for elem in l: c=0 e="" while c<len(elem) and elem!="" and (elem[c]==" " or elem[c]=="\t"): c+=1 if c!=len(elem): if elem[0]=="\t": e=elem[1:]+"\n" else: e=elem+"\n" ncod+=e c2+=1 return zkv else: return t except Exception,e: print "Error al convertir" print "Quizas son parametros que no has pasado en la data" print e
def Builder(file): f=open(file,"r") script=f.read() f.close() lineas=script.split("\n") inclusiones={} codigo="" c=0 piezas={} hechos=[] tigger=True #fase de lectura while c<len(lineas): linea_current=lineas[c] if "#:include " in linea_current and "#:include force " not in linea_current: pieza=linea_current[len("#:include "):].split(",") for elem in pieza: f=open(elem,"r") pieza_cod2=convertir(f.read()) pieza_cod=pieza_cod2.split("\n") f.close() f=open("logs/log.kv","w") f.write(pieza_cod2) f.close() c2=0 widget="" widgets=[] while c2<len(pieza_cod): if ">:" in pieza_cod[c2] and "<" in pieza_cod[c2]: tmp_widget=pieza_cod[c2].replace(">:",":").replace("<","") if "@" in tmp_widget: tmp_widget=tmp_widget[:tmp_widget.find("@")]+":" piezas[tmp_widget]=[] widget=tmp_widget else: if widget!="": if "@" in widget: widget=widget[:widget.find("@")]+":" piezas[widget].append(pieza_cod[c2]) widgets.append(widget) c2+=1 else: tab=zu.getTab(linea_current) temp_piezas=copy.copy(piezas) for elem in temp_piezas: if elem in linea_current and linea_current.replace("\n","").replace("\t","".replace(" ",""))==elem: c3=0 wcod="" while c3<len(temp_piezas[elem]): linea_widget_current=temp_piezas[elem] c4=c+1 tab2= zu.getTab(lineas[c4]) #print "#################" #print [tab2],[lineas[c4]] while tab2 == zu.getTab(lineas[c4]): linea_current=lineas[c4] if ":" in linea_current and ":" != linea_current: v=linea_current.split(":") linea_widget_current=tab+temp_piezas[elem][c3] if linea_widget_current[:len(v[0])]== linea_current[:len(v[0])] and linea_current[:len(v[0])] not in hechos: temp_piezas[elem][c3]=linea_current[len(tab):] hechos.append(linea_current[:len(v[0])]) c4+=1 wcod+=tab+temp_piezas[elem][c3]+"\n" c3+=1 codigo+=wcod c=c4-1 else: if len(codigo)>=len(lineas[c]+"\n"): if codigo[-len(lineas[c]+"\n"):]!=lineas[c]+"\n": codigo+=lineas[c]+"\n" else: codigo+=lineas[c]+"\n" c+=1 return codigo