def test_get_namespace(): key, namespace = ns.get_namespace('http://sometest.ns/ns#') assert key == 'TEST' assert namespace == Namespace('http://sometest.ns/ns#') key1, namespace1 = ns.get_namespace('http://sometest1.ns/ns#') assert key1 == 'NS1' assert namespace1 == Namespace('http://sometest1.ns/ns#')
def test_02_get_namespce(self): key, namespace = ns.get_namespace('http://sometest.ns/ns#') self.assertEqual(key, 'TEST') self.assertEqual(namespace, Namespace('http://sometest.ns/ns#')) key1, namespace1 = ns.get_namespace('http://sometest1.ns/ns#') self.assertEqual(key1, 'NS1') self.assertEqual(namespace1, Namespace('http://sometest1.ns/ns#'))
def get_namespace(base): """ Return the `namespace` short hand notation and the URI based on the URI `base`. The namespace is a `rdf.namespace.Namespace` .. code-block:: python >>> key, namespace = ns.get_namespace('http://sometest.ns/ns#') >>> print key, namespace TEST, http://sometest.ns/ns# """ global __anonymous_count ns_dict = sys.modules[__name__].__dict__ if not type(base) in [str, unicode]: base = str(base) try: prefix = __INVERTED__[base] uri = ns_dict[prefix] except KeyError: prefix = '%s%d' % (__anonymous, __anonymous_count + 1) __anonymous_count += 1 uri = Namespace(base) register(**{prefix: uri}) return prefix, uri
def resource(cls, session, resources_namespace, concept, id): # Make sure resources_namespace is an instance of Namespace if not isinstance(resources_namespace, Namespace): resources_namespace = Namespace(resources_namespace) Concept = session.get_class(concept) return Concept(resources_namespace[id])
def __init__(self, resources_namespace, concept_class): assert issubclass(concept_class, Resource) and inspect.isclass(concept_class) self._Resource = concept_class # Make sure resources_namespace is an instance of Namespace if not isinstance(resources_namespace, Namespace): resources_namespace = Namespace(resources_namespace) self._namespace = resources_namespace
def __init__(self, resources_namespace, concept_class): '''the `resource` is the :class:`surf.resource.Resource` class for which the REST interface is exposed, the `resources_namespace` represents the URI that instances will be using as subjects''' self.__concept_class = concept_class # Make sure resources_namespace is an instance of Namespace if not isinstance(resources_namespace, Namespace): resources_namespace = Namespace(resources_namespace) self.__namespace = resources_namespace
def uuid_subject(namespace=None): '''the function generates a unique subject in the provided `namespace` based on the :meth:`uuid.uuid4()` method, If `namespace` is not specified than the default `SURF` namespace is used .. code-block:: python >>> print util.uuid_subject(ns.SIOC) http://rdfs.org/sioc/ns#1b6ca1d5-41ed-4768-b86a-42185169faff ''' if not namespace: namespace = get_fallback_namespace() if not isinstance(namespace, Namespace): namespace = Namespace(namespace) return namespace[str(uuid4())]
def register_fallback(namespace): """ Register a fallback namespace to use when creating resource without specifying subject. .. code-block:: python >>> ns.register_fallback('http://example.com/fallback#') >>> Person = session.get_class(ns.FOAF.Person) >>> p = Person() >>> p.subject http://example.com/fallback#093d460a-a768-49a9-8813-aa5b321d94a8 """ if not isinstance(namespace, Namespace): namespace = Namespace(namespace) global __fallback_namespace __fallback_namespace = namespace
def resource(cls, session, resources_namespace, concept_class, id): """ convenience method to get a resource given the arguments :param session: the *SuRF* session :type session: :class:`surf.session.Session` :param resources_namespace: the `uri` used by instances :type resources_namespace: :class:`rdflib.namespace.Namespace` or str :param concept_class: the class for which the REST interface is exposed :type concept_class: :class:`surf.resource.Resource` :param str id: the resources id :return: the resource :rtype: :class:`surf.resources.Resource` """ # Make sure resources_namespace is an instance of Namespace if not isinstance(resources_namespace, Namespace): resources_namespace = Namespace(resources_namespace) _Resource = session.get_class(concept_class) return _Resource(resources_namespace[id])
def uuid_subject(namespace=None): """ This function generates a unique subject in the provided `namespace` based on the :func:`uuid.uuid4()` method, If `namespace` is not specified than the default `SURF` namespace is used .. code-block:: python >>> from surf import namespace as ns >>> print uuid_subject(ns.SIOC) http://rdfs.org/sioc/ns#1b6ca1d5-41ed-4768-b86a-42185169faff :param namespace: the given namespace :type namespace: None or :class:`rdflib.namespace.Namespace` or str or unicode :return: the *RDF* subject identifier in the specified namespace :rtype: :class:`rdflib.term.URIRef` """ if not namespace: namespace = get_fallback_namespace() if not isinstance(namespace, Namespace): namespace = Namespace(namespace) return namespace[str(uuid4())]
def register(**namespaces): """ Register a namespace with a shorthand notation with the `namespace` manager. The arguments are passed in as key-value pairs. .. code-block:: python >>> ns.register(test='http://sometest.ns/ns#') >>> print ns.TEST http://sometest.ns/ns# """ ns_dict = sys.modules[__name__].__dict__ for key in namespaces: uri = namespaces[key] prefix = key.upper() if not type(uri) in [Namespace, ClosedNamespace]: uri = Namespace(uri) ns_dict[prefix] = uri # Also keep inverted dict up-to-date. __add_inverted(prefix) __add_direct(prefix)
def test_register(): ns.register(test='http://sometest.ns/ns#') assert ns.TEST == Namespace('http://sometest.ns/ns#')
def test_get_prefix(): name = ns.get_prefix(Namespace('http://sometest.ns/ns#')) assert name == 'TEST' name1 = ns.get_prefix('http://sometest1.ns/ns#') assert name1 == 'NS1'
def test_get_namespace_url(): url = ns.get_namespace_url('TEST') assert url == Namespace('http://sometest.ns/ns#') url1 = ns.get_namespace_url('NS1') assert url1 == Namespace('http://sometest1.ns/ns#')
def test_04_get_prefix(self): name = ns.get_prefix(Namespace('http://sometest.ns/ns#')) self.assertEqual(name, 'TEST') name1 = ns.get_prefix('http://sometest1.ns/ns#') self.assertEqual(name1, 'NS1')
def test_03_get_namespace_url(self): url = ns.get_namespace_url('TEST') self.assertEqual(url, Namespace('http://sometest.ns/ns#')) url1 = ns.get_namespace_url('NS1') self.assertEqual(url1, Namespace('http://sometest1.ns/ns#'))
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. # -*- coding: utf-8 -*- __author__ = ['Cosmin Basca', 'Peteris Caune'] from copy import deepcopy import sys from surf.rdf import ClosedNamespace, Namespace, RDF, RDFS __anonymous = 'NS' __anonymous_count = 0 ANNOTATION = Namespace('http://www.w3.org/2000/10/annotation-ns#') ANNOTEA = Namespace('http://www.w3.org/2002/01/bookmark#') ATOM = Namespace('http://atomowl.org/ontologies/atomrdf#') BIBO = Namespace('http://purl.org/ontology/bibo/') BIBO_DEGREES = Namespace('http://purl.org/ontology/bibo/degrees/') BIBO_EVENTS = Namespace('http://purl.org/ontology/bibo/events/') BIBO_ROLES = Namespace('http://purl.org/ontology/bibo/roles/') BIBO_STATUS = Namespace('http://purl.org/ontology/bibo/status/') CALENDAR = Namespace('http://www.w3.org/2002/12/cal/icaltzd#') CONTACT = Namespace('http://www.w3.org/2000/10/swap/pim/contact#') CORRIB_TAX = Namespace('http://jonto.corrib.org/taxonomies#') DBLP = Namespace('http://www4.wiwiss.fu-berlin.de/dblp/terms.rdf#') DBPEDIA = Namespace('http://dbpedia.org/property/') DC = Namespace('http://purl.org/dc/elements/1.1/') DCTERMS = Namespace('http://purl.org/dc/terms/') DOAP = Namespace('http://usefulinc.com/ns/doap#')
def test_01_register(self): ns.register(test='http://sometest.ns/ns#') self.assertEqual(ns.TEST, Namespace('http://sometest.ns/ns#'))