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

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.4.0.tar.gz (122.8 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.4.0-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file dirlay-0.4.0.tar.gz.

File metadata

  • Download URL: dirlay-0.4.0.tar.gz
  • Upload date:
  • Size: 122.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for dirlay-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0eebe0ac89dbbe46574f44aee2d605c6a9238c532fc1609578c813e225d270b0
MD5 cc2691ecf78dba36922934a8928cd173
BLAKE2b-256 583d06ed57638b6ac4e257f035dd0927226700db82bc2ab99e331c4e442a8faa

See more details on using hashes here.

File details

Details for the file dirlay-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: dirlay-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for dirlay-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5fdd4eb0b7a4c3cf90f553db3f492851290f192fe0479d224c05eda7d76469a7
MD5 66666747f2935e895f1273b16ccbe4cb
BLAKE2b-256 8500b6078d86a0d8660fd39785489d3edca142d427b78a135be6c32992e56e2e

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