Ejemplo n.º 1
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    c = environment['request'].controller
    f = environment['request'].function
    cpath = os.path.join(folder, 'compiled')
    if os.path.exists(cpath):
        for model in listdir(cpath, '^models_\w+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
        path = os.path.join(cpath, 'models')
        models = listdir(path, '^\w+\.pyc$', 0, sort=False)
        compiled = True
    else:
        path = os.path.join(folder, 'models')
        models = listdir(path, '^\w+\.py$', 0, sort=False)
        compiled = False
    paths = (path, os.path.join(path, c), os.path.join(path, c, f))
    for model in models:
        if not os.path.split(model)[0] in paths and c != 'appadmin':
            continue
        elif compiled:
            code = read_pyc(model)
        elif is_gae:
            code = getcfs(model, model,
                          lambda: compile2(read_file(model), model))
        else:
            code = getcfs(model, model, None)
        restricted(code, environment, layer=model)
Ejemplo n.º 2
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    c = environment['request'].controller
    f = environment['request'].function
    cpath = os.path.join(folder, 'compiled')
    if os.path.exists(cpath):
        for model in listdir(cpath, '^models_\w+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
        path = os.path.join(cpath, 'models')
        models = listdir(path, '^\w+\.pyc$',0,sort=False)
        compiled=True
    else:
        path = os.path.join(folder, 'models')
        models = listdir(path, '^\w+\.py$',0,sort=False)
        compiled=False
    paths = (path, os.path.join(path,c), os.path.join(path,c,f))
    for model in models:
        if not os.path.split(model)[0] in paths and c!='appadmin':
            continue
        elif compiled:
            code = read_pyc(model)
        elif is_gae:
            code = getcfs(model, model,
                          lambda: compile2(read_file(model), model))
        else:
            code = getcfs(model, model, None)
        restricted(code, environment, layer=model)
Ejemplo n.º 3
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment["request"].folder
    c = environment["request"].controller
    f = environment["request"].function
    cpath = pjoin(folder, "compiled")
    if os.path.exists(cpath):
        for model in listdir(cpath, "^models_\w+\.pyc$", 0):
            restricted(read_pyc(model), environment, layer=model)
        path = pjoin(cpath, "models")
        models = listdir(path, "^\w+\.pyc$", 0, sort=False)
        compiled = True
    else:
        path = pjoin(folder, "models")
        models = listdir(path, "^\w+\.py$", 0, sort=False)
        compiled = False
    n = len(path) + 1
    for model in models:
        regex = environment["response"].models_to_run
        if isinstance(regex, list):
            regex = re_compile("|".join(regex))
        file = model[n:].replace(os.path.sep, "/").replace(".pyc", ".py")
        if not regex.search(file) and c != "appadmin":
            continue
        elif compiled:
            code = read_pyc(model)
        elif is_gae:
            code = getcfs(model, model, lambda: compile2(read_file(model), model))
        else:
            code = getcfs(model, model, None)
        restricted(code, environment, layer=model)
Ejemplo n.º 4
0
def run_view_in(environment):
    """ 
    executes the view in response.view or generic.html.
    it tries the precompiled views_controller_function.pyc first.
    """

    folder = environment['request'].folder
    response = environment['response']
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        filename = os.path.join(
            path, 'views_%s.pyc' % response.view[:-5].replace('/', '_'))
        if not os.path.exists(filename):
            filename = os.path.join(folder, 'compiled/', 'views_generic.pyc')
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid view',
                       web2py_error='invalid view')
        code = read_pyc(filename)
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'views/', response.view)
        if not os.path.exists(filename):
            response.view = 'generic.html'
        filename = os.path.join(folder, 'views/', response.view)
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid view',
                       web2py_error='invalid view')
        layer = filename
        if is_gae:
            ccode = getcfs(layer, filename, lambda : \
                           compile(parse_template(response.view,
                           os.path.join(folder, 'views/'),
                           context=environment).replace('\r\n', '\n'),
                           layer, 'exec'))
        else:
            ccode = parse_template(response.view,
                                   os.path.join(folder, 'views/'),
                                   context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 5
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled')
    if os.path.exists(path):
        for model in listdir(path, '^models_.+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
    else:
        models = listdir(os.path.join(folder, 'models'), '^\w+\.py$', 0)
        for model in models:
            layer = model
            if is_gae:
                code = getcfs(model, model,
                              lambda: compile2(open(model, 'r').read(), layer))
            else:
                code = getcfs(model, model, None)
            restricted(code, environment, layer)
Ejemplo n.º 6
0
def run_view_in(environment):
    """ 
    executes the view in response.view or generic.html.
    it tries the precompiled views_controller_function.pyc first.
    """

    folder = environment["request"].folder
    response = environment["response"]
    path = os.path.join(folder, "compiled/")
    if os.path.exists(path):
        filename = os.path.join(path, "views_%s.pyc" % response.view[:-5].replace("/", "_"))
        if not os.path.exists(filename):
            filename = os.path.join(folder, "compiled/", "views_generic.pyc")
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid view", web2py_error="invalid view")
        code = read_pyc(filename)
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, "views/", response.view)
        if not os.path.exists(filename):
            response.view = "generic.html"
        filename = os.path.join(folder, "views/", response.view)
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid view", web2py_error="invalid view")
        layer = filename
        if is_gae:
            ccode = getcfs(
                layer,
                filename,
                lambda: compile(
                    parse_template(response.view, os.path.join(folder, "views/"), context=environment).replace(
                        "\r\n", "\n"
                    ),
                    layer,
                    "exec",
                ),
            )
        else:
            ccode = parse_template(response.view, os.path.join(folder, "views/"), context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 7
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled')
    if os.path.exists(path):
        for model in listdir(path, '^models_.+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
    else:
        models = listdir(os.path.join(folder, 'models'), '^\w+\.py$',
                         0)
        for model in models:
            layer = model
            if is_gae:
                code = getcfs(model, model,
                              lambda: compile2(open(model, 'r').read(),layer))
            else:
                code = getcfs(model, model, None)
            restricted(code, environment, layer)
Ejemplo n.º 8
0
def run_models_in(environment):
    """
    runs all models (in the app specified by the current folder) 
    in the environment. it tries precompiled models first.
    """

    folder = environment["request"].folder
    path = os.path.join(folder, "compiled/")
    if os.path.exists(path):
        for model in listdir(path, "^models_.+\.pyc$", 0):
            restricted(read_pyc(model), environment, layer=model)
    else:
        models = listdir(os.path.join(folder, "models/"), "^\w+\.py$", 0)
        for model in models:
            layer = model
            if is_gae:
                code = getcfs(
                    model, model, lambda: compile(open(model, "r").read().replace("\r\n", "\n"), layer, "exec")
                )
            else:
                code = getcfs(model, model, None)
            restricted(code, environment, layer)
Ejemplo n.º 9
0
def run_view_in(environment):
    """
    Executes the view for the requested action.
    The view is the one specified in `response.view` or determined by the url
    or `view/generic.extension`
    It tries the pre-compiled views_controller_function.pyc before compiling it.
    """

    request = environment["request"]
    response = environment["response"]
    view = response.view
    folder = request.folder
    path = pjoin(folder, "compiled")
    badv = "invalid view (%s)" % view
    if response.generic_patterns:
        patterns = response.generic_patterns
        regex = re_compile("|".join(map(fnmatch.translate, patterns)))
        short_action = "%(controller)s/%(function)s.%(extension)s" % request
        allow_generic = regex.search(short_action)
    else:
        allow_generic = False
    if not isinstance(view, str):
        ccode = parse_template(view, pjoin(folder, "views"), context=environment)
        restricted(ccode, environment, "file stream")
    elif os.path.exists(path):
        x = view.replace("/", "_")
        files = ["views_%s.pyc" % x]
        if allow_generic:
            files.append("views_generic.%s.pyc" % request.extension)
        # for backward compatibility
        if request.extension == "html":
            files.append("views_%s.pyc" % x[:-5])
            if allow_generic:
                files.append("views_generic.pyc")
        # end backward compatibility code
        for f in files:
            filename = pjoin(path, f)
            if os.path.exists(filename):
                code = read_pyc(filename)
                restricted(code, environment, layer=filename)
                return
        raise HTTP(404, rewrite.thread.routes.error_message % badv, web2py_error=badv)
    else:
        filename = pjoin(folder, "views", view)
        if not os.path.exists(filename) and allow_generic:
            view = "generic." + request.extension
            filename = pjoin(folder, "views", view)
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badv, web2py_error=badv)
        layer = filename
        if is_gae:
            ccode = getcfs(
                layer,
                filename,
                lambda: compile2(parse_template(view, pjoin(folder, "views"), context=environment), layer),
            )
        else:
            ccode = parse_template(view, pjoin(folder, "views"), context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 10
0
def run_models_in(environment):
    """
    runs all models (in the app specified by the current folder) 
    in the environment. it tries precompiled models first.
    """

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        for model in listdir(path, '^models_.+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
    else:
        models = listdir(os.path.join(folder, 'models/'), '^\w+\.py$', 0)
        for model in models:
            layer = model
            if is_gae:
                code = getcfs(model, model, lambda : \
                              compile(open(model, 'r'
                              ).read().replace('\r\n', '\n'), layer,
                              'exec'))
            else:
                code = getcfs(model, model, None)
            restricted(code, environment, layer)
Ejemplo n.º 11
0
def run_view_in(environment):
    """
    Executes the view for the requested action.
    The view is the one specified in `response.view` or determined by the url
    or `view/generic.extension`
    It tries the pre-compiled views_controller_function.pyc before compiling it.
    """

    request = environment['request']
    response = environment['response']
    folder = request.folder
    path = os.path.join(folder, 'compiled')
    badv = 'invalid view (%s)' % response.view
    if not isinstance(response.view, str):
        ccode = parse_template(response.view, os.path.join(folder, 'views'),
                               context=environment)
        restricted(ccode, environment, 'file stream')
    elif os.path.exists(path):
        x = response.view.replace('/', '_')
        if request.extension == 'html':
            # for backward compatibility
            files = [os.path.join(path, 'views_%s.pyc' % x),
                     os.path.join(path, 'views_%s.pyc' % x[:-5]),
                     os.path.join(path, 'views_generic.html.pyc'),
                     os.path.join(path, 'views_generic.pyc')]
        else:
            files = [os.path.join(path, 'views_%s.pyc' % x),
                     os.path.join(path, 'views_generic.%s.pyc'
                                  % request.extension)]
        for filename in files:
            if os.path.exists(filename):
                code = read_pyc(filename)
                restricted(code, environment, layer=filename)
                return
        raise HTTP(400,
                   rewrite.thread.routes.error_message % badv,
                   web2py_error=badv)
    else:
        filename = os.path.join(folder, 'views', response.view)
        if not os.path.exists(filename):
            response.view = 'generic.' + request.extension
        filename = os.path.join(folder, 'views', response.view)
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.thread.routes.error_message % badv,
                       web2py_error=badv)
        layer = filename
        if is_gae:
            ccode = getcfs(layer, filename,
                           lambda: compile2(parse_template(response.view,
                                            os.path.join(folder, 'views'),
                                            context=environment),layer))
        else:
            ccode = parse_template(response.view,
                                   os.path.join(folder, 'views'),
                                   context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 12
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment["request"].folder
    path = pjoin(folder, "compiled")
    badc = "invalid controller (%s/%s)" % (controller, function)
    badf = "invalid function (%s/%s)" % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, "controllers_%s_%s.pyc" % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badf, web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == "_TEST":
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first

        paths = (global_settings.gluon_parent, abspath("site-packages", gluon=True), abspath("gluon", gluon=True), "")
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badc, web2py_error=badc)
        environment["__symbols__"] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(404, rewrite.thread.routes.error_message % badc, web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404, rewrite.thread.routes.error_message % badf, web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ":" + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment["response"]
    vars = response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars, unicode):
        vars = vars.encode("utf8")
    elif hasattr(vars, "xml") and callable(vars.xml):
        vars = vars.xml()
    return vars
Ejemplo n.º 13
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    c = environment['request'].controller
    f = environment['request'].function
    cpath = pjoin(folder, 'compiled')
    if os.path.exists(cpath):
        for model in listdir(cpath, '^models_\w+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
        path = pjoin(cpath, 'models')
        models = listdir(path, '^\w+\.pyc$',0,sort=False)
        compiled=True
    else:
        path = pjoin(folder, 'models')
        models = listdir(path, '^\w+\.py$',0,sort=False)
        compiled=False
    n = len(path) + 1
    for model in models:
        regex = environment['response'].models_to_run
        if isinstance(regex, list):
            regex = re_compile('|'.join(regex))
        file = model[n:].replace(os.path.sep, '/').replace('.pyc', '.py')
        if not regex.search(file) and c!= 'appadmin':
            continue
        elif compiled:
            code = read_pyc(model)
        elif is_gae:
            code = getcfs(model, model,
                          lambda: compile2(read_file(model), model))
        else:
            code = getcfs(model, model, None)
        restricted(code, environment, layer=model)
Ejemplo n.º 14
0
def run_models_in(environment):
    """
    Runs all models (in the app specified by the current folder)
    It tries pre-compiled models first before compiling them.
    """

    folder = environment['request'].folder
    c = environment['request'].controller
    f = environment['request'].function
    cpath = pjoin(folder, 'compiled')
    if os.path.exists(cpath):
        for model in listdir(cpath, '^models_\w+\.pyc$', 0):
            restricted(read_pyc(model), environment, layer=model)
        path = pjoin(cpath, 'models')
        models = listdir(path, '^\w+\.pyc$', 0)
        compiled = True
    else:
        path = pjoin(folder, 'models')
        models = listdir(path, '^\w+\.py$', 0)
        compiled = False
    n = len(path) + 1
    for model in models:
        regex = environment['response'].models_to_run
        if isinstance(regex, list):
            regex = re_compile('|'.join(regex))
        file = model[n:].replace(os.path.sep, '/').replace('.pyc', '.py')
        if not regex.search(file) and c != 'appadmin':
            continue
        elif compiled:
            code = read_pyc(model)
        elif is_gae:
            code = getcfs(model, model,
                          lambda: compile2(read_file(model), model))
        else:
            code = getcfs(model, model, None)
        restricted(code, environment, layer=model)
Ejemplo n.º 15
0
def run_controller_in(controller, function, environment):
    """
    runs the controller.function() (for the app specified by 
    the current folder)  in the environment. 
    it tries precompiled controller_function.pyc first.
    """

    # if compiled should run compiled!

    folder = environment["request"].folder
    path = os.path.join(folder, "compiled/")
    if os.path.exists(path):
        filename = os.path.join(path, "controllers_%s_%s.pyc" % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid function", web2py_error="invalid function")
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == "_TEST":
        filename = os.path.join(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid controller", web2py_error="invalid controller")
        environment["__symbols__"] = environment.keys()
        code = open(filename, "r").read()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, "controllers/%s.py" % controller)
        if not os.path.exists(filename):
            raise HTTP(400, error_message_custom % "invalid controller", web2py_error="invalid controller")
        code = open(filename, "r").read()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400, error_message_custom % "invalid function", web2py_error="invalid function")
        code = """%s

response._vars=response._caller(%s)
""" % (
            code,
            function,
        )
        if is_gae:
            layer = filename + ":" + function
            code = getcfs(layer, filename, lambda: compile(code.replace("\r\n", "\n"), layer, "exec"))
        restricted(code, environment, filename)
    response = environment["response"]
    if response.postprocessing:
        for p in response.postprocessing:
            response._vars = p(response._vars)
    if type(response._vars) == types.StringType:
        response.body = response._vars
    elif type(response._vars) == types.GeneratorType:
        response.body = response._vars
    elif type(response._vars) != types.DictType:
        response.body = str(response._vars)
Ejemplo n.º 16
0
def run_controller_in(controller, function, environment):
    """
    runs the controller.function() (for the app specified by 
    the current folder)  in the environment. 
    it tries precompiled controller_function.pyc first.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        filename = os.path.join(
            path, 'controllers_%s_%s.pyc' % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        environment['__symbols__'] = environment.keys()
        code = open(filename, 'r').read()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        code = open(filename, 'r').read()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400,
                       error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        code = '''%s

response._vars=response._caller(%s)
''' % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda : \
                          compile(code.replace('\r\n', '\n'), layer,
                          'exec'))
        restricted(code, environment, filename)
    response = environment['response']
    if response.postprocessing:
        for p in response.postprocessing:
            response._vars = p(response._vars)
    if type(response._vars) == types.StringType:
        response.body = response._vars
    elif type(response._vars) == types.GeneratorType:
        response.body = response._vars
    elif type(response._vars) != types.DictType:
        response.body = str(response._vars)
Ejemplo n.º 17
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!
    folder = environment['request'].folder
    path = pjoin(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, 'controllers_%s_%s.pyc'
                         % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first
        paths = (global_settings.gluon_parent, abspath(
            'site-packages', gluon=True), abspath('gluon', gluon=True), '')
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars = response._vars
    if response.postprocessing:
        vars = reduce(lambda vars, p: p(vars), response.postprocessing, vars)
    if isinstance(vars, unicode):
        vars = vars.encode('utf8')
    elif hasattr(vars, 'xml') and callable(vars.xml):
        vars = vars.xml()
    return vars
Ejemplo n.º 18
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!
    folder = environment['request'].folder
    path = pjoin(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = pjoin(path, 'controllers_%s_%s.pyc'
                                 % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        # TESTING: adjust the path to include site packages
        from settings import global_settings
        from admin import abspath, add_path_first
        paths = (global_settings.gluon_parent, abspath('site-packages', gluon=True),  abspath('gluon', gluon=True), '')
        [add_path_first(path) for path in paths]
        # TESTING END

        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        code = read_file(filename)
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = pjoin(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badc,
                       web2py_error=badc)
        code = read_file(filename)
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename,  lambda: compile2(code,layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars=response._vars
    if response.postprocessing:
        vars = reduce(lambda vars, p: p(vars), response.postprocessing, vars)
    if isinstance(vars,unicode):
        vars = vars.encode('utf8')
    elif hasattr(vars,'xml') and callable(vars.xml):
        vars = vars.xml()
    return vars
Ejemplo n.º 19
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled/')
    if os.path.exists(path):
        filename = os.path.join(path, 'controllers_%s_%s.pyc'
                                 % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        environment['__symbols__'] = environment.keys()
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py'
                                 % controller)
        if not os.path.exists(filename):
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid controller',
                       web2py_error='invalid controller')
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(400,
                       rewrite.params.error_message_custom % 'invalid function',
                       web2py_error='invalid function')
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: \
                          compile(code.replace('\r\n', '\n'), layer,
                          'exec'))
        restricted(code, environment, filename)
    response = environment['response']
    vars=response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars,unicode):
        vars = vars.encode('utf8')
    if hasattr(vars,'xml'):
        vars = vars.xml()
    return vars
Ejemplo n.º 20
0
def run_view_in(environment):
    """
    Executes the view for the requested action.
    The view is the one specified in `response.view` or determined by the url
    or `view/generic.extension`
    It tries the pre-compiled views_controller_function.pyc before compiling it.
    """
    request = environment['request']
    response = environment['response']
    view = response.view
    folder = request.folder
    path = pjoin(folder, 'compiled')
    badv = 'invalid view (%s)' % view
    if response.generic_patterns:
        patterns = response.generic_patterns
        regex = re_compile('|'.join(map(fnmatch.translate, patterns)))
        short_action =  '%(controller)s/%(function)s.%(extension)s' % request
        allow_generic = regex.search(short_action)
    else:
        allow_generic = False
    if not isinstance(view, str):
        ccode = parse_template(view, pjoin(folder, 'views'),
                               context=environment)
        restricted(ccode, environment, 'file stream')
    elif os.path.exists(path):
        x = view.replace('/', '_')
        files = ['views_%s.pyc' % x]
        if allow_generic:
            files.append('views_generic.%s.pyc' % request.extension)
        # for backward compatibility
        if request.extension == 'html':
            files.append('views_%s.pyc' % x[:-5])
            if allow_generic:
                files.append('views_generic.pyc')
        # end backward compatibility code
        for f in files:
            filename = pjoin(path,f)
            if os.path.exists(filename):
                code = read_pyc(filename)
                restricted(code, environment, layer=filename)
                return
        raise HTTP(404,
                   rewrite.THREAD_LOCAL.routes.error_message % badv,
                   web2py_error=badv)
    else:
        filename = pjoin(folder, 'views', view)
        if not os.path.exists(filename) and allow_generic:
            view = 'generic.' + request.extension
            filename = pjoin(folder, 'views', view)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badv,
                       web2py_error=badv)
        layer = filename
        if is_gae:
            ccode = getcfs(layer, filename,
                           lambda: compile2(parse_template(view,
                                            pjoin(folder, 'views'),
                                            context=environment),layer))
        else:
            ccode = parse_template(view,
                                   pjoin(folder, 'views'),
                                   context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 21
0
def run_controller_in(controller, function, environment):
    """
    Runs the controller.function() (for the app specified by
    the current folder).
    It tries pre-compiled controller_function.pyc first before compiling it.
    """

    # if compiled should run compiled!

    folder = environment['request'].folder
    path = os.path.join(folder, 'compiled')
    badc = 'invalid controller (%s/%s)' % (controller, function)
    badf = 'invalid function (%s/%s)' % (controller, function)
    if os.path.exists(path):
        filename = os.path.join(
            path, 'controllers_%s_%s.pyc' % (controller, function))
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badf,
                       web2py_error=badf)
        restricted(read_pyc(filename), environment, layer=filename)
    elif function == '_TEST':
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badc,
                       web2py_error=badc)
        environment['__symbols__'] = environment.keys()
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        code += TEST_CODE
        restricted(code, environment, layer=filename)
    else:
        filename = os.path.join(folder, 'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badc,
                       web2py_error=badc)
        fp = open(filename, 'r')
        code = fp.read()
        fp.close()
        exposed = regex_expose.findall(code)
        if not function in exposed:
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badf,
                       web2py_error=badf)
        code = "%s\nresponse._vars=response._caller(%s)\n" % (code, function)
        if is_gae:
            layer = filename + ':' + function
            code = getcfs(layer, filename, lambda: compile2(code, layer))
        restricted(code, environment, filename)
    response = environment['response']
    vars = response._vars
    if response.postprocessing:
        for p in response.postprocessing:
            vars = p(vars)
    if isinstance(vars, unicode):
        vars = vars.encode('utf8')
    if hasattr(vars, 'xml'):
        vars = vars.xml()
    return vars
Ejemplo n.º 22
0
def run_view_in(environment):
    """
    Executes the view for the requested action.
    The view is the one specified in `response.view` or determined by the url
    or `view/generic.extension`
    It tries the pre-compiled views_controller_function.pyc before compiling it.
    """
    request = environment['request']
    response = environment['response']
    view = response.view
    folder = request.folder
    path = pjoin(folder, 'compiled')
    badv = 'invalid view (%s)' % view
    if response.generic_patterns:
        patterns = response.generic_patterns
        regex = re_compile('|'.join(map(fnmatch.translate, patterns)))
        short_action = '%(controller)s/%(function)s.%(extension)s' % request
        allow_generic = regex.search(short_action)
    else:
        allow_generic = False
    if not isinstance(view, str):
        ccode = parse_template(view, pjoin(folder, 'views'),
                               context=environment)
        restricted(ccode, environment, 'file stream')
    elif os.path.exists(path):
        x = view.replace('/', '_')
        files = ['views_%s.pyc' % x]
        if allow_generic:
            files.append('views_generic.%s.pyc' % request.extension)
        # for backward compatibility
        if request.extension == 'html':
            files.append('views_%s.pyc' % x[:-5])
            if allow_generic:
                files.append('views_generic.pyc')
        # end backward compatibility code
        for f in files:
            filename = pjoin(path, f)
            if os.path.exists(filename):
                code = read_pyc(filename)
                restricted(code, environment, layer=filename)
                return
        raise HTTP(404,
                   rewrite.THREAD_LOCAL.routes.error_message % badv,
                   web2py_error=badv)
    else:
        filename = pjoin(folder, 'views', view)
        if not os.path.exists(filename) and allow_generic:
            view = 'generic.' + request.extension
            filename = pjoin(folder, 'views', view)
        if not os.path.exists(filename):
            raise HTTP(404,
                       rewrite.THREAD_LOCAL.routes.error_message % badv,
                       web2py_error=badv)
        layer = filename
        if is_gae:
            ccode = getcfs(layer, filename,
                           lambda: compile2(parse_template(view,
                                            pjoin(folder, 'views'),
                                            context=environment), layer))
        else:
            ccode = parse_template(view,
                                   pjoin(folder, 'views'),
                                   context=environment)
        restricted(ccode, environment, layer)
Ejemplo n.º 23
0
def run_view_in(environment):
    """
    Executes the view for the requested action.
    The view is the one specified in `response.view` or determined by the url
    or `view/generic.extension`
    It tries the pre-compiled views_controller_function.pyc before compiling it.
    """

    request = environment['request']
    response = environment['response']
    folder = request.folder
    path = os.path.join(folder, 'compiled')
    badv = 'invalid view (%s)' % response.view
    patterns = response.generic_patterns or []
    regex = re.compile('|'.join(fnmatch.translate(r) for r in patterns))
    short_action =  '%(controller)s/%(function)s.%(extension)s' % request
    allow_generic = patterns and regex.search(short_action)
    if not isinstance(response.view, str):
        ccode = parse_template(response.view, os.path.join(folder, 'views'),
                               context=environment)
        restricted(ccode, environment, 'file stream')
    elif os.path.exists(path):
        x = response.view.replace('/', '_')
        files = ['views_%s.pyc' % x]
        if allow_generic:
            files.append('views_generic.%s.pyc' % request.extension)
        # for backward compatibility
        if request.extension == 'html':
            files.append('views_%s.pyc' % x[:-5])
            if allow_generic:
                files.append('views_generic.pyc')
        # end backward compatibility code
        for f in files:
            filename = os.path.join(path,f)
            if os.path.exists(filename):
                code = read_pyc(filename)
                restricted(code, environment, layer=filename)
                return
        raise HTTP(404,
                   rewrite.thread.routes.error_message % badv,
                   web2py_error=badv)
    else:
        filename = os.path.join(folder, 'views', response.view)
        if not os.path.exists(filename) and allow_generic:
            response.view = 'generic.' + request.extension
            filename = os.path.join(folder, 'views', response.view)
        if not os.path.exists(filename):
            # Troubleshoot suggested by http://groups.google.com/group/web2py/browse_thread/thread/1c9ed82b640d04a9/82c734b68b79a16a?lnk=gst&q=invalid+view#82c734b68b79a16a
            badv = filename
            raise HTTP(404,
                       rewrite.thread.routes.error_message % badv,
                       web2py_error=badv)
        layer = filename
        if is_gae:
            ccode = getcfs(layer, filename,
                           lambda: compile2(parse_template(response.view,
                                            os.path.join(folder, 'views'),
                                            context=environment),layer))
        else:
            ccode = parse_template(response.view,
                                   os.path.join(folder, 'views'),
                                   context=environment)
        restricted(ccode, environment, layer)