Downloads translated versions of the files

def download_files(fapi, file_name, conf, use_cache, cache_dir=None):
    """ Downloads translated versions of the files
    """
    retrieval_type              = conf.get('retrieval-type', 'published')
    include_original_strings    = 'true' if conf.get('include-original-strings', False) else 'false'
    save_pattern                = conf.get('save-pattern')

    if not save_pattern:
        raise SmarterlingError("File %s doesn't have a save-pattern" % file_name)

    if cache_dir and not os.path.exists(cache_dir):
        print("Creating cache dir: %s" % (cache_dir, ))
        os.makedirs(cache_dir)

    uri = file_uri(file_name, conf)
    translated_items = get_translated_items(fapi, uri, use_cache, cache_dir=cache_dir)

    for item in translated_items:
        item                = AttributeDict(item)
        locale              = item.locale
        locale_underscore   = locale.replace("-", "_")
        locale_android_res  = locale.replace("-", "-r")
        locale_parts        = locale.split("-")
        language            = locale_parts[0]
        region              = locale_parts[1] if len(locale_parts)>1 else ""

        file_response = get_translated_file(
            fapi, file_uri(file_name, conf), locale, retrieval_type,
            include_original_strings, use_cache, cache_dir=cache_dir)

        if not file_response:
            print("%s translation not found for %s" % (item.locale, file_name))
            continue
        print("Processing %s translation for %s" % (item.locale, file_name))

        (fd, work_file) = tempfile.mkstemp()
        try:
            with open(work_file, 'w') as f:
                f.write(file_response)
        finally:
            os.close(fd)

        for filter_cmd in conf.get('filters', []):
            (fd, tmp_file) = tempfile.mkstemp()
            try :
                filter_cmd = filter_cmd.replace("{input_file}", work_file)
                filter_cmd = filter_cmd.replace("{output_file}", tmp_file)
                filter_cmd = filter_cmd.replace("{locale}", locale)
                filter_cmd = filter_cmd.replace("{locale_underscore}", locale_underscore)
                filter_cmd = filter_cmd.replace("{locale_android_res}", locale_android_res)
                filter_cmd = filter_cmd.replace("{language}", language)
                filter_cmd = filter_cmd.replace("{region}", region)
                print(" running filter: %s " % filter_cmd)
                if os.system(filter_cmd) != 0:
                    raise SmarterlingError("Non 0 exit code from filter: %s" % filter_cmd)
                shutil.move(tmp_file, work_file)
            finally:
                os.close(fd)

        if conf.has_key('save-cmd'):
            save_command = conf.get('save-cmd')
            save_command = save_command.replace("{input_file}", work_file)
            save_command = save_command.replace("{locale}", locale)
            save_command = save_command.replace("{locale_underscore}", locale_underscore)
            save_command = save_command.replace("{locale_android_res}", locale_android_res)
            save_command = save_command.replace("{language}", language)
            save_command = save_command.replace("{region}", region)
            print(" running save command: %s " % save_command)
            if os.system(save_command) != 0:
                raise SmarterlingError("Non 0 exit code from save command: %s" % save_command)

        elif conf.has_key('save-pattern'):
            save_file = conf.get('save-pattern')
            save_file = save_file.replace("{locale}", locale)
            save_file = save_file.replace("{locale_underscore}", locale_underscore)
            save_file = save_file.replace("{locale_android_res}", locale_android_res)
            save_file = save_file.replace("{language}", language)
            save_file = save_file.replace("{region}", region)
            save_dir = os.path.dirname(save_file)
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            elif not os.path.isdir(save_dir):
                raise SmarterlingError("Expected %s to be a directory, but it's an existing file" % save_dir)
            print(" saving output to: %s " % save_file)
            shutil.move(work_file, save_file)

        else:
            raise SmarterlingError("no save-cmd or save-pattern for: %s" % file_name)