§
    kÃ¿aÚ9  ã                   óX   — d Z ddlmZmZmZ ddlmZ ddlmZ dgZ	 G d„ de
¦  «        ZdS )aÜ$  
The :class:`~rdflib.resource.Resource` class wraps a
:class:`~rdflib.graph.Graph`
and a resource reference (i.e. a :class:`rdflib.term.URIRef` or
:class:`rdflib.term.BNode`) to support a resource-oriented way of
working with a graph.

It contains methods directly corresponding to those methods of the Graph
interface that relate to reading and writing data. The difference is that a
Resource also binds a resource identifier, making it possible to work without
tracking both the graph and a current subject. This makes for a "resource
oriented" style, as compared to the triple orientation of the Graph API.

Resulting generators are also wrapped so that any resource reference values
(:class:`rdflib.term.URIRef`s and :class:`rdflib.term.BNode`s) are in turn
wrapped as Resources. (Note that this behaviour differs from the corresponding
methods in :class:`~rdflib.graph.Graph`, where no such conversion takes place.)


Basic Usage Scenario
--------------------

Start by importing things we need and define some namespaces::

    >>> from rdflib import *
    >>> FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    >>> CV = Namespace("http://purl.org/captsolo/resume-rdf/0.2/cv#")

Load some RDF data::

    >>> graph = Graph().parse(format='n3', data='''
    ... @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    ... @prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
    ... @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    ... @prefix cv: <http://purl.org/captsolo/resume-rdf/0.2/cv#> .
    ...
    ... @base <http://example.org/> .
    ...
    ... </person/some1#self> a foaf:Person;
    ...     rdfs:comment "Just a Python & RDF hacker."@en;
    ...     foaf:depiction </images/person/some1.jpg>;
    ...     foaf:homepage <http://example.net/>;
    ...     foaf:name "Some Body" .
    ...
    ... </images/person/some1.jpg> a foaf:Image;
    ...     rdfs:label "some 1"@en;
    ...     rdfs:comment "Just an image"@en;
    ...     foaf:thumbnail </images/person/some1-thumb.jpg> .
    ...
    ... </images/person/some1-thumb.jpg> a foaf:Image .
    ...
    ... [] a cv:CV;
    ...     cv:aboutPerson </person/some1#self>;
    ...     cv:hasWorkHistory [ cv:employedIn </#company>;
    ...             cv:startDate "2009-09-04"^^xsd:date ] .
    ... ''')

Create a Resource::

    >>> person = Resource(
    ...     graph, URIRef("http://example.org/person/some1#self"))

Retrieve some basic facts::

    >>> person.identifier
    rdflib.term.URIRef(u'http://example.org/person/some1#self')

    >>> person.value(FOAF.name)
    rdflib.term.Literal(u'Some Body')

    >>> person.value(RDFS.comment)
    rdflib.term.Literal(u'Just a Python & RDF hacker.', lang=u'en')

Resources can be sliced (like graphs, but the subject is fixed)::

    >>> for name in person[FOAF.name]:
    ...     print(name)
    Some Body
    >>> person[FOAF.name : Literal("Some Body")]
    True

Resources as unicode are represented by their identifiers as unicode::

    >>> %(unicode)s(person)  #doctest: +SKIP
    u'Resource(http://example.org/person/some1#self'

Resource references are also Resources, so you can easily get e.g. a qname
for the type of a resource, like::

    >>> person.value(RDF.type).qname()
    u'foaf:Person'

Or for the predicates of a resource::

    >>> sorted(
    ...     p.qname() for p in person.predicates()
    ... )  #doctest: +NORMALIZE_WHITESPACE +SKIP
    [u'foaf:depiction', u'foaf:homepage',
     u'foaf:name', u'rdf:type', u'rdfs:comment']

