# Path: n_utils/ndt.py
# def find_include(basefile):
#     if os.path.isfile(basefile):
#         return basefile
#     for search_dir in include_dirs:
#         if os.path.isfile(search_dir + basefile):
#             return search_dir + basefile
#     return None
# 
# def find_all_includes(pattern):
#     ret = []
#     dirs = list(include_dirs)
#     dirs.insert(0, "./")
#     for search_dir in dirs:
#         for next_match in glob.glob(search_dir + pattern):
#             ret.append(next_match)
#     return ret
# 
# Path: n_utils/aws_infra_util.py
# def yaml_load(stream):
#     for name in INTRISINC_FUNCS:
#         yaml.add_multi_constructor(name, INTRISINC_FUNCS[name], Loader=yaml.SafeLoader)
# 
#     class OrderedLoader(yaml.SafeLoader):
#         pass
# 
#     def construct_mapping(loader, node):
#         loader.flatten_mapping(node)
#         return OrderedDict(loader.construct_pairs(node))
#     OrderedLoader.add_constructor(
#         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
#         construct_mapping)
# 
#     return yaml.load(stream, OrderedLoader)
# 
# def yaml_save(data):
#     class OrderedDumper(yaml.SafeDumper):
#         pass
# 
#     def _dict_representer(dumper, data):
#         return dumper.represent_mapping(
#             yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
#             list(data.items()))
# 
#     OrderedDumper.add_representer(OrderedDict, _dict_representer)
#     return yaml.dump(data, None, OrderedDumper, default_flow_style=False)
# 
# Path: n_utils/cf_utils.py
# def has_output_selector(stack, outputname, mapper):
#     if 'Outputs' not in stack:
#         return False
#     for output in stack['Outputs']:
#         if output['OutputKey'] == outputname:
#             return mapper(stack)
#     return False
# 
# def select_stacks(selector):
#     ret = []
#     paginator = boto3.client('cloudformation').get_paginator('describe_stacks')
#     for page in paginator.paginate():
#         for stack in page.get('Stacks'):
#             selected = selector(stack)
#             if selected:
#                 ret.append(selected)
#     return ret


from builtins import zip
from builtins import input
from builtins import str
from builtins import object
from copy import deepcopy
from collections import OrderedDict
from subprocess import Popen, PIPE
from argcomplete.completers import ChoicesCompleter
from awscli.customizations.configure.writer import ConfigFileWriter
from n_utils.ndt import find_include, find_all_includes
from n_utils.aws_infra_util import yaml_load, yaml_save
from n_utils.cf_utils import has_output_selector, select_stacks
import os
import random
import re
import shutil
import subprocess
import stat
import sys
import argparse
import argcomplete
import boto3
import ipaddr
    """ Creates roles necessary for baking images and deploying stacks
    """

    def __init__(self):
        ContextClassBase.__init__(self, ['network_stack', 'vault_stack'])
        self._ask_network_stack()
        self._ask_vault_stack()

    def _ask_network_stack(self):
        self.network_stack = "Network stack ({0}):\n"

        def network_sel(stack): return has_output_selector(stack, "VPC",
                                                           lambda stack: stack['StackName'])
        self.network_stacks = select_stacks(network_sel)
        if self.network_stacks:
            index = 1
            for stack_name in self.network_stacks:
                self.network_stack = self.network_stack + str(index) + ": " + \
                    stack_name + "\n"
                index = index + 1
        self.value_mappers["network_stack"] = _map_list
        self.template_transformers.append(lambda myself: _set_first_parameter(myself.template,
                                                                              "paramNetworkStack",
                                                                              myself.network_stack))

    def _ask_vault_stack(self):
        self.vault_stack = "Vault stack ({0}):\n"

        def vault_sel(stack): return has_output_selector(stack, "decryptPolicy",
                                                         lambda stack: stack['StackName'])

