2

不好意思问,但我正在使用 webapp2,我正在模板化一个解决方案,以便更容易地根据这个 google webapp2 路由功能定义路由。但这一切都取决于能够TYPE_NAME在子级别进行定义。这个想法是父母设置一切,孩子只需要实现该_list功能。我遇到的问题TYPE_NAME是没有,我需要它成为孩子。

#main WSGI is extended to have this function 
class WSGIApplication(webapp2.WSGIApplication):
    def route(self, *args, **kwargs):
        def wrapper(func):
            self.router.add(webapp2.Route(handler=func, *args, **kwargs))
            return func

        return wrapper

from main import application
class ParentHandler(RequestHandler):
    TYPE_NAME = None

    @application.route('/', name="list_%s" %TYPE_NAME)
    def list(self):
          return self._list()

class ChildHandler(ParentHandler):
    TYPE_NAME = 'child'

    def _list(self):
         return []

我尝试了几个使用“类属性”的解决方案,但没有成功。打开其他想法,我基本上只需要子类继承装饰属性并执行它们。

编辑:

对于所有坐在座位边缘想知道我如何解决这个问题的人,我无法从装饰器中得到我需要的一切,所以我最终使用了一个元。我还添加了一个_URLS参数以允许添加其他“路线”。它将 custom功能映射到路线。真的很想使用装饰器,但无法让它工作。

class RequestURLMeta(type):
    def __new__(mcs, name, bases, dct):
        result = super(RequestURLMeta, mcs).__new__(mcs, name, bases, dct)
        urls = getattr(result, '_URLS', {}) or {}
        for k,v in urls.iteritems():
            template = v.pop('template')
            app.route(getattr(result, k), template, **v)

        if getattr(result, 'TYPE_NAME', None):
            app.route(result.list, result.ROOT_PATH, methods=['GET'],name="%s" % result.TYPE_NAME)
        #other ones went here..

    return result

class ParentHandler(RequestHandler):
    __metaclass__ = RequestURLMeta


class ChildHandler(ParentHandler):
    TYPE_NAME = 'child'
    _URLS = { 'custom': '/custom', 'TYPE_NAME': 'custom_test' }
    def _list(self):
        return []
    def custom(self):  pass
4

2 回答 2

1

我认为要使它起作用,您将需要使用metaclass。它可能类似于以下内容(未经测试):

from main import application

class RouteMeta(type):
    def __new__(mcs, name, bases, dct):
        type_name = dct.get("TYPE_NAME")
        if type_name is not None:
            @application.route('/', type_name)
            def list(self):
                return self._list()
            dct["list"] = list
        return super(RouteMeta, mcs).__new__(mcs, name, bases, dct)

class ParentHandler(RequestHandler):
    __metaclass__ = RouteMeta

class ChildHandler(ParentHandler):
    TYPE_NAME = 'child'

    def _list(self):
         return []

Instead of having the list() method an attribute of ParentHandler, it is dynamically created for classes that inherit from ParentHandler and have TYPE_NAME defined.

If RequestHandler also uses a custom metaclass, have RouteMeta inherit from RequestHandler.__metaclass__ instead of type.

于 2013-01-25T18:29:19.503 回答
0

这段代码:

@application.route('/', name="list_%s" %TYPE_NAME)
def list(self):*emphasized text*
    ...

在语义上与此相同:

def list(self):
    ...
list = application.route('/', name="list_%s" %TYPE_NAME)(list)

即该方法route在范围内被调用ParentHandler,无论您尝试什么惰性方法,它都不起作用。你应该尝试一些不同的东西:

from main import application

def route_list(klass):
    klass.list = application.route('/',
        name="list_%s" % klass.TYPE_NAME)(klass.list)
    return klass

class ParentHandler(RequestHandler):

    def list(self):
          return self._list()

class ChildHandler(ParentHandler):
    TYPE_NAME = 'child'

    def _list(self):
         return []

# in python3 would be:
# @route_list
# class ChildHandler(ParentHandler):
#   ...
ChildHandler = route_list(ChildHandler)
于 2013-01-25T18:22:01.383 回答