Skip to main content

Create and recursively fill a temporary directory

Project description

Creates a temporary directory using tempfile.TemporaryDirectory and then fills it with files. Great for tests!

tdir is a context manager and decorator that runs functions or test

suites in a temporary directory filled with files

fill recursively fills a directory (temporary or not)

EXAMPLE: as a context manager

import tdir

with tdir(
    'one.txt', 'two.txt',
    three='some information',
    four=Path('/some/existing/file'),
    subdirectory1={
        'file.txt': 'blank lines\n\n\n\n',
        'subdirectory': ['a', 'b', 'c']
    },
):

EXAMPLE: as a decorator

from pathlib import Path
import unittest

@tdir
def my_function():
    # Do some work in a temporary directory


# Decorate a TestCase so each test runs in a new temporary directory
@tdir('a', foo='bar')
class MyTest(unittest.TestCast):
    def test_something(self):
        assert Path('a').read_text() = 'a\n'

    def test_something_else(self):
        assert Path('foo').read_text() = 'bar\n'


# Decorate single test in a unitttest
class MyTest2(unittest.TestCast):
    @tdir(foo='bar', baz=bytes(range(4)))  # binary files are possible
    def test_something(self):
        assert Path('foo').read_text() = 'bar\n'
        assert Path('baz').read_bytes() = bytes(range(4)))

    # Run in an empty temporary directory
    @tdir
    def test_something_else(self):
        assert not Path('a').exists()
        assert Path().absolute() != self.ORIGINAL_PATH

    ORIGINAL_PATH = Path().absolute()

API

Class tdir.tdir`

(tdir.py, 81-149)

Set up a temporary directory, fill it with files, then tear it down at the end of an operation.

tdir can be used either as a context manager, or a decorator that works on functions or classes.

ARGUMENTS
args, kwargs:

Files to put into the temporary directory. See the documentation for tdir.fill()

cwd:

If True (the default), change the working directory to the tdir at the start of the operation and restore the original working directory at the end.

methods:

Which methods on classes to decorate. See https://github.com/rec/dek/blob/master/README.rst#dekdekdecorator-deferfalse-methodsnone

tdir.tdir.__new__(cls, *args, cwd=True, methods='test', **kwargs)

(tdir.py, 105-127)

Create and return a new object. See help(type) for accurate signature.

tdir.tdir.__enter__(self)

(tdir.py, 128-139)

tdir.tdir.__exit__(self, *args)

(tdir.py, 140-146)

tdir.tdir.__call__(self, *args, **kwargs)

(tdir.py, 147-149)

Call self as a function.

Class tdir.tdec`

(tdir.py, 81-149)

Set up a temporary directory, fill it with files, then tear it down at the end of an operation.

tdir can be used either as a context manager, or a decorator that works on functions or classes.

ARGUMENTS
args, kwargs:

Files to put into the temporary directory. See the documentation for tdir.fill()

cwd:

If True (the default), change the working directory to the tdir at the start of the operation and restore the original working directory at the end.

methods:

Which methods on classes to decorate. See https://github.com/rec/dek/blob/master/README.rst#dekdekdecorator-deferfalse-methodsnone

tdir.tdec.__new__(cls, *args, cwd=True, methods='test', **kwargs)

(tdir.py, 105-127)

Create and return a new object. See help(type) for accurate signature.

tdir.tdec.__enter__(self)

(tdir.py, 128-139)

tdir.tdec.__exit__(self, *args)

(tdir.py, 140-146)

tdir.tdec.__call__(self, *args, **kwargs)

(tdir.py, 147-149)

Call self as a function.

tdir.fill(root, *args, **kwargs)

(tdir.py, 154-216)

Recursively fills a directory.

ARGUMENTS
root:

The root directory to fill

args:

A list of strings, dictionaries or Paths.

For strings, a file is created with that string as name and contents.

For dictionaries, the contents are used to recursively create and fill the directory.

For Paths, the file is copied into the target directory

kwargs:

A dictionary mapping file or directory names to values.

If the key’s value is a string it is used to file a file of that name.

If it’s a dictionary, its contents are used to recursively create and fill a subdirectory.

If it’s a Path, that file is copied to the target directory.

(automatically generated by doks on 2020-07-10T16:03:56.532666)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tdir-0.12.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tdir-0.12.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file tdir-0.12.0.tar.gz.

File metadata

  • Download URL: tdir-0.12.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/18.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for tdir-0.12.0.tar.gz
Algorithm Hash digest
SHA256 a2f7a698c56ac6aa4f0bae9eb0c0fe27b1b3c96c2ecf712a0dda4bae06695e75
MD5 ec4f0250ba2a33fb94ca3f7e171ae4bf
BLAKE2b-256 0f77e33fc77c04fc49040752ae4ac91831424e7510de2c2fb79cb93d33c12fd1

See more details on using hashes here.

File details

Details for the file tdir-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: tdir-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/18.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for tdir-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f8d1f6885f754d438bb09f6ab3406535bde911e03794f7af79e0c07b2484a48
MD5 247522fe7ad303d4519f4a630322d7da
BLAKE2b-256 ca06fd113068ecee229acf73bd005e5b6f69f06697cd85646a6bfc7a2ad63679

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page