<commit_before># -*- encoding : utf-8 -*-

class Phrase::Tool::Commands::Pull < Phrase::Tool::Commands::Base
  ALLOWED_DOWNLOAD_FORMATS = %w(yml po xml strings json resx ts qph ini plist properties xlf)
  DEFAULT_DOWNLOAD_FORMAT = "yml"
  
  def initialize(options, args)
    super(options, args)
    require_auth_token!
    
    @locale = @args[1]
    
    # TODO: remove DEFAULT_DOWNLOAD_FORMAT when phrase app has been updated
    @format = @options.get(:format) || config.format || DEFAULT_DOWNLOAD_FORMAT
    @target = @options.get(:target)
    @target ||= Phrase::Tool::Formats.target_directory(@format) if format_valid?(@format)
  end
  
  def execute!
    (print_error("Invalid format: #{@format}") and exit_command) unless format_valid?(@format)
    locales_to_download.compact.each do |locale|
      print_message "Downloading #{locale.name}..."
      fetch_translations_for_locale(locale, @format)
    end
  end
  
private
  def fetch_translations_for_locale(locale, format)
    begin
      content = api_client.download_translations_for_locale(locale.name, format)
      store_content_in_locale_file(locale, content)
    rescue Exception => e
      print_error "Failed"
      print_server_error(e.message)
    end
  end
  
  def store_content_in_locale_file(locale, content)
    directory = Phrase::Tool::Formats.directory_for_locale_in_format(locale, @format)
    filename = Phrase::Tool::Formats.filename_for_locale_in_format(locale, @format)
    path = File.join(base_directory, directory)
    target = File.join(path, filename)
    begin
      FileUtils.mkpath(path)
      File.open(target, "w") do |file|
        file.write(content)
      end
      print_message "Saved to #{clean_path target}".green
    rescue
      print_error("Cannot write file to target folder (#{path})")
      exit_command
    end
  end

  def clean_path(str)<commit_msg>added force_encoding to prevent Encoding::UndefinedConversionError<commit_after># -*- encoding : utf-8 -*-

class Phrase::Tool::Commands::Pull < Phrase::Tool::Commands::Base
  ALLOWED_DOWNLOAD_FORMATS = %w(yml po xml strings json resx ts qph ini plist properties xlf)
  DEFAULT_DOWNLOAD_FORMAT = "yml"
  
  def initialize(options, args)
    super(options, args)
    require_auth_token!
    
    @locale = @args[1]
    
    # TODO: remove DEFAULT_DOWNLOAD_FORMAT when phrase app has been updated
    @format = @options.get(:format) || config.format || DEFAULT_DOWNLOAD_FORMAT
    @target = @options.get(:target)
    @target ||= Phrase::Tool::Formats.target_directory(@format) if format_valid?(@format)
  end
  
  def execute!
    (print_error("Invalid format: #{@format}") and exit_command) unless format_valid?(@format)
    locales_to_download.compact.each do |locale|
      print_message "Downloading #{locale.name}..."
      fetch_translations_for_locale(locale, @format)
    end
  end
  
private
  def fetch_translations_for_locale(locale, format)
    begin
      content = api_client.download_translations_for_locale(locale.name, format)
      store_content_in_locale_file(locale, content)
    rescue Exception => e
      print_error "Failed"
      print_server_error(e.message)
    end
  end
  
  def store_content_in_locale_file(locale, content)
    directory = Phrase::Tool::Formats.directory_for_locale_in_format(locale, @format)
    filename = Phrase::Tool::Formats.filename_for_locale_in_format(locale, @format)
    path = File.join(base_directory, directory)
    target = File.join(path, filename)
    begin
      FileUtils.mkpath(path)
      File.open(target, "w") do |file|
        file.write(content.force_encoding("UTF-8"))
      end
      print_message "Saved to #{clean_path target}".green
    rescue
      print_error("Cannot write file to target folder (#{path})")
      exit_command
    end
  end

  def clean_path(str)
{'fraction_non_alphanumeric': 0.10576164167324388, 'fraction_numerical': 0.0013154433043935806, 'mean_word_length': 3.9960578186596583, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2341427', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1190, 'n_words': 257}