filesystem_tree.py

This library provides a class for managing a filesystem tree. Test fixture is the driving use-case.

Installation

filesystem_tree is available on GitHub and on PyPI:

$ pip install filesystem_tree

We test against Python 2.6, 2.7, 3.2, and 3.3.

filesystem_tree is MIT-licensed.

API Reference

class filesystem_tree.FilesystemTree(*treedef, **kw)[source]

Represent a filesystem tree.

Parameters:
  • treedef – Any positional arguments are passed through to mk.
  • root (string) – The root of the filesystem tree. If not specified or None, a temporary directory will be created and used. (May only be supplied as a keyword argument.)
  • should_dedent (bool) – Sets the instance default for whether or not the contents of files are dedented before being written. (May only be supplied as a keyword argument.)

Create a new instance of this class every time you need an isolated filesystem tree:

>>> fs = FilesystemTree()

This creates a temporary directory, the path to which you can access with fs.root:

>>> isdir(fs.root)
True
prefix = 'filesystem-tree-'

The prefix to use when making a temporary directory as root.

root = None

The root of the filesystem tree that this object represents.

should_dedent = True

Whether or not to automatically dedent file contents on write.

mk(*treedef, **kw)[source]

Builds a filesystem tree in root based on treedef.

Parameters:
  • treedef – The definition of a filesystem tree.
  • should_dedent (bool) – Controls whether or not the contents of files are dedented before being written. If not specified, should_dedent is used. (May only be supplied as a keyword argument.)
Raises :

TypeError, if treedef contains anything besides strings and tuples; ValueError, if treedef contains a tuple that doesn’t have two or three items

Returns:

None

This method iterates over the items in treedef, creating directories for any strings, and files for any tuples. For file tuples, the first item is the path of the file, the second is the contents to write, and the third (optional) item is whether to dedent the contents first before writing it. All paths must be specified using / as the separator (they will be automatically converted to the native path separator for the current platform). Any intermediate directories will be created as necessary.

So for example if you instantiate a FilesystemTree:

>>> fs = FilesystemTree()

And you call mk with:

>>> fs.mk(('path/to/file.txt', 'Greetings, program!'))

Then you’ll have one file in your tree:

>>> os.listdir(os.path.join(fs.root, 'path', 'to'))
['file.txt']

And it will have the content you asked for:

>>> open(fs.resolve('path/to/file.txt')).read()
'Greetings, program!'

The automatic dedenting is so you can use multi-line strings in indented code blocks to specify file contents and indent it with the rest of your code, but not have the indents actually written to the file. For example:

>>> def foo():
...     fs.mk(('other/file.txt', '''
...     Here is a list of things:
...         - Thing one.
...         - Thing two.
...         - Thing three.
...     '''))
...
>>> foo()
>>> print(open(fs.resolve('other/file.txt')).read())

Here is a list of things:
    - Thing one.
    - Thing two.
    - Thing three.
resolve(path='')[source]

Given a relative path, return an absolute path.

Parameters:path – A path relative to root using / as the separator
Returns:An absolute path using the native path separator, with symlinks removed

The return value of resolve with no arguments is equivalent to root.

remove()[source]

Remove the filesystem tree at root.

Returns:None
Read the Docs v: 1.0.0
Versions
latest
1.0.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.