def compile_views(folder): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: raise Exception("%s in %s" % (e, fname)) filename = 'views.%s.py' % fname.replace(os.path.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: if skip_failed_views: failed_views.append(file) else: raise Exception("%s in %s" % (e, file)) else: filename = ('views/%s.py' % file).replace('/', '_').replace('\\', '_') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, '^[\w/\-]+(\.\w+)*$'): try: data = parse_template(fname, path) except Exception, e: if skip_failed_views: failed_views.append(fname) else: raise Exception("%s in %s" % (e, fname)) else: filename = ('views/%s.py' % fname).replace('/', '_').replace('\\', '_') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename)
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, REGEX_VIEW_PATH, followlinks=True): try: data = parse_template(fname, path) except Exception as e: if skip_failed_views: failed_views.append(fname) else: # FIXME: should raise something more specific than Exception raise Exception("%s in %s" % (e, fname)) else: filename = 'views.%s.py' % fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename) return failed_views or None
def compile_views(folder, skip_failed_views=False): """ Compiles all the views in the application specified by `folder` """ path = pjoin(folder, 'views') failed_views = [] for fname in listdir(path, REGEX_VIEW_PATH): try: data = parse_template(fname, path) except Exception as e: if skip_failed_views: failed_views.append(fname) else: # FIXME: should raise something more specific than Exception raise Exception("%s in %s" % (e, fname)) else: filename = 'views.%s.py' % fname.replace(os.sep, '.') filename = pjoin(folder, 'compiled', filename) write_file(filename, data) save_pyc(filename) os.unlink(filename) return failed_views or None
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 = current.request response = current.response view = environment['response'].view folder = request.folder cpath = pjoin(folder, 'compiled') badv = 'invalid view (%s)' % view patterns = response.get('generic_patterns') layer = None scode = None if patterns: regex = re_compile('|'.join(fnmatch.translate(p) for p in 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) layer = 'file stream' else: filename = pjoin(folder, 'views', view) if exists(cpath): # compiled views x = view.replace('/', '.') files = ['views.%s.pyc' % x] is_compiled = exists(pjoin(cpath, files[0])) # Don't use a generic view if the non-compiled view exists. if is_compiled or (not is_compiled and not exists(filename)): 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: compiled = pjoin(cpath, f) if exists(compiled): ccode = getcfs(compiled, compiled, lambda: read_pyc(compiled)) layer = compiled break # if the view is not compiled if not layer: if not exists(filename) and allow_generic: view = 'generic.' + request.extension filename = pjoin(folder, 'views', view) if not exists(filename): raise HTTP(404, rewrite.THREAD_LOCAL.routes.error_message % badv, web2py_error=badv) # Parse template scode = parse_template(view, pjoin(folder, 'views'), context=environment) # Compile template ccode = compile2(scode, filename) layer = filename restricted(ccode, environment, layer=layer, scode=scode) # parse_template saves everything in response body return environment['response'].body.getvalue()
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 x = view.replace('/', '_') files.append('views_%s.pyc' % x) if allow_generic: files.append('views_generic.%s.pyc' % request.extension) 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)
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 = current.request response = current.response view = environment['response'].view folder = request.folder path = pjoin(folder, 'compiled') badv = 'invalid view (%s)' % view patterns = response.get('generic_patterns') if 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 x = view.replace('/', '_') files.append('views_%s.pyc' % x) if allow_generic: files.append('views_generic.%s.pyc' % request.extension) 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)
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 = current.request response = current.response view = environment['response'].view folder = request.folder cpath = pjoin(folder, 'compiled') badv = 'invalid view (%s)' % view patterns = response.get('generic_patterns') layer = None if 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) layer = 'file stream' else: filename = pjoin(folder, 'views', view) if os.path.exists(cpath): # compiled views x = view.replace('/', '.') files = ['views.%s.pyc' % x] is_compiled = os.path.exists(pjoin(cpath, files[0])) # Don't use a generic view if the non-compiled view exists. if is_compiled or (not is_compiled and not os.path.exists(filename)): 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: compiled = pjoin(cpath, f) if os.path.exists(compiled): ccode = getcfs(compiled, compiled, lambda: read_pyc(compiled)) layer = compiled break 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 # Compile the template ccode = parse_template(view, pjoin(folder, 'views'), context=environment) restricted(ccode, environment, layer=layer) # parse_template saves everything in response body return environment['response'].body.getvalue()
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 patterns = response.get("generic_patterns") if 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 x = view.replace("/", "_") files.append("views_%s.pyc" % x) if allow_generic: files.append("views_generic.%s.pyc" % request.extension) 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)