commit 7d2d89aabdacca34610b17d418965e187384adc6 Author: 0xee Date: Mon Nov 25 16:59:08 2019 +0100 Add initial version diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..1ae5cbe --- /dev/null +++ b/default.nix @@ -0,0 +1,10 @@ +{ python3 }: +with python3.pkgs; +buildPythonPackage rec { + name = "mqtt-config"; + src = ./.; + propagatedBuildInputs = []; + buildInputs = [ pyyaml paho-mqtt ]; + doCheck = false; + shellHook = ""; +} diff --git a/mqtt-config b/mqtt-config new file mode 100755 index 0000000..959f91e --- /dev/null +++ b/mqtt-config @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import argparse +import yaml + +import paho.mqtt.publish + + +def read_config(config_dict, base_topic=''): + ret = {} + for elem in config_dict: + if 'topic' not in elem: + raise Exception("Invalid config") + sep = '/' if base_topic else '' + topic = '{}{}{}'.format(base_topic.rstrip("/"), sep, + elem["topic"].lstrip("/")) + if 'children' in elem: + ret.update(read_config(elem['children'], base_topic=topic)) + if 'value' in elem: + ret[topic] = elem['value'] + return ret + + +def main(): + options = argparse.ArgumentParser() + options.add_argument('--broker', '-b', default='localhost') + options.add_argument('--port', '-p', default='1883', type=int) + options.add_argument('config') + args = options.parse_args() + + with open(args.config, 'r') as f: + config_dict = yaml.load(f, Loader=yaml.Loader) + + topics = read_config(config_dict) + + msgs = [(topic, value, 1, True) for topic, value in topics.items()] + paho.mqtt.publish.multiple(msgs, + hostname=args.broker, + port=args.port, + client_id='configuration') + + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1bf0993 --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +from distutils.core import setup + +setup(name='mqtt-config', + version='0.1', + description='MQTT Configurator', + author='0xee', + author_email='mqtt-config@0xee.eu', + packages=[], + scripts=['mqtt-config']) diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..d0cf41d --- /dev/null +++ b/shell.nix @@ -0,0 +1,2 @@ +{ pkgs ? import {} }: +pkgs.callPackage ./. {}