diff options
author | Costa Tsaousis <costa@tsaousis.gr> | 2017-02-07 21:58:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-07 21:58:53 +0200 |
commit | 78b4a6a1e1cd2b9f522f6626abeab0f3daa180e1 (patch) | |
tree | 6afdf1849759ee1ac9025be5a5884ec84f9f8c2d /plugins.d | |
parent | 70417d058f73384caee1224c9c729c394834170b (diff) | |
parent | e621007e591fb71bbec6c41fbd26caba61d58e6f (diff) |
Merge pull request #1737 from l2isbad/pythond_plugin_improvements
python.d.plugin: YAML output is ordered now
Diffstat (limited to 'plugins.d')
-rwxr-xr-x | plugins.d/python.d.plugin | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/plugins.d/python.d.plugin b/plugins.d/python.d.plugin index b4e6473a62..44b729094c 100755 --- a/plugins.d/python.d.plugin +++ b/plugins.d/python.d.plugin @@ -67,6 +67,26 @@ try: except ImportError: msg.fatal('Cannot find yaml library') +try: + from collections import OrderedDict + ORDERED = True + DICT = OrderedDict + msg.info('YAML output is ordered') +except ImportError: + ORDERED = False + DICT = dict + msg.info('YAML output is unordered') +else: + def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict): + class OrderedLoader(Loader): + pass + def construct_mapping(loader, node): + loader.flatten_mapping(node) + return object_pairs_hook(loader.construct_pairs(node)) + OrderedLoader.add_constructor( + yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, + construct_mapping) + return yaml.load(stream, OrderedLoader) class PythonCharts(object): """ @@ -235,7 +255,7 @@ class PythonCharts(object): # check if there are dict in config dict many_jobs = False for name in config: - if type(config[name]) is dict: + if isinstance(config[name], DICT): many_jobs = True break @@ -420,7 +440,10 @@ def read_config(path): """ try: with open(path, 'r') as stream: - config = yaml.load(stream) + if ORDERED: + config = ordered_load(stream, yaml.SafeLoader) + else: + config = yaml.load(stream) except (OSError, IOError): msg.error(str(path), "is not a valid configuration file") return None |