-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.py
executable file
·64 lines (52 loc) · 2.04 KB
/
loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
Include and compare BOTH choices for loader implementation in `ansible.parsing.yaml.loader`
"""
from yaml.composer import Composer
from yaml.reader import Reader
from yaml.scanner import Scanner
from yaml.parser import Parser
from yaml.resolver import Resolver
try:
from yaml import CParser
except ImportError:
try:
from _yaml import CParser
except ImportError:
try:
from yaml._yaml import CParser
except ImportError:
try:
from yaml.cyaml import CParser
except ImportError:
print('Could not find a CParser implementation. \
The CParser bug cannot be demonstrated.')
raise SystemExit
from ansible.parsing.yaml.constructor import AnsibleConstructor
class AnsibleCLoader(CParser, AnsibleConstructor, Resolver):
def __init__(self, stream, file_name=None, vault_secrets=None):
CParser.__init__(self, stream)
AnsibleConstructor.__init__(self, file_name=file_name, vault_secrets=vault_secrets)
Resolver.__init__(self)
class AnsiblePyLoader(Reader, Scanner, Parser, Composer, AnsibleConstructor, Resolver):
def __init__(self, stream, file_name=None, vault_secrets=None):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)
AnsibleConstructor.__init__(self, file_name=file_name, vault_secrets=vault_secrets)
Resolver.__init__(self)
if __name__ == '__main__':
from sys import argv
from json import dumps
def validate(loader_class, filename):
print('\x1b[1;33m', loader_class, filename, '\x1b[m')
try:
with open(filename, 'rb') as stream:
data = loader_class(stream).get_single_data()
print(dumps(data, indent=2))
except Exception as exception:
print('\x1b[1;31m', exception, '\x1b[m')
for filename in argv[1:]:
validate(AnsibleCLoader, filename)
validate(AnsiblePyLoader, filename)