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