Hello!
Thank to Alan Etkin in the spanish group for:
           [http://groups.google.com/group/web2py/browse_thread/thread/
b239c9aaf3b47b2b/1dc875d27dab5c87?lnk=gst&q=textarea
+input#1dc875d27dab5c87]

I asked [https://groups.google.com/forum/#!topic/web2py/pcHQx0gV0mo]

This is the solution
    ## record's id
    id = 1
    code = db.codigo[id].el_codigo
    ## the solution
    code = code.replace('\r','')

Question:
    I feel that this code works but is not quite correct!?

In case you want to reproduce the experiment:

1. Copy to db.py:
db.define_table('codigo', Field('el_codigo', 'text'))
2. Open appadmin and in db.codigo insert:
##
from gluon import *
def testFunc(globales=''):
    ##redirect('testImportCode')
    ##redirect('index')
    cont = globales['testtable']()
    return dict(cont=cont)
    ##return "spam from database! works!"
class testClass:
    def testMethod(self):
        return "eggs from database"
3.Remember the recors's id
4. Copy to  default.py:
##
def farg():
    code = \
    """
def testFunc():
    return "spam from farg!"
class testClass:
    def testMethod(self):
        return "eggs from farg!"
    """
    return code
def testtable():
    table = SQLTABLE(db(db.codigo.id>0).select())
    return table
##
## call this function
##
def testImportCodeFromDataBase():
    # Example
    ## change this id
    id = 1
    code = db.codigo[id].el_codigo
    ## the solution
    code = code.replace('\r','')
    ## experimen
    ##code = code.replace('\r\n','<-br->')

    m = importCode(code,"test")

    ## Atention:
    ##
    ## import copy
    ## arg = copy.copy(globals())

    arg = globals().copy()
    spam  = m.testFunc(globales=arg)

    o = m.testClass()
    eggs = o.testMethod()

    cont = DIV(P(m), P(spam), P(o), P(eggs))
    ##cont = code
    return cont

def importCode(code,name,add_to_sys_modules=0):
    """
    Import dynamically generated code as a module. code is the
    object containing the code (a string, a file handle or an
    actual compiled code object, same types as accepted by an
    exec statement). The name is the name to give to the module,
    and the final argument says wheter to add it to sys.modules
    or not. If it is added, a subsequent import statement using
    name will return this module. If it is not added to sys.modules
    import will try to load it in the normal fashion.

    import foo
    is equivalent to
    foofile = open("/path/to/foo.py")
    foo = importCode(foofile,"foo",1)

    Returns a newly generated module.
    """
    import sys
    import imp
    module = imp.new_module(name)
    exec code in module.__dict__
    if add_to_sys_modules:
        sys.modules[name] = module
    return module

5. run [...]/default/testImportCodeFromDataBase

Can someone switch on the light.PLEASE.
Lazarof

Reply via email to