<commit_before>action :create do

  if new_resource.group.empty?
    key_group = value_for_platform(
      'freebsd' => { 'default' => 'wheel' },
      'default' => 'root'
    )
  else
    key_group = new_resource.group
  end

  begin
    key_item = Chef::EncryptedDataBagItem.load(new_resource.data_bag, canonicalize_node(new_resource.search_id))

    if key_item.nil?
      # Register the node as pending
      data = { "id" => "__#{canonicalize(n)}", "node" => n }
      item = Chef::EncryptedDataBagItem.encrypt_data_bag_item(data, Chef::EncryptedDataBagItem.load_secret(config[:secret_file]))
      data_bag = Chef::DataBagItem.new
      data_bag.data_bag(tarsnap_data_bag)
      data_bag.raw_data = item
      data_bag.save
    else
      # Otherwise write out the key locally
      file "#{new_resource.key_path}/#{new_resource.key_file}" do
        cookbook new_resource.cookbook
        mode "0600"
        owner new_resource.owner
        group key_group
        contents key_item['key']
      end
      # ...and destroy any pending data bag placeholder
      Chef::DataBagItem.destroy(tarsnap_data_bag, "__#{canonicalize(n)}")
    end

    new_resource.updated_by_last_action(true)
  rescue Chef::Exceptions::ValidationFailed => e
    Chef::Log.warn("Unable to retrieve the tarsnap key from the data bag!!!")
  end

end

def canonicalize_node(fqdn)
  fqdn.gsub('.', '_')
end<commit_msg>Move this outside of the try<commit_after>action :create do

  if new_resource.group.empty?
    key_group = value_for_platform(
      'freebsd' => { 'default' => 'wheel' },
      'default' => 'root'
    )
  else
    key_group = new_resource.group
  end

  key_item = Chef::EncryptedDataBagItem.load(new_resource.data_bag, canonicalize_node(new_resource.search_id))

  begin
    if key_item.nil?
      # Register the node as pending
      data = { "id" => "__#{canonicalize(n)}", "node" => n }
      item = Chef::EncryptedDataBagItem.encrypt_data_bag_item(data, Chef::EncryptedDataBagItem.load_secret(config[:secret_file]))
      data_bag = Chef::DataBagItem.new
      data_bag.data_bag(tarsnap_data_bag)
      data_bag.raw_data = item
      data_bag.save
    else
      # Otherwise write out the key locally
      file "#{new_resource.key_path}/#{new_resource.key_file}" do
        cookbook new_resource.cookbook
        mode "0600"
        owner new_resource.owner
        group key_group
        contents key_item['key']
      end
      # ...and destroy any pending data bag placeholder
      Chef::DataBagItem.destroy(tarsnap_data_bag, "__#{canonicalize(n)}")
    end

    new_resource.updated_by_last_action(true)
  rescue Chef::Exceptions::ValidationFailed => e
    Chef::Log.warn("Unable to retrieve the tarsnap key from the data bag!!!")
  end

end

def canonicalize_node(fqdn)
  fqdn.gsub('.', '_')
end
{'fraction_non_alphanumeric': 0.11115069469184183, 'fraction_numerical': 0.0028500178126113287, 'mean_word_length': 3.3738317757009346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6152906', 'n_tokens_mistral': 944, 'n_tokens_neox': 925, 'n_words': 188}