<commit_before>#!/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 sys
import os
import pypuppetdb
import jinja2


LOG = logging.getLogger('puppetdb_stencil')
METAPARAMS = ('require', 'before', 'subscribe', 'notify', 'audit', 'loglevel', 'noop', 'schedule', 'stage', 'alias', 'tag')
ALLOWED_METAPARAMS = ('alias')
NAMED_OBJECTS = ('host', 'hostgroup', 'servicegroup', 'servicedependency', 'contact', 'contactgroup', 'timeperiod', 'command')
# 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 is_resource_visible(resource, localsite):
    return resource.exported and (
        ('only-cross-site' not in resource.tags and 'no-cross-site' not in resource.tags) or
        ('only-cross-site' in resource.tags and 'no-cross-site' not in resource.tags and localsite == 'false') or
        ('only-cross-site' not in resource.tags and 'no-cross-site' in resource.tags and localsite == 'true')
    )


def render_resources(database, resource_type, localsite, 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.
    """
    # database.resources() is a generator, but we need to iterate it twice, so make a copy
    r = database.resources(resource_type)
    resources = []
    try:
        template = ENVIRONMENT.select_template(template_names)
    except jinja2.TemplatesNotFound:
        LOG.error('No template found for {0}'.format(resource_type))
    else:
        icinga_config = ''
        object_name = resource_type[7:]
        named_object = object_name in NAMED_OBJECTS
        service_dependencies = {}
        for resource in r:
            resources.append(resource)
            envs_to_ignore = []
            if is_resource_visible(resource, localsite):
                dto = {
                    'object_name': object_name,
                    'named_object': named_object,
                    'name': resource.name,
                    'parameters': []
                }
                # capture resource parameters from puppet
                for key, value in resource.parameters.items():
                    if (key not in METAPARAMS or key in ALLOWED_METAPARAMS) and (isinstance(value, list)):
                        dto['parameters'].append({key: ','.join(value)})
                    else:
                        dto['parameters'].append({key: value})
                    envs_to_ignore.append((object_name + '_' + key).upper())
                # capture environment variable defaults
                for name in os.environ:
                    nameparts = name.split('_')
                    if nameparts[0].lower() == object_name and name not in envs_to_ignore:
                        dto['parameters'].append({'_'.join(nameparts[1:]).lower(): os.environ[name].lower()})
                icinga_config += template.render(dto=dto) + '\n'
            # collect child service dependencies under parent service_description
            for tag in resource.tags:
                if 'parent:' in tag:
                    parent_service_description_list = tag.split(':')
                    if len(parent_service_description_list) == 2:
                        parent_service_description = parent_service_description_list[1]
                        if parent_service_description not in service_dependencies:
                            service_dependencies[parent_service_description] = []
                        service_dependencies[parent_service_description].append(resource)
        # render service dependencies
        if len(service_dependencies) > 0:<commit_msg>notifications_enabled = 0 in both regions<commit_after>#!/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 sys
import os
import pypuppetdb
import jinja2


LOG = logging.getLogger('puppetdb_stencil')
METAPARAMS = ('require', 'before', 'subscribe', 'notify', 'audit', 'loglevel', 'noop', 'schedule', 'stage', 'alias', 'tag')
ALLOWED_METAPARAMS = ('alias')
NAMED_OBJECTS = ('host', 'hostgroup', 'servicegroup', 'servicedependency', 'contact', 'contactgroup', 'timeperiod', 'command')
# 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 is_resource_visible(resource, localsite):
    return resource.exported and (
        ('only-cross-site' not in resource.tags and 'no-cross-site' not in resource.tags) or
        ('only-cross-site' in resource.tags and 'no-cross-site' not in resource.tags and localsite == 'false') or
        ('only-cross-site' not in resource.tags and 'no-cross-site' in resource.tags and localsite == 'true')
    )


def render_resources(database, resource_type, localsite, 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.
    """
    # database.resources() is a generator, but we need to iterate it twice, so make a copy
    r = database.resources(resource_type)
    resources = []
    try:
        template = ENVIRONMENT.select_template(template_names)
    except jinja2.TemplatesNotFound:
        LOG.error('No template found for {0}'.format(resource_type))
    else:
        icinga_config = ''
        object_name = resource_type[7:]
        named_object = object_name in NAMED_OBJECTS
        service_dependencies = {}
        for resource in r:
            resources.append(resource)
            envs_to_ignore = []
            if is_resource_visible(resource, localsite):
                dto = {
                    'object_name': object_name,
                    'named_object': named_object,
                    'name': resource.name,
                    'parameters': []
                }
                # capture resource parameters from puppet
                for key, value in resource.parameters.items():
                    if key not in METAPARAMS or key in ALLOWED_METAPARAMS:
                        if isinstance(value, list):
                            dto['parameters'].append({key: ','.join(value)})
                            envs_to_ignore.append((object_name + '_' + key).upper())
                        # CIRRUS-4549: a value of 2 is the signal to
                        # take the environment variable default. A
                        # value of 0 or 1 should be used as-is from
                        # puppet.
                        elif not (key == 'notifications_enabled' and str(value) == '2'):
                            dto['parameters'].append({key: value})
                            envs_to_ignore.append((object_name + '_' + key).upper())
                # capture environment variable defaults
                for name in os.environ:
                    nameparts = name.split('_')
                    if nameparts[0].lower() == object_name and name not in envs_to_ignore:
                        dto['parameters'].append({'_'.join(nameparts[1:]).lower(): os.environ[name].lower()})
                icinga_config += template.render(dto=dto) + '\n'
            # collect child service dependencies under parent service_description
            for tag in resource.tags:
                if 'parent:' in tag:
                    parent_service_description_list = tag.split(':')
                    if len(parent_service_description_list) == 2:
                        parent_service_description = parent_service_description_list[1]
                        if parent_service_description not in service_dependencies:
                            service_dependencies[parent_service_description] = []
                        service_dependencies[parent_service_description].append(resource)
        # render service dependencies
        if len(service_dependencies) > 0:
{'fraction_non_alphanumeric': 0.1004959848842702, 'fraction_numerical': 0.00413320736891828, 'mean_word_length': 2.402571313780635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '615252', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2189, 'n_words': 605}