Skip to content

Commit 63c464c

Browse files
committed
Add DataDict and Downloadable class to Files
1 parent 2496aec commit 63c464c

6 files changed

Lines changed: 81 additions & 86 deletions

File tree

opus/data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def values(self):
2828
def append(self, key, value):
2929
self._data[key] = value
3030

31+
def load(self, dict):
32+
self._data = dict
33+
3134
class Data(DataDict):
3235
def __init__(self, json):
3336
DataDict.__init__(self)

opus/files.py

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

3-
class File(object):
3+
from .data import DataDict
4+
from .wget import Downloadable
5+
6+
7+
class File(DataDict):
48
'''Files for a single observation'''
59
def __init__(self, ring_obs_id, data):
10+
DataDict.__init__(self)
611
self.ring_obs_id = ring_obs_id
7-
self._data = data
812

9-
for label, files in self._data.items():
13+
for label, files in data.items():
1014
if label == 'preview_image':
11-
key = 'previews'
15+
key = 'PREVIEWS'
1216
value = Preview(files)
1317
else:
14-
key = label.lower()
18+
key = label.upper()
1519
value = FileList(files)
1620

17-
setattr(self, key, value)
21+
self.append(key, value)
1822

1923
def __repr__(self):
20-
return 'OPUS API Files for observation: {}'.format(self.ring_obs_id)
24+
return 'OPUS API Files for observation: {}\n'.format(self.ring_obs_id) + \
25+
'\n'.join('\n=> {}\n{}'.format(key, value) for key, value in self.items())
2126

2227
def __str__(self):
2328
return self.ring_obs_id
2429

25-
class Preview(object):
30+
31+
class Preview(DataDict):
2632
'''Previews for a single observation'''
2733
def __init__(self, previews=[]):
34+
DataDict.__init__(self)
2835
for preview in previews:
29-
key = preview.split('_')[-1].replace('.jpg', '').replace('.png','')
30-
setattr(self, key, preview)
36+
key = preview.split('_')[-1].split('.')[0]
37+
self.append(key, Downloadable(preview))
3138

3239
def __repr__(self):
33-
return '\n'.join(
34-
['{} -> {}'.format(key.title(), value)
35-
for key, value in self.__dict__.items()]
36-
)
40+
return '\n'.join(' - {}: {}'.format(key, value) for key, value in self.items())
3741

38-
class FileList(object):
42+
class FileList(DataDict):
3943
'''Files list for a single observation'''
4044
def __init__(self, files=[]):
45+
DataDict.__init__(self)
4146
for f in files:
4247
if f.endswith('.LBL'):
4348
key = 'LBL'
@@ -52,47 +57,19 @@ def __init__(self, files=[]):
5257
else:
5358
raise ValueError('Unknown file format `{}`'.format(f.lower().split('/')[-1]))
5459

55-
setattr(self, key, f)
60+
self.append(key, Downloadable(f))
5661

5762
def __repr__(self):
58-
return '\n'.join(
59-
['{} -> {}'.format(key, value)
60-
for key, value in self.__dict__.items()]
61-
)
63+
return '\n'.join(' - {}: {}'.format(key, value) for key, value in self.items())
6264

6365

64-
class Files(object):
66+
class Files(DataDict):
6567
def __init__(self, json):
66-
self.json = json
67-
self.imgs = list(self.json['data'].keys())
68-
self.index = 0
68+
DataDict.__init__(self)
69+
self._json = json
70+
for key, value in json['data'].items():
71+
self.append(key, File(key, value))
6972

7073
def __repr__(self):
71-
return 'OPUS API Files object (with {} files)'.format(self.count)
72-
73-
def __len__(self):
74-
return self.count
75-
76-
def __getitem__(self, index):
77-
ring_obs_id = self.imgs[index]
78-
data = self.json['data'][ring_obs_id]
79-
return File(ring_obs_id, data)
80-
81-
def __iter__(self):
82-
return self
83-
84-
def __next__(self):
85-
try:
86-
result = self.__getitem__(self.index)
87-
except IndexError:
88-
self.index = 0
89-
raise StopIteration
90-
self.index += 1
91-
return result
92-
93-
def next(self):
94-
return self.__next__()
95-
96-
@property
97-
def count(self):
98-
return len(self.json['data'])
74+
return 'OPUS API Files object (with {} files):\n'.format(len(self)) + \
75+
'\n'.join(' - {}'.format(key) for key in self.keys())

opus/wget.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class Downloadable(object):
88
def __init__(self, url):
99
self.url = url
1010

11+
def __repr__(self):
12+
return 'OPUS API downloadable file:\n -> {}'.format(str(self))
13+
14+
def __str__(self):
15+
return self.url
16+
1117
def download(self, out=None):
1218
outdir = None
1319
if out and os.path.isdir(out):

tests/test_data.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def el(data):
2424

2525

2626
def test_data_dict(data_dict):
27-
data_dict.append('abc', 'foo')
27+
data_dict.load({'abc': 'foo', 'def': 'bar'})
2828
assert repr(data_dict) == '<OPUS API generic class for data objects>'
29-
assert len(data_dict) == 1
29+
assert len(data_dict) == 2
3030
assert data_dict['abc'] == 'foo'
31+
assert data_dict['def'] == 'bar'
3132

3233

3334
def test_data_dict_iter(data_dict):

tests/test_files.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,49 @@ def file_vims(files):
2222
return File('S_CUBE_CO_VIMS_1558621524_VIS', json['data']['S_CUBE_CO_VIMS_1558621524_VIS'])
2323

