python 一个简单的Pyramid插件的例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个简单的Pyramid插件的例子相关的知识,希望对你有一定的参考价值。

from pyramid.view import view_config
import pyramid_plugin
@view_config(route_name='home', renderer='form_view.mak')
def my_view(request):
    Car = pyramid_plugin.get_car(request)
    Car.get_type()
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###

[app:main]
use = egg:formencode-tut

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = 
    pyramid_debugtoolbar
    pyramid_plugin
mako.directories = formencodetut:templates
plugin.make = Camaro SS
# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###

[loggers]
keys = root, formencodetut

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_formencodetut]
level = DEBUG
handlers =
qualname = formencodetut

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
# -*- coding: utf-8 -*-
"""
Created on 05/03/14

@author: dwanyoike

Example of a pyramid Framework Addon
"""

from zope.interface import Interface

def get_car(request):
    """ used to retrieve the object we saved with registerUtility the 
        object is stored in the request and is available globally"""  
    registry = getattr(request, 'registry', None)
    if registry is None:
        registry = request
    return registry.getUtility(IPyramidutiliy)

class IPyramidutiliy(Interface):
    pass

class Car(object):
    def __init__(self,settings):
        self.car_make = settings['plugin.make']

    def get_type(self):
        print self.car_make + " go Vroooom "

def includeme(config):

    #Fetch settings from our development.ini or production.ini
    settings = config.registry.settings

    #Create a new car passing our setting fro our .ini files
    new_car = Car(settings)

    #Add the Car object to registry
    config.registry.registerUtility(new_car, IPyramidutiliy)

以上是关于python 一个简单的Pyramid插件的例子的主要内容,如果未能解决你的问题,请参考以下文章

Python Web框架:Django vs Flask vs Pyramid

Python Pyramid 捕获空字节攻击

使用 Python3 在 Pyramid 中使用 Websocket

选择一个 Python Web 框架:Django vs Flask vs Pyramid

如何从 Python Pyramid 为 AngularJS 应用程序提供 index.html 文件?

使用 Pyramid 压缩所有 HTTP 流量