Ejemplo n.º 1
0
    def test_urllib_lib_bar(self):
        """urllib lib"""
        # progress using python-clint lib
        print 'PROGRESS BAR from clint lib and URLLIB2'
        _chunk = 16 * 1024

        req = urllib2.urlopen(URL)
        meta = req.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print "Downloading: %s Bytes: %s" % (URL, file_size)
        total_remote_length = int(req.headers.get('content-length'))
        self.assertEqual(
            req.code, 200,
            "Request failed: {0}\n{1}".format(req.code, req.headers))
        pbar_label = 'test.qcow'
        with open(PATH_SAVE, 'wb') as fread:
            with progressbar(length=total_remote_length,
                             fill_char=style('#', fg='green'),
                             empty_char=' ',
                             label=pbar_label) as barr:

                while True:
                    chunk = req.read(_chunk)
                    if not chunk:
                        break
                    fread.write(chunk)
                    barr.update(len(chunk))
Ejemplo n.º 2
0
    def download(link, file_dst, label):
        """Create HTTP request to download a file"""
        # delimit labels to be at same size and a better visualization
        # when printing the progres bar
        fill_out = 20 - len(label)
        extra = " " * fill_out
        pbar_label = label + extra
        LOG.debug("Starting download %s" % link)
        try:
            req = urlopen(link)
            remote_file_size = int(req.headers.get('content-length'))
            CHUNK = 16 * 1024
            with open(file_dst, 'wb') as fp:
                with progressbar(length=remote_file_size,
                                 fill_char=style('#', fg='green'),
                                 empty_char=' ',
                                 label=pbar_label,
                                 show_percent=True) as bar:
                    while True:
                        chunk = req.read(CHUNK)
                        if not chunk:
                            break
                        fp.write(chunk)
                        bar.update(len(chunk))

            LOG.debug("Download complete, file %s saved locally" % file_dst)
        except (HTTPError, RequestException, Exception) as ex:
            raise ex
Ejemplo n.º 3
0
 def mycli():
     """Sample cmd with progress bar"""
     click.echo('Start')
     with progressbar(range(10), label='xyz') as it:
         for _ in it:
             pass
     click.echo('End')
Ejemplo n.º 4
0
 def mycli():
     """Sample cmd with progress bar"""
     click.echo('Start')
     with progressbar(range(10), label='xyz') as it:
         for _ in it:
             pass
     click.echo('End')
Ejemplo n.º 5
0
def progbar(n=2**10, sleep_time=.009):
    """This is a graphics tool for coolness that display a progress bar that iterates does @n iterations, each of which
    taking @sleep_time seconds."""
    i = 0
    with progressbar(range(n), fill_char="=", empty_char=".") as bar:
        for item in bar:
            i += item
            time.sleep(sleep_time)
    print("Result:", i)
Ejemplo n.º 6
0
 def test_click_lib_bar(self):
     """click lib"""
     resp = get(URL, stream=True)
     self.assertEqual(
         resp.status_code, 200,
         "Request failed: {0}\n{1}".format(resp.status_code, resp.content))
     total_length = int(resp.headers.get('content-length'))
     # progress using python-click lib
     print 'PROGRESS BAR from click lib'
     with open(PATH_SAVE, 'wb+') as fread:
         expected_size = (total_length / 1024) + 1
         with progressbar(resp.iter_content(1024),
                          length=expected_size,
                          label='test.qcow') as chunks:
             for chunk in chunks:
                 fread.write(chunk)
                 fread.flush()
Ejemplo n.º 7
0
                          ],
                        )
def test_secure_dburl(input, expected_result):
    assert secure_dburl(input) == expected_result

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# IMPORTANT=======================================================================================
# THE FOLLOWING TESTS INVOLVING PROGRESSBARS PRINTOUT    
# WILL FAIL IN PYDEV 5.2.0 and PYTHON 3.6.2 (typical bytes vs string error)
# RUN FROM TERMINAL
# IMPORTANT=======================================================================================
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@pytest.mark.skip(reason="fails if run from within n eclipse because of cryptic bytes vs string propblem")
@patch("stream2segment.utils.Nop", side_effect=lambda *a, **v: Nop(*a, **v))
@patch("stream2segment.utils.click_progressbar", side_effect=lambda *a, **v: progressbar(*a, **v))
def test_progressbar(mock_pbar, mock_nop):
    '''this test has problems with eclipse'''
    N = 5
    with get_progressbar(False) as bar:  # no-op
        for i in range(N):
            bar.update(i)
    assert mock_nop.call_count == 1
    assert mock_pbar.call_count == 0

    with get_progressbar(False, length=0) as bar:  # no-op
        for i in range(N):
            bar.update(i)
    assert mock_nop.call_count == 2
    assert mock_pbar.call_count == 0