From ad1b58990f582f7d01dd4fc875237dee189a5464 Mon Sep 17 00:00:00 2001 From: Stefanie Taepke Date: Fri, 12 Oct 2018 12:55:16 +0200 Subject: [PATCH] move multilang-logic to add_triple-function --- ckanext/dcat/profiles.py | 54 +++++++++---------------- ckanext/dcat/tests/test_base_profile.py | 27 ------------- 2 files changed, 18 insertions(+), 63 deletions(-) diff --git a/ckanext/dcat/profiles.py b/ckanext/dcat/profiles.py index 0641743d..b71b613a 100644 --- a/ckanext/dcat/profiles.py +++ b/ckanext/dcat/profiles.py @@ -133,49 +133,16 @@ def _object(self, subject, predicate): return _object return None - def _object_value(self, subject, predicate, multilang=False): + def _object_value(self, subject, predicate): ''' Given a subject and a predicate, returns the value of the object - Both subject and predicate must be rdflib URIRef or BNode objects - If found, the unicode representation is returned, else an empty string ''' - default_lang = config.get('ckan.locale_default', 'en') - lang_dict = {} for o in self.g.objects(subject, predicate): - if multilang and o.language: - lang_dict[o.language] = unicode(o) - elif multilang: - lang_dict[default_lang] = unicode(o) - else: - return unicode(o) - if multilang: - # when translation does not exist, create an empty one - for lang in get_langs(): - if lang not in lang_dict: - lang_dict[lang] = '' - return lang_dict + return unicode(o) return '' - def _add_multilang_value(self, subject, predicate, dataset_key, dataset_dict): # noqa - multilang_values = dataset_dict.get(dataset_key) - if multilang_values: - try: - for key, values in multilang_values.iteritems(): - if values: - # the values can be either a multilang-dict or they are - # nested in another iterable (e.g. keywords) - if not hasattr(values, '__iter__'): - values = [values] - for value in values: - self.g.add((subject, predicate, Literal(value, lang=key))) # noqa - # if multilang_values is not iterable, it is simply added as a non- - # translated Literal - except AttributeError: - self.g.add( - (subject, predicate, Literal(multilang_values))) # noqa - def _object_value_int(self, subject, predicate): ''' Given a subject and a predicate, returns the value of the object as an @@ -520,19 +487,22 @@ def _add_list_triples_from_dict(self, _dict, subject, items): def _add_triples_from_dict(self, _dict, subject, items, list_value=False, - date_value=False): + date_value=False, + multilang=False): for item in items: key, predicate, fallbacks, _type = item self._add_triple_from_dict(_dict, subject, predicate, key, fallbacks=fallbacks, list_value=list_value, date_value=date_value, + multilang=multilang, _type=_type) def _add_triple_from_dict(self, _dict, subject, predicate, key, fallbacks=None, list_value=False, date_value=False, + multilang=False, _type=Literal, value_modifier=None): ''' @@ -566,6 +536,8 @@ def _add_triple_from_dict(self, _dict, subject, predicate, key, self._add_list_triple(subject, predicate, value, _type) elif value and date_value: self._add_date_triple(subject, predicate, value, _type) + elif value and multilang: + self._add_multilang_triple(subject, predicate, value) elif value: # Normal text value # ensure URIRef items are preprocessed (space removal/url encoding) @@ -573,6 +545,16 @@ def _add_triple_from_dict(self, _dict, subject, predicate, key, _type = CleanedURIRef self.g.add((subject, predicate, _type(value))) + def _add_multilang_triple(self, subject, predicate, multilang_values): # noqa + for key, values in multilang_values.iteritems(): + if values: + # the values can be either a multilang-dict or they are + # nested in another iterable (e.g. keywords) + if not hasattr(values, '__iter__'): + values = [values] + for value in values: + self.g.add((subject, predicate, Literal(value, lang=key))) # noqa + def _add_list_triple(self, subject, predicate, value, _type=Literal): ''' Adds as many triples to the graph as values diff --git a/ckanext/dcat/tests/test_base_profile.py b/ckanext/dcat/tests/test_base_profile.py index 8728c757..4d037a80 100644 --- a/ckanext/dcat/tests/test_base_profile.py +++ b/ckanext/dcat/tests/test_base_profile.py @@ -113,33 +113,6 @@ def test_object_value_not_found(self): eq_(value, '') - def test_object_value_multilang(self): - - p = RDFProfile(_default_graph()) - - p.g.add((URIRef('http://example.org/datasets/1'), - DCT.title, Literal('Test Datensatz 1', lang='de'))) - - p.g.add((URIRef('http://example.org/datasets/1'), - DCT.title, Literal('Test Dataset 1', lang='en'))) - - value = p._object_value(URIRef('http://example.org/datasets/1'), - DCT.title, multilang=True) - - assert isinstance(value, dict) - eq_(value.get('de'), u'Test Datensatz 1') - eq_(value.get('en'), u'Test Dataset 1') - - def test_object_value_multilang_missing_lang_param(self): - - p = RDFProfile(_default_graph()) - - value = p._object_value(URIRef('http://example.org/datasets/1'), - DCT.title, multilang=True) - - assert isinstance(value, dict) - eq_(value.get('en'), u'Test Dataset 1') - def test_object_int(self): p = RDFProfile(_default_graph())