Module f.io

Part of fixture

Working with temporary file systems.

TempIO: a temporary file system object

This object is useful for tests that need to set up a directory structure and work with files and paths. Once you instantiate it, you have a temporary directory that will self-destruct when it falls out of scope:

>>> from fixture import TempIO
>>> tmp = TempIO()
>>> tmp #doctest: +ELLIPSIS
'/.../tmp_fixture...'

Add sub-directories by simply assigning an attribute the basename of the new subdirectory, like so:

>>> tmp.incoming = "incoming"
>>> tmp.incoming.exists()
True

The new attribute is now an absolute path to a subdirectory, "incoming", of the tmp root, as well as an object itself. Note that tmp and tmp.incoming are just string objects, but with several os.path methods mixed in for convenience. See the DirPath API for details. However, you can pass it to other objects and it will represent itself as its absolute path.

You can also insert files to the directory with putfile():

>>> foopath = tmp.incoming.putfile("foo.txt", "contents of foo")
>>> tmp.incoming.join("foo.txt").exists()
True

The directory root will self-destruct when it goes out of scope or atexit. You can explicitly delete the object at your test's teardown if you like:

>>> tmpdir = str(tmp) # making sure it's a copy
>>> del tmp
>>> os.path.exists(tmpdir)
False

The fixture.io module

Function TempIO self-destructing, temporary directory.
Function _expunge called internally to remove a tmp dir.
Function _expunge_all exit function to remove all registered tmp dirs.
Function mkdirall walks the path and makes any non-existant dirs.
Function putfile opens filename in writing mode, writes contents and closes.
Class DirPath a directory path.
Class DeletableDirPath No class docstring; 2/2 methods documented
def TempIO(deferred=False, **kw):

self-destructing, temporary directory.

Takes the same keyword args as tempfile.mkdtemp with these additional keywords:

  • deferred

    • if True, destruction will be put off until atexit. Otherwise, it will be destructed when it falls out of scope
def _expunge(tmpdir):
called internally to remove a tmp dir.
def _expunge_all():
exit function to remove all registered tmp dirs.
def mkdirall(path, mkdir=os.mkdir):
walks the path and makes any non-existant dirs.

optional keyword mkdir is the callback for making a single dir

def putfile(filename, contents, filelike=None, mode=None):
opens filename in writing mode, writes contents and closes.

if filelike is None then it will be created with open() and the prefixed path will be walked to create non-existant dirs.

API Documentation for fixture, generated by pydoctor.