Follow relations and get more data from their Resources as well::

    >>> for pic in person.objects(FOAF.depiction):
    ...     print(pic.identifier)
    ...     print(pic.value(RDF.type).qname())
    ...     print(pic.label())
    ...     print(pic.comment())
    ...     print(pic.value(FOAF.thumbnail).identifier)
    http://example.org/images/person/some1.jpg
    foaf:Image
    some 1
    Just an image
    http://example.org/images/person/some1-thumb.jpg

    >>> for cv in person.subjects(CV.aboutPerson):
    ...     work = list(cv.objects(CV.hasWorkHistory))[0]
    ...     print(work.value(CV.employedIn).identifier)
    ...     print(work.value(CV.startDate))
    http://example.org/#company
    2009-09-04

It's just as easy to work with the predicates of a resource::

    >>> for s, p in person.subject_predicates():
    ...     print(s.value(RDF.type).qname())
    ...     print(p.qname())
    ...     for s, o in p.subject_objects():
    ...         print(s.value(RDF.type).qname())
    ...         print(o.value(RDF.type).qname())
    cv:CV
    cv:aboutPerson
    cv:CV
    foaf:Person

This is useful for e.g. inspection::

    >>> thumb_ref = URIRef("http://example.org/images/person/some1-thumb.jpg")
    >>> thumb = Resource(graph, thumb_ref)
    >>> for p, o in thumb.predicate_objects():
    ...     print(p.qname())
    ...     print(o.qname())
    rdf:type
    foaf:Image

Similarly, adding, setting and removing data is easy::

    >>> thumb.add(RDFS.label, Literal("thumb"))
    >>> print(thumb.label())
    thumb
    >>> thumb.set(RDFS.label, Literal("thumbnail"))
    >>> print(thumb.label())
    thumbnail
    >>> thumb.remove(RDFS.label)
    >>> list(thumb.objects(RDFS.label))
    []


Schema Example
--------------

With this artificial schema data::

    >>> graph = Graph().parse(format='n3', data='''
    ... @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    ... @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    ... @prefix owl: <http://www.w3.org/2002/07/owl#> .
    ... @prefix v: <http://example.org/def/v#> .
    ...
    ... v:Artifact a owl:Class .
    ...
    ... v:Document a owl:Class;
    ...     rdfs:subClassOf v:Artifact .
    ...
    ... v:Paper a owl:Class;
    ...     rdfs:subClassOf v:Document .
    ...
    ... v:Choice owl:oneOf (v:One v:Other) .
    ...
    ... v:Stuff a rdf:Seq; rdf:_1 v:One; rdf:_2 v:Other .
    ...
    ... ''')

From this class::

    >>> artifact = Resource(graph, URIRef("http://example.org/def/v#Artifact"))

we can get at subclasses::

    >>> subclasses = list(artifact.transitive_subjects(RDFS.subClassOf))
    >>> [c.qname() for c in subclasses]
    [u'v:Artifact', u'v:Document', u'v:Paper']

and superclasses from the last subclass::

    >>> [c.qname() for c in subclasses[-1].transitive_objects(RDFS.subClassOf)]
    [u'v:Paper', u'v:Document', u'v:Artifact']

Get items from the Choice::

    >>> choice = Resource(graph, URIRef("http://example.org/def/v#Choice"))
    >>> [it.qname() for it in choice.value(OWL.oneOf).items()]
    [u'v:One', u'v:Other']

And the sequence of Stuff::

    >>> stuff = Resource(graph, URIRef("http://example.org/def/v#Stuff"))
    >>> [it.qname() for it in stuff.seq()]
    [u'v:One', u'v:Other']

On add, other resources are auto-unboxed:
    >>> paper = Resource(graph, URIRef("http://example.org/def/v#Paper"))
    >>> paper.add(RDFS.subClassOf, artifact)
    >>> artifact in paper.objects(RDFS.subClassOf) # checks Resource instance
    True
    >>> (paper._identifier, RDFS.subClassOf, artifact._identifier) in graph
    True


Technical Details
-----------------

Comparison is based on graph and identifier::

    >>> g1 = Graph()
    >>> t1 = Resource(g1, URIRef("http://example.org/thing"))
    >>> t2 = Resource(g1, URIRef("http://example.org/thing"))
    >>> t3 = Resource(g1, URIRef("http://example.org/other"))
    >>> t4 = Resource(Graph(), URIRef("http://example.org/other"))

    >>> t1 is t2
    False

    >>> t1 == t2
    True
    >>> t1 != t2
    False

    >>> t1 == t3
    False
    >>> t1 != t3
    True

    >>> t3 != t4
    True

    >>> t3 < t1 and t1 > t3
    True
    >>> t1 >= t1 and t1 >= t3
    True
    >>> t1 <= t1 and t3 <= t1
    True

    >>> t1 < t1 or t1 < t3 or t3 > t1 or t3 > t3
    False

