yamldirs-从yaml规范创建目录和文件(包括内容)。
yamldirs的Python项目详细描述
亚马尔得斯
从yaml规范创建目录和文件(包括内容)。
创建此模块是为了快速创建和清理目录树 用于测试目的。
安装:
pip install yamldirs
使用量
yaml记录语法为:
fieldname: content fieldname2: | multi line content nested: record: content
yamldirs解释(可能是嵌套的)yaml记录结构并创建 镜像yaml结构的磁盘上文件结构。
测试最常见的使用场景通常如下:
from yamldirs import create_files def test_relative_imports(): files = """ foodir: - __init__.py - a.py: | from . import b - b.py: | from . import c - c.py """ with create_files(files) as workdir: # workdir is now created inside the os's temp folder, containing # 4 files, of which two are empty and two contain import # statements. Current directory is workdir. # `workdir` is automatically removed after the with statement.
如果您不想workdir消失(通常是在测试失败的情况下 你想检查目录树)你需要改变 对账单:
with create_files(files, cleanup=False) as workdir: ...
yamldirs当然也可以在测试场景之外使用:
from yamldirs import Filemaker Filemaker('path/to/parent/directory', """ foo.txt: | hello bar.txt: | world """)
语法
如果您是yaml新手,收到一个yaml.parser错误,您不理解, 通过在线验证程序运行yaml可能会很有用。 (例如https://codebeautify.org/yaml-validator)。
创建单个文件的yaml语法:
foo.txt
包含内容的文件使用yaml record(associative array)语法 字段名(冒号+空格的左边)是文件名,值是文件名 内容。一个包含文本的文件
foo.txt: hello world
对于更多文本,最好使用续行(|来保持行 换行符和>将单行换行符转换为空格:
foo.txt: | Lorem ipsum dolor sit amet, vis no altera doctus sanctus, oratio euismod suscipiantur ne vix, no duo inimicus adversarium. Et amet errem vis. Aeterno accusamus sed ei, id eos inermis epicurei. Quo enim sonet iudico ea, usu et possit euismod.
要创建空文件,您可以执行以下操作:
foo.txt: "" bar.txt: ""
但为了方便起见,您还可以使用yaml list语法:
- foo.txt - bar.txt
为了更方便起见,可以使用列表创建包含内容的文件 只有一个字段的记录数:
- foo.txt: | hello - bar.txt: | world
注意
这相当于这个json:[{"foo.txt":"hello"}, {"bar.txt": "world"}]
当您同时拥有空文件和非空文件时,这尤其有用:
mymodule: - __init__.py - mymodule.py: | print "hello world"
包含两个(空)文件的目录(具有列表值的yaml记录字段):
foo: - bar - baz
空目录必须使用yaml的内联列表语法:
foo: []
包含文件的嵌套目录:
foo: - a.txt: | contents of the file named a.txt - bar: - b.txt: | contents of the file named b.txt
值得注意的是,不能将record和list语法混合在一起 嵌套级别:
# wrong dir1: # top-level record - file1 # first level is a list.. - file2 # .. file1 and file2 are here empty files dir2: # <== ERROR: You cannot define a mapping item when in a sequence - file3 - file4
解决方案是使dir2成为一个列表项:
dir1: - file1 - file2 - dir2: # <== Correct. - file3 - file4
相应的json是:
>>> print json.dumps(yaml.load(""" ... dir1: ... - file1 ... - file2 ... - dir2: ... - file3 ... - file4 ... """), indent=4) { "dir1": [ "file1", "file2", { "dir2": [ "file3", "file4" ] } ] }
或者创建第一级(b, c, d以下)记录字段:
a: b: b c: c d: e: e
对应的json:
>>> print json.dumps(yaml.load(""" ... a: ... b: b ... c: c ... d: ... e: e ... """), indent=4) { "a": { "c": "c", "b": "b", "d": { "e": "e" } } }
注意
(JSON格式) yaml是json的一个超集,因此您还可以使用json语法 方便。
扩展yamldirs
要扩展yamldirs以使用其他存储后端,您需要 从yamldirs.filemaker.FilemakerBase继承并重写以下内容 方法:
class Filemaker(FilemakerBase): def goto_directory(self, dirname): os.chdir(dirname) def makedir(self, dirname, content): cwd = os.getcwd() os.mkdir(dirname) os.chdir(dirname) self.make_list(content) os.chdir(cwd) def make_file(self, filename, content): with open(filename, 'w') as fp: fp.write(content) def make_empty_file(self, fname): open(fname, 'w').close()