Skip to content

Commit 2496aec

Browse files
committed
Create downloadable class
1 parent 974fa7b commit 2496aec

4 files changed

Lines changed: 87 additions & 98 deletions

File tree

opus/images.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3-
from .wget import download
3+
from .data import DataDict
4+
from .wget import Downloadable
45

5-
class Image(object):
6+
7+
class Image(Downloadable):
68
def __init__(self, ring_obs_id, path, img):
9+
Downloadable.__init__(self, path + img)
710
self.ring_obs_id = ring_obs_id
811
self.path = path
912
self.img = img
@@ -14,18 +17,12 @@ def __repr__(self):
1417
def __str__(self):
1518
return self.ring_obs_id
1619

17-
@property
18-
def url(self):
19-
return self.path + self.img
20-
21-
def download(self, out=None):
22-
download(self.url, out)
2320

24-
class Images(object):
21+
class Images(DataDict):
2522
def __init__(self, json, size):
23+
DataDict.__init__(self)
2624
self._json = json
2725
self.size = size
28-
self._data = {}
2926
for img in json['data']:
3027
el = Image(img['ring_obs_id'], img['path'], img['img'])
3128
self._data[str(el)] = el
@@ -34,25 +31,6 @@ def __repr__(self):
3431
return 'OPUS API Images object (with {} {} images):\n'.format(len(self), self.size) + \
3532
'\n'.join(' - {}'.format(key) for key, _ in self.items())
3633

37-
def __len__(self):
38-
return len(self._data)
39-
40-
def __getitem__(self, attr):
41-
return self._data[attr]
42-
43-
def __iter__(self):
44-
return iter(self._data)
45-
46-
def keys(self):
47-
return self._data.keys()
48-
49-
def items(self):
50-
return self._data.items()
51-
52-
def values(self):
53-
return self._data.values()
54-
55-
5634
@property
5735
def order(self):
5836
return self._json['order']

opus/wget.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
import requests
44
import shutil
55

6-
def download(url, out=None):
7-
outdir = None
8-
if out and os.path.isdir(out):
9-
outdir = out
10-
out = None
116

12-
if out is None:
13-
filename = url.split('/')[-1]
14-
else:
15-
filename = out
7+
class Downloadable(object):
8+
def __init__(self, url):
9+
self.url = url
1610

17-
if outdir:
18-
filename = os.path.join(outdir, filename)
11+
def download(self, out=None):
12+
outdir = None
13+
if out and os.path.isdir(out):
14+
outdir = out
15+
out = None
1916

20-
r = requests.get(url, stream=True)
21-
with open(filename, 'wb') as f:
22-
shutil.copyfileobj(r.raw, f)
17+
if out is None:
18+
filename = self.url.split('/')[-1]
19+
else:
20+
filename = out
2321

24-
return filename
22+
if outdir:
23+
filename = os.path.join(outdir, filename)
24+
25+
r = requests.get(self.url, stream=True)
26+
with open(filename, 'wb') as f:
27+
shutil.copyfileobj(r.raw, f)
28+
29+
return filename

tests/test_images.py

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,67 +21,21 @@ def image(size):
2121
json = JSON.loads(open('tests/api/image/'+size+'/'+ring_obs_id+'.json', 'r').read())
2222
return Image(ring_obs_id, json['path'], json['data'][0]['img'])
2323

24-
def test_images_meta(images, size):
25-
r = repr(images)
26-
assert 'OPUS API Images object' in r
27-
assert 'S_IMG_CO_ISS_1508094647_N' in r
28-
assert len(images) == 10
29-
assert images.order == 'obs_general.time1'
30-
assert images.size == size
31-
assert isinstance(images['S_IMG_CO_ISS_1508094647_N'], Image)
3224

33-
def test_images_image(image):
25+
def test_image(image):
3426
assert repr(image) == 'OPUS API Image object: S_IMG_CO_ISS_1459551972_N'
3527
assert str(image) == 'S_IMG_CO_ISS_1459551972_N'
3628
assert image.ring_obs_id == 'S_IMG_CO_ISS_1459551972_N'
3729
assert image.path == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/'
3830
assert image.img == 'COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
3931
assert image.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
4032

41-
def test_images_iter(images):
42-
for key in images:
43-
assert key in images.keys()
44-
break
45-
for value in images.values():
46-
assert isinstance(value, Image)
47-
break
48-
49-
50-
@responses.activate
51-
def test_image_download(image):
52-
fname = os.path.basename(image.url)
53-
54-
with open('tests/api/image/med/'+fname, 'rb') as img:
55-
responses.add(responses.GET, image.url,
56-
body=img.read(), status=200,
57-
content_type='image/jpeg',
58-
stream=True
59-
)
60-
61-
image.download(out='tests/')
62-
63-
assert len(responses.calls) == 1
64-
assert responses.calls[0].request.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
6533

66-
assert os.path.isfile('tests/'+fname)
67-
os.remove('tests/'+fname)
68-
69-
70-
@responses.activate
71-
def test_image_download_output(image):
72-
fname = os.path.basename(image.url)
73-
74-
with open('tests/api/image/med/'+fname, 'rb') as img:
75-
responses.add(responses.GET, image.url,
76-
body=img.read(), status=200,
77-
content_type='image/jpeg',
78-
stream=True
79-
)
80-
81-
image.download(out='tests/test.jpg')
82-
83-
assert len(responses.calls) == 1
84-
assert responses.calls[0].request.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
34+
def test_images_meta(images, size):
35+
r = repr(images)
36+
assert 'OPUS API Images object' in r
37+
assert 'S_IMG_CO_ISS_1508094647_N' in r
38+
assert len(images) == 10
39+
assert images.order == 'obs_general.time1'
40+
assert images.size == size
8541

86-
assert os.path.isfile('tests/test.jpg')
87-
os.remove('tests/test.jpg')

tests/test_wget.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
import os
4+
import responses
5+
6+
from opus.wget import Downloadable
7+
8+
9+
@pytest.fixture
10+
def url():
11+
return 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
12+
13+
@pytest.fixture
14+
def downloadable(url):
15+
return Downloadable(url)
16+
17+
@responses.activate
18+
def test_image_download(downloadable):
19+
fname = 'N1459551972_1_med.jpg'
20+
with open('tests/api/image/med/'+fname, 'rb') as img:
21+
responses.add(responses.GET, downloadable.url,
22+
body=img.read(), status=200,
23+
content_type='image/jpeg',
24+
stream=True
25+
)
26+
27+
downloadable.download(out='tests/')
28+
29+
assert len(responses.calls) == 1
30+
assert responses.calls[0].request.url == downloadable.url
31+
32+
assert os.path.isfile('tests/'+fname)
33+
os.remove('tests/'+fname)
34+
35+
36+
@responses.activate
37+
def test_image_download_output(downloadable):
38+
fname = 'N1459551972_1_med.jpg'
39+
with open('tests/api/image/med/'+fname, 'rb') as img:
40+
responses.add(responses.GET, downloadable.url,
41+
body=img.read(), status=200,
42+
content_type='image/jpeg',
43+
stream=True
44+
)
45+
46+
downloadable.download(out='tests/test.jpg')
47+
48+
assert len(responses.calls) == 1
49+
assert responses.calls[0].request.url == downloadable.url
50+
51+
assert os.path.isfile('tests/test.jpg')
52+
os.remove('tests/test.jpg')

0 commit comments

Comments
 (0)