21
21
from warnings import warn
22
22
23
23
24
- def byteDeserializer (topic , payload ):
25
- """
26
- byteDeserializer returns an unaltered payload to the caller
27
- """
28
-
29
- return payload
30
-
31
-
32
24
class Consumer (_impl ):
33
25
"""
34
26
Create a new Kafka Consumer instance.
@@ -53,15 +45,15 @@ class Consumer(_impl):
53
45
"""
54
46
def __new__ (cls , * args , ** kwargs ):
55
47
if 'key_deserializer' in kwargs or 'value_deserializer' in kwargs :
56
- return super (Consumer , cls ).__new__ (DeserializingConsumer , * args , ** kwargs )
48
+ return super (Consumer , cls ).__new__ (SerializingConsumer , * args , ** kwargs )
57
49
return super (Consumer , cls ).__new__ (cls , * args , ** kwargs )
58
50
59
51
60
- class DeserializingConsumer (Consumer ):
52
+ class SerializingConsumer (Consumer ):
61
53
"""
62
- DeserializingConsumer extends Consumer with configurable key and value deserializer.
54
+ SerializingConsumer extends Consumer with configurable key and value deserializer.
63
55
64
- Instances of DeserializingConsumer cannot be created directly.
56
+ Instances of SerializingConsumer cannot be created directly.
65
57
To obtain an instance of this class instantiate a Consumer with a key and/or value deserializer.
66
58
67
59
Duplicate params have been omitted for brevity. See Consumer for class documentation.
@@ -73,10 +65,15 @@ class DeserializingConsumer(Consumer):
73
65
__slots__ = ["_key_deserializer" , "_value_deserializer" ]
74
66
75
67
def __new__ (cls , * args , ** kwargs ):
76
- raise TypeError ("DeserializingConsumer is a non user-instantiable class" )
68
+ raise TypeError ("SerializingConsumer is a non user-instantiable class" )
69
+
70
+ @staticmethod
71
+ def byteSerializer (topic , data ):
72
+ """ Pass-through serializer """
73
+ return data
77
74
78
75
# conf must remain optional as long as kwargs are supported
79
- def __init__ (self , conf = {}, key_deserializer = byteDeserializer , value_deserializer = byteDeserializer ,
76
+ def __init__ (self , conf = {}, key_deserializer = None , value_deserializer = None ,
80
77
on_commit = None , stats_cb = None , throttle_cb = None , logger = None , ** kwargs ):
81
78
82
79
if not isinstance (conf , dict ):
@@ -90,26 +87,27 @@ def __init__(self, conf={}, key_deserializer=byteDeserializer, value_deserialize
90
87
"all keyword arguments must match the constructor signature explicitly." ,
91
88
category = DeprecationWarning , stacklevel = 2 )
92
89
90
+ # Ensure the default serializer cannot be overwritten with None on instantiation
91
+ if key_deserializer is None :
92
+ key_deserializer = SerializingConsumer .byteSerializer
93
+
94
+ if value_deserializer is None :
95
+ value_deserializer = SerializingConsumer .byteSerializer
96
+
93
97
self ._key_deserializer = key_deserializer
94
98
self ._value_deserializer = value_deserializer
95
99
96
100
# Callbacks can be set in the conf dict or *ideally* as parameters.
97
- # Handle both cases prior to passing along to _impl
98
- # If callbacks are configured in both places parameter values take precedence.
99
- if not on_commit :
100
- on_commit = conf .get ('on_commit' , None )
101
-
102
- if not stats_cb :
103
- stats_cb = conf .get ('stats_cb' , None )
104
-
105
- if not throttle_cb :
106
- throttle_cb = conf .get ('throttle_cb' , None )
107
-
108
- if not logger :
109
- logger = conf .get ('logger' , None )
101
+ # Raise a SyntaxError if a callback is set in both places.
102
+ for var , name in [(logger , 'logger' ), (on_commit , 'on_commit' ),
103
+ (stats_cb , 'stats_cb' ), (throttle_cb , 'throttle_cb' )]:
104
+ if all ([var , conf .get (name , None )]):
105
+ raise SyntaxError ("{} parameter repeated" .format (name ))
106
+ if var is None :
107
+ var = conf .get (name , None )
110
108
111
- super (DeserializingConsumer , self ).__init__ (conf , on_commit = on_commit , stats_cb = stats_cb ,
112
- throttle_cb = throttle_cb , logger = logger )
109
+ super (SerializingConsumer , self ).__init__ (conf , on_commit = on_commit , stats_cb = stats_cb ,
110
+ throttle_cb = throttle_cb , logger = logger )
113
111
114
112
def poll (self , timeout = - 1.0 ):
115
113
"""
@@ -127,7 +125,7 @@ def poll(self, timeout=-1.0):
127
125
:raises RuntimeError: If called on a closed consumer.
128
126
"""
129
127
130
- msg = super (DeserializingConsumer , self ).poll (timeout )
128
+ msg = super (SerializingConsumer , self ).poll (timeout )
131
129
132
130
if msg is None or msg .error ():
133
131
return msg
@@ -157,7 +155,7 @@ def consume(self, num_messages=1, timeout=-1):
157
155
:raises ValueError: If num_messages > 1M.
158
156
"""
159
157
160
- msgset = super (DeserializingConsumer , self ).consume (num_messages , timeout )
158
+ msgset = super (SerializingConsumer , self ).consume (num_messages , timeout )
161
159
for msg in msgset :
162
160
if msg .error ():
163
161
continue
0 commit comments