跳转到主要内容

yamldirs - 从yaml规范创建目录和文件。

项目描述

https://github.com/datakortet/yamldirs/actions/workflows/ci-cd.yml/badge.svg https://codecov.io/gh/datakortet/yamldirs/branch/master/graph/badge.svg?token=rG43wgZ3aK Downloads Documentation Status

从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 (11.8 KB 查看散列

上传时间

构建分发

yamldirs-1.1.16-py2.py3-none-any.whl (12.2 KB 查看散列

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面