7
7
used for determining members of a party.
8
8
9
9
"""
10
+ from __future__ import annotations
11
+
12
+ from abc import ABC , abstractmethod
13
+ from typing import TYPE_CHECKING
10
14
import uuid
11
15
12
16
from kazoo .exceptions import NodeExistsError , NoNodeError
13
17
18
+ if TYPE_CHECKING :
19
+ from typing import Generator , List , Optional
20
+
21
+ from kazoo .client import KazooClient
22
+
14
23
15
- class BaseParty (object ):
24
+ class BaseParty (ABC ):
16
25
"""Base implementation of a party."""
17
26
18
- def __init__ (self , client , path , identifier = None ):
27
+ def __init__ (
28
+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
29
+ ):
19
30
"""
20
31
:param client: A :class:`~kazoo.client.KazooClient` instance.
21
32
:param path: The party path to use.
@@ -29,17 +40,17 @@ def __init__(self, client, path, identifier=None):
29
40
self .ensured_path = False
30
41
self .participating = False
31
42
32
- def _ensure_parent (self ):
43
+ def _ensure_parent (self ) -> None :
33
44
if not self .ensured_path :
34
45
# make sure our parent node exists
35
46
self .client .ensure_path (self .path )
36
47
self .ensured_path = True
37
48
38
- def join (self ):
49
+ def join (self ) -> None :
39
50
"""Join the party"""
40
51
return self .client .retry (self ._inner_join )
41
52
42
- def _inner_join (self ):
53
+ def _inner_join (self ) -> None :
43
54
self ._ensure_parent ()
44
55
try :
45
56
self .client .create (self .create_path , self .data , ephemeral = True )
@@ -49,38 +60,49 @@ def _inner_join(self):
49
60
# suspended connection
50
61
self .participating = True
51
62
52
- def leave (self ):
63
+ def leave (self ) -> bool :
53
64
"""Leave the party"""
54
65
self .participating = False
55
66
return self .client .retry (self ._inner_leave )
56
67
57
- def _inner_leave (self ):
68
+ def _inner_leave (self ) -> bool :
58
69
try :
59
70
self .client .delete (self .create_path )
60
71
except NoNodeError :
61
72
return False
62
73
return True
63
74
64
- def __len__ (self ):
75
+ def __len__ (self ) -> int :
65
76
"""Return a count of participating clients"""
66
77
self ._ensure_parent ()
67
78
return len (self ._get_children ())
68
79
69
- def _get_children (self ):
80
+ def _get_children (self ) -> List [ str ] :
70
81
return self .client .retry (self .client .get_children , self .path )
71
82
83
+ @property
84
+ @abstractmethod
85
+ def create_path (self ) -> str :
86
+ ...
87
+
72
88
73
89
class Party (BaseParty ):
74
90
"""Simple pool of participating processes"""
75
91
76
92
_NODE_NAME = "__party__"
77
93
78
- def __init__ (self , client , path , identifier = None ):
94
+ def __init__ (
95
+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
96
+ ):
79
97
BaseParty .__init__ (self , client , path , identifier = identifier )
80
98
self .node = uuid .uuid4 ().hex + self ._NODE_NAME
81
- self .create_path = self .path + "/" + self .node
99
+ self ._create_path = self .path + "/" + self .node
100
+
101
+ @property
102
+ def create_path (self ) -> str :
103
+ return self ._create_path
82
104
83
- def __iter__ (self ):
105
+ def __iter__ (self ) -> Generator [ str , None , None ] :
84
106
"""Get a list of participating clients' data values"""
85
107
self ._ensure_parent ()
86
108
children = self ._get_children ()
@@ -93,7 +115,7 @@ def __iter__(self):
93
115
except NoNodeError : # pragma: nocover
94
116
pass
95
117
96
- def _get_children (self ):
118
+ def _get_children (self ) -> List [ str ] :
97
119
children = BaseParty ._get_children (self )
98
120
return [c for c in children if self ._NODE_NAME in c ]
99
121
@@ -109,12 +131,18 @@ class ShallowParty(BaseParty):
109
131
110
132
"""
111
133
112
- def __init__ (self , client , path , identifier = None ):
134
+ def __init__ (
135
+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
136
+ ):
113
137
BaseParty .__init__ (self , client , path , identifier = identifier )
114
138
self .node = "-" .join ([uuid .uuid4 ().hex , self .data .decode ("utf-8" )])
115
- self .create_path = self .path + "/" + self .node
139
+ self ._create_path = self .path + "/" + self .node
140
+
141
+ @property
142
+ def create_path (self ) -> str :
143
+ return self ._create_path
116
144
117
- def __iter__ (self ):
145
+ def __iter__ (self ) -> Generator [ str , None , None ] :
118
146
"""Get a list of participating clients' identifiers"""
119
147
self ._ensure_parent ()
120
148
children = self ._get_children ()
0 commit comments