11import functools
2+ import warnings
23from engineio import json as _json
34
45(CONNECT , DISCONNECT , EVENT , ACK , CONNECT_ERROR , BINARY_EVENT , BINARY_ACK ) = \
@@ -21,6 +22,8 @@ class Packet:
2122
2223 uses_binary_events = True
2324 json = _json
25+ _configure_args = ((),())
26+ _subclass_registry = {}
2427
2528 def __init__ (self , packet_type = EVENT , data = None , namespace = None , id = None ,
2629 binary = None , encoded_packet = None ):
@@ -192,3 +195,23 @@ def _to_dict(self):
192195 if self .id is not None :
193196 d ['id' ] = self .id
194197 return d
198+
199+ @classmethod
200+ def configure (cls , * args , ** kwargs ):
201+ configure_args = (args , tuple (sorted (kwargs .items ())))
202+ try :
203+ args_hash = hash (configure_args )
204+ except TypeError :
205+ warnings .warn ('Packet.configure() called with unhashable '
206+ 'arguments; subclass caching will not work.' ,
207+ RuntimeWarning )
208+ args_hash = None
209+
210+ if args_hash in cls ._subclass_registry :
211+ return cls ._subclass_registry [args_hash ]
212+ return cls ._configure (* args , ** kwargs )
213+
214+ @classmethod
215+ def _configure (cls , * args , ** kwargs ):
216+ raise NotImplementedError ('Packet._configure() must be implemented '
217+ 'by subclasses.' )
0 commit comments