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