Skip to content

Commit

Permalink
Merge pull request #16 from /issues/9-escape-rdf-uris
Browse files Browse the repository at this point in the history
Selectively escape URI characters in RDF output
  • Loading branch information
alecpm authored Mar 24, 2022
2 parents 9715b1d + a2e7b5e commit ad66b43
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pleiades/rdf/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import csv
import os
import re
import urllib
from urlparse import urlparse

import geojson
import logging
from pleiades import capgrids
from rdflib import BNode, Literal, Namespace, RDF, URIRef
from rdflib import BNode, Literal, Namespace, RDF, URIRef as URIRef_rdflib
from rdflib.graph import Graph
from shapely.geometry import asShape, box
from shapely import wkt
Expand Down Expand Up @@ -62,6 +63,24 @@

log = logging.getLogger('pleiades.rdf')


_invalid_uri_chars = '<>" {}|\\^`[]%#\t'
_uri_char_replacements = {c: urllib.quote_plus(c) for c in _invalid_uri_chars}


def URIRef(value):
"""Wrap the rdflib.URIRef() constructor to selectively escape
characters first.
"""
clean = value

for char in _uri_char_replacements:
if char in value:
clean = clean.replace(char, _uri_char_replacements[char])

return URIRef_rdflib(clean)


def geoContext(place):
note = place.getModernLocation() or ""
if not note:
Expand Down

0 comments on commit ad66b43

Please sign in to comment.