Directory layout object for testing and documentation
Project description
dirlay
Directory layout object for testing and documentation
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/c.md'] - add, update, delete nodes:
tree |= {'d': {}},del tree['a'] - create tree under given or temporary directory
contextmanagerinterface to unlink tree on exit
- reference nodes by paths:
- Fully typed
- Python 2 support (using pathlib2)
Installation
$ pip install dirlay[rich]
Usage
>>> from dirlay import Dir
Define directory structure and files content:
>>> layout = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> layout.data == {'a': {'b': {'c.txt': 'ccc'}, 'd.txt': 'ddd'}}
True
>>> layout['a/b/c.txt']
<Node 'a/b/c.txt': 'ccc'>
>>> 'z.txt' in layout
False
Content of files and directories can be updated:
>>> layout |= {'a/d.txt': {'e.txt': 'eee'}}
>>> layout['a/b/c.txt'].data *= 2
>>> layout.root()
<Node '.': {'a': {...}}>
>>> layout.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 layout.mktree():
... assert getcwd() != layout.basedir # cwd not changed
... str(layout['a/b/c.txt'].path.read_text())
'cccccc'
Optionally, change current working directory to a layout subdir, and change back after context manager is exited.
>>> with layout.mktree(chdir='a/b'):
... assert getcwd() == layout.basedir / 'a/b'
... str(Path('c.txt').read_text())
'cccccc'
Create directory layout tree
Directory layout can be constructed from dict:
>>> layout = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> layout.basedir is None
True
>>> layout.mktree()
<Dir '/tmp/...': {'a': ...}>
>>> layout.basedir
PosixPath('/tmp/...')
And remove when not needed anymore:
>>> layout.rmtree()
Chdir to subdirectory
>>> import os
>>> os.chdir('/tmp')
When layout is instantiated, current directory remains unchanged:
>>> layout = Dir({'a/b/c.txt': 'ccc'})
>>> layout.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
layout.basedir.
>>> layout.basedir
PosixPath('/tmp/...')
>>> layout.chdir()
>>> getcwd()
PosixPath('/tmp/...')
If chdir has argument, it must be a path relative to basedir.
>>> layout.chdir('a/b')
>>> getcwd()
PosixPath('/tmp/.../a/b')
When directory is removed, current directory is restored:
>>> layout.rmtree()
>>> getcwd()
PosixPath('/tmp')
Print as tree
>>> layout = Dir({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> layout.print_rich()
๐ .
โโโ ๐ a
โโโ ๐ b
โ โโโ ๐ c.txt
โโโ ๐ d.txt
Display basedir path and file content:
>>> layout.mktree()
<Dir '/tmp/...': ...>
>>> layout.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:
>>> layout.print_rich(show_data=True, hide_root=True)
๐ a
โโโ ๐ b
โ โโโ ๐ c.txt
โ โญโโโโโโฎ
โ โ ccc โ
โ โฐโโโโโโฏ
โโโ ๐ d.txt
โญโโโโโโฎ
โ ddd โ
โฐโโโโโโฏ
>>> layout.rmtree()
See also
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dirlay-0.3.1.tar.gz.
File metadata
- Download URL: dirlay-0.3.1.tar.gz
- Upload date:
- Size: 122.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adeec2071197e7db23db0e9d6d0ed8e684073cf86588da37f9e025127bd1a15d
|
|
| MD5 |
45f22a84c19ee13a62adfef308fcdb80
|
|
| BLAKE2b-256 |
316e966a1d758dfc90b218868fc0957647e5fea4e3bf0fe771cb217f4faf0916
|
File details
Details for the file dirlay-0.3.1-py2.py3-none-any.whl.
File metadata
- Download URL: dirlay-0.3.1-py2.py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a69fa3663808f5cb971d4c8ed6eae757a2589b719219b679eae2ed251323f81
|
|
| MD5 |
3cae6a9d5ab886d7ba629fffc9028b33
|
|
| BLAKE2b-256 |
563450d548101bdd674f73562e061047f8d1c9fd5711fab44d111159152ab87a
|