@@ -30,13 +30,11 @@ def affirm_ready_for_create(topics: List[Topic]):
30
30
31
31
class TopicManager :
32
32
"""
33
- The source of all topic management with quixstreams .
33
+ The source of all topic management for a Quix Streams Application .
34
34
35
- Generally initialized and managed automatically by an `Application`,
36
- but allows a user to work with it directly when needed, such as using it alongside
37
- a plain `Producer` to create its topics.
35
+ Intended only for internal use by Application.
38
36
39
- See methods for details .
37
+ To create a Topic, use Application.topic() or generate them directly .
40
38
"""
41
39
42
40
# Default topic params
@@ -118,19 +116,17 @@ def all_topics(self) -> Dict[str, Topic]:
118
116
"""
119
117
return {topic .name : topic for topic in self ._all_topics_list }
120
118
121
- def _resolve_topic_name (self , name : str ) -> str :
119
+ def _finalize_topic (self , topic : Topic ) -> Topic :
122
120
"""
123
- Here primarily for adjusting the topic name for Quix topics .
121
+ Validates the original topic name and returns the Topic .
124
122
125
- Also validates topic name is not too long.
126
-
127
- :return: name, no changes (identity function)
123
+ Does more in QuixTopicManager.
128
124
"""
129
- if len (name ) > self ._max_topic_name_len :
125
+ if len (topic . name ) > self ._max_topic_name_len :
130
126
raise TopicNameLengthExceeded (
131
- f"Topic { name } exceeds the { self ._max_topic_name_len } character limit"
127
+ f"' { topic . name } ' exceeds the { self ._max_topic_name_len } character limit"
132
128
)
133
- return name
129
+ return topic
134
130
135
131
def _format_nested_name (self , topic_name : str ) -> str :
136
132
"""
@@ -171,9 +167,7 @@ def _internal_name(
171
167
:return: formatted topic name
172
168
"""
173
169
nested_name = self ._format_nested_name (topic_name )
174
- return self ._resolve_topic_name (
175
- f"{ topic_type } __{ '--' .join ([self ._consumer_group , nested_name , suffix ])} "
176
- )
170
+ return f"{ topic_type } __{ '--' .join ([self ._consumer_group , nested_name , suffix ])} "
177
171
178
172
def _create_topics (
179
173
self , topics : List [Topic ], timeout : float , create_timeout : float
@@ -264,42 +258,44 @@ def topic(
264
258
265
259
:return: Topic object with creation configs
266
260
"""
267
- name = self ._resolve_topic_name (name )
268
-
269
261
if not config :
270
262
config = TopicConfig (
271
263
num_partitions = self .default_num_partitions ,
272
264
replication_factor = self .default_replication_factor ,
273
265
extra_config = self .default_extra_config ,
274
266
)
275
- topic = Topic (
276
- name = name ,
277
- value_serializer = value_serializer ,
278
- value_deserializer = value_deserializer ,
279
- key_serializer = key_serializer ,
280
- key_deserializer = key_deserializer ,
281
- config = config ,
282
- timestamp_extractor = timestamp_extractor ,
267
+
268
+ topic = self ._finalize_topic (
269
+ Topic (
270
+ name = name ,
271
+ value_serializer = value_serializer ,
272
+ value_deserializer = value_deserializer ,
273
+ key_serializer = key_serializer ,
274
+ key_deserializer = key_deserializer ,
275
+ config = config ,
276
+ timestamp_extractor = timestamp_extractor ,
277
+ )
283
278
)
284
- self ._topics [name ] = topic
279
+ self ._topics [topic . name ] = topic
285
280
return topic
286
281
287
- def register (self , topic : Topic ):
282
+ def register (self , topic : Topic ) -> Topic :
288
283
"""
289
284
Register an already generated :class:`quixstreams.models.topics.Topic` to the topic manager.
290
285
291
286
The topic name and config can be updated by the topic manager.
292
287
293
288
:param topic: The topic to register
294
289
"""
295
- topic .name = self ._resolve_topic_name (topic .name )
296
290
if topic .config is None :
297
291
topic .config = TopicConfig (
298
292
num_partitions = self .default_num_partitions ,
299
293
replication_factor = self .default_replication_factor ,
300
294
extra_config = self .default_extra_config ,
301
295
)
296
+ topic = self ._finalize_topic (topic )
302
297
self ._topics [topic .name ] = topic
298
+ return topic
303
299
304
300
def repartition_topic (
305
301
self ,
@@ -324,21 +320,21 @@ def repartition_topic(
324
320
325
321
:return: `Topic` object (which is also stored on the TopicManager)
326
322
"""
327
- name = self ._internal_name ( "repartition" , topic_name , operation )
328
-
329
- topic = Topic (
330
- name = name ,
331
- value_deserializer = value_deserializer ,
332
- key_deserializer = key_deserializer ,
333
- value_serializer = value_serializer ,
334
- key_serializer = key_serializer ,
335
- config = self . _get_source_topic_config (
336
- topic_name ,
337
- extras_imports = self ._groupby_extra_config_imports_defaults ,
338
- timeout = timeout if timeout is not None else self . _timeout ,
339
- ),
323
+ topic = self ._finalize_topic (
324
+ Topic (
325
+ name = self . _internal_name ( "repartition" , topic_name , operation ),
326
+ value_deserializer = value_deserializer ,
327
+ key_deserializer = key_deserializer ,
328
+ value_serializer = value_serializer ,
329
+ key_serializer = key_serializer ,
330
+ config = self . _get_source_topic_config (
331
+ topic_name ,
332
+ extras_imports = self . _groupby_extra_config_imports_defaults ,
333
+ timeout = timeout if timeout is not None else self ._timeout ,
334
+ ) ,
335
+ )
340
336
)
341
- self ._repartition_topics [name ] = topic
337
+ self ._repartition_topics [topic . name ] = topic
342
338
return topic
343
339
344
340
def changelog_topic (
@@ -373,7 +369,6 @@ def changelog_topic(
373
369
:return: `Topic` object (which is also stored on the TopicManager)
374
370
"""
375
371
376
- topic_name = self ._resolve_topic_name (topic_name )
377
372
source_topic_config = self ._get_source_topic_config (
378
373
topic_name ,
379
374
extras_imports = self ._changelog_extra_config_imports_defaults ,
@@ -387,13 +382,15 @@ def changelog_topic(
387
382
extra_config = source_topic_config .extra_config ,
388
383
)
389
384
390
- topic = Topic (
391
- name = self ._internal_name ("changelog" , topic_name , store_name ),
392
- key_serializer = "bytes" ,
393
- value_serializer = "bytes" ,
394
- key_deserializer = "bytes" ,
395
- value_deserializer = "bytes" ,
396
- config = changelog_config ,
385
+ topic = self ._finalize_topic (
386
+ Topic (
387
+ name = self ._internal_name ("changelog" , topic_name , store_name ),
388
+ key_serializer = "bytes" ,
389
+ value_serializer = "bytes" ,
390
+ key_deserializer = "bytes" ,
391
+ value_deserializer = "bytes" ,
392
+ config = changelog_config ,
393
+ )
397
394
)
398
395
self ._changelog_topics .setdefault (topic_name , {})[store_name ] = topic
399
396
return topic
0 commit comments