ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
adctk.publisher.MultiPublisher Class Reference

Interface for a group of publishers all being fed the same message(s). More...

Public Member Functions

 __init__ (self, bool strict, str|list[str]|dict|None plugins)
 
str version (self)
 
None add (self, Publisher publisher)
 
int publish (self, builder.Builder b)
 
None terminate (self)
 finalize() all configured publishers
 
None pause (self)
 pause() all configured publishers
 
None resume (self)
 resume() all configured publishers
 
list[str] get_names (self)
 

Public Attributes

 defaults
 
 paused
 
 plugin_prefix
 
 strict
 
 factory
 
 pvec
 

Protected Member Functions

 _config_from_dict (self, dict[dict[str]] plugins, strict)
 
 _config_from_list (self, list[str] plugins, bool strict)
 
 _config_from_env (self, str env_name, bool strict)
 

Protected Attributes

 _version
 

Detailed Description

Interface for a group of publishers all being fed the same message(s).

Constructor & Destructor Documentation

◆ __init__()

adctk.publisher.MultiPublisher.__init__ (   self,
bool  strict,
str|list[str]|dict|None  plugins 
)
Parameters
strictx
plugins
269 def __init__(self, strict: bool, plugins: str|list[str]|dict|None):
270 """! @param strict x
271 @param plugins
272 """
273 self._version = "1.0.0"
274 self.defaults = None
275 self.paused = False
276 self.plugin_prefix = None
277 self.strict = strict
278 self.pvec : [Publisher] = []
279 from . import factory # break import loop
280 self.factory = factory.Factory
281 if plugins is None:
282 return
283 if isinstance(plugins, str):
284 self._config_from_env(plugins, strict)
285 return
286 if isinstance(plugins, list):
287 if len(plugins):
288 self._config_from_list(plugins, strict)
289 else:
290 self._config_from_env("ADC_MULTI_PUBLISHER_NAMES", strict)
291 return
292 if isinstance(plugins, dict):
293 self._config_from_dict(plugins, strict)
294 return
295 raise ValueError("plugins argument of unexpected type: {plugins}")
296

Member Function Documentation

◆ _config_from_dict()

adctk.publisher.MultiPublisher._config_from_dict (   self,
dict[dict[str]]  plugins,
  strict 
)
protected
@brief custom conf plugin X using plugins[X] dictionary.
238 def _config_from_dict(self, plugins: dict[dict[str]], strict):
239 """@brief custom conf plugin X using plugins[X] dictionary."""
240 for plug, d in plugins.items():
241 p = self.factory.get_publisher(plug)
242 if strict and not p:
243 raise ValueError(f"MultiPublisher unable to construct plugin {plug}")
244 if d:
245 if not isinstance(d, dict):
246 raise ValueError(f"plugins must be a dictionary of dictionaries of strings, not {plugins}")
247 p.config(d)
248 else:
249 p.config({})
250 p.initialize()
251 self.add(p)
252

◆ _config_from_env()

adctk.publisher.MultiPublisher._config_from_env (   self,
str  env_name,
bool  strict 
)
protected
@brief configure with list following env_name.split(":")
261 def _config_from_env(self, env_name: str, strict: bool):
262 """@brief configure with list following env_name.split(":")"""
263 env_val = os.getenv(env_name, "")
264 if env_val is None or not len(env_val):
265 return
266 plugins = env_val.split(":")
267 self._config_from_list(plugins, strict)
268

◆ _config_from_list()

adctk.publisher.MultiPublisher._config_from_list (   self,
list[str]  plugins,
bool  strict 
)
protected
@brief configure each plugin named in the list with defaults & env.
253 def _config_from_list(self, plugins: list[str], strict: bool):
254 """@brief configure each plugin named in the list with defaults & env."""
255 for i in plugins:
256 p = self.factory.get_publisher(i)
257 if strict and not p:
258 raise ValueError(f"MultiPublisher unable to construct plugin {i}")
259 self.add(p)
260

◆ add()

None adctk.publisher.MultiPublisher.add (   self,
Publisher  publisher 
)
 @param Add publisher to the dispatch list
301 def add(self, publisher: Publisher) -> None:
302 """ @param Add publisher to the dispatch list"""
303 if not publisher:
304 return None
305 self.pvec.append(publisher)
306 print(f"DEB: Adding publisher {publisher.name()}")
307 return None
308

◆ get_names()

list[str] adctk.publisher.MultiPublisher.get_names (   self)
344 def get_names(self) -> list[str]:
345 n = []
346 for i in self.pvec:
347 n.append(i.name())
348 return n

◆ pause()

None adctk.publisher.MultiPublisher.pause (   self)

pause() all configured publishers

330 def pause(self) -> None:
331 """! pause() all configured publishers"""
332 # print(f"PAUSING {len(self.pvec)} PUBLISHERS")
333 for pub in self.pvec:
334 pub.pause()
335 self.paused = True
336

◆ publish()

int adctk.publisher.MultiPublisher.publish (   self,
builder.Builder  b 
)
Parameters
bbuilder to publish via all configured publishers.
Returns
count of publishers for which publish() returns an error.
309 def publish(self, b: builder.Builder) -> int:
310 """! @param b builder to publish via all configured publishers.
311
312 @return count of publishers for which publish() returns an error.
313 """
314 if self.paused:
315 return 0
316 err = 0
317 for pub in self.pvec:
318 e = pub.publish(b)
319 if e:
320 err += 1
321 log.logger.debug("Publish failed for plugin %s", pub.name())
322 return err
323

◆ resume()

None adctk.publisher.MultiPublisher.resume (   self)

resume() all configured publishers

337 def resume(self) -> None:
338 """! resume() all configured publishers"""
339 # print(f"RESUMING {len(self.pvec)} PUBLISHERS")
340 for pub in self.pvec:
341 pub.resume()
342 self.paused = False
343

◆ terminate()

None adctk.publisher.MultiPublisher.terminate (   self)

finalize() all configured publishers

324 def terminate(self) -> None:
325 """! finalize() all configured publishers"""
326 for pub in self.pvec:
327 pub.finalize()
328 self.pvec = []
329

◆ version()

str adctk.publisher.MultiPublisher.version (   self)
 @return the version of the MultiPublisher
297 def version(self) -> str:
298 """ @return the version of the MultiPublisher"""
299 return self._version
300

Member Data Documentation

◆ _version

adctk.publisher.MultiPublisher._version
protected

◆ defaults

adctk.publisher.MultiPublisher.defaults

◆ factory

adctk.publisher.MultiPublisher.factory

◆ paused

adctk.publisher.MultiPublisher.paused

◆ plugin_prefix

adctk.publisher.MultiPublisher.plugin_prefix

◆ pvec

adctk.publisher.MultiPublisher.pvec

◆ strict

adctk.publisher.MultiPublisher.strict

The documentation for this class was generated from the following file: