2016-12-28 69 views
0

我是新來的Python和水泥應用,對象在蟒蛇水泥沒有屬性_meta應用

我只是通過在水泥應用程序提供的文檔會和我面臨着以下問題

Traceback (most recent call last): 
    File "/Users/Amith/python/learning/inter.py", line 80, in <module> 
    app.handler.register(MyHandler) 
    File "/Users/Amith/python/learning/connectors/lib/python2.7/site-packages/cement/core/handler.py", line 195, in register 
    if not hasattr(obj._meta, 'label') or not obj._meta.label: 
AttributeError: 'MyHandler' object has no attribute '_meta' 

我沒有從文檔中找到很多幫助,也沒有驗證其他幾個網站。

請在下面找到

from cement.core.foundation import CementApp 
from cement.core.interface import Interface, Attribute 
from cement.core import interface 
from cement.core.handler import CementBaseHandler 

def my_validator(klass, obj): 
    members = [ 
     '_setup', 
     'do_something', 
     'my_var', 
     ] 
    interface.validate(MyInterface, obj, members) 

class MyInterface(Interface): 
    class IMeta: 
     label = 'myinterface' 
     validator = my_validator 
    # Must be provided by the implementation 
    Meta = Attribute('Handler Meta-data') 
    my_var = Attribute('A variable of epic proportions.') 

    def _setup(app_obj): 
     """ The setup function """ 

    def do_something(): 
     """  This function does something. """ 

class MyHandler(CementBaseHandler): 
    class Meta: 
     interface = MyInterface 
     label = 'my_handler' 
     description = 'This handler implements MyInterface' 
     config_defaults = dict(
      foo='bar' 
      ) 

    my_var = 'This is my var' 

    def __init__(self): 
     self.app = None 

    def _setup(self, app_obj): 
     self.app = app_obj 

    def do_something(self): 
     print "Doing work!" 


class MyApp(CementApp): 
    class Meta: 
     label = 'myapp' 
     define_handlers = [MyInterface] 
     handlers = [MyHandler] 

app = CementApp('myapp') 
# define interfaces after app is created 
app.setup() 
print app.config.keys('myapp') 
app.config.set('myapp', 'debug', True) 
print app.config.keys('myapp') 
app.handler.define(MyInterface) 
app.handler.register(MyHandler) 
app.run() 

我的代碼任何幫助將大大appriciated

+0

從http://builtoncement.com/2.10/dev/interfaces_and_handlers.html – amith

回答

1

的問題是,MyHandler不正確子類CementBaseHandler。任何方法需要被子類與super() ...例如:

class MyHandler(CementBaseHandler): 
    class Meta: 
     interface = MyInterface 
     label = 'my_handler' 
     description = 'This handler implements MyInterface' 
     config_defaults = dict(
      foo='bar' 
     ) 
    my_var = 'This is my var' 

    def __init__(self, *args, **kw): 
     super(MyHandler, self).__init__(*args, **kw) 

     # add your code here (if necessary) 

    def _setup(self, app_obj): 
     super(MyHandler, self)._setup(app_obj) 

     # add your code here if necessary 

    def do_something(self): 
     print "Doing work!" 

注意使用super()的修改MyHandler.__init__()MyHandler._setup() ...這個固定的問題,對我來說。

對於文檔中給您帶來的不便,我們深表歉意,同時感謝您使用正確/可用/可重現的示例指出了這一不便之處。我在Github的項目中添加了一個問題,讓它在下一個版本中得到修復。

+0

真棒它的工作原理參考 – amith

相關問題