def parse(self, data, _format=None): ''' Parses and RDF graph serialization and into the class graph It calls the rdflib parse function with the provided data and format. Data is a string with the serialized RDF graph (eg RDF/XML, N3 ... ). By default RF/XML is expected. The optional parameter _format can be used to tell rdflib otherwise. It raises a ``RDFParserException`` if there was some error during the parsing. Returns nothing. ''' _format = url_to_rdflib_format(_format) if _format == 'pretty-xml': _format = 'xml' try: self.g.parse(data=data, format=_format) # Apparently there is no single way of catching exceptions from all # rdflib parsers at once, so if you use a new one and the parsing # exceptions are not cached, add them here. # PluginException indicates that an unknown format was passed. except (SyntaxError, xml.sax.SAXParseException, rdflib.plugin.PluginException, TypeError), e: raise RDFParserException(e)
def serialize_dataset(self, dataset_dict, _format='xml'): ''' Given a CKAN dataset dict, returns an RDF serialization The serialization format can be defined using the `_format` parameter. It must be one of the ones supported by RDFLib, defaults to `xml`. Returns a string with the serialized dataset ''' log.debug('Entrando en serialize_dataset') self.graph_from_dataset(dataset_dict) _format = url_to_rdflib_format(_format) if _format == 'json-ld': output = self.g.serialize(format=_format, auto_compact=True) else: output = self.g.serialize(format=_format) return output
def serialize_catalog(self, catalog_dict=None, dataset_dicts=None, _format='xml', pagination_info=None): ''' Returns an RDF serialization of the whole catalog `catalog_dict` can contain literal values for the dcat:Catalog class like `title`, `homepage`, etc. If not provided these would get default values from the CKAN config (eg from `ckan.site_title`). If passed a list of CKAN dataset dicts, these will be also serializsed as part of the catalog. **Note:** There is no hard limit on the number of datasets at this level, this should be handled upstream. The serialization format can be defined using the `_format` parameter. It must be one of the ones supported by RDFLib, defaults to `xml`. `pagination_info` may be a dict containing keys describing the results pagination. See the `_add_pagination_triples()` method for details. Returns a string with the serialized catalog ''' log.debug('Entrando en serialize_catalog') catalog_ref = self.graph_from_catalog(catalog_dict) if dataset_dicts: for dataset_dict in dataset_dicts: dataset_ref = self.graph_from_dataset(dataset_dict) self.g.add((catalog_ref, DCAT.dataset, dataset_ref)) if pagination_info: self._add_pagination_triples(pagination_info) _format = url_to_rdflib_format(_format) output = self.g.serialize(format=_format) return output