frequenz-client-base 0.2.1__tar.gz → 0.4.0__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.
Files changed (21) hide show
  1. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/PKG-INFO +3 -1
  2. frequenz-client-base-0.4.0/RELEASE_NOTES.md +18 -0
  3. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/pyproject.toml +27 -24
  4. frequenz-client-base-0.4.0/src/frequenz/client/base/_grpchacks.py +46 -0
  5. frequenz-client-base-0.4.0/src/frequenz/client/base/channel.py +75 -0
  6. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/streaming.py +24 -23
  7. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz_client_base.egg-info/PKG-INFO +3 -1
  8. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz_client_base.egg-info/SOURCES.txt +2 -0
  9. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz_client_base.egg-info/requires.txt +28 -20
  10. frequenz-client-base-0.2.1/RELEASE_NOTES.md +0 -6
  11. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/LICENSE +0 -0
  12. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/MANIFEST.in +0 -0
  13. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/README.md +0 -0
  14. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/setup.cfg +0 -0
  15. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/__init__.py +0 -0
  16. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/conftest.py +0 -0
  17. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/conversion.py +0 -0
  18. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/py.typed +0 -0
  19. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz/client/base/retry.py +0 -0
  20. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz_client_base.egg-info/dependency_links.txt +0 -0
  21. {frequenz-client-base-0.2.1 → frequenz-client-base-0.4.0}/src/frequenz_client_base.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: frequenz-client-base
3
- Version: 0.2.1
3
+ Version: 0.4.0
4
4
  Summary: Utilities for writing Frequenz API clients
5
5
  Author-email: Frequenz Energy-as-a-Service GmbH <floss@frequenz.com>
6
6
  License: MIT
@@ -19,6 +19,8 @@ Classifier: Topic :: Software Development :: Libraries
19
19
  Classifier: Typing :: Typed
20
20
  Requires-Python: <4,>=3.11
21
21
  Description-Content-Type: text/markdown
22
+ Provides-Extra: grpcio
23
+ Provides-Extra: grpclib
22
24
  Provides-Extra: dev-flake8
23
25
  Provides-Extra: dev-formatting
24
26
  Provides-Extra: dev-mkdocs
@@ -0,0 +1,18 @@
1
+ # Frequenz Client Base Library Release Notes
2
+
3
+ ## Summary
4
+
5
+ <!-- Here goes a general summary of what this release is about -->
6
+
7
+ ## Upgrading
8
+
9
+ - You should now install the dependency using `frequenz-client-base[grpcio]` (or `frequenz-client-base[grpclib]`) if you want to migrate to `grpclib`).
10
+
11
+ ## New Features
12
+
13
+ - `GrpcStreamBroadcaster` is now compatible with both `grpcio` and `grpclib` implementations of gRPC. Just install `frequenz-client-base[grpcio]` or `frequenz-client-base[grpclib]` to use the desired implementation and everything should work as expected.
14
+ - A new module `channel` with a function to parse URIs to create `grpclib` client `Channel` instances.
15
+
16
+ ## Bug Fixes
17
+
18
+ - Fixed retrying for `GrpcStreamBroadcaster` when the retry interval is set to 0 (before it would stop retrying if the interval was set to 0).
@@ -5,7 +5,7 @@
5
5
  requires = [
6
6
  "setuptools == 68.1.0",
7
7
  "setuptools_scm[toml] == 7.1.0",
8
- "frequenz-repo-config[lib] == 0.9.1",
8
+ "frequenz-repo-config[lib] == 0.9.2",
9
9
  ]
10
10
  build-backend = "setuptools.build_meta"
11
11
 
@@ -15,7 +15,6 @@ description = "Utilities for writing Frequenz API clients"
15
15
  readme = "README.md"
16
16
  license = { text = "MIT" }
17
17
  keywords = ["frequenz", "python", "lib", "library", "client-base"]
