-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfields.py
40 lines (31 loc) · 1.13 KB
/
fields.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
# -*- coding: utf-8 -*-
import re
from .data import DataDict
def splitKey(key):
'''Split from first uppercase word (ie., `body`)'''
body = re.findall('[^a-z]+', key)[0]
field = key.split(body)[1]
return body, field
class Field(object):
def __init__(self, field, json):
if len(json) == 0:
raise KeyError('Unknown field `{}`'.format(field))
self._json = json
self.field = field
self.label = json['label']
self.category = json['category']
self.slug = json['slug']
self.old_slug = json['old_slug']
def __repr__(self):
return '{} ({}):\n => Category {} / Slug: {}'.format(self.label, self.field, self.category, self.slug)
def __str__(self):
return self.label
class Fields(DataDict):
def __init__(self, json):
DataDict.__init__(self)
self._json = json
for key, value in json.items():
self.append(key.lower(), Field(key, value))
def __repr__(self):
return 'OPUS API list of all fields available ({}):\n'.format(len(self)) + \
'\n'.join(' - {}'.format(key) for key in self)