3
‰d%  ã               @   s    d dl mZ d dl mZ d dl mZ dZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d d	lmZ d d
lmZ d dlmZ G dd„ deƒZdd„ Zdd„ ZdS )é    )Úabsolute_import)Údivision)Úprint_functionaß  
A Describer is a stateful utility for creating RDF statements in a
semi-declarative manner. It has methods for creating literal values, rel and
rev resource relations (somewhat resembling RDFa).

The `rel` and ``rev`` methods return a context manager which sets the current
about to the referenced resource for the context scope (for use with the
``with`` statement).

Full example in the ``to_rdf`` method below::

    >>> import datetime
    >>> from rdflib.graph import Graph
    >>> from rdflib.namespace import Namespace, RDFS, FOAF
    >>>
    >>> ORG_URI = "http://example.org/"
    >>>
    >>> CV = Namespace("http://purl.org/captsolo/resume-rdf/0.2/cv#")
    >>>
    >>> class Person(object):
    ...     def __init__(self):
    ...         self.first_name = u"Some"
    ...         self.last_name = u"Body"
    ...         self.username = "some1"
    ...         self.presentation = u"Just a Python & RDF hacker."
    ...         self.image = "/images/persons/" + self.username + ".jpg"
    ...         self.site = "http://example.net/"
    ...         self.start_date = datetime.date(2009, 9, 4)
    ...     def get_full_name(self):
    ...         return u" ".join([self.first_name, self.last_name])
    ...     def get_absolute_url(self):
    ...         return "/persons/" + self.username
    ...     def get_thumbnail_url(self):
    ...         return self.image.replace('.jpg', '-thumb.jpg')
    ...
    ...     def to_rdf(self):
    ...         graph = Graph()
    ...         graph.bind('foaf', FOAF)
    ...         graph.bind('cv', CV)
    ...         lang = 'en'
    ...         d = Describer(graph, base=ORG_URI)
    ...         d.about(self.get_absolute_url()+'#person')
    ...         d.rdftype(FOAF.Person)
    ...         d.value(FOAF.name, self.get_full_name())
    ...         d.value(FOAF.givenName, self.first_name)
    ...         d.value(FOAF.familyName, self.last_name)
    ...         d.rel(FOAF.homepage, self.site)
    ...         d.value(RDFS.comment, self.presentation, lang=lang)
    ...         with d.rel(FOAF.depiction, self.image):
    ...             d.rdftype(FOAF.Image)
    ...             d.rel(FOAF.thumbnail, self.get_thumbnail_url())
    ...         with d.rev(CV.aboutPerson):
    ...             d.rdftype(CV.CV)
    ...             with d.rel(CV.hasWorkHistory):
    ...                 d.value(CV.startDate, self.start_date)
    ...                 d.rel(CV.employedIn, ORG_URI+"#company")
    ...         return graph
    ...
    >>> person_graph = Person().to_rdf()
    >>> expected = Graph().parse(data='''<?xml version="1.0" encoding="utf-8"?>
    ... <rdf:RDF
    ...   xmlns:foaf="http://xmlns.com/foaf/0.1/"
    ...   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    ...   xmlns:cv="http://purl.org/captsolo/resume-rdf/0.2/cv#"
    ...   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    ...   <foaf:Person rdf:about="http://example.org/persons/some1#person">
    ...     <foaf:name>Some Body</foaf:name>
    ...     <foaf:givenName>Some</foaf:givenName>
    ...     <foaf:familyName>Body</foaf:familyName>
    ...     <foaf:depiction>
    ...       <foaf:Image
    ...         rdf:about=
    ...             "http://example.org/images/persons/some1.jpg">
    ...         <foaf:thumbnail
    ...         rdf:resource=
    ...             "http://example.org/images/persons/some1-thumb.jpg"/>
    ...       </foaf:Image>
    ...     </foaf:depiction>
    ...     <rdfs:comment xml:lang="en">
    ...             Just a Python &amp; RDF hacker.
    ...     </rdfs:comment>
    ...     <foaf:homepage rdf:resource="http://example.net/"/>
    ...   </foaf:Person>
    ...   <cv:CV>
    ...     <cv:aboutPerson
    ...         rdf:resource="http://example.org/persons/some1#person">
    ...     </cv:aboutPerson>
    ...     <cv:hasWorkHistory>
    ...       <rdf:Description>
    ...         <cv:startDate
    ...             rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
    ...             >2009-09-04</cv:startDate>
    ...         <cv:employedIn rdf:resource="http://example.org/#company"/>
    ...       </rdf:Description>
    ...     </cv:hasWorkHistory>
    ...   </cv:CV>
    ... </rdf:RDF>
    ... ''')
    >>>
    >>> from rdflib.compare import isomorphic
    >>> isomorphic(person_graph, expected)  #doctest: +SKIP
    True
)Úcontextmanager)ÚGraph)ÚRDF)ÚBNode)Ú
Identifier)ÚLiteral)ÚURIRefc               @   sV   e Zd Zddd„Zdd„ Zdd„ Zddd	„Zdd
d„Zdd„ Zdd„ Z	e
dd„ ƒZdS )Ú	DescriberNc             C   s2   |d krt ƒ }|| _|| _g | _| j|p*d ƒ d S )N)r   ÚgraphÚbaseÚ	_subjectsÚabout)Úselfr   r   r   © r   ú9/tmp/pip-build-7vycvbft/rdflib/rdflib/extras/describer.pyÚ__init__{   s    zDescriber.__init__c             K   s<   |j d| jƒ t|f|Ž}| jr,|| jd< n| jj|ƒ dS )a  
        Sets the current subject. Will convert the given object into an
        ``URIRef`` if it's not an ``Identifier``.

        Usage::

            >>> d = Describer()
            >>> d._current() #doctest: +ELLIPSIS
            rdflib.term.BNode(...)
            >>> d.about("http://example.org/")
            >>> d._current()
            rdflib.term.URIRef(u'http://example.org/')

        r   é   Néÿÿÿÿ)Ú
