<commit_before>require 'sfn'
require 'sparkle_formation'

require 'pathname'

module Sfn
  module CommandModule
    # Template handling helper methods
    module Template

      # cloudformation directories that should be ignored
      TEMPLATE_IGNORE_DIRECTORIES = %w(components dynamics registry)

      module InstanceMethods

        # Load the template file
        #
        # @param args [Symbol] options (:allow_missing)
        # @return [Hash] loaded template
        def load_template_file(*args)
          unless(config[:template])
            set_paths_and_discover_file!
            unless(File.exists?(config[:file].to_s))
              unless(args.include?(:allow_missing))
                ui.fatal "Invalid formation file path provided: #{config[:file]}"
                raise IOError.new "Failed to locate file: #{config[:file]}"
              end
            end
          end
          if(config[:template])
            config[:template]
          elsif(config[:file])
            if(config[:processing])
              sf = SparkleFormation.compile(config[:file], :sparkle)
              if(sf.nested? && !sf.isolated_nests?)
                raise TypeError.new('Template does not contain isolated stack nesting! Sfn does not support mixed mixed resources within root stack!')
              end
              run_callbacks_for(:stack, :stack_name => arguments.first, :sparkle_stack => sf)
              if(sf.nested? && config[:apply_nesting])
                if(config[:apply_nesting] == true)
                  config[:apply_nesting] = :deep
                end
                case config[:apply_nesting].to_sym
                when :deep
                  process_nested_stack_deep(sf)
                when :shallow
                  process_nested_stack_shallow(sf)
                else
                  raise ArgumentError.new "Unknown nesting style requested: #{config[:apply_nesting].inspect}!"
                end
              else
                sf.dump.merge('sfn_nested_stack' => !!sf.nested?)
              end
            else
              template = _from_json(File.read(config[:file]))
              run_callbacks_for(:stack, :stack_name => arguments.first, :hash_stack => template)
              template
            end
          else
            raise ArgumentError.new 'Failed to locate template for processing!'
          end
        end

        # Processes template using the original shallow workflow
        #
        # @param sf [SparkleFormation] stack
        # @return [Hash] dumped stack
        def process_nested_stack_shallow(sf)
          sf.apply_nesting(:shallow) do |stack_name, stack_definition, resource|
            run_callbacks_for(:stack, :stack_name => stack_name, :hash_stack => stack)
            run_callbacks_for(:stack, stack_name, stack_definition)
            bucket = provider.connection.api_for(:storage).buckets.get(
              config[:nesting_bucket]
            )
            if(config[:print_only])
              resource['Properties']['TemplateURL'] = "http://example.com/bucket/#{name_args.first}_#{stack_name}.json"
            else
              resource['Properties'].delete('Stack')
              unless(bucket)
                raise "Failed to locate configured bucket for stack template storage (#{bucket})!"
              end
              file = bucket.files.build
              file.name = "#{name_args.first}_#{stack_name}.json"
              file.content_type = 'text/json'
              file.body = MultiJson.dump(Sfn::Utils::StackParameterScrubber.scrub!(stack_definition))
              file.save
              url = URI.parse(file.url)
              resource['Properties']['TemplateURL'] = "#{url.scheme}://#{url.host}#{url.path}"
            end
          end
        end

        # Processes template using new deep workflow
        #
        # @param sf [SparkleFormation] stack
        # @return [Hash] dumped stack
        def process_nested_stack_deep(sf)
          sf.apply_nesting(:deep) do |stack_name, stack, resource|
            run_callbacks_for(:stack, :stack_name => stack_name, :sparkle_stack => stack)
            stack_definition = stack.compile.dump!
            stack_resource = resource._dump
            bucket = provider.connection.api_for(:storage).buckets.get(
              config[:nesting_bucket]
            )
            params = Hash[
              stack_definition.fetch('Parameters', {}).map do |k,v|
                next if stack_resource['Properties'].fetch('Parameters', {}).keys.include?(k)<commit_msg>Update shallow nesting interaction to match library expectations<commit_after>require 'sfn'
require 'sparkle_formation'

require 'pathname'

module Sfn
  module CommandModule
    # Template handling helper methods
    module Template

      # cloudformation directories that should be ignored
      TEMPLATE_IGNORE_DIRECTORIES = %w(components dynamics registry)

      module InstanceMethods

        # Load the template file
        #
        # @param args [Symbol] options (:allow_missing)
        # @return [Hash] loaded template
        def load_template_file(*args)
          unless(config[:template])
            set_paths_and_discover_file!
            unless(File.exists?(config[:file].to_s))
              unless(args.include?(:allow_missing))
                ui.fatal "Invalid formation file path provided: #{config[:file]}"
                raise IOError.new "Failed to locate file: #{config[:file]}"
              end
            end
          end
          if(config[:template])
            config[:template]
          elsif(config[:file])
            if(config[:processing])
              sf = SparkleFormation.compile(config[:file], :sparkle)
              if(sf.nested? && !sf.isolated_nests?)
                raise TypeError.new('Template does not contain isolated stack nesting! Sfn does not support mixed mixed resources within root stack!')
              end
              run_callbacks_for(:stack, :stack_name => arguments.first, :sparkle_stack => sf)
              if(sf.nested? && config[:apply_nesting])
                if(config[:apply_nesting] == true)
                  config[:apply_nesting] = :deep
                end
                case config[:apply_nesting].to_sym
                when :deep
                  process_nested_stack_deep(sf)
                when :shallow
                  process_nested_stack_shallow(sf)
                else
                  raise ArgumentError.new "Unknown nesting style requested: #{config[:apply_nesting].inspect}!"
                end
              else
                sf.dump.merge('sfn_nested_stack' => !!sf.nested?)
              end
            else
              template = _from_json(File.read(config[:file]))
              run_callbacks_for(:stack, :stack_name => arguments.first, :hash_stack => template)
              template
            end
          else
            raise ArgumentError.new 'Failed to locate template for processing!'
          end
        end

        # Processes template using the original shallow workflow
        #
        # @param sf [SparkleFormation] stack
        # @return [Hash] dumped stack
        def process_nested_stack_shallow(sf)
          sf.apply_nesting(:shallow) do |stack_name, stack, resource|
            run_callbacks_for(:stack, :stack_name => stack_name, :sparkle_stack => stack)
            stack_definition = stack.compile.dump!
            bucket = provider.connection.api_for(:storage).buckets.get(
              config[:nesting_bucket]
            )
            if(config[:print_only])
              template_url = "http://example.com/bucket/#{name_args.first}_#{stack_name}.json"
            else
              resource.properties.delete!(:stack)
              unless(bucket)
                raise "Failed to locate configured bucket for stack template storage (#{bucket})!"
              end
              file = bucket.files.build
              file.name = "#{name_args.first}_#{stack_name}.json"
              file.content_type = 'text/json'
              file.body = MultiJson.dump(Sfn::Utils::StackParameterScrubber.scrub!(stack_definition))
              file.save
              url = URI.parse(file.url)
              template_url = "#{url.scheme}://#{url.host}#{url.path}"
            end
            resource.properties.set!('TemplateURL', template_url)
          end
        end

        # Processes template using new deep workflow
        #
        # @param sf [SparkleFormation] stack
        # @return [Hash] dumped stack
        def process_nested_stack_deep(sf)
          sf.apply_nesting(:deep) do |stack_name, stack, resource|
            run_callbacks_for(:stack, :stack_name => stack_name, :sparkle_stack => stack)
            stack_definition = stack.compile.dump!
            stack_resource = resource._dump
            bucket = provider.connection.api_for(:storage).buckets.get(
              config[:nesting_bucket]
            )
            params = Hash[
              stack_definition.fetch('Parameters', {}).map do |k,v|
                next if stack_resource['Properties'].fetch('Parameters', {}).keys.include?(k)
{'fraction_non_alphanumeric': 0.09805857189865087, 'fraction_numerical': 0.0, 'mean_word_length': 1.9729377241604173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '503970', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2305, 'n_words': 519}