谷歌应用引擎,webapp2,DomainRoute和懒惰的处理程序

时间:2012-09-18 11:21:00

标签: python google-app-engine webapp2

我正在尝试在我的主要GAE应用程序中支持一个或多或少孤立的应用程序,该应用程序托管在不同的子域中。

我将此子域的处理程序放在“oficina”文件夹中的“mediciones.py”文件中。

所以我有:

  1. /main.py
  2. /oficina/mediciones.py
  3. 并且在mediciones.py内:
  4. class Router(webapp2.RequestHandler):
        def get(self):
          [... code ...]
    
    class Listado(webapp.RequestHandler):
        def get(self):
          [... code ...]
    

    等。对于所有必要的处理程序。

    在“main.py”里面:

    application = webapp2.WSGIApplication([
        DomainRoute('ventas.domain.com',
            [webapp2.Route(r'/nueva', handler='oficina.mediciones.MedicionNueva', name="nueva-medicion"),
             webapp2.Route(r'/listado', handler="oficina.mediciones.Listado", name="listado-mediciones"),
             webapp2.Route(r'/medicion/(\d+)/', handler="oficina.mediciones.MedicionDetalles", name="detalles-mediciones"),
             webapp2.Route(r'/rellenar_medicion/(\d+)/', handler="oficina.mediciones.MedicionRellenar", name="rellenar-medicion"),
             webapp2.Route(r'/editar_medicion/(\d+)/', handler="oficina.mediciones.MedicionEditar", name="editar-medicion"),
             webapp2.Route('/', handler="oficina.mediciones.Router")
            ]),
    ('/(.+)',
         DirectView),
        ('/?',
            HomeView),
        ], debug=True)
    

    但是当我尝试去ventas.domain.com或者去ventas.domain.com/listado时,我明白了:

    Traceback (most recent call last):
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
        rv = self.handle_exception(request, response, e)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
        rv = self.router.dispatch(request, response)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1272, in default_dispatcher
        self.handlers[handler] = handler = import_string(handler)
      File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1851, in import_string
        return getattr(__import__(module, None, None, [obj]), obj)
    ImportStringError: import_string() failed for 'oficina.mediciones.Router'. Possible reasons are:
    
    - missing __init__.py in a package;
    - package or module path not included in sys.path;
    - duplicated package or module name taking precedence in sys.path;
    - missing module, class, function or variable;
    
    Original exception:
    
    AttributeError: 'module' object has no attribute 'Router'
    
    Debugged import:
    
    - 'oficina' found in '/base/data/home/apps/s~pavimentos-arquiservi-web-hrd/11-0-0.361841284178483516/oficina/__init__.pyc'.
    - 'oficina.mediciones' found in '/base/data/home/apps/s~pavimentos-arquiservi-web-hrd/11-0-0.361841284178483516/oficina/mediciones/__init__.pyc'.
    - 'oficina.mediciones.Router' not found.
    

    (用“Listado”替换“Router”,或者为每种情况替换适当的处理程序。)

    处理程序已定义,但为什么不在它们的位置找到它们?

    谢谢和问候。

    伊万

1 个答案:

答案 0 :(得分:0)

如果应用程序是真正自包含的,您可以考虑使用不同的方法在单独的模块中拆分处理程序。在app.yaml中,您可以定义不同的网址前缀,并由不同的模块处理。

E.g。

- url: /oficina/.*
  script: ofinina.mediciones.py

- url: /.*
  script: main.py

不幸的是,您无法仅通过域名将这些路线分开,因此,根据您的要求,这可能不适合您。

作为替代方法,您可以将所需的包导入main.py并直接使用类名,而不是为处理程序使用字符串名称。这减少了包裹的自我遏制。但话又说回来,用字符串文字引用类名也是如此。