Skip to main content

dirlay

Directory layout object for testing and documentation

license pypi python versions tests coverage tested with multipython uses docsub mypy uv Ruff

Features

  • Create directory tree and files from Python dict
  • Chdir to tree subdirectories
  • Display as rich tree for documentation
  • Developer friendly syntax:
    • reference nodes by paths: tree['a/b.md']
    • get sub-paths: tree / 'a/b.md' (relative), tree // 'a/b.md' (absolute)
    • add, update, delete nodes: tree |= {'d': {}}, del tree['a']
    • create tree under given or temporary directory
    • contextmanager interface to unlink tree on exit
  • Fully typed
  • Python 2 support (using pathlib2)

Installation

$ pip install dirlay[rich]

Usage

>>> from dirlay import Dir

Define directory structure and files content:

>>> tree = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> tree.data == {'a': {'b': {'c.txt': 'ccc'}, 'd.txt': 'ddd'}}
True
>>> tree / 'a/b/c.txt'
PosixPath('a/b/c.txt')
>>> tree['a/b/c.txt']
<Node 'a/b/c.txt': 'ccc'>
>>> 'z.txt' in tree
False

Content of files and directories can be updated:

>>> tree |= {'a/d.txt': {'e.txt': 'eee'}}
>>> tree['a/b/c.txt'].data *= 2
>>> tree.root()
<Node '.': {'a': {...}}>
>>> tree.data == {'a': {'b': {'c.txt': 'cccccc'}, 'd.txt': {'e.txt': 'eee'}}}
True

Instantiate on the file system (in temporary directory by default) and remove when exiting the context.

>>> with tree.mktree():
...     assert getcwd() != tree.basedir  # cwd not changed
...     str(tree['a/b/c.txt'].abspath.read_text())
'cccccc'

Optionally, change current working directory to a layout subdir, and change back after context manager is exited.

>>> with tree.mktree(chdir='a/b'):
...     assert getcwd() == tree.basedir / 'a/b'
...     str(Path('c.txt').read_text())
'cccccc'

Create directory layout tree

Directory layout can be constructed from dict:

>>> tree = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> tree.basedir is None
True
>>> tree.mktree()
<Dir '/tmp/...': {'a': ...}>
>>> tree.basedir
PosixPath('/tmp/...')

And remove when not needed anymore:

>>> tree.rmtree()

Chdir to subdirectory

>>> import os
>>> os.chdir('/tmp')

When layout is instantiated, current directory remains unchanged:

>>> tree = Dir({'a/b/c.txt': 'ccc'})
>>> tree.mktree()
<Dir '/tmp/...': {'a': {'b': {'c.txt': 'ccc'}}}>
>>> getcwd()
PosixPath('/tmp')

On first chdir, initial working directory is stored internally, and will be restored on destroy. Without argument, chdir sets current directory to tree.basedir.

>>> tree.basedir
PosixPath('/tmp/...')
>>> tree.chdir()
>>> getcwd()
PosixPath('/tmp/...')

If chdir has argument, it must be a path relative to basedir.

>>> tree.chdir('a/b')
>>> getcwd()
PosixPath('/tmp/.../a/b')

When directory is removed, current directory is restored:

>>> tree.rmtree()
>>> getcwd()
PosixPath('/tmp')

Print as tree

>>> tree = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> tree.print_rich()
📂 .
└── 📂 a
    ├── 📂 b
    │   └── 📄 c.txt
    └── 📄 d.txt

Display basedir path and file content:

>>> tree.mktree()
<Dir '/tmp/...': ...>
>>> tree.print_rich(real_basedir=True, show_data=True)
📂 /tmp/...
└── 📂 a
    ├── 📂 b
    │   └── 📄 c.txt
    │       ╭─────╮
    │       │ ccc │
    │       ╰─────╯
    └── 📄 d.txt
        ╭─────╮
        │ ddd │
        ╰─────╯

Extra keyword arguments will be passed through to rich.tree.Tree:

>>> tree.print_rich(show_data=True, hide_root=True)
📂 a
├── 📂 b
│   └── 📄 c.txt
│       ╭─────╮
│       │ ccc │
│       ╰─────╯
└── 📄 d.txt
    ╭─────╮
    │ ddd │
    ╰─────╯

>>> tree.rmtree()

See also

Metadata

Release files for dirlay 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dirlay 0.4.0
File Size Uploaded Provenance Actions
dirlay-0.4.0.tar.gz 122.8 kB Without provenance Details for dirlay-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dirlay 0.4.0
File Interpreter ABI Platform Actions
dirlay-0.4.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for dirlay-0.4.0-py2.py3-none-any.whl

Total release size: 136.6 kB

Release files / dirlay-0.4.0.tar.gz

Download URL dirlay-0.4.0.tar.gz
Size 122.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0eebe0ac89dbbe46574f44aee2d605c6a9238c532fc1609578c813e225d270b0
BLAKE2b-256 checksum
How to use checksums
583d06ed57638b6ac4e257f035dd0927226700db82bc2ab99e331c4e442a8faa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Release files / dirlay-0.4.0-py2.py3-none-any.whl

Download URL dirlay-0.4.0-py2.py3-none-any.whl
Size 13.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5fdd4eb0b7a4c3cf90f553db3f492851290f192fe0479d224c05eda7d76469a7
BLAKE2b-256 checksum
How to use checksums
8500b6078d86a0d8660fd39785489d3edca142d427b78a135be6c32992e56e2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Security for dirlay 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page