-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetadata.py
54 lines (43 loc) · 1.75 KB
/
metadata.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
# -*- coding: utf-8 -*-
import six
from datetime import datetime as dt
from .data import DataDict
def read_time(time):
'''Read date time'''
if time == 'No':
return None
try:
return dt.strptime(time, '%Y-%m-%dT%H:%M:%S')
except ValueError:
try:
return dt.strptime(time, '%Y-%m-%dT%H:%M:%S.%f')
except ValueError:
try:
return dt.strptime(time, '%Y-%jT%H:%M:%S.%f')
except ValueError:
raise ValueError('Time `{}` does not match either `%Y-%m-%dT%H:%M:%S.%f` nor `%Y-%jT%H:%M:%S.%f`'.format(time))
class Metadata(DataDict):
def __init__(self, json):
DataDict.__init__(self)
self._json = json
for label, constraints in self._json.items():
key = label.upper().replace(' ', '_').replace('_CONSTRAINTS', '')
value = Constraints(key, constraints)
self.append(key, value)
self.opus_id = self['GENERAL']['opusid']
def __repr__(self):
return 'OPUS API Metadata Ring observation: {}\n'.format(self.opus_id) + \
'\n'.join('\n{}'.format(values) for values in self.values())
class Constraints(DataDict):
def __init__(self, name, constraints={}):
DataDict.__init__(self)
self.name = name
for key, value in constraints.items():
if 'time' in key and isinstance(value, six.text_type):
value = read_time(value)
if value is not None and value != 'N/A' and value != -999:
self.append(key, value)
def __repr__(self):
return '=> {} constraints\n' .format(self.name) + \
'\n'.join(' - {}: {}'.format(key, value)
for key, value in self.items())