Name | yamldirs JSON |
Version |
1.1.16
JSON |
| download |
home_page | https://github.com/datakortet/yamldirs |
Summary | yamldirs - create directories and files (incl. contents) from yaml spec. |
upload_time | 2021-11-10 18:42:41 |
maintainer | |
docs_url | https://pythonhosted.org/yamldirs/ |
author | Bjorn Pettersen |
requires_python | |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
yamldirs - create directories and files (incl. contents) from yaml spec.
========================================================================
.. image:: https://github.com/datakortet/yamldirs/actions/workflows/ci-cd.yml/badge.svg
:target: https://github.com/datakortet/yamldirs/actions/workflows/ci-cd.yml
.. image:: https://codecov.io/gh/datakortet/yamldirs/branch/master/graph/badge.svg?token=rG43wgZ3aK
:target: https://codecov.io/gh/datakortet/yamldirs
.. image:: https://pepy.tech/badge/yamldirs
:target: https://pepy.tech/project/yamldirs
:alt: Downloads
.. image:: https://readthedocs.org/projects/yamldirs/badge/?version=latest
:target: https://yamldirs.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
Create directories and files (including content) from yaml spec.
This module was created to rapidly create, and clean up, directory trees
for testing purposes.
Installation::
pip install yamldirs
**New in version 1.1.7:** a (very basic) ``yamldirs`` command has been added::
yamldirs dirname
prints the yaml for the directory tree rooted at ``dirname`` (the formatting, while
technically correct, probably needs some human editing).
To extract the directory tree defined by ``file.yaml``::
yamldirs file.yaml
the extension needs to be exactly ``.yaml``.
Usage
-----
The YAML record syntax is::
fieldname: content
fieldname2: |
multi
line
content
nested:
record: content
``yamldirs`` interprets a (possibly nested) yaml record structure and creates
on-disk file structures that mirrors the yaml structure.
The most common usage scenario for testing will typically look like this::
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.
If you don't want the workdir to disappear (typically the case if a test fails
and you want to inspect the directory tree) you'll need to change the
with-statement to::
with create_files(files, cleanup=False) as workdir:
...
``yamldirs`` can of course be used outside of testing scenarios too::
from yamldirs import Filemaker
Filemaker('path/to/parent/directory', """
foo.txt: |
hello
bar.txt: |
world
""")
Syntax
------
If you're new to yaml and receive a ``yaml.parser`` error you don't understand,
it might be useful to run your yaml through an online validater
(e.g. https://codebeautify.org/yaml-validator).
The yaml syntax to create a single file::
foo.txt
Files with contents uses the YAML record (associative array) syntax with the
field name (left of colon+space) is the file name, and the value is the file
contents. Eg. a single file containing the text `hello world`::
foo.txt: hello world
for more text it is better to use a continuation line (``|`` to keep line
breaks and ``>`` to convert single newlines to spaces)::
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.
To create empty files you can do::
foo.txt: ""
bar.txt: ""
but as a convenience you can also use yaml list syntax::
- foo.txt
- bar.txt
For even more convenience, files with content can be created using lists
of records with only one field each::
- foo.txt: |
hello
- bar.txt: |
world
.. note:: This is equivalent to this json: ``[{"foo.txt": "hello"}, {"bar.txt": "world"}]``
This is especially useful when you have a mix of empty and non-empty filess::
mymodule:
- __init__.py
- mymodule.py: |
print "hello world"
directory with two (empty) files (YAML record field with list value)::
foo:
- bar
- baz
an empty directory must use YAML's inline list syntax::
foo: []
nested directories with files::
foo:
- a.txt: |
contents of the file named a.txt
- bar:
- b.txt: |
contents of the file named b.txt
It's worth noting that you cannot mix record and list syntax in the same
nesting level::
# 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
the solution is to make ``dir2`` a list item::
dir1:
- file1
- file2
- dir2: # <== Correct.
- file3
- file4
the corresponding json is::
>>> print json.dumps(yaml.load("""
... dir1:
... - file1
... - file2
... - dir2:
... - file3
... - file4
... """), indent=4)
{
"dir1": [
"file1",
"file2",
{
"dir2": [
"file3",
"file4"
]
}
]
}
or make the first level (``b, c, d`` below) record fields::
a:
b: b
c: c
d:
e: e
corresponding json::
>>> print json.dumps(yaml.load("""
... a:
... b: b
... c: c
... d:
... e: e
... """), indent=4)
{
"a": {
"c": "c",
"b": "b",
"d": {
"e": "e"
}
}
}
.. note:: (Json)
YAML is a superset of json, so you can also use json syntax if that is more
convenient.
Extending yamldirs
------------------
To extend ``yamldirs`` to work with other storage backends, you'll need to
inherit from ``yamldirs.filemaker.FilemakerBase`` and override the following
methods::
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()
Raw data
{
"_id": null,
"home_page": "https://github.com/datakortet/yamldirs",
"name": "yamldirs",
"maintainer": "",
"docs_url": "https://pythonhosted.org/yamldirs/",
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Bjorn Pettersen",
"author_email": "bp@datakortet.no",
"download_url": "https://files.pythonhosted.org/packages/3a/f9/320c30604d3ca0d9ca0afc7408167731cc5e87838399a9bad8d72ad8b84e/yamldirs-1.1.16.tar.gz",
"platform": "",
"description": "\n\nyamldirs - create directories and files (incl. contents) from yaml spec.\n========================================================================\n\n.. image:: https://github.com/datakortet/yamldirs/actions/workflows/ci-cd.yml/badge.svg\n :target: https://github.com/datakortet/yamldirs/actions/workflows/ci-cd.yml\n\n\n.. image:: https://codecov.io/gh/datakortet/yamldirs/branch/master/graph/badge.svg?token=rG43wgZ3aK\n :target: https://codecov.io/gh/datakortet/yamldirs\n\n\n.. image:: https://pepy.tech/badge/yamldirs\n :target: https://pepy.tech/project/yamldirs\n :alt: Downloads\n \n.. image:: https://readthedocs.org/projects/yamldirs/badge/?version=latest\n :target: https://yamldirs.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n \n\nCreate directories and files (including content) from yaml spec.\n\nThis module was created to rapidly create, and clean up, directory trees\nfor testing purposes.\n\nInstallation::\n\n pip install yamldirs\n\n**New in version 1.1.7:** a (very basic) ``yamldirs`` command has been added::\n\n yamldirs dirname\n\nprints the yaml for the directory tree rooted at ``dirname`` (the formatting, while\ntechnically correct, probably needs some human editing). \n\nTo extract the directory tree defined by ``file.yaml``::\n\n yamldirs file.yaml\n\nthe extension needs to be exactly ``.yaml``.\n\n\n\nUsage\n-----\n\nThe YAML record syntax is::\n\n fieldname: content\n fieldname2: |\n multi\n line\n content\n nested:\n record: content\n\n``yamldirs`` interprets a (possibly nested) yaml record structure and creates\non-disk file structures that mirrors the yaml structure.\n\nThe most common usage scenario for testing will typically look like this::\n\n from yamldirs import create_files\n\n def test_relative_imports():\n files = \"\"\"\n foodir:\n - __init__.py\n - a.py: |\n from . import b\n - b.py: |\n from . import c\n - c.py\n \"\"\"\n with create_files(files) as workdir:\n # workdir is now created inside the os's temp folder, containing\n # 4 files, of which two are empty and two contain import\n # statements. Current directory is workdir.\n\n # `workdir` is automatically removed after the with statement.\n\nIf you don't want the workdir to disappear (typically the case if a test fails\nand you want to inspect the directory tree) you'll need to change the\nwith-statement to::\n\n with create_files(files, cleanup=False) as workdir:\n ...\n\n``yamldirs`` can of course be used outside of testing scenarios too::\n\n from yamldirs import Filemaker\n\n Filemaker('path/to/parent/directory', \"\"\"\n foo.txt: |\n hello\n bar.txt: |\n world\n \"\"\")\n\nSyntax\n------\nIf you're new to yaml and receive a ``yaml.parser`` error you don't understand,\nit might be useful to run your yaml through an online validater\n(e.g. https://codebeautify.org/yaml-validator).\n\nThe yaml syntax to create a single file::\n\n foo.txt\n\nFiles with contents uses the YAML record (associative array) syntax with the\nfield name (left of colon+space) is the file name, and the value is the file\ncontents. Eg. a single file containing the text `hello world`::\n\n foo.txt: hello world\n\nfor more text it is better to use a continuation line (``|`` to keep line\nbreaks and ``>`` to convert single newlines to spaces)::\n\n foo.txt: |\n Lorem ipsum dolor sit amet, vis no altera doctus sanctus,\n oratio euismod suscipiantur ne vix, no duo inimicus\n adversarium. Et amet errem vis. Aeterno accusamus sed ei,\n id eos inermis epicurei. Quo enim sonet iudico ea, usu\n et possit euismod.\n\nTo create empty files you can do::\n\n foo.txt: \"\"\n bar.txt: \"\"\n\nbut as a convenience you can also use yaml list syntax::\n\n - foo.txt\n - bar.txt\n\nFor even more convenience, files with content can be created using lists\nof records with only one field each::\n\n - foo.txt: |\n hello\n - bar.txt: |\n world\n\n.. note:: This is equivalent to this json: ``[{\"foo.txt\": \"hello\"}, {\"bar.txt\": \"world\"}]``\n\nThis is especially useful when you have a mix of empty and non-empty filess::\n\n mymodule:\n - __init__.py\n - mymodule.py: |\n print \"hello world\"\n\ndirectory with two (empty) files (YAML record field with list value)::\n\n foo:\n - bar\n - baz\n\nan empty directory must use YAML's inline list syntax::\n\n foo: []\n\nnested directories with files::\n\n foo:\n - a.txt: |\n contents of the file named a.txt\n - bar:\n - b.txt: |\n contents of the file named b.txt\n\nIt's worth noting that you cannot mix record and list syntax in the same\nnesting level::\n\n # wrong\n dir1: # top-level record\n - file1 # first level is a list..\n - file2 # .. file1 and file2 are here empty files\n dir2: # <== ERROR: You cannot define a mapping item when in a sequence\n - file3\n - file4\n\nthe solution is to make ``dir2`` a list item::\n\n dir1: \n - file1 \n - file2 \n - dir2: # <== Correct.\n - file3\n - file4\n\nthe corresponding json is::\n\n >>> print json.dumps(yaml.load(\"\"\"\n ... dir1:\n ... - file1\n ... - file2\n ... - dir2:\n ... - file3\n ... - file4\n ... \"\"\"), indent=4)\n {\n \"dir1\": [\n \"file1\",\n \"file2\",\n {\n \"dir2\": [\n \"file3\",\n \"file4\"\n ]\n }\n ]\n }\n\nor make the first level (``b, c, d`` below) record fields::\n\n a:\n b: b\n c: c\n d:\n\t e: e\n\ncorresponding json::\n\n >>> print json.dumps(yaml.load(\"\"\"\n ... a:\n ... b: b\n ... c: c\n ... d:\n ... e: e\n ... \"\"\"), indent=4)\n {\n \"a\": {\n \"c\": \"c\",\n \"b\": \"b\",\n \"d\": {\n \"e\": \"e\"\n }\n }\n }\n\n.. note:: (Json)\n YAML is a superset of json, so you can also use json syntax if that is more\n convenient.\n\nExtending yamldirs\n------------------\nTo extend ``yamldirs`` to work with other storage backends, you'll need to\ninherit from ``yamldirs.filemaker.FilemakerBase`` and override the following\nmethods::\n\n class Filemaker(FilemakerBase):\n def goto_directory(self, dirname):\n os.chdir(dirname)\n\n def makedir(self, dirname, content):\n cwd = os.getcwd()\n os.mkdir(dirname)\n os.chdir(dirname)\n self.make_list(content)\n os.chdir(cwd)\n\n def make_file(self, filename, content):\n with open(filename, 'w') as fp:\n fp.write(content)\n\n def make_empty_file(self, fname):\n open(fname, 'w').close()\n\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "yamldirs - create directories and files (incl. contents) from yaml spec.",
"version": "1.1.16",
"project_urls": {
"Homepage": "https://github.com/datakortet/yamldirs"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e64e83456f08e2df53c344ef1cefefac7c54f7fd9776012744f913ca95556596",
"md5": "c049541d646403292b74c61337af4014",
"sha256": "257bfc4052c611e118cc2d69ad72eac14041b6b0e6b9a97d169f6d9050a0b341"
},
"downloads": -1,
"filename": "yamldirs-1.1.16-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "c049541d646403292b74c61337af4014",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 12196,
"upload_time": "2021-11-10T18:42:40",
"upload_time_iso_8601": "2021-11-10T18:42:40.240661Z",
"url": "https://files.pythonhosted.org/packages/e6/4e/83456f08e2df53c344ef1cefefac7c54f7fd9776012744f913ca95556596/yamldirs-1.1.16-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3af9320c30604d3ca0d9ca0afc7408167731cc5e87838399a9bad8d72ad8b84e",
"md5": "11a993dfb99bd6de6927c00eb39238ea",
"sha256": "a67ba7923d31f59191cdf94c3854915ec6399b72d50965e046bb8d14554be231"
},
"downloads": -1,
"filename": "yamldirs-1.1.16.tar.gz",
"has_sig": false,
"md5_digest": "11a993dfb99bd6de6927c00eb39238ea",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11750,
"upload_time": "2021-11-10T18:42:41",
"upload_time_iso_8601": "2021-11-10T18:42:41.563125Z",
"url": "https://files.pythonhosted.org/packages/3a/f9/320c30604d3ca0d9ca0afc7408167731cc5e87838399a9bad8d72ad8b84e/yamldirs-1.1.16.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-11-10 18:42:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "datakortet",
"github_project": "yamldirs",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [],
"lcname": "yamldirs"
}