#!/usr/bin/env python
"""
puppetdb-stencil is a tool to render puppet resources using templates.
"""

from __future__ import print_function
from __future__ import unicode_literals

import argparse
import logging
import pypuppetdb
import jinja2

LOG = logging.getLogger('puppetdb_stencil')

METAPARAMS = ('require', 'before', 'subscribe', 'notify', 'audit', 'loglevel',
              'noop', 'schedule', 'stage', 'alias', 'tag')

# Allow templates from anywhere on the filesystem
LOADER = jinja2.FileSystemLoader(['.', '/'])
EXTENSIONS = ['jinja2.ext.with_', 'jinja2.ext.loopcontrols']
ENVIRONMENT = jinja2.Environment(trim_blocks=True, lstrip_blocks=True,
                                 loader=LOADER, extensions=EXTENSIONS)


def render_resources(database, resource_type, template_names):
    """
    Render resources of the given type. They are queried from the given
    database and rendered using the first template from template_names that can
    be loaded.
    """
    resources = database.resources(resource_type)
    try:
        template = ENVIRONMENT.select_template(template_names)
    except jinja2.TemplatesNotFound:
        LOG.error('No template found for {0}'.format(resource_type))
    else:
        return template.render(resource_type=resource_type,
                               resources=resources, metaparams=METAPARAMS)


def main():
    """
    Main function
    """
    parser = argparse.ArgumentParser(prog='puppetdb_stencil')
    parser.add_argument('resource_types', metavar='RESOURCE_TYPE', nargs='+')
    parser.add_argument('--templates', '-t', metavar='TEMPLATE', nargs='*')
    parser.add_argument('--debug', '-d', action='store_true')
    parser.add_argument('--host', '-H', default='localhost')
    parser.add_argument('--port', '-p', default='8080')

    args = parser.parse_args()
    logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARN)

    database = pypuppetdb.connect(host=args.host, port=args.port)

    for resource_type in args.resource_types:
        templates = ['{0}.jinja2'.format(resource_type)]
        if args.templates:
            templates += args.templates
        print(render_resources(database, resource_type, templates))


if __name__ == '__main__':
    main()

@@ -375,16 +375,26 @@
         
+ 'ensure',
  'noop',
@@ -431,16 +431,16 @@
  'tag')%0A
-
 %0A# Allow
@@ -727,25 +727,24 @@
 XTENSIONS)%0A%0A
-%0A
 def render_r
@@ -791,16 +791,23 @@
 te_names
+, query
 ):%0A    %22
@@ -980,24 +980,68 @@
 ed.%0A    %22%22%22%0A
+    full_query = %5B%5D%0A    if query == %22%22:%0A    
     resource
@@ -1076,16 +1076,279 @@
 rce_type
+, query='%5B%22and%22,%5B%22~%22, %22tag%22, %22%22 %5D%5D')%0A    else:%0A        for tag in query:%0A            qtag = '%5B%22=%22, %22tag%22, %22%25s%22 %5D' %25 str(tag) %0A            full_query.append(qtag)%0A        resources = database.resources(resource_type, query='%5B%22and%22, %25s %5D' %25 (%22, %22.join(full_query)) 
 )%0A    tr
@@ -2107,17 +2107,78 @@
 '8080')%0A
+    parser.add_argument('--tag', '-g', default='', nargs='+')
 %0A
-
     args
@@ -2277,17 +2277,29 @@
 g.WARN)%0A
+    tag=None
 %0A
-
     data
@@ -2527,16 +2527,16 @@
 mplates%0A
-
         
@@ -2592,16 +2592,26 @@
 emplates
+, args.tag
 ))%0A%0A%0Aif 

Add tag filtering to stencil