快速创建文件和目录结构。
项目描述
treeshape 允许您在磁盘上快速创建文件和目录结构。
例如
from treeshape import ( CONTENT, make_tree, PERMISSIONS, ) make_tree('.', { 'logs/': None, 'README': {CONTENT: "A simple directory layout\n"}, 'data/input': {CONTENT: "All of our input data\n"}, 'bin/script': {CONTENT: "#!/bin/sh\necho 'Hello'\n", PERMISSIONS: 0755}, })
将创建如下目录结构
$ find . . ./logs ./data ./data/input ./README $ cat README A simple directory layout $ cat data/input All of our input data
这对于接触磁盘的测试特别有用。
如果您不喜欢明确表达,也可以从粗略规范创建相同的目录结构
from treeshape import ( from_rough_spec, make_tree, ) make_tree('.', from_rough_spec([ 'logs/', ('README', "A simple directory layout\n"), ('data/input', "All of our input data\n"), ('bin/script', "#!/bin/sh\necho 'Hello'\n", 0755), ]))
这还提供为 Fixture(见 python-fixtures)。因此,如果您正在使用 testtools,您也可以在测试期间这样做
def test_a_thing(self): self.useFixture(FileTree(from_rough_spec([ 'logs/', ('README', "A simple directory layout\n"), ('data/input', "All of our input data\n"), ]))) # your test here