77used for determining members of a party.
88
99"""
10+ from __future__ import annotations
11+
12+ from abc import ABC , abstractmethod
13+ from typing import TYPE_CHECKING
1014import uuid
1115
1216from kazoo .exceptions import NodeExistsError , NoNodeError
1317
18+ if TYPE_CHECKING :
19+ from typing import Generator , List , Optional
20+
21+ from kazoo .client import KazooClient
22+
1423
15- class BaseParty (object ):
24+ class BaseParty (ABC ):
1625 """Base implementation of a party."""
1726
18- def __init__ (self , client , path , identifier = None ):
27+ def __init__ (
28+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
29+ ):
1930 """
2031 :param client: A :class:`~kazoo.client.KazooClient` instance.
2132 :param path: The party path to use.
@@ -29,17 +40,17 @@ def __init__(self, client, path, identifier=None):
2940 self .ensured_path = False
3041 self .participating = False
3142
32- def _ensure_parent (self ):
43+ def _ensure_parent (self ) -> None :
3344 if not self .ensured_path :
3445 # make sure our parent node exists
3546 self .client .ensure_path (self .path )
3647 self .ensured_path = True
3748
38- def join (self ):
49+ def join (self ) -> None :
3950 """Join the party"""
4051 return self .client .retry (self ._inner_join )
4152
42- def _inner_join (self ):
53+ def _inner_join (self ) -> None :
4354 self ._ensure_parent ()
4455 try :
4556 self .client .create (self .create_path , self .data , ephemeral = True )
@@ -49,38 +60,49 @@ def _inner_join(self):
4960 # suspended connection
5061 self .participating = True
5162
52- def leave (self ):
63+ def leave (self ) -> bool :
5364 """Leave the party"""
5465 self .participating = False
5566 return self .client .retry (self ._inner_leave )
5667
57- def _inner_leave (self ):
68+ def _inner_leave (self ) -> bool :
5869 try :
5970 self .client .delete (self .create_path )
6071 except NoNodeError :
6172 return False
6273 return True
6374
64- def __len__ (self ):
75+ def __len__ (self ) -> int :
6576 """Return a count of participating clients"""
6677 self ._ensure_parent ()
6778 return len (self ._get_children ())
6879
69- def _get_children (self ):
80+ def _get_children (self ) -> List [ str ] :
7081 return self .client .retry (self .client .get_children , self .path )
7182
83+ @property
84+ @abstractmethod
85+ def create_path (self ) -> str :
86+ ...
87+
7288
7389class Party (BaseParty ):
7490 """Simple pool of participating processes"""
7591
7692 _NODE_NAME = "__party__"
7793
78- def __init__ (self , client , path , identifier = None ):
94+ def __init__ (
95+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
96+ ):
7997 BaseParty .__init__ (self , client , path , identifier = identifier )
8098 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
82104
83- def __iter__ (self ):
105+ def __iter__ (self ) -> Generator [ str , None , None ] :
84106 """Get a list of participating clients' data values"""
85107 self ._ensure_parent ()
86108 children = self ._get_children ()
@@ -93,7 +115,7 @@ def __iter__(self):
93115 except NoNodeError : # pragma: nocover
94116 pass
95117
96- def _get_children (self ):
118+ def _get_children (self ) -> List [ str ] :
97119 children = BaseParty ._get_children (self )
98120 return [c for c in children if self ._NODE_NAME in c ]
99121
@@ -109,12 +131,18 @@ class ShallowParty(BaseParty):
109131
110132 """
111133
112- def __init__ (self , client , path , identifier = None ):
134+ def __init__ (
135+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
136+ ):
113137 BaseParty .__init__ (self , client , path , identifier = identifier )
114138 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
116144
117- def __iter__ (self ):
145+ def __iter__ (self ) -> Generator [ str , None , None ] :
118146 """Get a list of participating clients' identifiers"""
119147 self ._ensure_parent ()
120148 children = self ._get_children ()
0 commit comments