Hash is computed from graph and identifier::

    >>> g1 = Graph()
    >>> t1 = Resource(g1, URIRef("http://example.org/thing"))

    >>> hash(t1) == hash(Resource(g1, URIRef("http://example.org/thing")))
    True

    >>> hash(t1) == hash(Resource(Graph(), t1.identifier))
    False
    >>> hash(t1) == hash(Resource(Graph(), URIRef("http://example.org/thing")))
    False

The Resource class is suitable as a base class for mapper toolkits. For
example, consider this utility for accessing RDF properties via qname-like
attributes::

    >>> class Item(Resource):
    ...
    ...     def __getattr__(self, p):
    ...         return list(self.objects(self._to_ref(*p.split('_', 1))))
    ...
    ...     def _to_ref(self, pfx, name):
    ...         return URIRef(self._graph.store.namespace(pfx) + name)

It works as follows::

    >>> graph = Graph().parse(format='n3', data='''
    ... @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    ... @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    ...
    ... @base <http://example.org/> .
    ... </person/some1#self>
    ...     foaf:name "Some Body";
    ...     foaf:depiction </images/person/some1.jpg> .
    ... </images/person/some1.jpg> rdfs:comment "Just an image"@en .
    ... ''')

    >>> person = Item(graph, URIRef("http://example.org/person/some1#self"))

    >>> print(person.foaf_name[0])
    Some Body

The mechanism for wrapping references as resources cooperates with subclasses.
Therefore, accessing referenced resources automatically creates new ``Item``
objects::

    >>> isinstance(person.foaf_depiction[0], Item)
    True

    >>> print(person.foaf_depiction[0].rdfs_comment[0])
    Just an image

