Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_struct():
data = {'hello': 'world'}
struct = Struct(**data)
assert struct.hello == 'world'
assert struct.get('hello') == 'world'
assert struct.get('unknown', default=42) == 42
assert repr(struct) == ""
assert struct.enum() == data
:param config: The configuration object
:param name: The name of the service plugin, e.g. "log" or "file"
:param data: The data to be converged into an appropriate item Struct object instance
"""
# Bootstrap mqttwarn core
# TODO: Optionally run w/o configuration.
bootstrap(config=config)
# Load designated service plugins
load_services([name])
service_logger_name = 'mqttwarn.services.{}'.format(name)
srv = make_service(mqttc=None, name=service_logger_name)
# Build a mimikry item instance for feeding to the service plugin
item = Struct(**data)
# TODO: Read configuration optionally from data.
item.config = config.config('config:' + name)
item.service = srv
item.target = 'mqttwarn'
item.data = {} # FIXME
# Launch plugin
module = service_plugins[name]['module']
response = module.plugin(srv, item)
logger.info('Plugin response: {}'.format(response))
if HAVE_JINJA is False and context.get_config(section, 'template'):
logger.warning("Templating not possible because Jinja2 is not installed")
if HAVE_JINJA is True:
template = context.get_config(section, 'template')
if template is not None:
try:
text = render_template(template, transform_data)
if text is not None:
item['message'] = text
except Exception as e:
logger.warning("Cannot render `%s' template: %s" % (template, e))
if item.get('message') is not None and len(item.get('message')) > 0:
st = Struct(**item)
notified = False
logger.info("Invoking service plugin for `%s'" % service)
try:
# Fire the plugin in a separate thread and kill it if it doesn't return in 10s
module = service_plugins[service]['module']
service_logger_name = 'mqttwarn.services.{}'.format(service)
srv = make_service(mqttc=mqttc, name=service_logger_name)
notified = timeout(module.plugin, (srv, st))
except Exception as e:
logger.exception("Cannot invoke service for `%s'" % service)
if not notified:
logger.warning("Notification of %s for `%s' FAILED or TIMED OUT" % (service, item.get('topic')))
else:
logger.warning("Notification of %s for `%s' suppressed: text is empty" % (service, item.get('topic')))