Skip to main content

Create and recursively fill a temporary directory

Project description

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

  • tdir is a context manager that runs in a populated temporary directory

  • tdec is a decorator to run functions or test suites in a populated temporary directory

  • fill recursively fills a directory (temporary or not)

EXAMPLE: to temporarily create a directory structure

import tdir

with tdir.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 for tests

from pathlib import Path
import tdir
import unittest

CWD = Path().absolute()


# Decorate a whole class 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'
        assert Path('foo').read_text() = 'bar\n'


# Decorate single tests
class MyTest(unittest.TestCast):
    @tdir(foo='bar', baz=bytes(range(4)))
    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() != CWD

API

tdir.tdir(*args, cwd=True, **kwargs)

(tdir.py, 75-108)

A context manager to create and fill a temporary directory.

ARGUMENTS
args:

A list of strings or dictionaries. 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.

cwd:

If true, change the working directory to the temp dir at the start of the context and restore the original working directory at the end.

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.

tdir.tdec(*args, **kwargs)

(tdir.py, 110-167)

Decorate a function or unittest.TestCase so it runs in a populated temporary directory.

If tdec() has exactly one callable argument, either a function or a class, tdec() decorates it to run in an empty temporary directory.

Otherwise, tdec() returns a decorator which decorates a function or class to run a temporary directory populated with entries from args and kwargs/

ARGUMENTS
args:

Either a single callable, or a list of strings or dictionaries. 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.

cwd:

If true, change the working directory to the temp dir at the start of the context and restore the original working directory at the end.

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.

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

(tdir.py, 169-231)

Recursively populate 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-06-29T20:14:56.309818)

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.11.3.tar.gz (3.9 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.11.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tdir-0.11.3.tar.gz
  • Upload date:
  • Size: 3.9 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.11.3.tar.gz
Algorithm Hash digest
SHA256 779b7495a72378e313c77ab9a220de5297ea04a826aa1e14dcc5f1bc7773552b
MD5 a5b0434c613a15c92f21d05fb5c67a03
BLAKE2b-256 f63587520b1d4ac5617c1164d161f0789bd602c71444910b003c06bea5278ec5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tdir-0.11.3-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.11.3-py3-none-any.whl
Algorithm Hash digest
SHA256 324f93c5d3150dba00aab46f7d0843d2ff3674d24e91a6d53797c8ae3fcd8f67
MD5 70913e8cda555a89808abc6b9b9683dd
BLAKE2b-256 0f68cf0067c00387d0d8d4f22bb55b4b4781ffd428f4accc364c441331eb4aae

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