é    )ÚNodeÚBNodeÚURIRef)ÚRDF)ÚPathÚResourcec                   ó6  — e Zd Zd„ Z ed„ ¦  «        Z ed„ ¦  «        Zd„ Zd„ Zd„ Z	d„ Z
d„ Zd	„ Zd
„ Zd„ Zd„ Zd)d„Zd„ Zd)d„Zd)d„Zd)d„Zd„ Zd„ Zd„ Zej        dddfd„Zd„ Zd„ Zd„ Zd)d„Zd)d„Zd„ Zd„ Z d„ Z!d „ Z"d!„ Z#d"„ Z$d#„ Z%d$„ Z&d%„ Z'd&„ Z(d'„ Z)d(„ Z*dS )*r   c                 ó"   — || _         || _        d S ©N©Ú_graphÚ_identifier)ÚselfÚgraphÚsubjects      ú1/usr/lib/python3/dist-packages/rdflib/resource.pyÚ__init__zResource.__init__?  s   € ØˆŒØ"ˆÔÐÐó    c                 ó   — | j         S r   )r   ©r   s    r   ú<lambda>zResource.<lambda>C  s   €  $¤+€ r   c                 ó   — | j         S r   ©r   r   s    r   r   zResource.<lambda>E  s	   €  tÔ'7€ r   c                 ó~   — t          t          ¦  «        t          | j        ¦  «        z  t          | j        ¦  «        z  S r   )Úhashr   r   r   r   s    r   Ú__hash__zResource.__hash__G  s-   € Ý•H‰~Œ~¥ T¤[Ñ 1Ô 1Ñ1µD¸Ô9IÑ4JÔ4JÑJÐJr   c                 ól   — t          |t          ¦  «        o| j        |j        k    o| j        |j        k    S r   )Ú
isinstancer   r   r   ©r   Úothers     r   Ú__eq__zResource.__eq__J  s8   € å�u�hÑ'Ô'ð 6Ø”˜uœ|Ò+ð6àÔ  EÔ$5Ò5ð	
r   c                 ó   — | |k     S r   © r   s     r   Ú__ne__zResource.__ne__Q  s   € Ø˜5’=Ð Ð r   c                 óP   — t          |t          ¦  «        r| j        |j        k     S dS )NF)r   r   r   r   s     r   Ú__lt__zResource.__lt__T  s*   € Ý�e�XÑ&Ô&ð 	ØÔ# eÔ&7Ò7Ð7à�5r   c                 ó   — | |k     p| |k     S r   r#   r   s     r   Ú__gt__zResource.__gt__Z  s   € Ø˜5’LÐ1 D¨E¢MÐ2Ð2r   c                 ó   — | |k     p| |k    S r   r#   r   s     r   Ú__le__zResource.__le__]  s   € Ø�eŠ|Ð,˜t uš}Ð,r   c                 ó   — | |k      S r   r#   r   s     r   Ú__ge__zResource.__ge__`  s   € Ø˜%’<ÐÐr   c                 ó*   — t          | j        ¦  «        S r   )Ústrr   r   s    r   Ú__unicode__zResource.__unicode__c  s   € Ý�4Ô#Ñ$Ô$Ð$r   c                 ó‚   — t          |t          ¦  «        r|j        }| j                             | j        ||f¦  «         d S r   )r   r   r   r   Úadd©r   ÚpÚos      r   r1   zResource.addf  ó?   € Ý�a�Ñ"Ô"ð 	Ø”ˆAàŒ�Š˜Ô)¨1¨aÐ0Ñ1Ô1Ð1Ð1Ð1r   Nc                 ó‚   — t          |t          ¦  «        r|j        }| j                             | j        ||f¦  «         d S r   )r   r   r   r   Úremover2   s      r   r7   zResource.removel  sA   € Ý�a�Ñ"Ô"ð 	Ø”ˆAàŒ×Ò˜DÔ,¨a°Ð3Ñ4Ô4Ð4Ð4Ð4r   c                 ó‚   — t          |t          ¦  «        r|j        }| j                             | j        ||f¦  «         d S r   )r   r   r   r   Úsetr2   s      r   r9   zResource.setr  r5   r   c                 óh   — |                       | j                             || j        ¦  «        ¦  «        S r   )Ú
_resourcesr   Úsubjectsr   ©r   Ú	predicates     r   r<   zResource.subjectsx  s)   € Ø�Š˜tœ{×3Ò3°I¸tÔ?OÑPÔPÑQÔQÐQr   c                 ó    — t          |t          ¦  «        r|j        }|                      | j                             | j        |¦  «        ¦  «        S r   )r   r   r   r;   r   Ú
predicates)r   r4   s     r   r@   zResource.predicates{  sB   € Ý�a�Ñ"Ô"ð 	Ø”ˆAà�Š˜tœ{×5Ò5°dÔ6FÈÑJÔJÑKÔKÐKr   c                 óh   — |                       | j                             | j        |¦  «        ¦  «        S r   )r;   r   Úobjectsr   r=   s     r   rB   zResource.objects�  s)   € Ø�Š˜tœ{×2Ò2°4Ô3CÀYÑOÔOÑPÔPÐPr   c                 óf   — |                       | j                             | j        ¦  «        ¦  «        S r   )Ú_resource_pairsr   Úsubject_predicatesr   r   s    r   rE   zResource.subject_predicates„  s)   € Ø×#Ò# D¤K×$BÒ$BÀ4ÔCSÑ$TÔ$TÑUÔUÐUr   c                 óf   — |                       | j                             | j        ¦  «        ¦  «        S r   )rD   r   Úsubject_objectsr   r   s    r   rG   zResource.subject_objects‡  s)   € Ø×#Ò# D¤K×$?Ò$?ÀÔ@PÑ$QÔ$QÑRÔRÐRr   c                 óf   — |                       | j                             | j        ¦  «        ¦  «        S r   )rD   r   Úpredicate_objectsr   r   s    r   rI   zResource.predicate_objectsŠ  s)   € Ø×#Ò# D¤K×$AÒ$AÀ$ÔBRÑ$SÔ$SÑTÔTÐTr   Tc           	      ó¦   — t          |t          ¦  «        r|j        }|                      | j                             | j        ||||¦  «        ¦  «        S r   )r   r   r   Ú_castr   Úvalue)r   r3   r4   ÚdefaultÚanys        r   rL   zResource.value�  sH   € Ý�a�Ñ"Ô"ð 	Ø”ˆAà�zŠz˜$œ+×+Ò+¨DÔ,<¸aÀÀGÈSÑQÔQÑRÔRÐRr   c                 ó@   — | j                              | j        ¦  «        S r   )r   Úlabelr   r   s    r   rP   zResource.label“  ó   € ØŒ{× Ò  Ô!1Ñ2Ô2Ð2r   c                 ó@   — | j                              | j        ¦  «        S r   )r   Úcommentr   r   s    r   rS   zResource.comment–  s   € ØŒ{×"Ò" 4Ô#3Ñ4Ô4Ð4r   c                 óf   — |                       | j                             | j        ¦  «        ¦  «        S r   )r;   r   Úitemsr   r   s    r   rU   zResource.items™  s'   € Ø�Š˜tœ{×0Ò0°Ô1AÑBÔBÑCÔCÐCr   c                 ój   — |                       | j                             | j        ||¦  «        ¦  «        S r   )r;   r   Útransitive_objectsr   ©r   r>   Úremembers      r   rW   zResource.transitive_objectsœ  s2   € Ø�ŠØŒK×*Ò*¨4Ô+;¸YÈÑQÔQñ
ô 
ð 	
r   c                 ój   — |                       | j                             || j        |¦  «        ¦  «        S r   )r;   r   Útransitive_subjectsr   rX   s      r   r[   zResource.transitive_subjects¡  s2   € Ø�ŠØŒK×+Ò+¨I°tÔ7GÈÑRÔRñ
ô 
ð 	
r   c                 óf   — |                       | j                             | j        ¦  «        ¦  «        S r   )r;   r   Úseqr   r   s    r   r]   zResource.seq¦  s%   € Ø�Š˜tœ{Ÿš¨tÔ/?Ñ@Ô@ÑAÔAÐAr   c                 ó@   — | j                              | j        ¦  «        S r   )r   Úqnamer   r   s    r   r_   zResource.qname©  rQ   r   c              #   ór   K  — |D ]1\  }}|                       |¦  «        |                       |¦  «        fV — Œ2d S r   ©rK   )r   ÚpairsÚs1Ús2s       r   rD   zResource._resource_pairs¬  sK   è è € Øð 	1ð 	1‰FˆB�Ø—*’*˜R‘.”. $§*¢*¨R¡.¤.Ð0Ð0Ð0Ð0Ð0ð	1ð 	1r   c              #   óœ   K  — |D ]F\  }}}|                       |¦  «        |                       |¦  «        |                       |¦  «        fV — ŒGd S r   ra   )r   ÚtriplesÚsr3   r4   s        r   Ú_resource_tripleszResource._resource_triples°  sY   è è € Øð 	>ð 	>‰GˆAˆq�!Ø—*’*˜Q‘-”- §¢¨A¡¤°·
²
¸1±´Ð=Ð=Ð=Ð=Ð=ð	>ð 	>r   c              #   óB   K  — |D ]}|                       |¦  «        V — Œd S r   ra   )r   ÚnodesÚnodes      r   r;   zResource._resources´  s:   è è € Øð 	#ð 	#ˆDØ—*’*˜TÑ"Ô"Ð"Ð"Ð"Ð"ð	#ð 	#r   c                 óh   — t          |t          t          f¦  «        r|                      |¦  «        S |S r   )r   r   r   Ú_new)r   rk   s     r   rK   zResource._cast¸  s-   € Ý�d�U¥F˜OÑ,Ô,ð 	Ø—9’9˜T‘?”?Ð"àˆKr   c                 ól   — |                       | j                             | j        d d f¦  «        ¦  «        S r   )rh   r   rf   Ú
identifierr   s    r   Ú__iter__zResource.__iter__¾  s6   € Ø×%Ò%ØŒK×Ò ¤°$¸Ð =Ñ>Ô>ñ
ô 
ð 	
r   c                 ó:  — t          |t          ¦  «        r³|j        rt          d¦  «        ‚|j        |j        }}t          |t          ¦  «        r|j        }t          |t          ¦  «        r|j        }|€|€|                      ¦   «         S |€|  	                    |¦  «        S |€|  
                    |¦  «        S | j        ||f| j        v S t          |t          t          f¦  «        r|  
                    |¦  «        S t          d|›dt          |¦  «        ›d�¦  «        ‚)NzSResources fix the subject for slicing, and can only be sliced by predicate/object. zTYou can only index a resource by a single rdflib term, a slice of rdflib terms, not z (ú))r   ÚsliceÚstepÚ	TypeErrorÚstartÚstopr   r   rI   r@   rB   ro   r   r   r   Útype)r   Úitemr3   r4   s       r   Ú__getitem__zResource.__getitem__Ã  s%  € Ý�d�EÑ"Ô"ð 	ØŒyð ÝØiñô ð ð ”:˜tœyˆqˆAÝ˜!�XÑ&Ô&ð "Ø”M�Ý˜!�XÑ&Ô&ð "Ø”M�Øˆy˜Q˜YØ×-Ò-Ñ/Ô/Ð/Ø�Ø—’ qÑ)Ô)Ð)Ø�Ø—|’| A‘”Ð&àœ¨¨AÐ.°$´+Ð=Ð=Ý˜�t¥T˜lÑ+Ô+ð 	Ø—<’< Ñ%Ô%Ð%å�)à�4�4�˜d™œ˜˜ð%ñô ð r   c                 ó2   — |                       ||¦  «         d S r   )r9   )r   ry   rL   s      r   Ú__setitem__zResource.__setitem__Þ  s   € Ø�Š��uÑÔÐÐÐr   c                 ó>   —  t          | ¦  «        | j        |¦  «        S r   )rx   r   )r   r   s     r   rm   zResource._newá  s   € Ø�t�D‰zŒz˜$œ+ wÑ/Ô/Ð/r   c                 ó   — d| j         z  S )NzResource(%s)r   r   s    r   Ú__str__zResource.__str__ä  s   € Ø Ô 0Ñ0Ð0r   c                 ó(   — d| j         ›d| j        ›d�S )Nz	Resource(ú,rr   r   r   s    r   Ú__repr__zResource.__repr__ç  s   € € Ø$(¤K K K°Ô1AÐ1AÐ1AÐBÐBr   r   )+Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   ro   r   r!   r$   r&   r(   r*   r,   r/   r1   r7   r9   r<   r@   rB   rE   rG   rI   r   rL   rP   rS   rU   rW   r[   r]   r_   rD   rh   r;   rK   rp   rz   r|   rm   r   r‚   r#   r   r   r   r   >  s¯  € € € € € ð#ð #ð #ð ˆHÐ-Ð-Ñ.Ô.€Eà�Ð7Ð7Ñ8Ô8€JðKð Kð Kð
ð 
ð 
ð!ð !ð !ðð ð ð3ð 3ð 3ð-ð -ð -ð ð  ð  ð%ð %ð %ð2ð 2ð 2ð5ð 5ð 5ð 5ð2ð 2ð 2ðRð Rð Rð RðLð Lð Lð LðQð Qð Qð QðVð Vð VðSð Sð SðUð Uð Uð ”i 4°¸4ð Sð Sð Sð Sð3ð 3ð 3ð5ð 5ð 5ðDð Dð Dð
ð 
ð 
ð 
ð

ð 
ð 
ð 
ð
Bð Bð Bð3ð 3ð 3ð1ð 1ð 1ð>ð >ð >ð#ð #ð #ðð ð ð
ð 
ð 
ð
ð ð ð6ð ð ð0ð 0ð 0ð1ð 1ð 1ðCð Cð Cð Cð Cr   N)Ú__doc__Úrdflib.termr   r   r   Úrdflib.namespacer   Úrdflib.pathsr   Ú__all__Úobjectr   r#   r   r   ú<module>r�      s¥   ððtð tðl	 ,Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð àˆ,€ðjCð jCð jCð jCð jCˆvñ jCô jCð jCð jCð jCr   