18
- # TODO(cookiecutter): Remove and add more classifiers if appropriate
19
18
  classifiers = [
20
19
  "Development Status :: 3 - Alpha",
21
20
  "Intended Audience :: Developers",
@@ -26,11 +25,9 @@ classifiers = [
26
25
  "Typing :: Typed",
27
26
  ]
28
27
  requires-python = ">= 3.11, < 4"
29
- # TODO(cookiecutter): Remove and add more dependencies if appropriate
30
28
  dependencies = [
31
- "frequenz-channels >= v1.0.0-beta.2, < 2",
32
- "grpcio >= 1.54.2, < 2",
33
- "protobuf >= 4.21.6, < 5",
29
+ "frequenz-channels >= v1.0.0-rc1, < 2",
30
+ "protobuf >= 4.21.6, < 6",
34
31
  "typing-extensions >= 4.5.0, < 5",
35
32
  ]
36
33
  dynamic = ["version"]
@@ -39,48 +36,52 @@ dynamic = ["version"]
39
36
  name = "Frequenz Energy-as-a-Service GmbH"
40
37
  email = "floss@frequenz.com"
41
38
 
42
- # TODO(cookiecutter): Remove and add more optional dependencies if appropriate
43
39
  [project.optional-dependencies]
40
+ grpcio = ["grpcio >= 1.54.2, < 2"]
41
+ grpclib = ["grpclib >= 0.4.0, < 0.5"]
44
42
  dev-flake8 = [
45
43
  "flake8 == 7.0.0",
46
44
  "flake8-docstrings == 1.7.0",
47
- "flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
45
+ "flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
48
46
  "pydoclint == 0.4.1",
49
47
  "pydocstyle == 6.3.0",
48
+ "frequenz-client-base[grpclib,grpcio]",
50
49
  ]
51
- dev-formatting = ["black == 24.2.0", "isort == 5.13.2"]
50
+ dev-formatting = ["black == 24.4.2", "isort == 5.13.2"]
52
51
  dev-mkdocs = [
53
- "black == 24.2.0",
54
- "Markdown==3.5.2",
52
+ "black == 24.4.2",
53
+ "Markdown==3.6",
55
54
  "mike == 2.0.0",
56
55
  "mkdocs-gen-files == 0.5.0",
57
56
  "mkdocs-literate-nav == 0.6.1",
58
57
  "mkdocs-macros-plugin == 1.0.5",
59
- "mkdocs-material == 9.5.12",
60
- "mkdocstrings[python] == 0.24.1",
61
- "frequenz-repo-config[lib] == 0.9.1",
58
+ "mkdocs-material == 9.5.20",
59
+ "mkdocstrings[python] == 0.25.0",
60
+ "frequenz-repo-config[lib] == 0.9.2",
61
+ "frequenz-client-base[grpclib,grpcio]",
62
62
  ]
63
63
  dev-mypy = [
64
- "mypy == 1.8.0",
65
- "types-Markdown == 3.5.0.20240129",
66
- "types-protobuf == 4.24.0.20240129",
64
+ "mypy == 1.10.0",
65
+ "types-Markdown == 3.6.0.20240316",
66
+ "types-protobuf == 5.26.0.20240422",
67
67
  "grpc-stubs == 1.53.0.5", # This dependency introduces breaking changes in patch releases
68
68
  # For checking the noxfile, docs/ script, and tests
69
69
  "frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
70
70
  ]
71
- dev-noxfile = ["nox == 2023.4.22", "frequenz-repo-config[lib] == 0.9.1"]
71
+ dev-noxfile = ["nox == 2024.4.15", "frequenz-repo-config[lib] == 0.9.2"]
72
72
  dev-pylint = [
73
73
  "pylint == 3.1.0",
74
74
  # For checking the noxfile, docs/ script, and tests
75
75
  "frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
76
76
  ]
77
77
  dev-pytest = [
78
- "pytest == 8.0.2",
79
- "frequenz-repo-config[extra-lint-examples] == 0.9.1",
80
- "pytest-mock == 3.12.0",
81
- "pytest-asyncio == 0.23.5",
82
- "async-solipsism == 0.5",
83
- "hypothesis == 6.98.15",
78
+ "pytest == 8.2.0",
79
+ "frequenz-repo-config[extra-lint-examples] == 0.9.2",
80
+ "pytest-mock == 3.14.0",
81
+ "pytest-asyncio == 0.23.6",
82
+ "async-solipsism == 0.6",
83
+ "hypothesis == 6.100.2",
84
+ "frequenz-client-base[grpclib,grpcio]",
84
85
  ]
85
86
  dev = [
86
87
  "frequenz-client-base[dev-mkdocs,dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]",
@@ -141,6 +142,8 @@ disable = [
141
142
  "line-too-long",
142
143
  "unused-variable",
143
144
  "unnecessary-lambda-assignment",
145
+ # Checked by mypy
146
+ "no-member",
144
147
  ]
145
148
 
146
149
  [tool.pytest.ini_options]
@@ -0,0 +1,46 @@
1
+ # License: MIT
2
+ # Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
+
4
+ """Hacks to deal with multiple grpc libraries.
5
+
6
+ This module conditionally imports the base exceptions from the `grpclib` and `grpcio`
7
+ libraries, assigning them a new name:
8
+
9
+ - [`GrpclibError`][] for [`grpclib.GRPCError`][]
10
+ - [`GrpcioError`][] for [`grpc.aio.AioRpcError`][]
11
+
12
+ If the libraries are not installed, the module defines dummy classes with the same names
13
+ to avoid import errors.
14
+
15
+ This way exceptions can be caught from both libraries independently of which one is
16
+ used. The unused library will just never raise any exceptions.
17
+ """
18
+
19
+
20
+ try:
21
+ from grpclib import GRPCError as GrpclibError
22
+ except ImportError:
23
+
24
+ class GrpclibError(Exception): # type: ignore[no-redef]
25
+ """A dummy class to avoid import errors.
26
+
27
+ This class will never be actually used, as it is only used for catching
28
+ exceptions from the grpclib library. If the grpclib library is not installed,
29
+ this class will never be instantiated.
30
+ """
31
+
32
+
33
+ try:
34
+ from grpc.aio import AioRpcError as GrpcioError
35
+ except ImportError:
36
+
37
+ class GrpcioError(Exception): # type: ignore[no-redef]
38
+ """A dummy class to avoid import errors.
39
+
40
+ This class will never be actually used, as it is only used for catching
41
+ exceptions from the grpc library. If the grpc library is not installed,
42
+ this class will never be instantiated.
43
+ """
44
+
45
+
46
+ __all__ = ["GrpclibError", "GrpcioError"]
@@ -0,0 +1,75 @@
1
+ # License: MIT
2
+ # Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
+
4
+ """Handling of gRPC channels."""
5
+
6
+ from urllib.parse import parse_qs, urlparse
7
+
8
+ from grpclib.client import Channel
9
+
10
+
11
+ def _to_bool(value: str) -> bool:
12
+ value = value.lower()
13
+ if value in ("true", "on", "1"):
14
+ return True
15
+ if value in ("false", "off", "0"):
16
+ return False
17
+ raise ValueError(f"Invalid boolean value '{value}'")
18
+
19
+
20
+ def parse_grpc_uri(uri: str, /, *, default_port: int = 9090) -> Channel:
21
+ """Create a grpclib client channel from a URI.
22
+
23
+ The URI must have the following format:
24
+
25
+ ```
26
+ grpc://hostname[:port][?ssl=false]
27
+ ```
28
+
29
+ A few things to consider about URI components:
30
+
31
+ - If any other components are present in the URI, a [`ValueError`][] is raised.
32
+ - If the port is omitted, the `default_port` is used.
33
+ - If a query parameter is passed many times, the last value is used.
34
+ - The only supported query parameter is `ssl`, which must be a boolean value and
35
+ defaults to `false`.
36
+ - Boolean query parameters can be specified with the following values
37
+ (case-insensitive): `true`, `1`, `on`, `false`, `0`, `off`.
38
+
39
+ Args:
40
+ uri: The gRPC URI specifying the connection parameters.
41
+ default_port: The default port number to use if the URI does not specify one.
42
+
43
+ Returns:
44
+ A grpclib client channel object.
45
+
46
+ Raises:
47
+ ValueError: If the URI is invalid or contains unexpected components.
48
+ """
49
+ parsed_uri = urlparse(uri)
50
+ if parsed_uri.scheme != "grpc":
51
+ raise ValueError(
52
+ f"Invalid scheme '{parsed_uri.scheme}' in the URI, expected 'grpc'", uri
53
+ )
54
+ if not parsed_uri.hostname:
55
+ raise ValueError(f"Host name is missing in URI '{uri}'", uri)
56
+ for attr in ("path", "fragment", "params", "username", "password"):
57
+ if getattr(parsed_uri, attr):
58
+ raise ValueError(
59
+ f"Unexpected {attr} '{getattr(parsed_uri, attr)}' in the URI '{uri}'",
60
+ uri,
61
+ )
62
+
63
+ options = {k: v[-1] for k, v in parse_qs(parsed_uri.query).items()}
64
+ ssl = _to_bool(options.pop("ssl", "false"))
65
+ if options:
66
+ raise ValueError(
67
+ f"Unexpected query parameters {options!r} in the URI '{uri}'",
68
+ uri,
69
+ )
70
+
71
+ return Channel(
72
+ host=parsed_uri.hostname,
73
+ port=parsed_uri.port or default_port,
74
+ ssl=ssl,
75
+ )
@@ -5,13 +5,13 @@
5
5
 
6
6
  import asyncio
7
7
  import logging
8
- from typing import Any, Callable, Generic, TypeVar
9
-
10
- import grpc.aio
8
+ from collections.abc import AsyncIterator, Callable
9
+ from typing import Generic, TypeVar
11
10
 
12
11
  from frequenz import channels
13
12
 
14
13
  from . import retry
14
+ from ._grpchacks import GrpcioError, GrpclibError
15
15
 
16
16
  _logger = logging.getLogger(__name__)
17
17
 
@@ -29,7 +29,7 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
29
29
  def __init__(
30
30
  self,
31
31
  stream_name: str,
32
- stream_method: Callable[[], grpc.aio.UnaryStreamCall[Any, InputT]],
32
+ stream_method: Callable[[], AsyncIterator[InputT]],
33
33
  transform: Callable[[InputT], OutputT],
34
34
  retry_strategy: retry.Strategy | None = None,
35
35
  ):
@@ -51,7 +51,7 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
51
51
  )
52
52
 
53
53
  self._channel: channels.Broadcast[OutputT] = channels.Broadcast(
54
- f"GrpcStreamBroadcaster-{stream_name}"
54
+ name=f"GrpcStreamBroadcaster-{stream_name}"
55
55
  )
56
56
  self._task = asyncio.create_task(self._run())
57
57
 
@@ -64,7 +64,7 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
64
64
  Returns:
65
65
  A new receiver.
66
66
  """
67
- return self._channel.new_receiver(maxsize=maxsize)
67
+ return self._channel.new_receiver(limit=maxsize)
68
68
 
69
69
  async def stop(self) -> None:
70
70
  """Stop the streaming helper."""
@@ -82,29 +82,30 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
82
82
  sender = self._channel.new_sender()
83
83
 
84
84
  while True:
85
- _logger.debug("Making call to grpc streaming method: %s", self._stream_name)
86
-
85
+ error: Exception | None = None
86
+ _logger.info("%s: starting to stream", self._stream_name)
87
87
  try:
88
88
  call = self._stream_method()
89
89
  async for msg in call:
90
90
  await sender.send(self._transform(msg))
91
- except grpc.aio.AioRpcError:
92
- _logger.exception(
93
- "Error in grpc streaming method: %s", self._stream_name
94
- )
95
- if interval := self._retry_strategy.next_interval():
96
- _logger.warning(
97
- "`%s`, connection ended, retrying %s in %0.3f seconds.",
98
- self._stream_name,
99
- self._retry_strategy.get_progress(),
100
- interval,
101
- )
102
- await asyncio.sleep(interval)
103
- else:
104
- _logger.warning(
105
- "`%s`, connection ended, retry limit exceeded %s.",
91
+ except (GrpcioError, GrpclibError) as err:
92
+ error = err
93
+ error_str = f"Error: {error}" if error else "Stream exhausted"
94
+ interval = self._retry_strategy.next_interval()
95
+ if interval is None:
96
+ _logger.error(
97
+ "%s: connection ended, retry limit exceeded (%s), giving up. %s.",
106
98
  self._stream_name,
107
99
  self._retry_strategy.get_progress(),
100
+ error_str,
108
101
  )
109
102
  await self._channel.close()
110
103
  break
104
+ _logger.warning(
105
+ "%s: connection ended, retrying %s in %0.3f seconds. %s.",
106
+ self._stream_name,
107
+ self._retry_strategy.get_progress(),
108
+ interval,
109
+ error_str,
110
+ )
111
+ await asyncio.sleep(interval)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: frequenz-client-base
3
- Version: 0.2.1
3
+ Version: 0.4.0
4
4
  Summary: Utilities for writing Frequenz API clients
5
5
  Author-email: Frequenz Energy-as-a-Service GmbH <floss@frequenz.com>
6
6
  License: MIT
@@ -19,6 +19,8 @@ Classifier: Topic :: Software Development :: Libraries
19
19
  Classifier: Typing :: Typed
20
20
  Requires-Python: <4,>=3.11
21
21
  Description-Content-Type: text/markdown
22
+ Provides-Extra: grpcio
23
+ Provides-Extra: grpclib
22
24
  Provides-Extra: dev-flake8
23
25
  Provides-Extra: dev-formatting
24
26
  Provides-Extra: dev-mkdocs
@@ -4,6 +4,8 @@ README.md
4
4
  RELEASE_NOTES.md
5
5
  pyproject.toml
6
6
  src/frequenz/client/base/__init__.py
7
+ src/frequenz/client/base/_grpchacks.py
8
+ src/frequenz/client/base/channel.py
7
9
  src/frequenz/client/base/conftest.py
8
10
  src/frequenz/client/base/conversion.py
9
11
  src/frequenz/client/base/py.typed
@@ -1,6 +1,5 @@
1
- frequenz-channels<2,>=v1.0.0-beta.2
2
- grpcio<2,>=1.54.2
3
- protobuf<5,>=4.21.6
1
+ frequenz-channels<2,>=v1.0.0-rc1
2
+ protobuf<6,>=4.21.6
4
3
  typing-extensions<5,>=4.5.0
5
4
 
6
5
  [dev]
@@ -12,41 +11,50 @@ flake8-docstrings==1.7.0
12
11
  flake8-pyproject==1.2.3
13
12
  pydoclint==0.4.1
14
13
  pydocstyle==6.3.0
14
+ frequenz-client-base[grpcio,grpclib]
15
15
 
16
16
  [dev-formatting]
17
- black==24.2.0
17
+ black==24.4.2
18
18
  isort==5.13.2
19
19
 
20
20
  [dev-mkdocs]
21
- black==24.2.0
22
- Markdown==3.5.2
21
+ black==24.4.2
22
+ Markdown==3.6
23
23
  mike==2.0.0
24
24
  mkdocs-gen-files==0.5.0
25
25
  mkdocs-literate-nav==0.6.1
26
26
  mkdocs-macros-plugin==1.0.5
27
- mkdocs-material==9.5.12
28
- mkdocstrings[python]==0.24.1
29
- frequenz-repo-config[lib]==0.9.1
27
+ mkdocs-material==9.5.20
28
+ mkdocstrings[python]==0.25.0
29
+ frequenz-repo-config[lib]==0.9.2
30
+ frequenz-client-base[grpcio,grpclib]
30
31
 
31
32
  [dev-mypy]
32
- mypy==1.8.0
33
- types-Markdown==3.5.0.20240129
34
- types-protobuf==4.24.0.20240129
33
+ mypy==1.10.0
34
+ types-Markdown==3.6.0.20240316
35
+ types-protobuf==5.26.0.20240422
35
36
  grpc-stubs==1.53.0.5
36
37
  frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
37
38
 
38
39
  [dev-noxfile]
39
- nox==2023.4.22
40
- frequenz-repo-config[lib]==0.9.1
40
+ nox==2024.4.15
41
+ frequenz-repo-config[lib]==0.9.2
41
42
 
42
43
  [dev-pylint]
43
44
  pylint==3.1.0
44
45
  frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
45
46
 
46
47
  [dev-pytest]
47
- pytest==8.0.2
48
- frequenz-repo-config[extra-lint-examples]==0.9.1
49
- pytest-mock==3.12.0
50
- pytest-asyncio==0.23.5
51
- async-solipsism==0.5
52
- hypothesis==6.98.15
48
+ pytest==8.2.0
49
+ frequenz-repo-config[extra-lint-examples]==0.9.2
50
+ pytest-mock==3.14.0
51
+ pytest-asyncio==0.23.6
52
+ async-solipsism==0.6
53
+ hypothesis==6.100.2
54
+ frequenz-client-base[grpcio,grpclib]
55
+
56
+ [grpcio]
57
+ grpcio<2,>=1.54.2
58
+
59
+ [grpclib]
60
+ grpclib<0.5,>=0.4.0
@@ -1,6 +0,0 @@
1
- # Frequenz Client Base Library Release Notes
2
-
3
- ## Summary
4
-
5
- * The generated docs now show the type of symbol in the table of contents and have cross-linking to the protobuf and channels documentation.
6
- * The minimum supported version of `protobuf` was downgraded to 4.21.6, so downstream projects having this older version as minimum requirements can still use this release.