예제 #1
0
    def test_download(self):
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            t, failures = download.save(city[0], city[1], should_archive=True)

            self.assertEqual(len(failures), 0)

            file_absolute = files.get_file_path(city_data, t)
            file_current = files.get_current_file_path(city_data)

            self.assertTrue(os.path.exists(file_absolute))
            self.assertTrue(os.path.exists(file_current))
예제 #2
0
    def test_cache(self):
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            # warm up the cache
            _, _ = download.save(city[0], city[1], should_archive=False)

            text, cache = download.get_current(city_data, max_cache_age=30)

            # check we've gotten a cached file
            self.assertGreater(cache, 0)

            info = json.loads(text)  # check the json can be parsed

            self.assertGreater(len(info), 0)  # check there is something
예제 #3
0
    def test_download(self):
        """
        Test that downloading the data results in physical file being created
        for all systems specified.
        """
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            t, failures = download.save(city[0], city[1], should_archive=True)

            self.assertEqual(len(failures), 0)

            file_absolute = files.get_file_path(city_data, t)
            file_current = files.get_current_file_path(city_data)

            self.assertTrue(os.path.exists(file_absolute))
            self.assertTrue(os.path.exists(file_current))
예제 #4
0
    def test_cache(self):
        """
        Tests that repeated requests to get data for a given system
        and city result in cached data being returned.
        """
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            # warm up the cache
            _, _ = download.save(city[0], city[1], should_archive=False)

            text, cache = download.get_current(city_data, max_cache_age=30)

            # check we've gotten a cached file
            self.assertGreater(cache, 0)

            self._assert_api_output_is_valid_json(text)
예제 #5
0
    def test_download_create_dir(self):
        # tests that script will attempt to create data directories
        # if they don't exist
        import shutil

        city_data = {'system': 'sharengo', 'name': 'milano'}
        data_dir = files.get_data_dir(city_data)

        # delete if already exists
        if os.path.exists(data_dir):
            shutil.rmtree(data_dir)

        # download
        t, failures = download.save(city_data['system'], city_data['name'], False)
        file_current = files.get_current_file_path(city_data)

        # test it was downloaded
        self.assertEqual(len(failures), 0)
        self.assertTrue(os.path.exists(file_current))
예제 #6
0
    def test_download_create_dir(self):
        """
        Tests that downloader will attempt to create data directories
        if they don't exist.
        """
        import shutil

        city_data = {'system': 'sharengo', 'name': 'milano'}
        data_dir = files.get_data_dir(city_data)

        # delete if already exists
        if os.path.exists(data_dir):
            shutil.rmtree(data_dir)

        # download
        t, failures = download.save(city_data['system'], city_data['name'], False)
        file_current = files.get_current_file_path(city_data)

        # test it was downloaded
        self.assertEqual(len(failures), 0)
        self.assertTrue(os.path.exists(file_current))
예제 #7
0
def process_commandline():
    if len(sys.argv) < 3:
        sys.exit('!!! must specify system and city to download (or system and "all")')

    requested_system = sys.argv[1].lower()
    requested_city = sys.argv[2].lower()

    if len(sys.argv) > 3:
        requested_archive = (sys.argv[3].lower() == 'archive')
    else:
        requested_archive = False

    t, failures = save(requested_system, requested_city, should_archive=requested_archive)

    end_time = datetime.datetime.utcnow()

    print('{timestamp} downloading {system} {city}, finished {end}'.format(
        timestamp=str(t), system=requested_system, city=requested_city, end=end_time))

    for failed in failures:
        message = '!!! could not download or save information for system {system} city {city}'
        print(message.format(system=failed[0], city=failed[1]))