o
    ÒmÆie'  ã                   @  sl   d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZ er*d dlmZ dgZG dd„ dƒZdS )	é    )Úannotations)ÚTYPE_CHECKINGÚIterableÚIteratorÚListÚOptional)ÚRDF)ÚBNodeÚNode)ÚGraphÚ
Collectionc                   @  s”   e Zd ZdZg fd/dd	„Zd0dd„Zd1dd„Zd2dd„Zd3dd„Zd4dd„Z	d5dd„Z
d6dd „Zd7d"d#„Zd8d$d%„Zd9d&d'„Zd:d*d+„Zd,d-„ Zd.S );r   a]  See "Emulating container types": <https://docs.python.org/reference/datamodel.html#emulating-container-types>

    ```python
    >>> from rdflib.term import Literal
    >>> from rdflib.graph import Graph
    >>> from pprint import pprint
    >>> listname = BNode()
    >>> g = Graph('Memory')
    >>> listItem1 = BNode()
    >>> listItem2 = BNode()
    >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> c = Collection(g,listname)
    >>> pprint([term.n3() for term in c])
    ['"1"^^<http://www.w3.org/2001/XMLSchema#integer>',
     '"2"^^<http://www.w3.org/2001/XMLSchema#integer>',
     '"3"^^<http://www.w3.org/2001/XMLSchema#integer>']
    >>> Literal(1) in c
    True
    >>> len(c)
    3
    >>> c._get_container(1) == listItem1
    True
    >>> c.index(Literal(2)) == 1
    True

    ```

    The collection is immutable if `uri` is the empty list (`http://www.w3.org/1999/02/22-rdf-syntax-ns#nil`).
    Úgraphr   Úurir
   Úseqú
List[Node]c                 C  s&   || _ |ptƒ | _|r| |7 } d S d S ©N)r   r	   r   )Úselfr   r   r   © r   úK/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/collection.pyÚ__init__8   s
   ÿzCollection.__init__ÚreturnÚstrc                 C  s   dd  dd„ | D ƒ¡ S )a@  
        ```python
        >>> from rdflib.term import Literal
        >>> from rdflib.graph import Graph
        >>> listname = BNode()
        >>> g = Graph('Memory')
        >>> listItem1 = BNode()
        >>> listItem2 = BNode()
        >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> c = Collection(g, listname)
        >>> print(c.n3()) #doctest: +NORMALIZE_WHITESPACE
        ( "1"^^<http://www.w3.org/2001/XMLSchema#integer>
            "2"^^<http://www.w3.org/2001/XMLSchema#integer>
            "3"^^<http://www.w3.org/2001/XMLSchema#integer> )

        ```
        z( %s )ú c                 S  s   g | ]}|  ¡ ‘qS r   )Ún3)Ú.0Úir   r   r   Ú
<listcomp>[   s    z!Collection.n3.<locals>.<listcomp>)Újoin©r   r   r   r   r   >   s   zCollection.n3ÚindexÚintúOptional[Node]c                 C  sV   t |tƒsJ ‚| j}| j}d}||k r)|d7 }| |tj¡}|du r%	 |S ||k s|S )z+Gets the first, rest holding node at index.r   é   N)Ú
isinstancer    r   r   Úvaluer   Úrest)r   r   r   Ú	containerr   r   r   r   Ú_get_container]   s   ûzCollection._get_containerc                 C  s   t t| j | j¡ƒƒS )zlength of items in collection.)ÚlenÚlistr   Úitemsr   r   r   r   r   Ú__len__j   s   zCollection.__len__Úitemc                 C  s’   | j }d}	 |tj|f| jv r|S t| j |tj¡ƒ}|d7 }|tjgkr.td|| j f ƒ‚|s7t	d| j  ƒ‚t
|ƒdksDJ d| j  ƒ‚|d }q)zM
        Returns the 0-based numerical index of the item in the list
        r   Tr"   z%s is not in %szMalformed RDF Collection: %s)r   r   Úfirstr   r)   Úobjectsr%   ÚnilÚ