# Path: n_utils/ndt.py
# def find_include(basefile):
#     if os.path.isfile(basefile):
#         return basefile
#     for search_dir in include_dirs:
#         if os.path.isfile(search_dir + basefile):
#             return search_dir + basefile
#     return None
# 
# def find_all_includes(pattern):
#     ret = []
#     dirs = list(include_dirs)
#     dirs.insert(0, "./")
#     for search_dir in dirs:
#         for next_match in glob.glob(search_dir + pattern):
#             ret.append(next_match)
#     return ret
# 
# Path: n_utils/aws_infra_util.py
# def yaml_load(stream):
#     for name in INTRISINC_FUNCS:
#         yaml.add_multi_constructor(name, INTRISINC_FUNCS[name], Loader=yaml.SafeLoader)
# 
#     class OrderedLoader(yaml.SafeLoader):
#         pass
# 
#     def construct_mapping(loader, node):
#         loader.flatten_mapping(node)
#         return OrderedDict(loader.construct_pairs(node))
#     OrderedLoader.add_constructor(
#         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
#         construct_mapping)
# 
#     return yaml.load(stream, OrderedLoader)
# 
# def yaml_save(data):
#     class OrderedDumper(yaml.SafeDumper):
#         pass
# 
#     def _dict_representer(dumper, data):
#         return dumper.represent_mapping(
#             yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
#             list(data.items()))
# 
#     OrderedDumper.add_representer(OrderedDict, _dict_representer)
#     return yaml.dump(data, None, OrderedDumper, default_flow_style=False)
# 
# Path: n_utils/cf_utils.py
# def has_output_selector(stack, outputname, mapper):
#     if 'Outputs' not in stack:
#         return False
#     for output in stack['Outputs']:
#         if output['OutputKey'] == outputname:
#             return mapper(stack)
#     return False
# 
# def select_stacks(selector):
#     ret = []
#     paginator = boto3.client('cloudformation').get_paginator('describe_stacks')
#     for page in paginator.paginate():
#         for stack in page.get('Stacks'):
#             selected = selector(stack)
#             if selected:
#                 ret.append(selected)
#     return ret


# Path: n_utils/cf_bootstrap.py
from builtins import zip
from builtins import input
from builtins import str
from builtins import object
from copy import deepcopy
from collections import OrderedDict
from subprocess import Popen, PIPE
from argcomplete.completers import ChoicesCompleter
from awscli.customizations.configure.writer import ConfigFileWriter
from n_utils.ndt import find_include, find_all_includes
from n_utils.aws_infra_util import yaml_load, yaml_save
from n_utils.cf_utils import has_output_selector, select_stacks
import os
import random
import re
import shutil
import subprocess
import stat
import sys
import argparse
import argcomplete
import boto3
import ipaddr

    """ Creates roles necessary for baking images and deploying stacks
    """

    def __init__(self):
        ContextClassBase.__init__(self, ['network_stack', 'vault_stack'])
        self._ask_network_stack()
        self._ask_vault_stack()

    def _ask_network_stack(self):
        self.network_stack = "Network stack ({0}):\n"

        def network_sel(stack): return has_output_selector(stack, "VPC",
                                                           lambda stack: stack['StackName'])
        self.network_stacks = select_stacks(network_sel)
        if self.network_stacks:
            index = 1
            for stack_name in self.network_stacks:
                self.network_stack = self.network_stack + str(index) + ": " + \
                    stack_name + "\n"
                index = index + 1
        self.value_mappers["network_stack"] = _map_list
        self.template_transformers.append(lambda myself: _set_first_parameter(myself.template,
                                                                              "paramNetworkStack",
                                                                              myself.network_stack))

    def _ask_vault_stack(self):
        self.vault_stack = "Vault stack ({0}):\n"

        def vault_sel(stack): return has_output_selector(stack, "decryptPolicy",
                                                         lambda stack: stack['StackName'])

        self.vault_stacks = select_stacks(vault_sel)