Skip to content

Commit

Permalink
minor tmpfile_utils fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Apr 27, 2024
1 parent 16af8a7 commit 97a67da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 8 additions & 2 deletions dcicutils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ def are_files_equal(filea: str, fileb: str) -> bool:
return False


def create_random_file(file: Optional[str] = None,
prefix: Optional[str] = None, suffix: Optional[str] = None,
def create_random_file(file: Optional[str] = None, prefix: Optional[str] = None, suffix: Optional[str] = None,
nbytes: int = 1024, binary: bool = False, line_length: Optional[int] = None) -> str:
"""
Write to the given file (name/path) some random content. If the given file is None then writes
to a temporary file. In either case, returns the file written to. The of bytes written is 1024
by default be can be specified with the nbytes argument; default to writing ASCII text but if
the binary argument is True then writes binary data as well; if not binary the content is in
lines of 80 characters each; use the line_length argumetn in this case to change the line length.
"""
if not isinstance(nbytes, int) or nbytes < 0:
nbytes = 0
if not isinstance(file, str) or not file:
Expand Down
17 changes: 9 additions & 8 deletions dcicutils/tmpfile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,31 @@ def temporary_directory() -> str:


@contextmanager
def temporary_file(name: Optional[str] = None, suffix: Optional[str] = None,
def temporary_file(name: Optional[str] = None, prefix: Optional[str] = None, suffix: Optional[str] = None,
content: Optional[Union[str, bytes, List[str]]] = None) -> str:
with temporary_directory() as tmp_directory_name:
tmp_file_name = os.path.join(tmp_directory_name, name or tempfile.mktemp(dir="")) + (suffix or "")
with open(tmp_file_name, "wb" if isinstance(content, bytes) else "w") as tmp_file:
tmp_file_name = f"{prefix or ''}{name or tempfile.mktemp(dir='')}{suffix or ''}"
tmp_file_path = os.path.join(tmp_directory_name, tmp_file_name)
with open(tmp_file_path, "wb" if isinstance(content, bytes) else "w") as tmp_file:
if content is not None:
tmp_file.write("\n".join(content) if isinstance(content, list) else content)
yield tmp_file_name
yield tmp_file_path


def create_temporary_file_name(prefix: Optional[str] = None, suffix: Optional[str] = None) -> str:
"""
Generates and returns the full path to file within the system temporary directory.
"""
tmp_file_name = f"{datetime.utcnow().strftime('%Y%m%d%H%M%S')}{str(uuid()).replace('-', '')}"
random_string = f"{datetime.utcnow().strftime('%Y%m%d%H%M%S')}{str(uuid()).replace('-', '')}"
tmp_file_name = f"{prefix or ''}{random_string}{suffix or ''}"
return os.path.join(tempfile.gettempdir(), tmp_file_name)


@contextmanager
def temporary_random_file(prefix: Optional[str] = None, suffix: Optional[str] = None,
nbytes: int = 1024, binary: bool = False, line_length: Optional[int] = None) -> str:
with temporary_file() as tmp_file_path:
create_random_file(tmp_file_path, prefix=prefix, suffix=suffix,
nbytes=nbytes, binary=binary, line_length=line_length)
with temporary_file(prefix=prefix, suffix=suffix) as tmp_file_path:
create_random_file(tmp_file_path, nbytes=nbytes, binary=binary, line_length=line_length)
yield tmp_file_path


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.8.4.1b12" # TODO: To become 8.8.5
version = "8.8.4.1b13" # TODO: To become 8.8.5
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <support@4dnucleome.org>"]
license = "MIT"
Expand Down

0 comments on commit 97a67da

Please sign in to comment.