Coverage for /scratch/builds/bob/bob.med.tb/miniconda/conda-bld/bob.med.tb_1637571489937/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.8/site-packages/bob/med/tb/scripts/tb.py: 97%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3 

4"""The main entry for bob med tb (click-based) scripts.""" 

5 

6import os 

7import sys 

8import time 

9import tempfile 

10import urllib.request 

11 

12import pkg_resources 

13import click 

14from click_plugins import with_plugins 

15from tqdm import tqdm 

16 

17from bob.extension.scripts.click_helper import AliasedGroup 

18 

19import logging 

20logger = logging.getLogger(__name__) 

21 

22def download_to_tempfile(url, progress=False): 

23 """Downloads a file to a temporary named file and returns it 

24 

25 Parameters 

26 ---------- 

27 

28 url : str 

29 The URL pointing to the file to download 

30 

31 progress : :py:class:`bool`, Optional 

32 If a progress bar should be displayed for downloading the URL. 

33 

34 

35 Returns 

36 ------- 

37 

38 f : tempfile.NamedTemporaryFile 

39 A named temporary file that contains the downloaded URL 

40 

41 """ 

42 

43 file_size = 0 

44 response = urllib.request.urlopen(url) 

45 meta = response.info() 

46 if hasattr(meta, "getheaders"): 

47 content_length = meta.getheaders("Content-Length") 

48 else: 

49 content_length = meta.get_all("Content-Length") 

50 

51 if content_length is not None and len(content_length) > 0: 

52 file_size = int(content_length[0]) 

53 

54 progress &= bool(file_size) 

55 

56 f = tempfile.NamedTemporaryFile() 

57 

58 with tqdm(total=file_size, disable=not progress) as pbar: 

59 while True: 

60 buffer = response.read(8192) 

61 if len(buffer) == 0: 

62 break 

63 f.write(buffer) 

64 pbar.update(len(buffer)) 

65 

66 f.flush() 

67 f.seek(0) 

68 return f 

69 

70@with_plugins(pkg_resources.iter_entry_points("bob.med.tb.cli")) 

71@click.group(cls=AliasedGroup) 

72def tb(): 

73 """Active Tuberculosis Detection On CXR commands."""