Google App Engine的项目结构 [英] Project structure for Google App Engine
问题描述
我刚开始使用Google App Engine的应用程序时,就开始使用该技术,并开展了一个我一直在考虑的项目,但从未着手开始。结果是 BowlSK 。然而,随着它的发展和功能的增加,保持组织性变得非常困难 - 主要是因为这是我的第一个python项目,在我开始工作之前我对它一无所知。
我有:
- 主级包含:
- 所有.py文件(不知道如何制作包)
- 主层次页面的所有.html模板 >
- 子目录:
- 用于css,images,js等的单独文件夹
示例:
http://www.bowlsk.com/ 映射到HomePage(默认包),模板在index.html -
http://www.bowlsk.com/games/view-series.html?series=7130 映射到ViewSeriesPage(再次,默认包),模板在games / view-series.html。
这很讨厌。我如何重组?我有两个想法:
$ b $ ul
主文件夹包含:appdef,indexes,main.py?
- 代码的子文件夹。这是否必须是我的第一个包?
- 模板的子文件夹。文件夹heirarchy会匹配包heirarchy
- css,images,js等单独的子文件夹。
-
包含appdef,索引,main.py的主文件夹
- 代码+模板的子文件夹。这样我的模板旁边就有了处理器类,因为在这个阶段,我添加了很多特性,所以对其中的修改意味着修改其他特性。同样,我是否必须将此文件夹名称作为我的类的第一个包名称?我希望该文件夹是src,但我不希望我的类是src.WhateverPage。 >
- app.yaml:将所有非静态请求指向main.py
- main.py:初始化应用程序并发送所有请求 >
- static / *:静态文件;直接由App Engine提供
- myapp / *。py:特定于应用程序的Python代码
- views.py,模型.py,tests.py,__init__.py和更多
- templates / *。html:templates(或myapp / templates / * .html)
是否有最佳做法?随着Django 1.0的出现,我现在能做些什么来提高我与它成为官方GAE模板引擎的能力?我会简单地开始尝试这些事情,看看哪个更好,但pyDev的重构支持似乎不能很好地处理包移动,因此重新开始所有这些工作可能是一项不重要的任务。
解决方案
使用python-django-and-google-app-engine开发io /快速开发 =noreferrer
GvR描述了他的幻灯片演示文稿。
在这里,我将从该页面发布一个稍微修改版本的布局/结构。我非常喜欢这个模式。你还提到你在包装上遇到麻烦。只要确保每个子文件夹都有一个__init__.py文件。它是空的。
锅炉文件
项目布局
以下是一些可能有帮助的代码示例:
< h2> main.py
导入wsgiref.handlers
$ b $ from google.appengine.ext import webapp
from myapp.views import *
application = webapp.WSGIApplication([
('/',IndexHandler),
('/ foo',FooHandler)
$ b def main():
wsgiref.handlers.CGIHandler()。run(application)
myapp / views.py
import os
导入日期时间
导入日志
导入时间
$ b从google.appengine.api导入urlfetch
从google.appengine.ext.webapp导入模板
从google.appengine.api导入用户
from google.appengine.ext从模型导入中导入webapp
*
class IndexHandler(webapp.RequestHandler):
def get(self):
date =foo
#做一些处理
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 ):
我认为这种布局适用于新的和相对较小的中型项目。对于较大型的项目,我建议分解视图和模型,使其拥有自己的子文件夹,如下所示:
项目布局
- static /:静态文件;直接由App Engine提供服务
- js / *。js
- images / *。gif | png | jpg li>
- css / *。css
- myapp /:应用程序结构
- models / * .py
- views / *。py
- tests / *。py
- 模板/ *。html:模板
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.
What I have:
- Main Level contains:
- all .py files (didn't know how to make packages work)
- all .html templates for main level pages
- Subdirectories:
- separate folders for css, images, js, etc.
- folders that hold .html templates for subdirecty-type urls
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"It's nasty. How do I restructure? I had 2 ideas:
Main Folder containing: appdef, indexes, 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.
Main Folder containing appdef, indexes, 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"
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.
解决方案First, I would suggest you have a look at "Rapid Development with Python, Django, and Google App Engine"
GvR describes a general/standard project layout on page 10 of his slide presentation.
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.
Boilerplate files
- These hardly vary between projects
- app.yaml: direct all non-static requests to main.py
- main.py: initialize app and send it all requests
Project lay-out
- static/*: static files; served directly by App Engine
- myapp/*.py: app-specific python code
- views.py, models.py, tests.py, __init__.py, and more
- templates/*.html: templates (or myapp/templates/*.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
- images/*.gif|png|jpg
- css/*.css
- myapp/: app structure
- models/*.py
- views/*.py
- tests/*.py
- templates/*.html: templates
这篇关于Google App Engine的项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!