ValueErrorÚ	Exceptionr(   )r   r,   Zlistnamer   Znewlinkr   r   r   r   n   s   ôzCollection.indexÚkeyc                 C  s6   |   |¡}|r| j |tj¡}|r|S t|ƒ‚t|ƒ‚)ÚTODO)r'   r   r$   r   r-   ÚKeyErrorÚ
IndexError)r   r2   ÚcÚvr   r   r   Ú__getitem__‚   s   
zCollection.__getitem__r$   ÚNonec                 C  s.   |   |¡}|r| j |tj|f¡ dS t|ƒ‚)r3   N)r'   r   Úsetr   r-   r5   )r   r2   r$   r6   r   r   r   Ú__setitem__Ž   s   
zCollection.__setitem__c                 C  sÎ   | |  | j }|  |¡}|sJ ‚t| ƒdkr|dkrdS |t| ƒd kr@|  |d ¡}| j  |tjtjf¡ | |ddf¡ dS |  |d ¡}|  |d ¡}|rR|sTJ ‚| |ddf¡ | |tj|f¡ dS )a  
        ```python
        >>> from rdflib.namespace import RDF, RDFS
        >>> from rdflib import Graph
        >>> from pprint import pformat
        >>> g = Graph()
        >>> a = BNode('foo')
        >>> b = BNode('bar')
        >>> c = BNode('baz')
        >>> g.add((a, RDF.first, RDF.type)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((a, RDF.rest, b)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.first, RDFS.label)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.rest, c)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.first, RDFS.comment)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> len(g)
        6
        >>> def listAncestry(node, graph):
        ...   for i in graph.subjects(RDF.rest, node):
        ...     yield i
        >>> [str(node.n3())
        ...   for node in g.transitiveClosure(listAncestry, RDF.nil)]
        ['_:baz', '_:bar', '_:foo']
        >>> lst = Collection(g, a)
        >>> len(lst)
        3
        >>> b == lst._get_container(1)
        True
        >>> c == lst._get_container(2)
        True
        >>> del lst[1]
        >>> len(lst)
        2
        >>> len(g)
        4

        ```
        r"   r   N)r   r'   r(   r:   r   r%   r/   Úremove)r   r2   r   ÚcurrentZ	priorlinkÚnextZpriorr   r   r   Ú__delitem__–   s   -
zCollection.__delitem__úIterator[Node]c                 C  s   | j  | j¡S )z"Iterator over items in Collections)r   r*   r   r   r   r   r   Ú__iter__Ö   s   zCollection.__iter__c                 C  s4   | j }	 | j |tj¡}|d u s|tjkr|S |}qr   )r   r   r$   r   r%   r/   )r   r&   r%   r   r   r   Ú_endÚ   s   ûzCollection._endc                 C  sx   |   ¡ }|tjkrtdƒ‚|tjdf| jv r%tƒ }| j |tj|f¡ |}| j 	|tj|f¡ | j 	|tjtjf¡ | S )a§  
        ```python
        >>> from rdflib.term import Literal
        >>> from rdflib.graph import Graph
        >>> listname = BNode()
        >>> g = Graph()
        >>> c = Collection(g,listname,[Literal(1),Literal(2)])
        >>> links = [
        ...     list(g.subjects(object=i, predicate=RDF.first))[0] for i in c]
        >>> len([i for i in links if (i, RDF.rest, RDF.nil) in g])
        1

        ```
        úCannot append to empty listN)
rB   r   r/   r0   r-   r   r	   r:   r%   Úadd)r   r,   ÚendÚnoder   r   r   Úappendä   s   
zCollection.appendÚotherúIterable[Node]c                 C  s–   |   ¡ }|tjkrtdƒ‚| j |tjd f¡ |D ]$}|tjd f| jv r3tƒ }| j 	|tj|f¡ |}| j 	|tj|f¡ q| j 	|tjtjf¡ | S )NrC   )
rB   r   r/   r0   r   r<   r%   r-   r	   rD   )r   rH   rE   r,   Znxtr   r   r   Ú__iadd__  s   
zCollection.__iadd__c                 C  sN   | j }| j}|r%| |tj¡}| |tjd f¡ | |tjd f¡ |}|s| S r   )r   r   r$   r   r%   r<   r-   )r   r&   r   r%   r   r   r   Úclear  s   üzCollection.clearN)r   r   r   r
   r   r   )r   r   )r   r    r   r!   )r   r    )r,   r
   r   r    )r2   r    r   r
   )r2   r    r$   r
   r   r9   )r2   r    r   r9   )r   r@   )r   r
   )r,   r
   r   r   )rH   rI   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r'   r+   r   r8   r;   r?   rA   rB   rG   rJ   rK   r   r   r   r   r      s    )







@



N)Ú
__future__r   Útypingr   r   r   r   r   Zrdflib.namespacer   Zrdflib.termr	   r
   Zrdflib.graphr   Ú__all__r   r   r   r   r   Ú<module>   s    