mirror of
https://github.com/Luzifer/mqtt2influx.git
synced 2024-10-18 05:44:19 +00:00
24 lines
596 B
Python
24 lines
596 B
Python
import json
|
|
import yaml
|
|
|
|
|
|
class YAMLLambda(yaml.YAMLObject):
|
|
yaml_tag = '!lambda'
|
|
|
|
def __init__(self, lambda_code):
|
|
self.code = lambda_code
|
|
|
|
def run(self, in_value):
|
|
return eval('lambda {}'.format(self.code))(in_value)
|
|
|
|
@classmethod
|
|
def from_yaml(cls, loader, node):
|
|
return YAMLLambda(node.value)
|
|
|
|
@classmethod
|
|
def to_yaml(cls, dumper, data):
|
|
return dumper.represent_scalar(cls.yaml_tag, data.code)
|
|
|
|
|
|
yaml.SafeLoader.add_constructor('!lambda', YAMLLambda.from_yaml)
|
|
yaml.SafeDumper.add_multi_representer(YAMLLambda, YAMLLambda.to_yaml)
|