from jaraco path 3 4 1 build a set of filesdirectories as described by the spec each key represents a pathname and the value represents the content content may be a nested directory spec readme txt a readme file foo init py bar init py baz py some code target getfixture tmppath buildspec target target joinpath foobaz py readtextencoding utf8 some code end from jaraco path from jaraco path 3 4 1 type ignore build a set of files directories as described by the spec each key represents a pathname and the value represents the content content may be a nested directory spec readme txt a readme file foo __init__ py bar __init__ py baz py some code target getfixture tmp_path build spec target target joinpath foo baz py read_text encoding utf 8 some code type ignore end from jaraco path
import pathlib
import functools

from typing import Dict, Union





FilesSpec = Dict[str, Union[str, bytes, 'FilesSpec']]  


def build(spec: FilesSpec, prefix=pathlib.Path()):
    
    for name, contents in spec.items():
        create(contents, pathlib.Path(prefix) / name)


@functools.singledispatch
def create(content: Union[str, bytes, FilesSpec], path):
    path.mkdir(exist_ok=True)
    build(content, prefix=path)  


@create.register
def _(content: bytes, path):
    path.write_bytes(content)


@create.register
def _(content: str, path):
    path.write_text(content, encoding='utf-8')