1#!/usr/bin/python
2# Copyright (c) 2014 The Native Client Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Fake downloader which can be used in place of http_download."""
7
8import os
9import shutil
10import urllib2
11
12class FakeDownloader(object):
13  """Testing replacement for http_download that copies files."""
14
15  def __init__(self, copy_func=None):
16    self._urls = {}
17    if copy_func is None:
18      self._copy_func = shutil.copyfile
19    else:
20      self._copy_func = copy_func
21    self._download_count = 0
22
23  def StoreURL(self, url, filepath):
24    """Stores a known url into the downloader to download."""
25    self._urls[url] = filepath
26
27  def Download(self, url, target, username=None, verbose=True, password=None,
28               logger=None):
29    if url not in self._urls:
30      raise urllib2.HTTPError(url, 404,
31                              'Fake Downloader retrieved invalid URL',
32                              [], None)
33    self._copy_func(self._urls[url], target)
34    self._download_count += 1
35
36  def GetDownloadCount(self):
37    return self._download_count
38