-
Notifications
You must be signed in to change notification settings - Fork 4
/
yex
executable file
·75 lines (63 loc) · 2.77 KB
/
yex
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
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
#
# MIT License
#
# Copyright (c) 2019 Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import yaml
import sys
def tree_walk(yd, groups, match):
if isinstance(yd, list):
for subtree in yd:
tree_walk(subtree, groups, match)
if isinstance(yd, dict):
for subtree in yd:
if args.verbose:
print("[group] {}".format(subtree))
if match and args.recursive:
tree_walk(yd[subtree], groups, 1)
elif (groups is not None) and (not subtree in groups):
tree_walk(yd[subtree], groups, 0)
else:
tree_walk(yd[subtree], groups, 1)
if isinstance(yd, str):
if match:
if args.space:
print("{} ".format(yd), end="")
else:
print(yd)
parser = argparse.ArgumentParser(description='YAML extractor')
parser.add_argument('filename')
parser.add_argument('--groups', nargs = '*', dest = 'groups', help='space separated list of groups')
parser.add_argument('-r', '--recursive', action="store_true", help='recursive')
parser.add_argument('-s', '--space', action = 'store_true', help='space as delimiter')
parser.add_argument('-v', '--verbose', action = 'store_true', help='verbose output')
args = parser.parse_args()
yamldata = yaml.load(open(args.filename, 'r'), Loader=yaml.SafeLoader);
if args.groups is None:
sys.stderr.write("Empty search criteria, extracting content from all groups\n")
if args.verbose and (args.groups is not None):
print("Parsing {} looing for groups: {}".format(args.filename, " ".join(map(str, args.groups))))
tree_walk(yamldata, args.groups, 0)
if args.space:
print()
sys.exit(0)