Skip to content

Commit

Permalink
move multilang-logic to add_triple-function
Browse files Browse the repository at this point in the history
  • Loading branch information
stefina committed Oct 12, 2018
1 parent 530c6d3 commit aa0a109
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions ckanext/dcat/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,49 +104,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
Expand Down Expand Up @@ -502,6 +469,7 @@ def _add_triple_from_dict(self, _dict, subject, predicate, key,
fallbacks=None,
list_value=False,
date_value=False,
multilang=False,
_type=Literal):
'''
Adds a new triple to the graph with the provided parameters
Expand All @@ -527,10 +495,22 @@ 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
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
Expand Down

0 comments on commit aa0a109

Please # to comment.