-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
126 lines (105 loc) · 2.61 KB
/
__init__.py
File metadata and controls
126 lines (105 loc) · 2.61 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
k3handy is collection of mostly used utilities.
"""
from __future__ import annotations
import logging
import os
import sys
from typing import Sequence
from importlib.metadata import version
__version__ = version("k3handy")
from . import path
from k3fs import fread
from k3fs import fwrite
from k3fs import ls_dirs
from k3fs import ls_files
from k3fs import makedirs
from k3fs import remove
from k3proc import command
from k3proc import CalledProcessError
from k3proc import TimeoutExpired
from k3str import to_bytes
from .path import pabs
from .path import pjoin
from .path import prebase
from .cmdutil import CmdFlag
from .cmdutil import CMD_RAISE_STDOUT
from .cmdutil import CMD_RAISE_ONELINE
from .cmdutil import CMD_NONE_ONELINE
from .cmdutil import cmd0
from .cmdutil import cmdf
from .cmdutil import cmdout
from .cmdutil import cmdpass
from .cmdutil import cmdtty
from .cmdutil import cmdx
from .cmdutil import parse_flag
from .cmdutil import dd
from .cmdutil import ddstack
__all__ = [
# from k3fs
"fread",
"fwrite",
"ls_dirs",
"ls_files",
"makedirs",
"remove",
# from k3proc
"command",
"CalledProcessError",
"TimeoutExpired",
# from k3str
"to_bytes",
# from .path
"path",
"pabs",
"pjoin",
"prebase",
# from .cmd
"CmdFlag",
"CMD_RAISE_STDOUT",
"CMD_RAISE_ONELINE",
"CMD_NONE_ONELINE",
"cmd0",
"cmdf",
"cmdout",
"cmdpass",
"cmdtty",
"cmdx",
"parse_flag",
"dd",
"ddstack",
# local
"display",
]
logger = logging.getLogger(__name__)
# Since 3.8 there is a stacklevel argument
ddstack_kwarg: dict[str, int] = {}
if sys.version_info.major == 3 and sys.version_info.minor >= 8:
ddstack_kwarg = {"stacklevel": 2}
def display(
stdout: int | str | Sequence[str] | None,
stderr: str | Sequence[str] | None = None,
) -> None:
"""
Output to stdout and stderr.
- ``display(1, "foo")`` write to stdout.
- ``display(1, ["foo", "bar"])`` write multilines to stdout.
- ``display(1, ("foo", "bar"))`` write multilines to stdout.
- ``display(("foo", "bar"), ["woo"])`` write multilines to stdout and stderr.
- ``display(None, ["woo"])`` write multilines to stderr.
"""
if isinstance(stdout, int):
fd = stdout
line = stderr
if isinstance(line, (list, tuple)):
lines = line
for ln in lines:
display(fd, ln)
return
os.write(fd, to_bytes(line))
os.write(fd, b"\n")
return
if stdout is not None:
display(1, stdout)
if stderr is not None:
display(2, stderr)