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__ (self , client : KazooClient , path : str , identifier : Optional [ str ] = None ):
1922 """
2023 :param client: A :class:`~kazoo.client.KazooClient` instance.
2124 :param path: The party path to use.
@@ -29,17 +32,17 @@ def __init__(self, client, path, identifier=None):
2932 self .ensured_path = False
3033 self .participating = False
3134
32- def _ensure_parent (self ):
35+ def _ensure_parent (self ) -> None :
3336 if not self .ensured_path :
3437 # make sure our parent node exists
3538 self .client .ensure_path (self .path )
3639 self .ensured_path = True
3740
38- def join (self ):
41+ def join (self ) -> None :
3942 """Join the party"""
4043 return self .client .retry (self ._inner_join )
4144
42- def _inner_join (self ):
45+ def _inner_join (self ) -> None :
4346 self ._ensure_parent ()
4447 try :
4548 self .client .create (self .create_path , self .data , ephemeral = True )
@@ -49,38 +52,48 @@ def _inner_join(self):
4952 # suspended connection
5053 self .participating = True
5154
52- def leave (self ):
55+ def leave (self ) -> bool :
5356 """Leave the party"""
5457 self .participating = False
5558 return self .client .retry (self ._inner_leave )
5659
57- def _inner_leave (self ):
60+ def _inner_leave (self ) -> bool :
5861 try :
5962 self .client .delete (self .create_path )
6063 except NoNodeError :
6164 return False
6265 return True
6366
64- def __len__ (self ):
67+ def __len__ (self ) -> int :
6568 """Return a count of participating clients"""
6669 self ._ensure_parent ()
6770 return len (self ._get_children ())
6871
69- def _get_children (self ):
72+ def _get_children (self ) -> List [ str ] :
7073 return self .client .retry (self .client .get_children , self .path )
7174
75+ @property
76+ @abstractmethod
77+ def create_path (self ) -> str :
78+ ...
79+
80+
7281
7382class Party (BaseParty ):
7483 """Simple pool of participating processes"""
7584
7685 _NODE_NAME = "__party__"
7786
78- def __init__ (self , client , path , identifier = None ):
87+ def __init__ (self , client : KazooClient , path : str , identifier : Optional [ str ] = None ):
7988 BaseParty .__init__ (self , client , path , identifier = identifier )
8089 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
8295
83- def __iter__ (self ):
96+ def __iter__ (self ) -> Generator [ str , None , None ] :
8497 """Get a list of participating clients' data values"""
8598 self ._ensure_parent ()
8699 children = self ._get_children ()
@@ -93,7 +106,7 @@ def __iter__(self):
93106 except NoNodeError : # pragma: nocover
94107 pass
95108
96- def _get_children (self ):
109+ def _get_children (self ) -> List [ str ] :
97110 children = BaseParty ._get_children (self )
98111 return [c for c in children if self ._NODE_NAME in c ]
99112
@@ -109,12 +122,17 @@ class ShallowParty(BaseParty):
109122
110123 """
111124
112- def __init__ (self , client , path , identifier = None ):
125+ def __init__ (self , client : KazooClient , path : str , identifier : Optional [ str ] = None ):
113126 BaseParty .__init__ (self , client , path , identifier = identifier )
114127 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+
116134
117- def __iter__ (self ):
135+ def __iter__ (self ) -> Generator [ str , None , None ] :
118136 """Get a list of participating clients' identifiers"""
119137 self ._ensure_parent ()
120138 children = self ._get_children ()
0 commit comments