Skip to main content

Directory layout object for testing and documentation

Project description

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/c.md']
    • 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:

>>> 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

dirlay-0.3.1.tar.gz (122.1 kB view details)

Uploaded Source

Built Distribution

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

dirlay-0.3.1-py2.py3-none-any.whl (13.6 kB view details)

Uploaded Python 2Python 3

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

Hashes for dirlay-0.3.1.tar.gz
Algorithm Hash digest
SHA256 adeec2071197e7db23db0e9d6d0ed8e684073cf86588da37f9e025127bd1a15d
MD5 45f22a84c19ee13a62adfef308fcdb80
BLAKE2b-256 316e966a1d758dfc90b218868fc0957647e5fea4e3bf0fe771cb217f4faf0916

See more details on using hashes here.

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

Hashes for dirlay-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a69fa3663808f5cb971d4c8ed6eae757a2589b719219b679eae2ed251323f81
MD5 3cae6a9d5ab886d7ba629fffc9028b33
BLAKE2b-256 563450d548101bdd674f73562e061047f8d1c9fd5711fab44d111159152ab87a

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