コード例 #1
0
ファイル: test_downloaders.py プロジェクト: ozayn/cartopy
def test_natural_earth_downloader():
    # downloads a file to a temporary location, and uses that temporary
    # location, then:
    #   * Checks that the file is only downloaded once even when calling
    #     multiple times
    #   * Checks that shapefiles have all the necessary files when downloaded
    #   * Checks that providing a path in a download item gets used rather
    #     than triggering another download

    tmp_dir = tempfile.mkdtemp()

    shp_path_template = os.path.join(tmp_dir,
                                     '{category}_{resolution}_{name}.shp')

    # picking a small-ish file to speed up download times, the file itself
    # isn't important - it is the download mechanism that is.
    format_dict = {
        'category': 'physical',
        'name': 'rivers_lake_centerlines',
        'resolution': '110m'
    }

    try:
        dnld_item = NEShpDownloader(target_path_template=shp_path_template)

        # check that the file gets downloaded the first time path is called
        with CallCounter(dnld_item, 'acquire_resource') as counter:
            shp_path = dnld_item.path(format_dict)
        assert counter.count == 1, 'Item not downloaded.'

        assert_equal(shp_path_template.format(**format_dict), shp_path)

        # check that calling path again doesn't try re-downloading
        with CallCounter(dnld_item, 'acquire_resource') as counter:
            assert_equal(dnld_item.path(format_dict), shp_path)
        assert counter.count == 0, 'Item was re-downloaded.'

        # check that we have the shp and the shx
        exts = ['.shp', '.shx']
        for ext in exts:
            stem = os.path.splitext(shp_path)[0]
            msg = "Shapefile's {0} file doesn't exist in {1}{0}".format(
                ext, stem)
            assert os.path.exists(stem + ext), msg

        # check that providing a pre downloaded path actually works
        pre_dnld = NEShpDownloader(target_path_template='/not/a/real/file.txt',
                                   pre_downloaded_path_template=shp_path)
        # check that the pre_dnld downloader doesn't re-download, but instead
        # uses the path of the previously downloaded item

        with CallCounter(pre_dnld, 'acquire_resource') as counter:
            assert_equal(pre_dnld.path(format_dict), shp_path)
        assert counter.count == 0, 'Aquire resource called more than once.'

    finally:
        shutil.rmtree(tmp_dir)
コード例 #2
0
def test_transform_PlateCarree_shortcut():
    src = ccrs.PlateCarree(central_longitude=0)
    target = ccrs.PlateCarree(central_longitude=180)

    # of the 3 paths, 2 of them cannot be short-cutted.
    pth1 = mpath.Path([[0.5, 0], [10, 10]])
    pth2 = mpath.Path([[0.5, 91], [10, 10]])
    pth3 = mpath.Path([[-0.5, 0], [10, 10]])

    trans = InterProjectionTransform(src, target)

    counter = CallCounter(target, 'project_geometry')

    with counter:
        trans.transform_path(pth1)
        # pth1 should allow a short-cut.
        assert counter.count == 0

    with counter:
        trans.transform_path(pth2)
        assert counter.count == 1

    with counter:
        trans.transform_path(pth3)
        assert counter.count == 2
コード例 #3
0
ファイル: test_downloaders.py プロジェクト: ozayn/cartopy
def test_downloading_simple_ascii():
    # downloads a file from the Google APIs. (very high uptime and file will
    # always be there - if this goes down, most of the internet would break!)
    # to test the downloading mechanisms.
    file_url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/{name}.js'

    format_dict = {'name': 'jquery'}

    with download_to_temp() as tmp_dir:
        target_template = os.path.join(tmp_dir, '{name}.txt')
        tmp_fname = target_template.format(**format_dict)

        dnld_item = cio.Downloader(file_url, target_template)

        assert_equal(dnld_item.target_path(format_dict), tmp_fname)

        with warnings.catch_warnings(record=True) as w:
            assert_equal(dnld_item.path(format_dict), tmp_fname)

            assert len(w) == 1, ('Expected a single download warning to be '
                                 'raised. Got {}.'.format(len(w)))
            assert issubclass(w[0].category, cio.DownloadWarning)

        with open(tmp_fname, 'r') as fh:
            _ = fh.readline()
            assert_equal(" * jQuery JavaScript Library v1.8.2\n",
                         fh.readline())

        # check that calling path again doesn't try re-downloading
        with CallCounter(dnld_item, 'acquire_resource') as counter:
            assert_equal(dnld_item.path(format_dict), tmp_fname)
        assert counter.count == 0, 'Item was re-downloaded.'