hive-common 0.0.1__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.
@@ -0,0 +1,37 @@
1
+ Metadata-Version: 2.1
2
+ Name: hive-common
3
+ Version: 0.0.1
4
+ Summary: Common code for Hive libraries and services
5
+ Author-email: Gary Benson <gary@gbenson.net>
6
+ Project-URL: Homepage, https://github.com/gbenson/hive/tree/main/libs/common
7
+ Project-URL: Source, https://github.com/gbenson/hive
8
+ Description-Content-Type: text/markdown
9
+
10
+ [![version badge]](https://pypi.org/project/hive-common/)
11
+
12
+ [version badge]: https://img.shields.io/pypi/v/hive-common?color=limegreen
13
+
14
+ # hive-common
15
+
16
+ Common code for Hive libraries and services
17
+
18
+ ## Installation
19
+
20
+ ### With PIP
21
+
22
+ ```sh
23
+ pip install hive-common
24
+ ```
25
+
26
+ ### From source
27
+
28
+ ```sh
29
+ git clone https://github.com/gbenson/hive.git
30
+ cd hive/libs/common
31
+ python3 -m venv .venv
32
+ . .venv/bin/activate
33
+ pip install --upgrade pip
34
+ pip install -r requirements.txt
35
+ pip install -e .
36
+ flake8 && pytest
37
+ ```
@@ -0,0 +1,28 @@
1
+ [![version badge]](https://pypi.org/project/hive-common/)
2
+
3
+ [version badge]: https://img.shields.io/pypi/v/hive-common?color=limegreen
4
+
5
+ # hive-common
6
+
7
+ Common code for Hive libraries and services
8
+
9
+ ## Installation
10
+
11
+ ### With PIP
12
+
13
+ ```sh
14
+ pip install hive-common
15
+ ```
16
+
17
+ ### From source
18
+
19
+ ```sh
20
+ git clone https://github.com/gbenson/hive.git
21
+ cd hive/libs/common
22
+ python3 -m venv .venv
23
+ . .venv/bin/activate
24
+ pip install --upgrade pip
25
+ pip install -r requirements.txt
26
+ pip install -e .
27
+ flake8 && pytest
28
+ ```
@@ -0,0 +1 @@
1
+ from .argument_parser import HiveArgumentParser as ArgumentParser
@@ -0,0 +1 @@
1
+ __version__ = "0.0.1"
@@ -0,0 +1,17 @@
1
+ import logging
2
+
3
+ from argparse import ArgumentParser
4
+
5
+ from .logging import getenv_log_level
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+
10
+ class HiveArgumentParser(ArgumentParser):
11
+ def parse_args(self, *args, **kwargs):
12
+ if (log_level := getenv_log_level()):
13
+ try:
14
+ logging.basicConfig(level=log_level)
15
+ except ValueError:
16
+ logger.warning(f"Ignoring LL={log_level!r}")
17
+ return super().parse_args(*args, *kwargs)
@@ -0,0 +1,13 @@
1
+ import os
2
+
3
+ from typing import Optional
4
+
5
+
6
+ def getenv_log_level() -> Optional[int | str]:
7
+ level = os.environ.get("LL", None)
8
+ if not level:
9
+ return None
10
+ try:
11
+ return int(level)
12
+ except ValueError:
13
+ return level.upper()
@@ -0,0 +1,16 @@
1
+ try:
2
+ from pep440_version_utils import Version
3
+ except ImportError:
4
+ pass
5
+
6
+
7
+ def assert_is_valid_version(version: str):
8
+ try:
9
+ assert str(Version(version)) == version
10
+ except Exception as e:
11
+ raise AssertionError(f"Invalid version {version!r}") from e
12
+
13
+
14
+ def want_to_see(caplog, msg):
15
+ failure_detail = f"didn't see: {msg}"
16
+ assert any(r.getMessage() == msg for r in caplog.records), failure_detail
@@ -0,0 +1,12 @@
1
+ BYTE = BYTES = 1
2
+ KiB = BYTE << 10
3
+ MiB = KiB << 10
4
+ GiB = MiB << 10
5
+ TiB = GiB << 10
6
+
7
+ SECOND = SECONDS = 1
8
+ MINUTE = MINUTES = 60 * SECONDS
9
+ HOUR = HOURS = 60 * MINUTES
10
+
11
+ MILLISECOND = MILLISECONDS = SECOND / 1000
12
+ MICROSECOND = MICROSECONDS = MILLISECOND / 1000
@@ -0,0 +1,37 @@
1
+ Metadata-Version: 2.1
2
+ Name: hive-common
3
+ Version: 0.0.1
4
+ Summary: Common code for Hive libraries and services
5
+ Author-email: Gary Benson <gary@gbenson.net>
6
+ Project-URL: Homepage, https://github.com/gbenson/hive/tree/main/libs/common
7
+ Project-URL: Source, https://github.com/gbenson/hive
8
+ Description-Content-Type: text/markdown
9
+
10
+ [![version badge]](https://pypi.org/project/hive-common/)
11
+
12
+ [version badge]: https://img.shields.io/pypi/v/hive-common?color=limegreen
13
+
14
+ # hive-common
15
+
16
+ Common code for Hive libraries and services
17
+
18
+ ## Installation
19
+
20
+ ### With PIP
21
+
22
+ ```sh
23
+ pip install hive-common
24
+ ```
25
+
26
+ ### From source
27
+
28
+ ```sh
29
+ git clone https://github.com/gbenson/hive.git
30
+ cd hive/libs/common
31
+ python3 -m venv .venv
32
+ . .venv/bin/activate
33
+ pip install --upgrade pip
34
+ pip install -r requirements.txt
35
+ pip install -e .
36
+ flake8 && pytest
37
+ ```
@@ -0,0 +1,16 @@
1
+ README.md
2
+ pyproject.toml
3
+ hive/common/__init__.py
4
+ hive/common/__version__.py
5
+ hive/common/argument_parser.py
6
+ hive/common/logging.py
7
+ hive/common/testing.py
8
+ hive/common/units.py
9
+ hive_common.egg-info/PKG-INFO
10
+ hive_common.egg-info/SOURCES.txt
11
+ hive_common.egg-info/dependency_links.txt
12
+ hive_common.egg-info/top_level.txt
13
+ tests/test_argument_parser.py
14
+ tests/test_is_valid_version.py
15
+ tests/test_units.py
16
+ tests/test_version.py
@@ -0,0 +1,26 @@
1
+ [project]
2
+ name = "hive-common"
3
+ dynamic = ["version"]
4
+ authors = [{ name = "Gary Benson", email = "gary@gbenson.net" }]
5
+ description = "Common code for Hive libraries and services"
6
+ readme = "README.md"
7
+
8
+ [project.urls]
9
+ Homepage = "https://github.com/gbenson/hive/tree/main/libs/common"
10
+ Source = "https://github.com/gbenson/hive"
11
+
12
+ [build-system]
13
+ requires = ["setuptools>=61.0"]
14
+ build-backend = "setuptools.build_meta"
15
+
16
+ [tool.setuptools.dynamic]
17
+ version = {attr = "hive.common.__version__.__version__"}
18
+
19
+ [tool.pytest.ini_options]
20
+ addopts = "--cov=hive.common"
21
+
22
+ [tool.coverage.run]
23
+ omit = [
24
+ "*/venv/*",
25
+ "*/.venv/*",
26
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,63 @@
1
+ import logging
2
+
3
+ import pytest
4
+
5
+ from hive.common import ArgumentParser
6
+ from hive.common.testing import want_to_see
7
+
8
+
9
+ def test_default_log_level(monkeypatch):
10
+ def mock_bc(*args, **kwargs):
11
+ pytest.fail("logging.basicConfig() was called")
12
+
13
+ with monkeypatch.context() as mp:
14
+ mp.setattr(logging, "basicConfig", mock_bc)
15
+ mp.delenv("LL", raising=False)
16
+ _ = ArgumentParser().parse_args()
17
+
18
+
19
+ @pytest.mark.parametrize(
20
+ ("user_level", "expect_level"),
21
+ (("debug", logging.DEBUG),
22
+ ("DEBUG", logging.DEBUG),
23
+ ("10", logging.DEBUG),
24
+ ("11", 11),
25
+ ("WARNING", logging.WARNING),
26
+ ("critical", logging.CRITICAL),
27
+ ))
28
+ def test_getenv_log_level(user_level, expect_level, monkeypatch):
29
+ loggers = []
30
+
31
+ def logger_name(i):
32
+ return f"{__name__}.test_getenv_log_level.{i}"
33
+
34
+ def mock_bc(level):
35
+ logger = logging.getLogger(logger_name(len(loggers)))
36
+ logger.setLevel(level)
37
+ loggers.append(logger)
38
+
39
+ with monkeypatch.context() as mp:
40
+ mp.setattr(logging, "basicConfig", mock_bc)
41
+ mp.setenv("LL", user_level)
42
+ _ = ArgumentParser().parse_args()
43
+
44
+ assert len(loggers) == 1
45
+ logger = loggers[0]
46
+ assert logger.name == logger_name(0)
47
+ assert logger.level == expect_level
48
+
49
+
50
+ def test_unknown_log_level(monkeypatch, caplog):
51
+ def mock_bc(level):
52
+ logger = logging.getLogger(f"{__name__}.test_unknown_log_level")
53
+ logger.setLevel(level)
54
+ pytest.fail(f"logger.setLevel({level!r}) didn't raise")
55
+
56
+ with monkeypatch.context() as mp:
57
+ mp.setattr(logging, "basicConfig", mock_bc)
58
+ mp.setenv("LL", "gonk")
59
+
60
+ with caplog.at_level(logging.WARNING):
61
+ _ = ArgumentParser().parse_args()
62
+
63
+ want_to_see(caplog, "Ignoring LL='GONK'")
@@ -0,0 +1,13 @@
1
+ import pytest
2
+
3
+ from hive.common.testing import assert_is_valid_version
4
+
5
+
6
+ @pytest.mark.parametrize(
7
+ "invalid_version",
8
+ (("1.2-3"),
9
+ ("1.2-3-final"),
10
+ ))
11
+ def test_is_valid_version(invalid_version):
12
+ with pytest.raises(AssertionError):
13
+ assert_is_valid_version(invalid_version)
@@ -0,0 +1,26 @@
1
+ from hive.common import units
2
+
3
+
4
+ def test_units():
5
+ assert (
6
+ ("BYTE", 1),
7
+ ("BYTES", 1),
8
+ ("GiB", 1_073_741_824),
9
+ ("HOUR", 3600),
10
+ ("HOURS", 3600),
11
+ ("KiB", 1024),
12
+ ("MICROSECOND", 1e-6),
13
+ ("MICROSECONDS", 1e-6),
14
+ ("MILLISECOND", 0.001),
15
+ ("MILLISECONDS", 0.001),
16
+ ("MINUTE", 60),
17
+ ("MINUTES", 60),
18
+ ("MiB", 1_048_576),
19
+ ("SECOND", 1),
20
+ ("SECONDS", 1),
21
+ ("TiB", 1099511627776),
22
+ ) == tuple(
23
+ (attr, getattr(units, attr))
24
+ for attr in sorted(dir(units))
25
+ if attr[0].isupper()
26
+ )
@@ -0,0 +1,6 @@
1
+ from hive.common.__version__ import __version__
2
+ from hive.common.testing import assert_is_valid_version
3
+
4
+
5
+ def test_version():
6
+ assert_is_valid_version(__version__)