def download_file(url, dest=None, chunk_size=1024, replace="ask",
                  label="Downloading {dest_basename} ({size:.2f}MB)",
                  expected_extension=None):
    """Download a file from a given URL and display progress.

    :param dest: If the destination exists and is a directory, the filename
        will be guessed from the Content-Disposition header. If the destination
        is an existing file, the user will either be prompted to overwrite, or
        the file will be replaced (depending on the value of **replace**). If
        the destination does not exist, it will be used as the filename.
    :param int chunk_size: bytes read in at a time.
    :param replace: If `False`, an existing destination file will not be
        overwritten.
    :param label: a string which is formatted and displayed as the progress bar
        label. Variables provided include *dest_basename*, *dest*, and *size*.
    :param expected_extension: if set, the filename will be sanitized to ensure
        it has the given extension. The extension should not start with a dot
        (`.`).
    """
    dest = Path(dest or url.split("/")[-1])
    response = get(url, stream=True)
    if (dest.exists()
            and dest.is_dir()
            and "Content-Disposition" in response.headers):
        content_disposition = rfc6266.parse_requests_response(response)
        if expected_extension is not None:
            filename = content_disposition.filename_sanitized(
                expected_extension)
        filename = secure_filename(filename)
        dest = dest / filename
    if dest.exists() and not dest.is_dir():
        if (replace is False
                or replace == "ask"
                and not click.confirm("Replace {}?".format(dest))):
            return str(dest)
    size = int(response.headers.get("content-length", 0))
    label = label.format(dest=dest, dest_basename=dest.name,
                         size=size/1024.0/1024)
    with click.open_file(str(dest), "wb") as f:
        content_iter = response.iter_content(chunk_size=chunk_size)
        with click.progressbar(content_iter, length=size/1024,
                               label=label) as bar:
            for chunk in bar:
                if chunk:
                    f.write(chunk)
                    f.flush()
    return str(dest)
def draw(self, context):
        import sys
        layout = self.layout

        userpref = context.user_preferences
        system = userpref.system

        split = layout.split()

        # 1. Column
        column = split.column()
        colsplit = column.split(percentage=0.85)

        col = colsplit.column()
        col.label(text="General:")
        col.prop(system, "dpi")
        col.label("Virtual Pixel Mode:")
        col.prop(system, "virtual_pixel_mode", text="")

        col.separator()

        col.prop(system, "frame_server_port")
        col.prop(system, "scrollback", text="Console Scrollback")

        col.separator()

        col.label(text="Sound:")
        col.row().prop(system, "audio_device", expand=False)
        sub = col.column()
        sub.active = system.audio_device != 'NONE' and system.audio_device != 'Null'
        #sub.prop(system, "use_preview_images")
        sub.prop(system, "audio_channels", text="Channels")
        sub.prop(system, "audio_mixing_buffer", text="Mixing Buffer")
        sub.prop(system, "audio_sample_rate", text="Sample Rate")
        sub.prop(system, "audio_sample_format", text="Sample Format")

        col.separator()

        col.label(text="Screencast:")
        col.prop(system, "screencast_fps")
        col.prop(system, "screencast_wait_time")

        col.separator()

        if userpref.addons.find('cycles') != -1:
            userpref.addons['cycles'].preferences.draw_impl(col, context)

        if hasattr(system, "opensubdiv_compute_type"):
            col.label(text="OpenSubdiv compute:")
            col.row().prop(system, "opensubdiv_compute_type", text="")

        # 2. Column
        column = split.column()
        colsplit = column.split(percentage=0.85)

        col = colsplit.column()
        col.label(text="OpenGL:")
        col.prop(system, "gl_clip_alpha", slider=True)
        col.prop(system, "use_mipmaps")
        col.prop(system, "use_gpu_mipmap")
        col.prop(system, "use_16bit_textures")

        col.separator()
        col.label(text="Selection")
        col.prop(system, "select_method", text="")

        col.separator()

        col.label(text="Anisotropic Filtering")
        col.prop(system, "anisotropic_filter", text="")

        col.separator()

        col.label(text="Window Draw Method:")
        col.prop(system, "window_draw_method", text="")
        col.prop(system, "multi_sample", text="")
        if sys.platform == "linux" and system.multi_sample != 'NONE':
            col.label(text="Might fail for Mesh editing selection!")
            col.separator()
        col.prop(system, "use_region_overlap")

        col.separator()

        col.label(text="Text Draw Options:")
        col.prop(system, "use_text_antialiasing")

        col.separator()

        col.label(text="Textures:")
        col.prop(system, "gl_texture_limit", text="Limit Size")
        col.prop(system, "texture_time_out", text="Time Out")
        col.prop(system, "texture_collection_rate", text="Collection Rate")

        col.separator()

        col.label(text="Images Draw Method:")
        col.prop(system, "image_draw_method", text="")

        col.separator()

        col.label(text="Sequencer/Clip Editor:")
        # currently disabled in the code
        # col.prop(system, "prefetch_frames")
        col.prop(system, "memory_cache_limit")

        # 3. Column
        column = split.column()

        column.label(text="Solid OpenGL lights:")

        split = column.split(percentage=0.1)
        split.label()
        split.label(text="Colors:")
        split.label(text="Direction:")

        lamp = system.solid_lights[0]
        opengl_lamp_buttons(column, lamp)

        lamp = system.solid_lights[1]
        opengl_lamp_buttons(column, lamp)

        lamp = system.solid_lights[2]
        opengl_lamp_buttons(column, lamp)

        column.separator()

        column.label(text="Color Picker Type:")
        column.row().prop(system, "color_picker_type", text="")

        column.separator()

        column.prop(system, "use_weight_color_range", text="Custom Weight Paint Range")
        sub = column.column()
        sub.active = system.use_weight_color_range
        sub.template_color_ramp(system, "weight_color_range", expand=True)

        column.separator()
        column.prop(system, "font_path_ui")
        column.prop(system, "font_path_ui_mono")

        if bpy.app.build_options.international:
            column.prop(system, "use_international_fonts")
            if system.use_international_fonts:
                column.prop(system, "language")
                row = column.row()
                row.label(text="Translate:", text_ctxt=i18n_contexts.id_windowmanager)
                row = column.row(align=True)
                row.prop(system, "use_translate_interface", text="Interface", toggle=True)
                row.prop(system, "use_translate_tooltips", text="Tooltips", toggle=True)
                row.prop(system, "use_translate_new_dataname", text="New Data", toggle=True)