大家新年好!
在 前一篇随笔中,大家了解了什么是Pylons,有哪些特点,今天笔者继续给介绍默认生成的项目结构。
通过Paste创建新的Pylons应用很简单,就是一句"paster create -t pylons [应用名]"命令,其中"-t pylons"或者全称"--template=pylons",用以告诉Paste我们新建的项目,将是一个Pylons应用,或者说,从一个预定义好的Pylons默认项目模板生成。如果你愿意,你也可以自己来制作新的模板以符合需要。"paster create --list-templates"可以查看当前可用的模板。
假定我们新建的Pylons项目名称为NewApp,那么执行"paster create -t pylons NewApp"后,我们将得到一个新的NewApp目录,其中包含一个docs目录(文档)、一个newapp目录(代码)、一个NewApp.egg-info目录(元数据)和一些顶级配置文件。
在开发过程中,我们经常会用到的是代码目录(这里是newapp)和位于项目根目录下的development.ini和test.ini文件了。注意这里newapp的大小写,Pylons在生成项目的时候,不论你指定的项目名称大小写是怎样,这里会自动.lower()转小写,只有项目顶级路径和egg信息会保留原始大小写,笔者认为这更加符合Web应用的nature。
代码目录下包括config、controllers、lib、model、public、templates和tests子目录,分别用于存放配置(如环境、中间件、路径查找逻辑)、控制器(处理请求)、全局辅助代码(全局变量、helpers等)、模型(后台数据访问)、静态页面/媒体文件、页面模板和测试代码。
最后说说ini文件:默认生成的development.ini和test.ini顾名思义分别对应开发和测试需要的基本配置,我们可以通过修改相应的参数配置来指定具体设定,如服务器IP和端口、数据库连接、日志等。看到这里你也许会问,那么产品环境呢?答案是默认没有生成,你可以从development.ini修改成需要的产品环境配置,也可以通过paster make-config newapp production.ini生成一个默认的产品环境典型配置。
以development.ini为例,Pylons的ini文件主要包括4个部分的内容:
1- 全局默认参数,如
[
DEFAULT
]
debug = true
# Uncomment and replace with the address which should receive any error reports
#email_to = you@yourdomain.com
smtp_server = localhost
error_email_from = paste@localhost
debug = true
# Uncomment and replace with the address which should receive any error reports
#email_to = you@yourdomain.com
smtp_server = localhost
error_email_from = paste@localhost
2- 服务器配置,如
[
server:main
]
use = egg:Paste#http
host = 127.0.0.1
port = 5000
use = egg:Paste#http
host = 127.0.0.1
port = 5000
3- 应用程序配置,如
[
app:main
]
use = egg:NewApp
full_stack = true
cache_dir = %(here)s/data
beaker.session.key = newapp
beaker.session.secret = somesecret
# If you'd like to fine-tune the individual locations of the cache data dirs
# for the Cache data , or the Session saves , un-comment the desired settings
# here:
#beaker.cache.data_dir = %(here)s/data/cache
#beaker.session.data_dir = %(here)s/data/sessions
# SQLAlchemy database URL
sqlalchemy.url = sqlite:///%(here)s/development.db
# WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT*
# Debug mode will enable the interactive debugging tool , allowing ANYONE to
# execute malicious code after an exception is raised.
#set debug = false
简单说明一下,这里的full_stack设置为true表示打开交互式调试和错误报告等功能,"%(here)s"会被替换成项目所在路径,类似相对路径url中的"."转绝对路径,而beaker.*为配置会话相关的设定,如缓存、cookie基本内容等。对于产品环境来说,比较重要的一个配置是"set debug=false",否则一旦出现异常,交互式调试将使得攻击者能够执行系统命令。
use = egg:NewApp
full_stack = true
cache_dir = %(here)s/data
beaker.session.key = newapp
beaker.session.secret = somesecret
# If you'd like to fine-tune the individual locations of the cache data dirs
# for the Cache data , or the Session saves , un-comment the desired settings
# here:
#beaker.cache.data_dir = %(here)s/data/cache
#beaker.session.data_dir = %(here)s/data/sessions
# SQLAlchemy database URL
sqlalchemy.url = sqlite:///%(here)s/development.db
# WARNING: *THE LINE BELOW MUST BE UNCOMMENTED ON A PRODUCTION ENVIRONMENT*
# Debug mode will enable the interactive debugging tool , allowing ANYONE to
# execute malicious code after an exception is raised.
#set debug = false
4- 日志,如
[
loggers
]
keys = root , routes , newapp , sqlalchemy
[ handlers ]
keys = console
[ formatters ]
keys = generic
[ logger_root ]
level = INFO
handlers = console
[ logger_routes ]
level = INFO
handlers =
qualname = routes.middleware
# " level = DEBUG " logs the route matched and routing variables.
[ logger_newapp ]
level = DEBUG
handlers =
qualname = newapp
[ logger_sqlalchemy ]
level = INFO
handlers =
qualname = sqlalchemy.engine
# " level = INFO " logs SQL queries.
# " level = DEBUG " logs SQL queries and results.
# " level = WARN " logs neither. (Recommended for production systems.)
[ handler_console ]
class = StreamHandler
args = (sys.stderr , )
level = NOTSET
formatter = generic
[ formatter_generic ]
format = %(asctime)s , %(msecs)03d %(levelname)- 5 .5s [ %(name)s ] %(message)s
datefmt = %H:%M:%S
keys = root , routes , newapp , sqlalchemy
[ handlers ]
keys = console
[ formatters ]
keys = generic
[ logger_root ]
level = INFO
handlers = console
[ logger_routes ]
level = INFO
handlers =
qualname = routes.middleware
# " level = DEBUG " logs the route matched and routing variables.
[ logger_newapp ]
level = DEBUG
handlers =
qualname = newapp
[ logger_sqlalchemy ]
level = INFO
handlers =
qualname = sqlalchemy.engine
# " level = INFO " logs SQL queries.
# " level = DEBUG " logs SQL queries and results.
# " level = WARN " logs neither. (Recommended for production systems.)
[ handler_console ]
class = StreamHandler
args = (sys.stderr , )
level = NOTSET
formatter = generic
[ formatter_generic ]
format = %(asctime)s , %(msecs)03d %(levelname)- 5 .5s [ %(name)s ] %(message)s
datefmt = %H:%M:%S
OK,这一篇就先讲到这儿,下一篇将介绍Routes和controller。