1
1
#!/usr/bin/env python
2
+ """
3
+ puppetdb-stencil is a tool to render puppet resources using templates.
4
+ """
5
+
2
6
from __future__ import print_function
3
7
from __future__ import unicode_literals
4
8
7
11
import pypuppetdb
8
12
import jinja2
9
13
10
- log = logging .getLogger ('puppetdb_stencil' )
14
+ LOG = logging .getLogger ('puppetdb_stencil' )
11
15
12
16
METAPARAMS = ('require' , 'before' , 'subscribe' , 'notify' , 'audit' , 'loglevel' ,
13
- 'noop' , 'schedule' , 'stage' , 'alias' , 'tag' )
17
+ 'noop' , 'schedule' , 'stage' , 'alias' , 'tag' )
14
18
15
19
# Allow templates from anywhere on the filesystem
16
- loader = jinja2 .FileSystemLoader (['.' , '/' ])
17
- environment = jinja2 .Environment ( trim_blocks = True , lstrip_blocks = True ,
18
- loader = loader ,
19
- extensions = [ 'jinja2.ext.with_' , 'jinja2.ext.loopcontrols' ] )
20
+ LOADER = jinja2 .FileSystemLoader (['.' , '/' ])
21
+ EXTENSIONS = [ ' jinja2.ext.with_' , 'jinja2.ext.loopcontrols' ]
22
+ ENVIRONMENT = jinja2 . Environment ( trim_blocks = True , lstrip_blocks = True ,
23
+ loader = LOADER , extensions = EXTENSIONS )
20
24
21
25
22
- def render_resources (db , resource_type , template_names ):
23
- resources = db .resources (resource_type )
26
+ def render_resources (database , resource_type , template_names ):
27
+ """
28
+ Render resources of the given type. They are queried from the given
29
+ database and rendered using the first template from template_names that can
30
+ be loaded.
31
+ """
32
+ resources = database .resources (resource_type )
24
33
try :
25
- template = environment .select_template (template_names )
34
+ template = ENVIRONMENT .select_template (template_names )
26
35
except jinja2 .TemplatesNotFound :
27
- log .error ('No template found for {0}' .format (resource_type ))
36
+ LOG .error ('No template found for {0}' .format (resource_type ))
28
37
else :
29
38
return template .render (resource_type = resource_type ,
30
- resources = resources , metaparams = METAPARAMS )
39
+ resources = resources , metaparams = METAPARAMS )
31
40
32
41
33
- if __name__ == '__main__' :
42
+ def main ():
43
+ """
44
+ Main function
45
+ """
34
46
parser = argparse .ArgumentParser (prog = 'puppetdb_stencil' )
35
47
parser .add_argument ('resource_types' , metavar = 'RESOURCE_TYPE' , nargs = '+' )
36
48
parser .add_argument ('--templates' , '-t' , metavar = 'TEMPLATE' , nargs = '*' )
@@ -41,10 +53,14 @@ def render_resources(db, resource_type, template_names):
41
53
args = parser .parse_args ()
42
54
logging .basicConfig (level = logging .DEBUG if args .debug else logging .WARN )
43
55
44
- db = pypuppetdb .connect (host = args .host , port = args .port )
56
+ database = pypuppetdb .connect (host = args .host , port = args .port )
45
57
46
58
for resource_type in args .resource_types :
47
59
templates = ['{0}.jinja2' .format (resource_type )]
48
60
if args .templates :
49
61
templates += args .templates
50
- print (render_resources (db , resource_type , templates ))
62
+ print (render_resources (database , resource_type , templates ))
63
+
64
+
65
+ if __name__ == '__main__' :
66
+ main ()
0 commit comments