个人工具
登录
查看“Zope3宝典/映射对象到文件系统”的源代码 - Ubuntu中文
页面
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“Zope3宝典/映射对象到文件系统”的源代码
来自Ubuntu中文
←
Zope3宝典/映射对象到文件系统
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
=== 22.3 Step III: The IReadDirectory implementation(22.3 步骤 III: IReadDirectory 的实现) === After all the preparations are complete, we are finally ready to give our content components, MessageBoard and Message, a cool filesystem representation.<br>在所有准备完成后,我们最终准备给我们的内容组件,留言薄和消息,一个酷的文件系统表示。 ==== 22.3.1 (a) The Implementation(22.3.1 (a) 实现) ==== The first fact we should notice is that zope.app.filerepresentation.ReadDirectory has already a nice implementation, except for the superfluous SiteManager and the missing contents file. So we simply take this class (subclass it) and merely overwrite keys(), get(key,default=None), and `<u>len</u>`(). All other methods depend on these three. So our code for the ReadDirectory looks something like that (place in filerepresentation.py): <br>我们应该注意的第一个事实是 zope.app.filerepresentation.ReadDirectory 已经有一个好的实现,除了多余的 SiteManager 和缺少的内容文件。因此我们只需简单的使用该类(它的子类)并且只要重写 keys(), get(key,default=None) 和 `<u>len</u>`() 即可。所有其它方法都有赖于这三个。所以我们为 ReadDirectory 所写的代码看上去如下所示(放在 filerepresentation.py 文件中): <pre><nowiki>#!python from zope.app.filerepresentation.interfaces import IReadDirectory from zope.app.folder.filerepresentation import \ ReadDirectory as ReadDirectoryBase class ReadDirectory(ReadDirectoryBase): """An special implementation of the directory.""" implements(IReadDirectory) def keys(self): keys = self.context.keys() return list(keys) + ['contents'] def get(self, key, default=None): if key == 'contents': return VirtualContentsFile(self.context) return self.context.get(key, default) def <u>len</u>(self): l = len(self.context) return l+1 </nowiki></pre> * Line 10-12: When being asked for a list of names available for this container, we get the list of keys plus our virtual contents file.<br>第 10-12 行: 当这容器要求可用名字列表时,我们得到一个附加我们虚拟内容文件的关键字列表。 * Line 14-17: All objects are simply found in the context (MessageBoard or Message) itself, except the contents. When the system asks for the contents, we simply give it a VirtualContentsFile instance that we prepared in the previous section and we do not have to worry about anything, since we know that the system knows how to handle zope.app.file.interfaces.IFile objects.<br>第 14-17 行: 除了内容,所有对象都可以在上下文(MessageBoard 或 Message)中被简单的发现。当系统要求内容时,我们可以简单地给它一个我们在上一节准备好的 VirtualContentsFile 实例。同时我们不用担心任何事,因为我们知道系统知道要如何处理 zope.app.file.interfaces.IFile 对象。 * Line 19-21: Obviously, we pretend to have one more object than we actually have. <br>第 19-21 行: 很明显,我们假定拥有比我们真正拥有的对象还多一个。 Now we are done with our implementation. Let’s write some unit tests to ensure the functionality and then register the filesystem components. <br>现在我们完成了实现。让我们写一些单元测试以保证功能,然后注册文件系统组件。 ==== 22.3.2 (b) The Tests(22.3.2 (b) 测试) ==== For testing the ReadDirectory implementation, we again need to test it with the MessageBoard and Message components. So similar to the previous tests, we have a base test with specific implementations. Also note that it will not be necessary to test all IReadDirectory methods, since they are already tested in the base class tests. So we are just going to test the methods we have overridden: <br>为了测试 ReadDirectory 的实现,我们还是需要用 MessageBoard 和 Message 组件来对它进行测试。所以同前个测试一样,我们对具体实现做一个基本测试。注意,并不需要测试 IReadDirectory 的所有方法,因为它们已经在基本类测试中测试过了。因此我们打算只测试我们没测试过的方法。 <pre><nowiki>#!python from book.messageboard.filerepresentation import ReadDirectory class ReadDirectoryTestBase(PlacelessSetup): def _makeDirectoryObject(self): raise NotImplemented def _makeTree(self): base = self._makeDirectoryObject() msg1 = Message() msg1.title = 'Message 1' msg1.description = 'This is Message 1.' msg11 = Message() msg11.title = 'Message 1-1' msg11.description = 'This is Message 1-1.' msg2 = Message() msg2.title = 'Message 1' msg2.description = 'This is Message 1.' msg1['msg11'] = msg11 base['msg1'] = msg1 base['msg2'] = msg2 return ReadDirectory(base) def testKeys(self): tree = self._makeTree() keys = list(tree.keys()) keys.sort() self.assertEqual(keys, ['contents', 'msg1', 'msg2']) keys = list(ReadDirectory(tree['msg1']).keys()) keys.sort() self.assertEqual(keys, ['contents', 'msg11']) def testGet(self): tree = self._makeTree() self.assertEqual(tree.get('msg1'), tree.context['msg1']) self.assertEqual(tree.get('msg3'), None) default = object() self.assertEqual(tree.get('msg3', default), default) self.assertEqual(tree.get('contents').<u>class</u>, VirtualContentsFile) def testLen(self): tree = self._makeTree() self.assertEqual(len(tree), 3) self.assertEqual(len(ReadDirectory(tree['msg1'])), 2) self.assertEqual(len(ReadDirectory(tree['msg2'])), 1) class MessageReadDirectoryTest(ReadDirectoryTestBase, unittest.TestCase): def _makeDirectoryObject(self): return Message() class MessageBoardReadDirectoryTest(ReadDirectoryTestBase, unittest.TestCase): def _makeDirectoryObject(self): return MessageBoard() </nowiki></pre> * Line 5-6: Return an instance of the object to be tested.<br>返回要被测试的对象实例 * Line 8-22: Create an interesting message tree on top of the base. This will allow some more detailed testing.<br>创建一个有趣的基准之上的消息树。这将允许再多的细节测试。 * Line 24-31: Make sure this contents file and the sub-messages are correctly listed.<br>确保该内容文件和子消息被正确列出。 * Line 33-40: Now let’s also make sure that the objects we get are the right ones.<br>现在让我们也确保我们得到的对象是对的。 * Line 42-46: A simple test for the number of contained items (including the contents).<br>简单测试所含项目数(包括内容)。 * Line 49-60: The concrete implementations of the base test. Nothing special.<br>基本测试的混和实现。没有特殊。 After you are done writing the tests, do not forget to add the two new TestCases to the TestSuite.<br>在你完成测试之后,不要忘了添加两个 TestCases 到 TestSuite中去。 ==== 22.3.3 (c) The Configuration(22.3.3 (c) 配置) ==== Finally we simply register our new components properly using the following ZCML directives:<br>最终我们适当地使用下列 ZCML 语句来注册我们的新的组件: <pre><nowiki> 1 <adapter 2 for=".interfaces.IMessageBoard" 3 provides="zope.app.filerepresentation.interfaces.IReadDirectory" 4 factory=".filerepresentation.ReadDirectory" 5 permission="zope.View"/> 6 7 <adapter 8 for=".interfaces.IMessage" 9 provides="zope.app.filerepresentation.interfaces.IReadDirectory" 10 factory=".filerepresentation.ReadDirectory" 11 permission="zope.View"/> </nowiki></pre> That’s it. You can now restart Zope and test the filesystem representation with an FTP client of your choice. In the following sequence diagram you can see how a request is guided to find its information and return it properly.<br>就是这样。你现在可以重启 Zope 并用你选的FTP客户端来测试文件系统表示。从下图的顺序图中你可以看到一个请求是如何被指引找到它的信息并以适当方式返回的。 PIC Figure 22.1: Collaboration diagram of the inner working from requesting the contents “file” to receiving the actual data.<br>图 22.1: 从请求内容“文件”到收到实际数据的内部运行的关联图
返回至
Zope3宝典/映射对象到文件系统
。