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 and remove directory tree with files
  • Chdir to layout subdirectories
  • Display as rich tree for docs
  • Syntactic sugar: mapping interface, context manager, append with | and |=
  • Uses pathlib2 for Python 2

Installation

$ pip install dirlay[rich]

Usage

>>> from dirlay import DirectoryLayout, to_tree

TL;DR

>>> layout = DirLayout() | {'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}}
>>> layout['a/b/c.txt']
PosixPath('a/b/c.txt')
>>> 'z.txt' in layout
False

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

>>> with layout:
...     layout.mktree()
...     str(layout['a/b/c.txt'].read_text())
'ccc'

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

>>> with layout:
...     layout.mktree()
...     layout.chdir('a/b')
...     str(Path('c.txt').read_text())
'ccc'

Create directory layout tree

Directory layout can be constructed from dict:

>>> layout = DirLayout({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> layout.basedir is None
True
>>> layout.mktree()
>>> 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 = DirLayout() | {'a': {'b/c.txt': 'ccc'}}
>>> layout.mktree()
>>> layout.getcwd()
PosixPath('/tmp')

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

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

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

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

When directory is removed, current directory is restored:

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

Print as tree

>>> layout = DirLayout({'a': {'b/c.txt': 'ccc', 'd.txt': 'ddd'}})
>>> layout.print_tree()
๐Ÿ“‚ .
โ””โ”€โ”€ ๐Ÿ“‚ a
    โ”œโ”€โ”€ ๐Ÿ“‚ b
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ c.txt
    โ””โ”€โ”€ ๐Ÿ“„ d.txt

Display basedir path and file contents:

>>> layout.mktree()
>>> layout.print_tree(real_basedir=True, show_content=True)
๐Ÿ“‚ /tmp/...
โ””โ”€โ”€ ๐Ÿ“‚ a
    โ”œโ”€โ”€ ๐Ÿ“‚ b
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ c.txt
    โ”‚       โ•ญโ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚       โ”‚ ccc โ”‚
    โ”‚       โ•ฐโ”€โ”€โ”€โ”€โ”€โ•ฏ
    โ””โ”€โ”€ ๐Ÿ“„ d.txt
        โ•ญโ”€โ”€โ”€โ”€โ”€โ•ฎ
        โ”‚ ddd โ”‚
        โ•ฐโ”€โ”€โ”€โ”€โ”€โ•ฏ

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

>>> layout.print_tree(real_basedir=True, show_content=True, hide_root=True)
๐Ÿ“‚ a
โ”œโ”€โ”€ ๐Ÿ“‚ b
โ”‚   โ””โ”€โ”€ ๐Ÿ“„ c.txt
โ”‚       โ•ญโ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚       โ”‚ ccc โ”‚
โ”‚       โ•ฐโ”€โ”€โ”€โ”€โ”€โ•ฏ
โ””โ”€โ”€ ๐Ÿ“„ d.txt
    โ•ญโ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ ddd โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ•ฏ

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.2.1.tar.gz (94.2 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.2.1-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for dirlay-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7625ade36583e7733a109c9b6ef69a0308931a55aa218287d3b18f685cfcb1e9
MD5 12bc900cb7904f344e422c255f795126
BLAKE2b-256 efe5d3ba1f9dd8392d4d85711bcd6288c623bd16fb49ae052da69a1bcd450c24

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dirlay-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ddadbf1a170aa088f66eefbfe0893917731f00fb59ff0c898bba3eb32733283a
MD5 c492e07ce384f16dc8ad465f0fc69d42
BLAKE2b-256 d2280aa558ec035f8d3a952af264f29ea27d9e37573aaae41b2ed90127e6b7c1

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