5
5
"""Generates argument_specs.yml from variables parsed in role.
6
6
7
7
Usage:
8
- vars2specs.py [-c] [-r DIR]
8
+ vars2specs.py [-c] [-i IND] [- r DIR]
9
9
10
10
Options:
11
11
-c Parse all roles in a collection [default: no]
12
+ -i IND --indent IND White space count for yaml indention. [default: 4]
12
13
-r DIR --role_dir=DIR Input role directory [default: ./].
13
14
"""
15
+ from textwrap import indent
14
16
import typing
15
17
import yaml
16
18
import docopt
@@ -71,7 +73,7 @@ class to generate arguments_spec.yml from parsed variables
71
73
"""
72
74
role_dir : Path
73
75
collection : bool
74
-
76
+ indent : int = 4
75
77
76
78
def __init__ (self , role : str , collection : bool ):
77
79
self .collection = collection
@@ -119,13 +121,12 @@ def generate_spec(self, path: Path, defined_vars):
119
121
except KeyError :
120
122
vartype = type (variables [var_name ]).__name__ if variables [var_name ] is not None else "str"
121
123
default = "default: %s" % self .quote_default (variables [var_name ], vartype ) if variables [var_name ] is not None else 'required: true'
122
- results .append ("""\
124
+ results .append ("""%s# line %s of %s
123
125
%s:
124
- # line %s of %s
125
126
%s
126
127
description: "%s"
127
128
type: "%s"
128
- """ % (var_name , linenumber , str (rel_path ), default , description , vartype ))
129
+ """ % (( " " * 3 * self . indent ) , linenumber , str (rel_path ), var_name , default , description , vartype ))
129
130
return results
130
131
131
132
@@ -143,7 +144,7 @@ def generate(self):
143
144
""" write argument specs """
144
145
yaml = YAML ()
145
146
yaml .preserve_quotes = True
146
- yaml .indent (mapping = 4 )
147
+ yaml .indent (mapping = self . indent )
147
148
yaml .width = 800
148
149
149
150
variable_specs = collections .defaultdict (list )
@@ -175,6 +176,8 @@ def main():
175
176
role_dir = args ['--role_dir' ] or './'
176
177
collection = args ['-c' ] or False
177
178
v2s = Vars2Specs (role_dir , collection )
179
+ if (args ['--indent' ]):
180
+ v2s .indent = int (args ['--indent' ])
178
181
v2s .generate ()
179
182
180
183
0 commit comments