def Function(args, source): # Leer código QS embebido en Source # asumir que es una funcion anónima, tal que: # -> function($args) { source } # compilar la funcion y devolver el puntero qs_source = """ function anon(%s) { %s } """ % (args, source) print("Compilando QS en línea: ", qs_source) from pineboolib.flparser import flscriptparse from pineboolib.flparser import postparse from pineboolib.flparser.pytnyzer import write_python_file, string_template import io prog = flscriptparse.parse(qs_source) tree_data = flscriptparse.calctree(prog, alias_mode=0) ast = postparse.post_parse(tree_data) tpl = string_template f1 = io.StringIO() write_python_file(f1, ast, tpl) pyprog = f1.getvalue() print("Resultado: ", pyprog) glob = {} loc = {} exec(pyprog, glob, loc) # ... y lo peor es que funciona. W-T-F. return loc["anon"]
def Function(args, source): # Leer código QS embebido en Source # asumir que es una funcion anónima, tal que: # -> function($args) { source } # compilar la funcion y devolver el puntero qs_source = """ function anon(%s) { %s } """ % (args, source) print("Compilando QS en línea: ", qs_source) from pineboolib.flparser import flscriptparse from pineboolib.flparser import postparse from pineboolib.flparser.pytnyzer import write_python_file, string_template import io prog = flscriptparse.parse(qs_source) tree_data = flscriptparse.calctree(prog, alias_mode=0) ast = postparse.post_parse(tree_data) tpl = string_template f1 = io.StringIO() write_python_file(f1, ast, tpl) pyprog = f1.getvalue() print("Resultado: ", pyprog) glob = {} loc = {} exec(pyprog, glob, loc) # ... y lo peor es que funciona. W-T-F. # return loc["anon"] return getattr(loc["FormInternalObj"], "anon")
def Function(*args): import importlib import sys as python_sys # Leer código QS embebido en Source # asumir que es una funcion anónima, tal que: # -> function($args) { source } # compilar la funcion y devolver el puntero arguments = args[:len(args)-1] source = args[len(args) -1] qs_source = """ function anon(%s) { %s } """ % (", ".join(arguments), source) #print("Compilando QS en línea: ", qs_source) from pineboolib.flparser import flscriptparse from pineboolib.flparser import postparse from pineboolib.flparser.pytnyzer import write_python_file import io prog = flscriptparse.parse(qs_source) tree_data = flscriptparse.calctree(prog, alias_mode=0) ast = postparse.post_parse(tree_data) dest_filename = "%s/anon.py" % aqApp.tmp_dir() #f1 = io.StringIO() if os.path.exists(dest_filename): os.remove(dest_filename) f1 = open(dest_filename, "w", encoding="UTF-8") write_python_file(f1, ast) f1.close() mod = None module_path = "tempdata.anon" if module_path in python_sys.modules: mod = importlib.reload(python_sys.modules[module_path]) else: mod = importlib.import_module(module_path) return mod.FormInternalObj().anon
def execute(options, args): if options.optdebug: print(options, args) if options.full: execpython = options.exec_python options.exec_python = False options.full = False options.toxml = True if options.verbose: print("Pass 1 - Parse and write XML file . . .") try: execute(options,args) except Exception: print("Error parseando:"); print(traceback.format_exc()) options.toxml = False options.topython = True if options.verbose: print("Pass 2 - Pythonize and write PY file . . .") try: execute(options,[ arg+".xml" for arg in args]) except Exception: print("Error convirtiendo:"); print(traceback.format_exc()) if execpython: options.exec_python = execpython if options.verbose: print("Pass 3 - Test PY file load . . .") options.topython = False try: execute(options,[ (arg+".xml.py").replace(".qs.xml.py",".qs.py") for arg in args]) except Exception: print("Error al ejecutar Python:"); print(traceback.format_exc()) print("Done.") elif options.exec_python: from . import qsatype for filename in args: realpath = os.path.realpath(filename) path, name = os.path.split(realpath) if not os.path.exists(realpath): print("Fichero no existe: %s" % name) continue mod = Module(name, path) if not mod.loadModule(): print("Error cargando modulo %s" % name) elif options.topython: from .pytnyzer import pythonize import io if options.cache: args = [ x for x in args if not os.path.exists((x+".py").replace(".qs.xml.py",".qs.py")) or os.path.getmtime(x) > os.path.getctime((x+".py").replace(".qs.xml.py",".qs.py")) ] nfs = len(args) for nf, filename in enumerate(args): bname = os.path.basename(filename) if options.storepath: destname = os.path.join(options.storepath,bname+".py") else: destname = filename+".py" destname = destname.replace(".qs.xml.py",".qs.py") if not os.path.exists(filename): print("Fichero %r no encontrado" % filename) continue if options.verbose: sys.stdout.write("Pythonizing File: %-35s . . . . (%.1f%%) \r" % (bname,100.0*(nf+1.0)/nfs)) if options.verbose: sys.stdout.flush(); old_stderr = sys.stdout stream = io.StringIO() sys.stdout = stream try: pythonize(filename, destname, destname + ".debug") except Exception: print("Error al pythonificar %r:" % filename) print(traceback.format_exc()) sys.stdout = old_stderr text = stream.getvalue() if len(text) > 2: print("%s: " % bname + ("\n%s: " % bname).join(text.splitlines())) else: if options.cache: args = [ x for x in args if not os.path.exists(x+".xml") or os.path.getmtime(x) > os.path.getctime(x+".xml")] nfs = len(args) for nf, filename in enumerate(args): bname = os.path.basename(filename) if options.verbose: sys.stdout.write("Parsing File: %-35s . . . . (%.1f%%) " % (bname,100.0*(nf+1.0)/nfs)) if options.verbose: sys.stdout.flush(); try: filecontent = open(filename,"r", encoding="latin-1").read() except Exception as e: print("Error: No se pudo abrir fichero %-35s \n" % (repr(filename)), e) continue prog = flscriptparse.parse(filecontent) sys.stdout.write("\r"); if not prog: print("Error: No se pudo abrir %-35s \n" % (repr(filename))) continue if prog["error_count"] > 0: print("Encontramos %d errores parseando: %-35s \n" % (prog["error_count"], repr(filename))) continue if options.toxml == False: # Si no se quiere guardar resultado, no hace falta calcular mas continue tree_data = None try: tree_data = flscriptparse.calctree(prog, alias_mode = 0) except Exception: print("Error al convertir a XML %r:" % bname) print("\n".join(traceback.format_exc().splitlines()[-7:])) if not tree_data: print("No se pudo parsear %-35s \n" % (repr(filename))) continue ast = post_parse(tree_data) if ast is None: print("No se pudo analizar %-35s \n" % (repr(filename))) continue if options.storepath: destname = os.path.join(options.storepath,bname+".xml") else: destname = filename+".xml" f1 = open(destname,"wb") f1.write(etree.tostring(ast, pretty_print = True)) f1.close()
def execute(options, args): if options.optdebug: print(options, args) if options.full: options.full = False options.toxml = True print("Pass 1 - Parse and write XML file . . .") execute(options,args) options.toxml = False options.topython = True print("Pass 2 - Pythonize and write PY file . . .") execute(options,[ arg+".xml" for arg in args]) options.topython = False options.exec_python = True #print "Pass 3 - Test PY file load . . ." #execute(options,[ (arg+".xml.py").replace(".qs.xml.py",".py") for arg in args]) print("Done.") elif options.exec_python: from . import qsatype for filename in args: realpath = os.path.realpath(filename) path, name = os.path.split(realpath) mod = Module(name, path) if mod.loadModule(): print(mod.module) print(mod.module.form) else: print("Error cargando modulo %s" % name) elif options.topython: from .pytnyzer import pythonize for filename in args: bname = os.path.basename(filename) if options.storepath: destname = os.path.join(options.storepath,bname+".py") else: destname = filename+".py" destname = destname.replace(".qs.xml.py",".py") pythonize(filename, destname) else: nfs = len(args) for nf, filename in enumerate(args): bname = os.path.basename(filename) sys.stdout.write("Parsing File: %-35s . . . . (%.1f%%) " % (bname,100.0*(nf+1.0)/nfs)) sys.stdout.flush(); prog = flscriptparse.parse(open(filename,"r", encoding="latin-1").read()) sys.stdout.write("\r"); if not prog: print("Error: No se pudo abrir %-35s \n" % (repr(filename))) continue if prog["error_count"] > 0: print("Encontramos %d errores parseando: %-35s \n" % (prog["error_count"], repr(filename))) continue if options.toxml == False: # Si no se quiere guardar resultado, no hace falta calcular mas continue tree_data = flscriptparse.calctree(prog, alias_mode = 0) if not tree_data: print("No se pudo parsear %-35s \n" % (repr(filename))) continue ast = post_parse(tree_data) if ast is None: print("No se pudo analizar %-35s \n" % (repr(filename))) continue if options.storepath: destname = os.path.join(options.storepath,bname+".xml") else: destname = filename+".xml" f1 = open(destname,"wb") f1.write(etree.tostring(ast, pretty_print = True)) f1.close()
def execute(options, args): if options.optdebug: print(options, args) if options.full: options.full = False options.toxml = True print("Pass 1 - Parse and write XML file . . .") execute(options, args) options.toxml = False options.topython = True print("Pass 2 - Pythonize and write PY file . . .") execute(options, [arg + ".xml" for arg in args]) options.topython = False options.exec_python = True #print "Pass 3 - Test PY file load . . ." #execute(options,[ (arg+".xml.py").replace(".qs.xml.py",".py") for arg in args]) print("Done.") elif options.exec_python: from . import qsatype for filename in args: realpath = os.path.realpath(filename) path, name = os.path.split(realpath) mod = Module(name, path) if mod.loadModule(): print(mod.module) print(mod.module.form) else: print("Error cargando modulo %s" % name) elif options.topython: from .pytnyzer import pythonize for filename in args: bname = os.path.basename(filename) if options.storepath: destname = os.path.join(options.storepath, bname + ".py") else: destname = filename + ".py" destname = destname.replace(".qs.xml.py", ".py") pythonize(filename, destname) else: nfs = len(args) for nf, filename in enumerate(args): bname = os.path.basename(filename) sys.stdout.write( "Parsing File: %-35s . . . . (%.1f%%) " % (bname, 100.0 * (nf + 1.0) / nfs)) sys.stdout.flush() prog = flscriptparse.parse( open(filename, "r", encoding="latin-1").read()) sys.stdout.write("\r") if not prog: print("Error: No se pudo abrir %-35s \n" % (repr(filename))) continue if prog["error_count"] > 0: print("Encontramos %d errores parseando: %-35s \n" % (prog["error_count"], repr(filename))) continue if options.toxml == False: # Si no se quiere guardar resultado, no hace falta calcular mas continue tree_data = flscriptparse.calctree(prog, alias_mode=0) if not tree_data: print("No se pudo parsear %-35s \n" % (repr(filename))) continue ast = post_parse(tree_data) if ast is None: print("No se pudo analizar %-35s \n" % (repr(filename))) continue if options.storepath: destname = os.path.join(options.storepath, bname + ".xml") else: destname = filename + ".xml" f1 = open(destname, "wb") f1.write(etree.tostring(ast, pretty_print=True)) f1.close()