k3ut 0.1.16__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
k3ut-0.1.16/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 张炎泼
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
k3ut-0.1.16/PKG-INFO ADDED
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.4
2
+ Name: k3ut
3
+ Version: 0.1.16
4
+ Summary: Unittest utilities with debug logging and test helpers
5
+ Author-email: Zhang Yanpo <drdr.xp@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/pykit3/k3ut
8
+ Project-URL: Documentation, https://k3ut.readthedocs.io
9
+ Keywords: unittest,testing,debug,logging,utility
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Topic :: Software Development :: Libraries
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Requires-Python: >=3.9
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7.0; extra == "dev"
24
+ Requires-Dist: ruff; extra == "dev"
25
+ Requires-Dist: coverage; extra == "dev"
26
+ Provides-Extra: publish
27
+ Requires-Dist: build; extra == "publish"
28
+ Requires-Dist: twine; extra == "publish"
29
+ Requires-Dist: pk3; extra == "publish"
30
+ Provides-Extra: docs
31
+ Requires-Dist: mkdocs>=1.5; extra == "docs"
32
+ Requires-Dist: mkdocs-material>=9.0; extra == "docs"
33
+ Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
34
+ Dynamic: license-file
35
+
36
+ # k3ut
37
+
38
+ [![Build Status](https://travis-ci.com/pykit3/k3ut.svg?branch=master)](https://travis-ci.com/pykit3/k3ut)
39
+ [![Documentation Status](https://readthedocs.org/projects/k3ut/badge/?version=stable)](https://k3ut.readthedocs.io/en/stable/?badge=stable)
40
+ [![Package](https://img.shields.io/pypi/pyversions/k3ut)](https://pypi.org/project/k3ut)
41
+
42
+ unittest util
43
+
44
+ k3ut is a component of [pykit3] project: a python3 toolkit set.
45
+
46
+
47
+ # Install
48
+
49
+ ```
50
+ pip install k3ut
51
+ ```
52
+
53
+ # Synopsis
54
+
55
+ ```python
56
+
57
+ ```
58
+
59
+ # Author
60
+
61
+ Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
62
+
63
+ # Copyright and License
64
+
65
+ The MIT License (MIT)
66
+
67
+ Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
68
+
69
+
70
+ [pykit3]: https://github.com/pykit3
k3ut-0.1.16/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # k3ut
2
+
3
+ [![Build Status](https://travis-ci.com/pykit3/k3ut.svg?branch=master)](https://travis-ci.com/pykit3/k3ut)
4
+ [![Documentation Status](https://readthedocs.org/projects/k3ut/badge/?version=stable)](https://k3ut.readthedocs.io/en/stable/?badge=stable)
5
+ [![Package](https://img.shields.io/pypi/pyversions/k3ut)](https://pypi.org/project/k3ut)
6
+
7
+ unittest util
8
+
9
+ k3ut is a component of [pykit3] project: a python3 toolkit set.
10
+
11
+
12
+ # Install
13
+
14
+ ```
15
+ pip install k3ut
16
+ ```
17
+
18
+ # Synopsis
19
+
20
+ ```python
21
+
22
+ ```
23
+
24
+ # Author
25
+
26
+ Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
27
+
28
+ # Copyright and License
29
+
30
+ The MIT License (MIT)
31
+
32
+ Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
33
+
34
+
35
+ [pykit3]: https://github.com/pykit3
@@ -0,0 +1,225 @@
1
+ """
2
+ unittest utility
3
+ """
4
+
5
+ import inspect
6
+ import logging
7
+ import os
8
+ import socket
9
+ import sys
10
+ import tempfile
11
+ import time
12
+ import unittest
13
+ from importlib.metadata import version
14
+
15
+ __version__ = version("k3ut")
16
+
17
+ tmpdir = tempfile.gettempdir()
18
+
19
+ _glb = {
20
+ "pykitut_logger": None,
21
+ }
22
+
23
+ debug_to_stderr = os.environ.get("UT_DEBUG") == "1"
24
+
25
+
26
+ # TODO make this configurable
27
+ # logging.basicConfig(level='INFO',
28
+ # format='[%(asctime)s,%(process)d-%(thread)d,%(filename)s,%(lineno)d,%(levelname)s] %(message)s',
29
+ # datefmt='%H:%M:%S'
30
+ # )
31
+
32
+ # logger = logging.getLogger('kazoo')
33
+ # logger.setLevel('INFO')
34
+
35
+
36
+ class Timer(object):
37
+ def __init__(self):
38
+ self.start = None
39
+ self.end = None
40
+
41
+ def __enter__(self):
42
+ self.start = time.time()
43
+ return self
44
+
45
+ def __exit__(self, errtype, errval, _traceback):
46
+ self.end = time.time()
47
+
48
+ def spent(self):
49
+ return (self.end or time.time()) - self.start
50
+
51
+
52
+ class ContextFilter(logging.Filter):
53
+ """
54
+ Add correct func, line number, line info to log record.
55
+
56
+ To fix the issue that when test case function use dd() instead of
57
+ logger.debug(), logging alwasy print context info of dd(), but not the
58
+ caller test_xxx.
59
+ """
60
+
61
+ def filter(self, record):
62
+ # skip this function
63
+ stack = inspect.stack()[1:]
64
+
65
+ for i, (frame, path, ln, func, line, xx) in enumerate(stack):
66
+ if frame.f_globals.get("__name__") == "pykitut" and func == "dd":
67
+ # this frame is dd(), find the caller
68
+ _, path, ln, func, line, xx = stack[i + 1]
69
+
70
+ record._fn = os.path.basename(path)
71
+ record._ln = ln
72
+ record._func = func
73
+ return True
74
+
75
+ record._fn = record.filename
76
+ record._ln = record.lineno
77
+ record._func = record.funcName
78
+ return True
79
+
80
+
81
+ def _init():
82
+ if _glb["pykitut_logger"] is not None:
83
+ return
84
+
85
+ log_name = "pykitut"
86
+ lvl = "DEBUG"
87
+ base_dir = tmpdir
88
+ log_fn = log_name + ".out"
89
+
90
+ logger = logging.getLogger(log_name)
91
+ logger.setLevel(lvl)
92
+
93
+ # do not add 2 handlers to one logger by default
94
+ if len(logger.handlers) == 0:
95
+ log_path = os.path.join(base_dir, log_fn)
96
+ fmt = "[%(asctime)s %(process)d-%(thread)d %(_fn)s:%(_ln)d %(_func)s] %(levelname)s %(message)s"
97
+ datefmt = "%H:%M:%S"
98
+
99
+ fh = logging.FileHandler(log_path)
100
+ fh.setFormatter(logging.Formatter(fmt=fmt, datefmt=datefmt))
101
+ logger.addHandler(fh)
102
+
103
+ logger.addFilter(ContextFilter())
104
+
105
+ if debug_to_stderr:
106
+ fmt = "[%(asctime)s %(process)d-%(thread)d %(_fn)s:%(_ln)d %(_func)s] %(levelname)s\n%(message)s"
107
+
108
+ stream = sys.stderr
109
+ stdhandler = logging.StreamHandler(stream)
110
+ stdhandler.setFormatter(logging.Formatter(fmt=fmt, datefmt=datefmt))
111
+ stdhandler.setLevel(lvl)
112
+
113
+ logger.addHandler(stdhandler)
114
+
115
+ _glb["pykitut_logger"] = logger
116
+
117
+
118
+ def dd(*msg):
119
+ """
120
+ debug level logging in a test case function test_xx.
121
+
122
+ dd() write log to stdout if unittest verbosity is 2.
123
+ And dd always write log to log file in /tmp dir.
124
+ """
125
+
126
+ s = " ".join([str(x) for x in msg])
127
+
128
+ _init()
129
+
130
+ logger = _glb["pykitut_logger"]
131
+ if logger:
132
+ logger.debug(s)
133
+
134
+
135
+ def get_ut_verbosity():
136
+ """
137
+ Return the verbosity setting of the currently running unittest
138
+ program, or 0 if none is running.
139
+ """
140
+
141
+ frame = _find_frame_by_self(unittest.TestProgram)
142
+ if frame is None:
143
+ return 0
144
+
145
+ self = frame.f_locals.get("self")
146
+
147
+ return self.verbosity
148
+
149
+
150
+ def get_case_logger():
151
+ """
152
+ Get a case specific logger.
153
+ The logger name is: `<module>.<class>.<function>`,
154
+ such as: `pykit.strutil.test.test_strutil.TestStrutil.test_format_line`
155
+
156
+ It must be called inside a test_* function of unittest.TestCase, or no
157
+ correct module/class/function name can be found.
158
+ """
159
+
160
+ frame = _find_frame_by_self(unittest.TestCase)
161
+
162
+ self = frame.f_locals.get("self")
163
+
164
+ module_name = frame.f_globals.get("__name__")
165
+ class_name = self.__class__.__name__
166
+ func_name = frame.f_code.co_name
167
+
168
+ nm = module_name + "." + class_name + "." + func_name
169
+
170
+ logger = logging.getLogger(nm)
171
+ for f in logger.filters:
172
+ if isinstance(f, ContextFilter):
173
+ break
174
+ else:
175
+ logger.addFilter(ContextFilter())
176
+
177
+ return logger
178
+
179
+
180
+ def _find_frame_by_self(clz):
181
+ """
182
+ Find the first frame on stack in which there is local variable 'self' of
183
+ type clz.
184
+ """
185
+
186
+ frame = inspect.currentframe()
187
+
188
+ while frame:
189
+ self = frame.f_locals.get("self")
190
+ if isinstance(self, clz):
191
+ return frame
192
+
193
+ frame = frame.f_back
194
+
195
+ return None
196
+
197
+
198
+ def wait_listening(ip, port, timeout=15, interval=0.5):
199
+ # Wait at most `timeout` second for a tcp listening service to serve.
200
+
201
+ laste = None
202
+ for ii in range(40):
203
+ try:
204
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
205
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
206
+ sock.connect((ip, port))
207
+ break
208
+ except socket.error as e:
209
+ dd("trying to connect to {0} failed".format(str((ip, port))))
210
+ sock.close()
211
+ time.sleep(0.4)
212
+ laste = e
213
+ else:
214
+ raise laste
215
+
216
+
217
+ def has_env(kv):
218
+ # kv: KEY=value
219
+
220
+ k, v = kv.split("=", 1)
221
+ return os.environ.get(k) == v
222
+
223
+
224
+ def pyver():
225
+ return sys.version
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.4
2
+ Name: k3ut
3
+ Version: 0.1.16
4
+ Summary: Unittest utilities with debug logging and test helpers
5
+ Author-email: Zhang Yanpo <drdr.xp@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/pykit3/k3ut
8
+ Project-URL: Documentation, https://k3ut.readthedocs.io
9
+ Keywords: unittest,testing,debug,logging,utility
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Topic :: Software Development :: Libraries
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Requires-Python: >=3.9
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7.0; extra == "dev"
24
+ Requires-Dist: ruff; extra == "dev"
25
+ Requires-Dist: coverage; extra == "dev"
26
+ Provides-Extra: publish
27
+ Requires-Dist: build; extra == "publish"
28
+ Requires-Dist: twine; extra == "publish"
29
+ Requires-Dist: pk3; extra == "publish"
30
+ Provides-Extra: docs
31
+ Requires-Dist: mkdocs>=1.5; extra == "docs"
32
+ Requires-Dist: mkdocs-material>=9.0; extra == "docs"
33
+ Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
34
+ Dynamic: license-file
35
+
36
+ # k3ut
37
+
38
+ [![Build Status](https://travis-ci.com/pykit3/k3ut.svg?branch=master)](https://travis-ci.com/pykit3/k3ut)
39
+ [![Documentation Status](https://readthedocs.org/projects/k3ut/badge/?version=stable)](https://k3ut.readthedocs.io/en/stable/?badge=stable)
40
+ [![Package](https://img.shields.io/pypi/pyversions/k3ut)](https://pypi.org/project/k3ut)
41
+
42
+ unittest util
43
+
44
+ k3ut is a component of [pykit3] project: a python3 toolkit set.
45
+
46
+
47
+ # Install
48
+
49
+ ```
50
+ pip install k3ut
51
+ ```
52
+
53
+ # Synopsis
54
+
55
+ ```python
56
+
57
+ ```
58
+
59
+ # Author
60
+
61
+ Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
62
+
63
+ # Copyright and License
64
+
65
+ The MIT License (MIT)
66
+
67
+ Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
68
+
69
+
70
+ [pykit3]: https://github.com/pykit3
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ __init__.py
4
+ pyproject.toml
5
+ ./__init__.py
6
+ k3ut.egg-info/PKG-INFO
7
+ k3ut.egg-info/SOURCES.txt
8
+ k3ut.egg-info/dependency_links.txt
9
+ k3ut.egg-info/requires.txt
10
+ k3ut.egg-info/top_level.txt
11
+ test/test_ut.py
@@ -0,0 +1,15 @@
1
+
2
+ [dev]
3
+ pytest>=7.0
4
+ ruff
5
+ coverage
6
+
7
+ [docs]
8
+ mkdocs>=1.5
9
+ mkdocs-material>=9.0
10
+ mkdocstrings[python]>=0.24
11
+
12
+ [publish]
13
+ build
14
+ twine
15
+ pk3
@@ -0,0 +1 @@
1
+ k3ut
@@ -0,0 +1,57 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "k3ut"
7
+ version = "0.1.16"
8
+ description = "Unittest utilities with debug logging and test helpers"
9
+ readme = "README.md"
10
+ license = {text = "MIT"}
11
+ requires-python = ">=3.9"
12
+ authors = [
13
+ { name = "Zhang Yanpo", email = "drdr.xp@gmail.com" }
14
+ ]
15
+ keywords = ["unittest", "testing", "debug", "logging", "utility"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Topic :: Software Development :: Libraries",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ ]
27
+ dependencies = []
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/pykit3/k3ut"
31
+ Documentation = "https://k3ut.readthedocs.io"
32
+
33
+ [project.optional-dependencies]
34
+ dev = [
35
+ "pytest>=7.0",
36
+ "ruff",
37
+ "coverage",
38
+ ]
39
+ publish = [
40
+ "build",
41
+ "twine",
42
+ "pk3",
43
+ ]
44
+ docs = [
45
+ "mkdocs>=1.5",
46
+ "mkdocs-material>=9.0",
47
+ "mkdocstrings[python]>=0.24",
48
+ ]
49
+
50
+ [tool.setuptools]
51
+ packages = ["k3ut"]
52
+
53
+ [tool.setuptools.package-dir]
54
+ k3ut = "."
55
+
56
+ [tool.ruff]
57
+ line-length = 120
k3ut-0.1.16/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,10 @@
1
+ import unittest
2
+
3
+ import k3ut
4
+
5
+ dd = k3ut.dd
6
+
7
+
8
+ class TestProcError(unittest.TestCase):
9
+ def test_nothing(self):
10
+ pass