Quixote学习笔记(3)

Understanding the root demo

Start the root demo by running the command:
simple_server.py --factory quixote.demo.create_publisher

In a browser, open http://localhost:8080 as before. Click around at will.

This is the default demo, but it is more complicated than the mini_demo described above. The create_publisher() function in quixote.demo.__init__.py creates a publisher whose root directory is an instance of quixote.demo.root.RootDirectory. Note that the source code is a file named "root.ptl". The suffix of "ptl" indicates that it is a PTL file, and the import must follow a call to quixote.enable_ptl() or else the source file will not be found or compiled. The quixote.demo.__init__.py file takes care of that.

Take a look at the source code in root.ptl. You will see code that looks like regular python, except that some function definitions have "[html]" between the function name and the parameter list. These functions are ptl templates. For details about PTL, see the PTL.txt file.

This RootDirectory class is similar to the one in mini_demo.py, in that it has a _q_index() method and '' appears in the _q_exports list. One new feature here is the presence of a tuple in the _q_exports list. Most of the time, the elements of the _q_exports lists are just strings that name attributes that should be available as URL components. This pattern does not work, however, when the particular URL component you want to use includes characters (like '.') that can't appear in Python attribute names. To work around these cases, the _q_exports list may contain tuples such as ("favicon.ico", "favicon_ico") to designate "favicon_ico" as the attribute name corresponding the the "favicon.ico" URL component.

Looking at the RootDirectoryMethods, including plain(), css() and favon_ico(), you will see examples where, in addition to returning a string containing the body of the HTTP response, the function also makes side-effect modifications to the response object itself, to set the content type and the expiration time for the response. Most of the time, these direct modifications to the response are not needed. When they are, though, the get_response() function gives you direct access to the response instance.

The RootDirectory here also sets an 'extras' attribute to be an instance of ExtraDirectory, imported from the quixote.demo.extras module. Note that 'extras' also appears in the _q_exports list. This is the ordinary way to extend your URL space through another '/'. For example, the URL path '/extras/' will result in a call to the ExtraDirectory instance's _q_index() method.



今天看的是demo.html的第三部分内容: Understanding the root demo。

在命令行输入:

simple_server.py --factory quixote.demo.create_publisher

在浏览器输入:

http://localhost:8080

我们看到了这样的页面:

Hello, world!

To understand what's going on here, be sure to read the doc/demo.txt file included with Quixote.

Here are some features of this demo:

  • simple: A Python function that generates a very simple document.
  • plain: A Python function that generates a plain text document.
  • error: A Python function that raises an exception.
  • publish_error: A Python function that raises a PublishError exception. This exception will be caught by a _q_exception_handler method.
  • dumpreq: Print out the contents of the HTTPRequest object.
  • css: The stylesheet for this document.
  • extras/: Demos of some of Quixote's more advanced features.
和mini_demo不同的是,mini_demo的程序都在一个文件mini_demo.py中,而root用到了PTL文件,代码分开在两个文件中:__init__.py和root.ptl。

__init__.py的内容很简单,是mini_demo.py的一部分:

from  quixote  import  enable_ptl
from  quixote.publish  import  Publisher
enable_ptl()

def  create_publisher():
    
from  quixote.demo.root  import  RootDirectory
    
return  Publisher(RootDirectory(), display_exceptions = ' plain ' )

root.ptl的内容也和mini_demo.py大部分类似,区别在几个地方:
  1. 有些函数名和参数列表之间有一个”[html]”,表示这些函数是ptl模板。
  2. _q_exports中出现了一个tuple,用来转化带”.“的路径组件,因为Python的属性名里是不能出现”."的。
  3. 有些方法例如plain()、css()和favicon_ico,他们修改reponse对象自身,而不是返回一个包含HTTP body的字符串。
  4. RootDirectory类的一个属性extras = ExtraDirectory(),这将调用ExtraDirectory的一个实例。具体的在下一部分里解释。
用到PTL需要注意:

from  quixote  import  enable_ptl
enable_ptl()

这两条语句不可少。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值