-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
83 lines (67 loc) · 2.66 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
from .data import DataDict
from .wget import Downloadable
class File(DataDict):
'''Files for a single observation'''
def __init__(self, opus_id, data):
DataDict.__init__(self)
self.opus_id = opus_id
for label, files in data.items():
if 'Browse Image' in label:
value = Preview(files)
else:
value = FileList(files)
key = label.replace('Browse Image ', '')\
.replace(' (calibrated unavailable)', '')\
.upper()\
.replace(' ', '_')\
.replace('(', '')\
.replace(')', '')
self.append(key, value)
def __repr__(self):
return 'OPUS API Files for observation: {}\n'.format(self.opus_id) + \
'\n'.join('\n=> {}\n{}'.format(key, value) for key, value in self.items())
def __str__(self):
return self.opus_id
class Preview(DataDict):
'''Previews for a single observation'''
def __init__(self, previews=[]):
DataDict.__init__(self)
for preview in previews:
key = preview.split('_')[-1].split('.')[0]
self.append(key, Downloadable(preview))
def __repr__(self):
return '\n'.join(' - {}: {}'.format(key, value) for key, value in self.items())
class FileList(DataDict):
'''Files list for a single observation'''
def __init__(self, files=[]):
DataDict.__init__(self)
for f in files:
if f.lower().endswith('.lbl'):
key = 'LBL'
elif f.endswith('.IMG'):
key = 'IMG'
elif f.endswith('.qub'):
key = 'qub'
elif f.endswith('.tab'):
key = 'TAB'
elif f.endswith(('.jpg', '.jpeg', '.jpeg_small')):
key = 'JPG'
elif f.endswith('.png'):
key = 'PNG'
elif f.lower().endswith('.fmt'):
key = f.lower().split('/')[-1].replace('.fmt', '')
else:
raise ValueError('Unknown file format `{}`'.format(f.lower().split('/')[-1]))
self.append(key, Downloadable(f))
def __repr__(self):
return '\n'.join(' - {}: {}'.format(key, value) for key, value in self.items())
class Files(DataDict):
def __init__(self, json):
DataDict.__init__(self)
self._json = json
for key, value in json['data'].items():
self.append(key, File(key, value))
def __repr__(self):
return 'OPUS API File objects (with {} files):\n'.format(len(self)) + \
'\n'.join(' - {}'.format(key) for key in self.keys())