yamldirs - create directories and files (incl. contents) from yaml spec.
Project description
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
Usage
The most common usage scenario for testing will typically look like this:
from yamldirs import create_files def test_relative_imports(): files = """ relimp: - __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. # `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 """)
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_root(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: if content: fp.write(content) if the backend has an efficient way of creating empty files you can also override:: def make_empty_file(self, fname): ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for yamldirs-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbe7f5c67c82d9f8e96c5d9d059b957f7e59ef92242efebd3ca315e4da7c2259 |
|
MD5 | 0bb6a4815866ff14c3fdaeca67f64e9c |
|
BLAKE2b-256 | 012e64ff3d595f0206c25d50bb6cba999765c74fb8c7653fcba3513667aa09b6 |