1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
9 _logger = logging.getLogger('arvados.vocabulary')
11 def load_vocabulary(api_client=None):
12 """Load the Arvados vocabulary from the API.
14 if api_client is None:
15 api_client = api('v1')
16 return Vocabulary(api_client.vocabulary())
18 class Vocabulary(object):
19 def __init__(self, voc_definition={}):
20 self.strict_keys = voc_definition.get('strict_tags', False)
23 for key_id, val in voc_definition.get('tags', {}).items():
24 strict = val.get('strict', False)
25 key_labels = [l['label'] for l in val.get('labels', [])]
27 for v_id, v_val in val.get('values', {}).items():
28 labels = [l['label'] for l in v_val.get('labels', [])]
29 values[v_id] = VocabularyValue(v_id, labels)
30 vk = VocabularyKey(key_id, key_labels, values, strict)
31 self.key_aliases[key_id.lower()] = vk
32 for alias in vk.aliases:
33 self.key_aliases[alias.lower()] = vk
35 def __getitem__(self, key):
36 return self.key_aliases[key.lower()]
38 def convert_to_identifiers(self, obj={}):
39 """Translate key/value pairs to machine readable identifiers.
41 return self._convert_to_what(obj, 'identifier')
43 def convert_to_labels(self, obj={}):
44 """Translate key/value pairs to human readable labels.
46 return self._convert_to_what(obj, 'preferred_label')
48 def _convert_to_what(self, obj={}, what=None):
49 if not isinstance(obj, dict):
50 raise ValueError("obj must be a dict")
51 if what not in ['preferred_label', 'identifier']:
52 raise ValueError("what attr must be 'preferred_label' or 'identifier'")
54 for k, v in obj.items():
57 k_what = getattr(self[k], what)
58 if isinstance(v, list):
62 v_what.append(getattr(self[k][x], what))
65 raise ValueError("value '%s' not found for key '%s'" % (x, k))
69 v_what = getattr(self[k][v], what)
72 raise ValueError("value '%s' not found for key '%s'" % (v, k))
75 raise KeyError("key '%s' not found" % k)
79 class VocabularyData(object):
80 def __init__(self, identifier, aliases=[]):
81 self.identifier = identifier
82 self.aliases = aliases
84 def __getattribute__(self, name):
85 if name == 'preferred_label':
86 return self.aliases[0]
87 return super(VocabularyData, self).__getattribute__(name)
89 class VocabularyValue(VocabularyData):
90 def __init__(self, identifier, aliases=[]):
91 super(VocabularyValue, self).__init__(identifier, aliases)
93 class VocabularyKey(VocabularyData):
94 def __init__(self, identifier, aliases=[], values={}, strict=False):
95 super(VocabularyKey, self).__init__(identifier, aliases)
97 self.value_aliases = {}
98 for v_id, v_val in values.items():
99 self.value_aliases[v_id.lower()] = v_val
100 for v_alias in v_val.aliases:
101 self.value_aliases[v_alias.lower()] = v_val
103 def __getitem__(self, key):
104 return self.value_aliases[key.lower()]