77used for determining members of a party.
88
99"""
10+ from abc import ABC , abstractmethod
11+ from typing import Generator , List , Optional
1012import uuid
13+ from kazoo .client import KazooClient
1114
1215from kazoo .exceptions import NodeExistsError , NoNodeError
1316
1417
15- class BaseParty (object ):
18+ class BaseParty (ABC ):
1619 """Base implementation of a party."""
1720
18- def __init__ (self , client , path , identifier = None ):
21+ def __init__ (
22+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
23+ ):
1924 """
2025 :param client: A :class:`~kazoo.client.KazooClient` instance.
2126 :param path: The party path to use.
@@ -29,17 +34,17 @@ def __init__(self, client, path, identifier=None):
2934 self .ensured_path = False
3035 self .participating = False
3136
32- def _ensure_parent (self ):
37+ def _ensure_parent (self ) -> None :
3338 if not self .ensured_path :
3439 # make sure our parent node exists
3540 self .client .ensure_path (self .path )
3641 self .ensured_path = True
3742
38- def join (self ):
43+ def join (self ) -> None :
3944 """Join the party"""
4045 return self .client .retry (self ._inner_join )
4146
42- def _inner_join (self ):
47+ def _inner_join (self ) -> None :
4348 self ._ensure_parent ()
4449 try :
4550 self .client .create (self .create_path , self .data , ephemeral = True )
@@ -49,38 +54,49 @@ def _inner_join(self):
4954 # suspended connection
5055 self .participating = True
5156
52- def leave (self ):
57+ def leave (self ) -> bool :
5358 """Leave the party"""
5459 self .participating = False
5560 return self .client .retry (self ._inner_leave )
5661
57- def _inner_leave (self ):
62+ def _inner_leave (self ) -> bool :
5863 try :
5964 self .client .delete (self .create_path )
6065 except NoNodeError :
6166 return False
6267 return True
6368
64- def __len__ (self ):
69+ def __len__ (self ) -> int :
6570 """Return a count of participating clients"""
6671 self ._ensure_parent ()
6772 return len (self ._get_children ())
6873
69- def _get_children (self ):
74+ def _get_children (self ) -> List [ str ] :
7075 return self .client .retry (self .client .get_children , self .path )
7176
77+ @property
78+ @abstractmethod
79+ def create_path (self ) -> str :
80+ ...
81+
7282
7383class Party (BaseParty ):
7484 """Simple pool of participating processes"""
7585
7686 _NODE_NAME = "__party__"
7787
78- def __init__ (self , client , path , identifier = None ):
88+ def __init__ (
89+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
90+ ):
7991 BaseParty .__init__ (self , client , path , identifier = identifier )
8092 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
8298
83- def __iter__ (self ):
99+ def __iter__ (self ) -> Generator [ str , None , None ] :
84100 """Get a list of participating clients' data values"""
85101 self ._ensure_parent ()
86102 children = self ._get_children ()
@@ -93,7 +109,7 @@ def __iter__(self):
93109 except NoNodeError : # pragma: nocover
94110 pass
95111
96- def _get_children (self ):
112+ def _get_children (self ) -> List [ str ] :
97113 children = BaseParty ._get_children (self )
98114 return [c for c in children if self ._NODE_NAME in c ]
99115
@@ -109,12 +125,18 @@ class ShallowParty(BaseParty):
109125
110126 """
111127
112- def __init__ (self , client , path , identifier = None ):
128+ def __init__ (
129+ self , client : KazooClient , path : str , identifier : Optional [str ] = None
130+ ):
113131 BaseParty .__init__ (self , client , path , identifier = identifier )
114132 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
116138
117- def __iter__ (self ):
139+ def __iter__ (self ) -> Generator [ str , None , None ] :
118140 """Get a list of participating clients' identifiers"""
119141 self ._ensure_parent ()
120142 children = self ._get_children ()
0 commit comments