Skip to content

Commit

Permalink
Re-use _path.build for building files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 13, 2023
1 parent 3112653 commit 5ff3f3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
5 changes: 5 additions & 0 deletions tests/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def _(content: str, path):
path.write_text(content, encoding='utf-8')


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


class Recording:
"""
A TreeMaker object that records everything that would be written.
Expand Down
34 changes: 4 additions & 30 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,38 +328,12 @@ def setUp(self):
build_files(EggInfoFile.files, prefix=self.site_dir)


def build_files(file_defs, prefix=pathlib.Path()):
"""Build a set of files/directories, as described by the
# dedent all text strings before writing
orig = _path.create.registry[str]
_path.create.register(str, lambda content, path: orig(DALS(content), path))

file_defs dictionary. Each key/value pair in the dictionary is
interpreted as a filename/contents pair. If the contents value is a
dictionary, a directory is created, and the dictionary interpreted
as the files within it, recursively.

For example:
{"README.txt": "A README file",
"foo": {
"__init__.py": "",
"bar": {
"__init__.py": "",
},
"baz.py": "# Some code",
}
}
"""
for name, contents in file_defs.items():
full_name = prefix / name
if isinstance(contents, dict):
full_name.mkdir()
build_files(contents, prefix=full_name)
else:
if isinstance(contents, bytes):
with full_name.open('wb') as f:
f.write(contents)
else:
with full_name.open('w', encoding='utf-8') as f:
f.write(DALS(contents))
build_files = _path.build


def build_record(file_defs):
Expand Down

0 comments on commit 5ff3f3b

Please sign in to comment.