2424

25-
def test_files_meta(files):
26-
assert repr(files) == 'OPUS API Files object (with 10 files)'
27-
assert len(files) == 10
28-
29-
def test_files_iter(files):
30-
files.next() == files[0]
31-
for ii, img in enumerate(files):
32-
img = files[ii]
33-
34-
def test_file_meta(file_iss):
35-
assert repr(file_iss) == 'OPUS API Files for observation: S_IMG_CO_ISS_1459551972_N'
36-
assert str(file_iss) == 'S_IMG_CO_ISS_1459551972_N'
37-
assert file_iss.ring_obs_id == 'S_IMG_CO_ISS_1459551972_N'
25+
def test_file_meta(file_vims):
26+
assert 'OPUS API Files for observation: S_CUBE_CO_VIMS_1558621524_VIS' in repr(file_vims)
27+
assert str(file_vims) == 'S_CUBE_CO_VIMS_1558621524_VIS'
28+
assert file_vims.ring_obs_id == 'S_CUBE_CO_VIMS_1558621524_VIS'
3829

3930
def test_file_previews(file_iss):
40-
previews = file_iss.previews
41-
assert isinstance(repr(previews), str)
42-
assert previews.full == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_full.jpg'
43-
assert previews.med == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
44-
assert previews.small == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_small.jpg'
45-
assert previews.thumb == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_thumb.jpg'
31+
previews = file_iss['PREVIEWS']
32+
assert str(previews['full']) == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_full.jpg'
33+
assert str(previews['med']) == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
34+
assert str(previews['small']) == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_small.jpg'
35+
assert str(previews['thumb']) == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_thumb.jpg'
4636

4737

4838
def test_file_list_iss(file_iss):
49-
raw_image = file_iss.raw_image
50-
assert raw_image.LBL == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.LBL'
51-
assert raw_image.IMG == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.IMG'
52-
assert raw_image.tlmtab == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/LABEL/TLMTAB.FMT'
53-
assert raw_image.prefix2 == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/LABEL/PREFIX2.FMT'
39+
raw_image = file_iss['RAW_IMAGE']
40+
assert str(raw_image['LBL']) == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.LBL'
41+
assert str(raw_image['IMG']) == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.IMG'
42+
assert str(raw_image['tlmtab']) == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/LABEL/TLMTAB.FMT'
43+
assert str(raw_image['prefix2']) == 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/LABEL/PREFIX2.FMT'
5444

55-
calibrated = file_iss.calibrated
56-
assert calibrated.LBL == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.LBL'
57-
assert calibrated.IMG == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.IMG'
58-
assert calibrated.tlmtab == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/LABEL/TLMTAB.FMT'
59-
assert calibrated.prefix2 == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/LABEL/PREFIX2.FMT'
45+
calibrated = file_iss['CALIBRATED']
46+
assert str(calibrated['LBL']) == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.LBL'
47+
assert str(calibrated['IMG']) == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.IMG'
48+
assert str(calibrated['tlmtab']) == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/LABEL/TLMTAB.FMT'
49+
assert str(calibrated['prefix2']) == 'https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/LABEL/PREFIX2.FMT'
6050

6151

6252
def test_file_list_vims(file_vims):
63-
assert isinstance(repr(file_vims.raw_spectral_image_cube), str)
53+
raw_spectral_image_cube = file_vims['RAW_SPECTRAL_IMAGE_CUBE']
54+
assert str(raw_spectral_image_cube['qub']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/data/2007137T054828_2007143T180509/v1558621524_1.qub'
55+
assert str(raw_spectral_image_cube['QUB']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/data/2007137T054828_2007143T180509/v1558621524_1.QUB'
56+
assert str(raw_spectral_image_cube['LBL']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/data/2007137T054828_2007143T180509/v1558621524_1.LBL'
57+
assert str(raw_spectral_image_cube['suffix_description']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/LABEL/suffix_description.fmt'
58+
assert str(raw_spectral_image_cube['core_description']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/LABEL/core_description.fmt'
59+
assert str(raw_spectral_image_cube['band_bin_center']) == 'https://pds-rings.seti.org/holdings/volumes/COVIMS_0xxx/COVIMS_0020/LABEL/band_bin_center.fmt'
60+
61+
62+
def test_files_meta(files):
63+
r = repr(files)
64+
assert 'OPUS API Files object (with 10 files)' in r
65+
assert 'S_CUBE_CO_VIMS_1558621524_VIS' in r
66+
assert len(files) == 10
67+
6468

6569
def test_file_list_err():
6670
with pytest.raises(ValueError):

tests/test_wget.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ def url():
1414
def downloadable(url):
1515
return Downloadable(url)
1616

17+
18+
def test_downloadable(downloadable):
19+
assert downloadable.url in repr(downloadable)
20+
1721
@responses.activate
18-
def test_image_download(downloadable):
22+
def test_download(downloadable):
1923
fname = 'N1459551972_1_med.jpg'
2024
with open('tests/api/image/med/'+fname, 'rb') as img:
2125
responses.add(responses.GET, downloadable.url,
@@ -34,7 +38,7 @@ def test_image_download(downloadable):
3438

3539

3640
@responses.activate
37-
def test_image_download_output(downloadable):
41+
def test_download_output(downloadable):
3842
fname = 'N1459551972_1_med.jpg'
3943
with open('tests/api/image/med/'+fname, 'rb') as img:
4044
responses.add(responses.GET, downloadable.url,

0 commit comments

Comments
 (0)