<<

yamldirs Documentation Release 1.1.8

Mar 06, 2020

Contents

1 Usage 3

2 Syntax 5

3 Extending yamldirs 9

4 API documentation 11

5 Indices and tables 13

Python Module Index 15

Index 17

i ii yamldirs Documentation, Release 1.1.8

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 yamldirs

New in version 1.1.7: a (very basic) yamldirs has been added: yamldirs dirname prints the yaml for the directory rooted dirname (the formatting, while technically correct, probably needs some human editing). To extract the directory tree defined by .yaml: yamldirs file.yaml the extension needs to be exactly .yaml.

Contents 1 yamldirs Documentation, Release 1.1.8

2 Contents CHAPTER 1

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 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 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 fails and you want to inspect the directory tree)

3 yamldirs Documentation, Release 1.1.8 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('/to/parent/directory', """ foo.txt: | hello bar.txt: | world """)

4 Chapter 1. Usage CHAPTER 2

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 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 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:

5 yamldirs Documentation, Release 1.1.8

- 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: # -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 dir2 a list item: dir1: - file1 - file2 - dir2: # <== Correct. - file3 - file4 the corresponding json is:

6 Chapter 2. Syntax yamldirs Documentation, Release 1.1.8

>>> 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.

7 yamldirs Documentation, Release 1.1.8

8 Chapter 2. Syntax CHAPTER 3

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.(dirname) os.chdir(dirname) self.make_list(content) os.chdir(cwd)

def make_file(self, filename, content): with open(filename,'') as fp: fp.(content)

def make_empty_file(self, fname): open(fname,'w').close()

9 yamldirs Documentation, Release 1.1.8

10 Chapter 3. Extending yamldirs CHAPTER 4

API documentation

class yamldirs.filemaker.Filemaker(root, fdef )

make_file(filename, content) Create a new file with name filename and content content. mkdir(dirname) popd() pushd(dirname) class yamldirs.filemaker.FilemakerBase(root, fdef ) Override marked methods to do something useful. Base class serves as a dry-run step generator. make_dict_item(dct) make_file(filename, content) Create a new file with name filename and content content. Must be overridden. make_list_item(lst) mkdir(dirname) popd() pushd(dirname) value2str(val) exception yamldirs.filemaker.UnknownType Found a that we don’t know how to handle. exception yamldirs.filemaker.YamlDirsException Base class for YamlDirs exceptions. yamldirs.filemaker.create_files(*args, **kwds) Contextmanager that creates a directory structure from a yaml description.

11 yamldirs Documentation, Release 1.1.8

12 Chapter 4. API documentation CHAPTER 5

Indices and tables

• genindex • modindex • search

13 yamldirs Documentation, Release 1.1.8

14 Chapter 5. Indices and tables Python Module Index

y yamldirs.filemaker, 11

15 yamldirs Documentation, Release 1.1.8

16 Python Module Index Index

C create_files() (in module yamldirs.filemaker), 11 F Filemaker (class in yamldirs.filemaker), 11 FilemakerBase (class in yamldirs.filemaker), 11 M make_dict_item() (yamldirs.filemaker.FilemakerBase method), 11 make_file() (yamldirs.filemaker.Filemaker method), 11 make_file() (yamldirs.filemaker.FilemakerBase method), 11 make_list_item() (yamldirs.filemaker.FilemakerBase method), 11 mkdir() (yamldirs.filemaker.Filemaker method), 11 mkdir() (yamldirs.filemaker.FilemakerBase method), 11 P popd() (yamldirs.filemaker.Filemaker method), 11 popd() (yamldirs.filemaker.FilemakerBase method), 11 pushd() (yamldirs.filemaker.Filemaker method), 11 pushd() (yamldirs.filemaker.FilemakerBase method), 11 U UnknownType, 11 V value2str() (yamldirs.filemaker.FilemakerBase method), 11 Y yamldirs.filemaker (module), 11 YamlDirsException, 11

17