-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
39 lines (29 loc) · 972 Bytes
/
utils.py
File metadata and controls
39 lines (29 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Provides common metods used by each GimpObject."""
from __future__ import annotations
from io import BytesIO
from pathlib import Path
def fileOpen(file: BytesIO | str | Path) -> tuple[str, bytes]:
"""Load a file.
:param fileName: can be a file name or a file-like object
"""
if isinstance(file, BytesIO):
with file:
return file.name, file.read()
pth = Path(file)
return pth.name, pth.read_bytes()
def save(data: bytearray | bytes, file: BytesIO | str | Path) -> None:
"""Save this gimp image to a file."""
if isinstance(file, BytesIO):
with file:
file.write(data)
return
pth = Path(file)
pth.write_bytes(data)
def repr_indent_lines(indent: int, lines: list[str]) -> str:
"""Represent lines with a given indentation (number of tabs).
:param int indent: number of tabs
:param list[str] lines: list of lines to represent
:return str: indented lines
"""
indentstr = indent * "\t"
return (indentstr) + ((f"\n{indentstr}").join(lines))