谷歌App引擎项目结构

时间:2023-01-24 17:49:12

I started an application in Google App Engine right when it came out, to play with the technology and work on a pet project that I had been thinking about for a long time but never gotten around to starting. The result is BowlSK. However, as it has grown, and features have been added, it has gotten really difficult to keep things organized - mainly due to the fact that this is my first python project, and I didn't know anything about it until I started working.

我在谷歌App Engine中启动了一个应用程序,它刚推出时,我就开始使用这个技术,并在一个我已经考虑了很长时间的pet项目上工作,但一直没有开始。结果是BowlSK。然而,随着它的发展和特性的增加,它变得非常难以保持组织——主要是因为这是我的第一个python项目,在我开始工作之前我对它一无所知。

What I have:

我有什么:

  • Main Level contains:
    • all .py files (didn't know how to make packages work)
    • 所有.py文件(不知道如何使包工作)
    • all .html templates for main level pages
    • 主页面的所有.html模板
  • 主级别包含:所有.py文件(不知道如何使包工作)主级别页面的所有.html模板
  • Subdirectories:
    • separate folders for css, images, js, etc.
    • 为css、图像、js等分离文件夹。
    • folders that hold .html templates for subdirecty-type urls
    • 为子目录类型的url保存.html模板的文件夹
  • 子目录:用于css、图像、js等的独立文件夹,这些文件夹保存子目录类型url的.html模板

Example:
http://www.bowlsk.com/ maps to HomePage (default package), template at "index.html"
http://www.bowlsk.com/games/view-series.html?series=7130 maps to ViewSeriesPage (again, default package), template at "games/view-series.html"

示例:http://www.bowlsk.com/映射到主页(默认包),模板在“索引”。html " http://www.bowlsk.com/games/view-series.html?系列=7130映射到ViewSeriesPage(同样是默认包),模板为“games/view-series.html”

It's nasty. How do I restructure? I had 2 ideas:

这是令人讨厌的。我怎么调整?我有2的想法:

  • Main Folder containing: appdef, indexes, main.py?

    主文件夹包含:appdef、索引、Main .py?

    • Subfolder for code. Does this have to be my first package?
    • 代码的子文件夹。这一定是我的第一个包裹吗?
    • Subfolder for templates. Folder heirarchy would match package heirarchy
    • 子文件夹的模板。文件夹的继承将与包的继承相匹配
    • Individual subfolders for css, images, js, etc.
    • 用于css、图像、js等的单独子文件夹。
  • Main Folder containing appdef, indexes, main.py?

    包含appdef、索引、main.py的主文件夹?

    • Subfolder for code + templates. This way I have the handler class right next to the template, because in this stage, I'm adding lots of features, so modifications to one mean modifications to the other. Again, do I have to have this folder name be the first package name for my classes? I'd like the folder to be "src", but I don't want my classes to be "src.WhateverPage"
    • 用于代码+模板的子文件夹。这样,我在模板旁边就有了handler类,因为在这个阶段,我添加了很多特性,所以对一个的修改意味着对另一个的修改。同样,我是否必须将这个文件夹名作为类的第一个包名?我希望文件夹是“src”,但我不希望我的类是“src. whateverpage”

Is there a best practice? With Django 1.0 on the horizon, is there something I can do now to improve my ability to integrate with it when it becomes the official GAE templating engine? I would simply start trying these things, and seeing which seems better, but pyDev's refactoring support doesn't seem to handle package moves very well, so it will likely be a non-trivial task to get all of this working again.

有没有最佳实践?有了Django 1.0,当它成为正式的GAE模板引擎时,我现在有什么事情可以改进我与它集成的能力吗?我只是开始尝试这些东西,看看哪一种更好,但是pyDev的重构支持似乎不能很好地处理包的移动,所以让所有这些重新工作可能是一项非常重要的任务。

6 个解决方案

#1


104  

First, I would suggest you have a look at "Rapid Development with Python, Django, and Google App Engine"

首先,我建议您看看“Python、Django和谷歌应用程序引擎的快速开发”

GvR describes a general/standard project layout on page 10 of his slide presentation.

GvR在幻灯片演示的第10页描述了一个通用/标准的项目布局。

Here I'll post a slightly modified version of the layout/structure from that page. I pretty much follow this pattern myself. You also mentioned you had trouble with packages. Just make sure each of your sub folders has an __init__.py file. It's ok if its empty.

在这里,我将从该页中发布稍微修改过的布局/结构版本。我自己也差不多遵循这种模式。你还提到你的包装有问题。确保每个子文件夹都有一个__init__。py文件。如果是空的也可以。

Boilerplate files

  • These hardly vary between projects
  • 这些项目之间几乎没有差别。
  • app.yaml: direct all non-static requests to main.py
  • 应用程序.yaml:将所有非静态请求指向main.py
  • main.py: initialize app and send it all requests
  • 主要。py:初始化app并发送所有请求

Project lay-out

  • static/*: static files; served directly by App Engine
  • 静态/ *:静态文件;直接由App引擎服务
  • myapp/*.py: app-specific python code
    • views.py, models.py, tests.py, __init__.py, and more
    • 的观点。py模型。py,测试。py,__init__。py等等
  • myapp / *。py:特定于应用程序的python代码视图。py模型。py,测试。py,__init__。py等等
  • templates/*.html: templates (or myapp/templates/*.html)
  • 模板/ *。html:模板(或myapp /模板/ * . html)

Here are some code examples that may help as well:

这里有一些代码示例也可能有帮助:

main.py

import wsgiref.handlers

from google.appengine.ext import webapp
from myapp.views import *

application = webapp.WSGIApplication([
  ('/', IndexHandler),
  ('/foo', FooHandler)
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

myapp/views.py

import os
import datetime
import logging
import time

from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from models import *

class IndexHandler(webapp.RequestHandler):
  def get(self):
    date = "foo"
    # Do some processing        
    template_values = {'data': data }
    path = os.path.join(os.path.dirname(__file__) + '/../templates/', 'main.html')
    self.response.out.write(template.render(path, template_values))

class FooHandler(webapp.RequestHandler):
  def get(self):
    #logging.debug("start of handler")

myapp/models.py

from google.appengine.ext import db

class SampleModel(db.Model):

I think this layout works great for new and relatively small to medium projects. For larger projects I would suggest breaking up the views and models to have their own sub-folders with something like:

我认为这种布局非常适合新项目和相对较小的中型项目。对于较大的项目,我建议将视图和模型拆分为拥有自己的子文件夹,内容如下:

Project lay-out

  • static/: static files; served directly by App Engine
    • js/*.js
    • js / * . js
    • images/*.gif|png|jpg
    • / * gif图片| png | jpg
    • css/*.css
    • css / * . css
  • 静态/:静态文件;直接由App Engine js/*服务。js / *图像。gif | png | jpg css / * . css
  • myapp/: app structure
    • models/*.py
    • 模型/ * . py
    • views/*.py
    • 视图/ * . py
    • tests/*.py
    • 测试/ * . py
    • templates/*.html: templates
    • 模板/ *。html:模板
  • / * myapp /:应用结构模型。/ * py视图。py测试/ *。py模板/ *。html:模板

#2


16  

My usual layout looks something like this:

我通常的布局是这样的:

  • app.yaml
  • app.yaml
  • index.yaml
  • index.yaml
  • request.py - contains the basic WSGI app
  • 请求。py -包含基本的WSGI应用程序
  • lib
    • __init__.py - common functionality, including a request handler base class
    • __init__。py -通用功能,包括请求处理程序基类
  • *__init__。py -通用功能,包括请求处理程序基类
  • controllers - contains all the handlers. request.yaml imports these.
  • 控制器——包含所有的处理器。请求。yaml进口这些。
  • templates
    • all the django templates, used by the controllers
    • 控制器使用的所有django模板
  • 模板所有django模板,由控制器使用
  • model
    • all the datastore model classes
    • 所有的数据存储模型类
  • 建模所有的数据存储模型类。
  • static
    • static files (css, images, etc). Mapped to /static by app.yaml
    • 静态文件(css、图像等)。通过app.yaml映射到/静态
  • 静态文件(css、图像等)。通过app.yaml映射到/静态

I can provide examples of what my app.yaml, request.py, lib/init.py, and sample controllers look like, if this isn't clear.

我可以提供我的app.yaml请求的示例。py,lib / init。py和样本控制器看起来是这样的,如果不清楚的话。

#3


11  

I implemented a google app engine boilerplate today and checked it on github. This is along the lines described by Nick Johnson above (who used to work for Google).

我今天实现了谷歌应用引擎样板,并在github上进行了检查。这与上面尼克•约翰逊(Nick Johnson)描述的思路一致(他曾为谷歌工作)。

Follow this link gae-boilerplate

遵循这个链接gae-boilerplate

#4


7  

I think the first option is considered the best practice. And make the code folder your first package. The Rietveld project developed by Guido van Rossum is a very good model to learn from. Have a look at it: http://code.google.com/p/rietveld

我认为第一种选择被认为是最佳实践。并使代码文件夹成为您的第一个包。圭多·范·罗瑟森开发的Rietveld项目是一个很好的学习模式。看看它:http://code.google.com/p/rietveld

With regard to Django 1.0, I suggest you start using the Django trunk code instead of the GAE built in django port. Again, have a look at how it's done in Rietveld.

关于Django 1.0,我建议您开始使用Django主干代码,而不是构建在Django端口中的GAE。同样,让我们来看看在里特维尔德是怎么做的。

#5


3  

I like webpy so I've adopted it as templating framework on Google App Engine.
My package folders are typically organized like this:

我喜欢webpy,所以我把它作为谷歌应用引擎的模板框架。我的包文件夹通常是这样组织的:

app.yaml
application.py
index.yaml
/app
   /config
   /controllers
   /db
   /lib
   /models
   /static
        /docs
        /images
        /javascripts
        /stylesheets
   test/
   utility/
   views/

Here is an example.

这是一个例子。

#6


1  

I am not entirely up to date on the latest best practices, et cetera when it comes to code layout, but when I did my first GAE application, I used something along your second option, where the code and templates are next to eachother.

当涉及到代码布局时,我并没有完全了解最新的最佳实践,等等,但是当我开发第一个GAE应用程序时,我在第二个选项中使用了一些东西,其中代码和模板彼此相邻。

There was two reasons for this - one, it kept the code and template nearby, and secondly, I had the directory structure layout mimic that of the website - making it (for me) a bit easier too remember where everything was.

这样做有两个原因:其一,它把代码和模板放在附近;其二,我让目录结构布局模仿了网站的布局——让它(对我来说)更容易记住所有东西在哪里。

#1


104  

First, I would suggest you have a look at "Rapid Development with Python, Django, and Google App Engine"

首先,我建议您看看“Python、Django和谷歌应用程序引擎的快速开发”

GvR describes a general/standard project layout on page 10 of his slide presentation.

GvR在幻灯片演示的第10页描述了一个通用/标准的项目布局。

Here I'll post a slightly modified version of the layout/structure from that page. I pretty much follow this pattern myself. You also mentioned you had trouble with packages. Just make sure each of your sub folders has an __init__.py file. It's ok if its empty.

在这里,我将从该页中发布稍微修改过的布局/结构版本。我自己也差不多遵循这种模式。你还提到你的包装有问题。确保每个子文件夹都有一个__init__。py文件。如果是空的也可以。

Boilerplate files

  • These hardly vary between projects
  • 这些项目之间几乎没有差别。
  • app.yaml: direct all non-static requests to main.py
  • 应用程序.yaml:将所有非静态请求指向main.py
  • main.py: initialize app and send it all requests
  • 主要。py:初始化app并发送所有请求

Project lay-out

  • static/*: static files; served directly by App Engine
  • 静态/ *:静态文件;直接由App引擎服务
  • myapp/*.py: app-specific python code
    • views.py, models.py, tests.py, __init__.py, and more
    • 的观点。py模型。py,测试。py,__init__。py等等
  • myapp / *。py:特定于应用程序的python代码视图。py模型。py,测试。py,__init__。py等等
  • templates/*.html: templates (or myapp/templates/*.html)
  • 模板/ *。html:模板(或myapp /模板/ * . html)

Here are some code examples that may help as well:

这里有一些代码示例也可能有帮助:

main.py

import wsgiref.handlers

from google.appengine.ext import webapp
from myapp.views import *

application = webapp.WSGIApplication([
  ('/', IndexHandler),
  ('/foo', FooHandler)
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

myapp/views.py

import os
import datetime
import logging
import time

from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from models import *

class IndexHandler(webapp.RequestHandler):
  def get(self):
    date = "foo"
    # Do some processing        
    template_values = {'data': data }
    path = os.path.join(os.path.dirname(__file__) + '/../templates/', 'main.html')
    self.response.out.write(template.render(path, template_values))

class FooHandler(webapp.RequestHandler):
  def get(self):
    #logging.debug("start of handler")

myapp/models.py

from google.appengine.ext import db

class SampleModel(db.Model):

I think this layout works great for new and relatively small to medium projects. For larger projects I would suggest breaking up the views and models to have their own sub-folders with something like:

我认为这种布局非常适合新项目和相对较小的中型项目。对于较大的项目,我建议将视图和模型拆分为拥有自己的子文件夹,内容如下:

Project lay-out

  • static/: static files; served directly by App Engine
    • js/*.js
    • js / * . js
    • images/*.gif|png|jpg
    • / * gif图片| png | jpg
    • css/*.css
    • css / * . css
  • 静态/:静态文件;直接由App Engine js/*服务。js / *图像。gif | png | jpg css / * . css
  • myapp/: app structure
    • models/*.py
    • 模型/ * . py
    • views/*.py
    • 视图/ * . py
    • tests/*.py
    • 测试/ * . py
    • templates/*.html: templates
    • 模板/ *。html:模板
  • / * myapp /:应用结构模型。/ * py视图。py测试/ *。py模板/ *。html:模板

#2


16  

My usual layout looks something like this:

我通常的布局是这样的:

  • app.yaml
  • app.yaml
  • index.yaml
  • index.yaml
  • request.py - contains the basic WSGI app
  • 请求。py -包含基本的WSGI应用程序
  • lib
    • __init__.py - common functionality, including a request handler base class
    • __init__。py -通用功能,包括请求处理程序基类
  • *__init__。py -通用功能,包括请求处理程序基类
  • controllers - contains all the handlers. request.yaml imports these.
  • 控制器——包含所有的处理器。请求。yaml进口这些。
  • templates
    • all the django templates, used by the controllers
    • 控制器使用的所有django模板
  • 模板所有django模板,由控制器使用
  • model
    • all the datastore model classes
    • 所有的数据存储模型类
  • 建模所有的数据存储模型类。
  • static
    • static files (css, images, etc). Mapped to /static by app.yaml
    • 静态文件(css、图像等)。通过app.yaml映射到/静态
  • 静态文件(css、图像等)。通过app.yaml映射到/静态

I can provide examples of what my app.yaml, request.py, lib/init.py, and sample controllers look like, if this isn't clear.

我可以提供我的app.yaml请求的示例。py,lib / init。py和样本控制器看起来是这样的,如果不清楚的话。

#3


11  

I implemented a google app engine boilerplate today and checked it on github. This is along the lines described by Nick Johnson above (who used to work for Google).

我今天实现了谷歌应用引擎样板,并在github上进行了检查。这与上面尼克•约翰逊(Nick Johnson)描述的思路一致(他曾为谷歌工作)。

Follow this link gae-boilerplate

遵循这个链接gae-boilerplate

#4


7  

I think the first option is considered the best practice. And make the code folder your first package. The Rietveld project developed by Guido van Rossum is a very good model to learn from. Have a look at it: http://code.google.com/p/rietveld

我认为第一种选择被认为是最佳实践。并使代码文件夹成为您的第一个包。圭多·范·罗瑟森开发的Rietveld项目是一个很好的学习模式。看看它:http://code.google.com/p/rietveld

With regard to Django 1.0, I suggest you start using the Django trunk code instead of the GAE built in django port. Again, have a look at how it's done in Rietveld.

关于Django 1.0,我建议您开始使用Django主干代码,而不是构建在Django端口中的GAE。同样,让我们来看看在里特维尔德是怎么做的。

#5


3  

I like webpy so I've adopted it as templating framework on Google App Engine.
My package folders are typically organized like this:

我喜欢webpy,所以我把它作为谷歌应用引擎的模板框架。我的包文件夹通常是这样组织的:

app.yaml
application.py
index.yaml
/app
   /config
   /controllers
   /db
   /lib
   /models
   /static
        /docs
        /images
        /javascripts
        /stylesheets
   test/
   utility/
   views/

Here is an example.

这是一个例子。

#6


1  

I am not entirely up to date on the latest best practices, et cetera when it comes to code layout, but when I did my first GAE application, I used something along your second option, where the code and templates are next to eachother.

当涉及到代码布局时,我并没有完全了解最新的最佳实践,等等,但是当我开发第一个GAE应用程序时,我在第二个选项中使用了一些东西,其中代码和模板彼此相邻。

There was two reasons for this - one, it kept the code and template nearby, and secondly, I had the directory structure layout mimic that of the website - making it (for me) a bit easier too remember where everything was.

这样做有两个原因:其一,它把代码和模板放在附近;其二,我让目录结构布局模仿了网站的布局——让它(对我来说)更容易记住所有东西在哪里。