setdefaultr   Úcast_identifierr   Úappend)r   ÚsubjectÚkwsr   r   r   r   ƒ   s
    zDescriber.aboutc             K   s&   t |f|Ž}| jj| jƒ ||fƒ dS )aÛ  
        Set a literal value for the given property. Will cast the value to an
        ``Literal`` if a plain literal is given.

        Usage::

            >>> from rdflib import URIRef
            >>> from rdflib.namespace import RDF, RDFS
            >>> d = Describer(about="http://example.org/")
            >>> d.value(RDFS.label, "Example")
            >>> d.graph.value(URIRef('http://example.org/'), RDFS.label)
            rdflib.term.Literal(u'Example')

        N)Ú
cast_valuer   ÚaddÚ_current)r   ÚpÚvr   r   r   r   Úvalue™   s    zDescriber.valuec             K   sB   |j d| jƒ t|ƒ}t|f|Ž}| jj| jƒ ||fƒ | j|ƒS )a   Set an object for the given property. Will convert the given object
        into an ``URIRef`` if it's not an ``Identifier``. If none is given, a
        new ``BNode`` is used.

        Returns a context manager for use in a ``with`` block, within which the
        given object is used as current subject.

        Usage::

            >>> from rdflib import URIRef
            >>> from rdflib.namespace import RDF, RDFS
            >>> d = Describer(about="/", base="http://example.org/")
            >>> _ctxt = d.rel(RDFS.seeAlso, "/about")
            >>> d.graph.value(URIRef('http://example.org/'), RDFS.seeAlso)
            rdflib.term.URIRef(u'http://example.org/about')

            >>> with d.rel(RDFS.seeAlso, "/more"):
            ...     d.value(RDFS.label, "More")
            >>> (URIRef('http://example.org/'), RDFS.seeAlso,
            ...         URIRef('http://example.org/more')) in d.graph
            True
            >>> d.graph.value(URIRef('http://example.org/more'), RDFS.label)
            rdflib.term.Literal(u'More')

        r   )r   r   r   r   r   r   Ú_subject_stack)r   r   Úor   r   r   r   Úrel«   s
    zDescriber.relc             K   sB   |j d| jƒ t|ƒ}t|f|Ž}| jj||| jƒ fƒ | j|ƒS )aÖ  
        Same as ``rel``, but uses current subject as *object* of the relation.
        The given resource is still used as subject in the returned context
        manager.

        Usage::

            >>> from rdflib import URIRef
            >>> from rdflib.namespace import RDF, RDFS
            >>> d = Describer(about="http://example.org/")
            >>> with d.rev(RDFS.seeAlso, "http://example.net/"):
            ...     d.value(RDFS.label, "Net")
            >>> (URIRef('http://example.net/'), RDFS.seeAlso,
            ...         URIRef('http://example.org/')) in d.graph
            True
            >>> d.graph.value(URIRef('http://example.net/'), RDFS.label)
            rdflib.term.Literal(u'Net')

        r   )r   r   r   r   r   r   r"   )r   r   Úsr   r   r   r   ÚrevÌ   s
    zDescriber.revc             C   s   | j j| jƒ tj|fƒ dS )a™  
        Shorthand for setting rdf:type of the current subject.

        Usage::

            >>> from rdflib import URIRef
            >>> from rdflib.namespace import RDF, RDFS
            >>> d = Describer(about="http://example.org/")
            >>> d.rdftype(RDFS.Resource)
            >>> (URIRef('http://example.org/'),
            ...     RDF.type, RDFS.Resource) in d.graph
            True

        N)r   r   r   r   Útype)r   Útr   r   r   Úrdftypeæ   s    zDescriber.rdftypec             C   s
   | j d S )Nr   r   )r   )r   r   r   r   r   ÷   s    zDescriber._currentc             c   s    | j j|ƒ d V  | j jƒ  d S )N)r   r   Úpop)r   r   r   r   r   r"   ú   s    zDescriber._subject_stack)NNN)N)N)Ú__name__Ú
__module__Ú__qualname__r   r   r!   r$   r&   r)   r   r   r"   r   r   r   r   r   y   s   

!
r   c             K   s   t | tƒst| f|Ž} | S )N)Ú
isinstancer
   )r    r   r   r   r   r     s    
r   c             K   s$   | pt ƒ } t| tƒs t| f|Ž} | S )N)r   r.   r	   r   )Úrefr   r   r   r   r     s    

r   N)Ú
__future__r   r   r   Ú__doc__Ú
contextlibr   Zrdflib.graphr   Zrdflib.namespacer   Zrdflib.termr   r	   r
   r   Úobjectr   r   r   r   r   r   r   Ú<module>   s   i 	