示例#1
0
    def __init__(self,
                 authorization_key=None,
                 project_id=None,
                 clear_previous=False):
        '''
        :param str project_id:
            This is the id of the project (available in the Dashboard/Projects, next to the project
            name).

        :param str authorization_key:
            The key which is used to authorize with the backend (available in the Dashboard/User
            Settings).
        '''
        if authorization_key is None:
            try:
                authorization_key = os.environ['SPEEDTIN_AUTHORIZATION_KEY']
            except KeyError:
                raise AssertionError(
                    'The authorization key to commit the data was not passed nor is available in the SPEEDTIN_AUTHORIZATION_KEY environment variable.'
                )

        if project_id is None:
            try:
                project_id = os.environ['SPEEDTIN_PROJECT_ID']
            except KeyError:
                raise AssertionError(
                    'The project_id to commit the data was not passed nor is available in the SPEEDTIN_PROJECT_ID environment variable.'
                )

        self.authorization_key = authorization_key
        self.project_id = project_id

        self.base_url = 'https://www.speedtin.com'
        self.post = requests.post
        self.get = requests.get
        self._local_cache = LocalCache(
            os.path.join(self._data_dir(), str(project_id)))

        if clear_previous:
            self._local_cache.clear('measurement')
        else:
            # Print that we have remaining data from a previous call
            found = []
            with self._local_cache.load('measurement') as measurement_data:
                for handle in measurement_data:
                    found.append(handle.data)

            if found:
                sys.stderr.write(
                    'Warning: PySpeedTinApi:\nIn a previous call the following measurements where not properly saved:\n'
                )
                for data in found:
                    sys.stderr.write(str(data))
                    sys.stderr.write('\n')
                sys.stderr.write(
                    '\nWhen committing, those values will also be saved...\n')
                sys.stderr.write(
                    'To prevent this from happening, pass "clear_previous=True" in the \n'
                    'PySpeedTinApi constructor or manually erase the contents at:\n%s\n\n'
                    % (self._data_dir()))
示例#2
0
def test_local_cache(tmpdir):
    print(tmpdir)
    local_cache = LocalCache(str(tmpdir))
    local_cache.add('benchmark', {'name': 'bench1'})
    local_cache.add(
        'benchmark',
        {'name': 'bench1'})  # Ignore this one (don't add duplicate data)
    local_cache.add('benchmark', {'name': 'bench2'})

    data_found = []
    with local_cache.load('benchmark') as benchmark_data:
        for handle in benchmark_data:
            assert not handle.has_rest_data()
            data_found.append(handle.data)
            handle.set_rest_data('something')
            assert handle.has_rest_data()

    assert data_found == [{'name': 'bench1'}, {'name': 'bench2'}]

    data_found = []
    rest_data_found = []
    with local_cache.load('benchmark') as benchmark_data:
        for handle in benchmark_data:
            assert handle.has_rest_data()
            data_found.append(handle.data)
            rest_data_found.append(handle.rest_data)

    assert data_found == [{'name': 'bench1'}, {'name': 'bench2'}]
    assert rest_data_found == ['something', 'something']

    data_found = []
    with local_cache.load('benchmark') as benchmark_data:
        for i, handle in enumerate(benchmark_data):
            if i == 0:
                handle.remove()
            data_found.append(handle.data)
    assert data_found == [{'name': 'bench1'}, {'name': 'bench2'}]

    data_found = []
    with local_cache.load('benchmark') as benchmark_data:
        for i, handle in enumerate(benchmark_data):
            data_found.append(handle.data)
    assert data_found == [{'name': 'bench2'}]