| 1 | # -*- coding: utf-8 -*- |
| 2 | # Description: |
| 3 | # Author: Ilya Mashchenko (ilyam8) |
| 4 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | |
| 6 | try: |
| 7 | from pyyaml3 import SafeLoader as YamlSafeLoader |
| 8 | except ImportError: |
| 9 | from yaml import SafeLoader as YamlSafeLoader |
| 10 | |
| 11 | try: |
| 12 | from collections import OrderedDict |
| 13 | except ImportError: |
| 14 | from third_party.ordereddict import OrderedDict |
| 15 | |
| 16 | DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map' |
| 17 | |
| 18 | |
| 19 | def dict_constructor(loader, node): |
| 20 | return OrderedDict(loader.construct_pairs(node)) |
| 21 | |
| 22 | |
| 23 | YamlSafeLoader.add_constructor(DEFAULT_MAPPING_TAG, dict_constructor) |
| 24 | |
| 25 | |
| 26 | def load_yaml(stream): |
| 27 | loader = YamlSafeLoader(stream) |
| 28 | try: |
| 29 | return loader.get_single_data() |
| 30 | finally: |
| 31 | loader.dispose() |
| 32 | |
| 33 | |
| 34 | def load_config(file_name): |
| 35 | with open(file_name, 'r') as stream: |
| 36 | return load_yaml(stream) |