yamldirs - 从yaml规范创建目录和文件。
项目描述
从yaml规范创建目录和文件。
此模块是为了快速创建和清理用于测试目的的目录树而创建的。
安装
pip install yamldirs
新特性(1.1.7版本): 添加了一个(非常基础的)yamldirs 命令
yamldirs dirname
打印出以 dirname 为根的目录树的yaml(格式虽然技术上正确,但可能需要一些人工编辑)。
要提取由 file.yaml 定义的目录树
yamldirs file.yaml
扩展名必须是 exactly .yaml。
用法
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.
如果您不希望工作目录消失(通常是在测试失败并希望检查目录树时的情况)您需要将with语句更改为
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记录(关联数组)语法,字段名(冒号加空格左侧)是文件名,值是文件内容。例如,包含文本 hello world 的单个文件
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 列表语法。
- foo.txt - bar.txt
为了更加方便,可以使用仅包含单个字段的记录列表来创建包含内容的文件。
- 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
请注意,您不能在相同嵌套级别中混合记录和列表语法
# 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" } } }
扩展 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()
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分发
构建分发
yamldirs-1.1.16.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | a67ba7923d31f59191cdf94c3854915ec6399b72d50965e046bb8d14554be231 |
|
MD5 | 11a993dfb99bd6de6927c00eb39238ea |
|
BLAKE2b-256 | 3af9320c30604d3ca0d9ca0afc7408167731cc5e87838399a9bad8d72ad8b84e |
yamldirs-1.1.16-py2.py3-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 257bfc4052c611e118cc2d69ad72eac14041b6b0e6b9a97d169f6d9050a0b341 |
|
MD5 | c049541d646403292b74c61337af4014 |
|
BLAKE2b-256 | e64e83456f08e2df53c344ef1cefefac7c54f7fd9776012744f913ca95556596 |