Skip to content

Commit

Permalink
Consolidate write util
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jun 29, 2019
1 parent 7382bf5 commit d292883
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion datafiles/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from . import config, formats, hooks
from .converters import Converter, List, map_type
from .utils import display, recursive_update, write_path as write
from .utils import display, recursive_update, write


Trilean = Optional[bool]
Expand Down
21 changes: 9 additions & 12 deletions datafiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from pprint import pformat
from shutil import get_terminal_size
from typing import Any, Dict
from typing import Any, Dict, Union

import log

Expand Down Expand Up @@ -59,18 +59,14 @@ def dedent(text: str) -> str:
return text.replace(' ' * indent, '')


def write(filename: str, text: str) -> None:
"""Write text to a given filename with logging."""
path = Path(filename).resolve()
text = dedent(text)
message = f'Writing file: {path}'
log.info(message)
log.debug('=' * len(message) + '\n' + (text or '<nothing>\n'))
path.write_text(text)

def write(filename_or_path: Union[str, Path], text: str) -> None:
"""Write text to a given file with logging."""
if isinstance(filename_or_path, Path):
path = filename_or_path
else:
path = Path(filename_or_path).resolve()
text = dedent(text)

def write_path(path: Path, text: str) -> None:
"""Write text to given path object with logging."""
message = f'Writing file: {path}'
log.debug(message)
line = '=' * len(message)
Expand All @@ -79,6 +75,7 @@ def write_path(path: Path, text: str) -> None:
else:
content = '∅\n'
log.debug(line + '\n' + content + line)

path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text)

Expand Down

0 comments on commit d292883

Please sign in to comment.