forked from dahaic/test-strategist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton_dump.py
executable file
·68 lines (62 loc) · 2.54 KB
/
skeleton_dump.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
65
66
67
68
#!/usr/bin/env python2
# Copyright (c) 2017 Red Hat, Inc. All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import re
def dump_yaml_snippet(name):
with open('template.yaml', 'r') as template_file:
template = template_file.read()
print(template.format(**{'name':name}))
parser = argparse.ArgumentParser()
parser.description = ('Creates yaml skeleton based on file structured by '
'indentation. For example of how to format input file '
'see example.skel file.')
parser.add_argument('-t', '--template',
dest='template', default='template.yaml')
parser.add_argument('structure_file')
options = parser.parse_args()
namespace = []
namespace_indent = [1]
with open(options.structure_file, 'r') as source:
line = source.readline()
mo = re.match('^( *)\* *(.*)', line)
previous_indent = len(mo.group(1))
previous = mo.group(2)
for line in source.readlines():
mo = re.match('^( *)\* *(.*)', line)
indent = len(mo.group(1))
text = mo.group(2)
if indent > previous_indent:
# it was a group, no printing
namespace += [previous]
namespace_indent += [previous_indent]
elif indent == previous_indent:
dump_yaml_snippet(' - '.join(namespace + [previous]))
else:
# got out of group
dump_yaml_snippet(' - '.join(namespace + [previous]))
if indent == namespace_indent[-1]:
# returning back to known group
namespace.pop()
previous_indent = namespace_indent.pop()
else:
# just a different subgroup
previous_indent = indent
previous = text
# no common code for this else branch
continue
previous_indent = indent
previous = text
# last line
dump_yaml_snippet(' - '.join(namespace + [previous]))