
    4Ysh^                        d dl Z d dl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mZmZ dZg d	Z e j"                  e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z ed      Z G d de      Zg dZedgz   Zeg dz   Z g dZ!d Z"efdZ#d Z$d 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/m0Z0 d d&l1m2Z2 d d'l3m4Z4 d d(l5m6Z6 d d)l7m8Z8 d d*l9m:Z: d d+l;m<Z< d d,l=m>Z> d d-l?m@Z@ d d.lAmBZB d d/lCmDZD d d0lEmFZF d d1lGmHZH d d2lImJZJ d d3lKmLZL d d4lMmNZN d d5lOmPZP d d6lQmRZR d d7lSmTZT d d8lUmVZV d d9lWmXZX d d:lYmZZZ y);    N)List)category)Path)	urldefrag)urljoin)URIRefVariable_is_valid_uria  
===================
Namespace Utilities
===================

RDFLib provides mechanisms for managing Namespaces.

In particular, there is a :class:`~rdflib.namespace.Namespace` class
that takes as its argument the base URI of the namespace.

.. code-block:: pycon

    >>> from rdflib.namespace import Namespace
    >>> RDFS = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")

Fully qualified URIs in the namespace can be constructed either by attribute
or by dictionary access on Namespace instances:

.. code-block:: pycon

    >>> RDFS.seeAlso
    rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#seeAlso')
    >>> RDFS['seeAlso']
    rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#seeAlso')


Automatic handling of unknown predicates
-----------------------------------------

As a programming convenience, a namespace binding is automatically
created when :class:`rdflib.term.URIRef` predicates are added to the graph.

Importable namespaces
-----------------------

The following namespaces are available by directly importing from rdflib:

* BRICK
* CSVW
* DC
* DCAT
* DCMITYPE
* DCTERMS
* DCAM
* DOAP
* FOAF
* ODRL2
* ORG
* OWL
* PROF
* PROV
* QB
* RDF
* RDFS
* SDO
* SH
* SKOS
* SOSA
* SSN
* TIME
* VANN
* VOID
* XSD

.. code-block:: pycon
    >>> from rdflib.namespace import RDFS
    >>> RDFS.seeAlso
    rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#seeAlso')
)	is_ncname	split_uri	NamespaceClosedNamespaceNamespaceManagerc                   P     e Zd ZdZd Zed        Zd Zd Zd Z	 fdZ
d Z xZS )	r   a  
    Utility class for quickly generating URIRefs with a common prefix

    >>> from rdflib.namespace import Namespace
    >>> n = Namespace("http://example.org/")
    >>> n.Person # as attribute
    rdflib.term.URIRef('http://example.org/Person')
    >>> n['first-name'] # as item - for things that are not valid python identifiers
    rdflib.term.URIRef('http://example.org/first-name')
    >>> n.Person in n
    True
    >>> n2 = Namespace("http://example2.org/")
    >>> n.Person in n2
    False
    c                     	 t         j                  | |      }|S # t        $ r t         j                  | |d      }Y |S w xY wNzutf-8str__new__UnicodeDecodeErrorclsvaluerts      y/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/namespace/__init__.pyr   zNamespace.__new__h   F    	2S%(B 	 " 	2S%1B		2     >>c                     t        | dz         S )Ntitle)r   selfs    r   r   zNamespace.titleo   s     dWn%%    c                 N    t        | t        |t              r|z         S dz         S )N )r   
isinstancer   r!   names     r   termzNamespace.termt   s&    djs&;dDEEDEEr"   c                 $    | j                  |      S Nr(   r!   keys     r   __getitem__zNamespace.__getitem__x       yy~r"   c                 R    |j                  d      rt        | j                  |      S N__)
startswithAttributeErrorr(   r&   s     r   __getattr__zNamespace.__getattr__{   s"    ??4   yyr"   c                 (    dt         |           dS )Nz
Namespace()super__repr__r!   	__class__s    r   r:   zNamespace.__repr__   s    EG,./q11r"   c                 $    |j                  |       S )a  Allows to check if a URI is within (starts with) this Namespace.

        >>> from rdflib import URIRef
        >>> namespace = Namespace('http://example.org/')
        >>> uri = URIRef('http://example.org/foo')
        >>> uri in namespace
        True
        >>> person_class = namespace['Person']
        >>> person_class in namespace
        True
        >>> obj = URIRef('http://not.example.org/bar')
        >>> obj in namespace
        False
        r3   r!   refs     r   __contains__zNamespace.__contains__   s     ~~d##r"   )__name__
__module____qualname____doc__r   propertyr   r(   r.   r5   r:   rA   __classcell__r<   s   @r   r   r   W   s;      & &F
2$r"   r   c                   <     e Zd ZdZd Z fdZ fdZ fdZ xZS )
URIPatterna8  
    Utility class for creating URIs according to some pattern
    This supports either new style formatting with .format
    or old-style with % operator

    >>> u=URIPattern("http://example.org/%s/%d/resource")
    >>> u%('books', 12345)
    rdflib.term.URIRef('http://example.org/books/12345/resource')

    c                     	 t         j                  | |      }|S # t        $ r t         j                  | |d      }Y |S w xY wr   r   r   s      r   r   zURIPattern.__new__   r   r   c                 4    t        t        |   |i |      S r*   )r   r9   __mod__r!   argskwargsr<   s      r   rM   zURIPattern.__mod__   s    egot6v677r"   c                 4    t        t        |   |i |      S r*   )r   r9   formatrN   s      r   rR   zURIPattern.format   s    egnd5f566r"   c                 (    dt         |           dS )NzURIPattern(r7   r8   r;   s    r   r:   zURIPattern.__repr__   s    UW-/022r"   )	rB   rC   rD   rE   r   rM   rR   r:   rG   rH   s   @r   rJ   rJ      s!    	873 3r"   rJ   c                        e Zd ZU dZeed<   dZeed<   dZeed<   g Z	e
e   ed<   dZeed<   d fd		Zd
 Zd Zd Zd Z fdZ xZS )DefinedNamespaceMetazH
    Utility metaclass for generating URIRefs with a common prefix

    _NST_warnF_fail_extras_underscore_numc                 b   t        |      }t        |      j                  d      rt        |   ||      S | j                  s| j
                  rS|| vrO| j
                  rt        d| d| j                   d      t        j                  d| d| j                   d       | j                  |   S )	Nr2   term '' not in namespace ''zCode: z is not defined in namespace    )
stacklevel)r   r3   r9   r.   rW   rX   r4   rV   warningswarnrB   )r   r'   defaultr<   s      r   r.   z DefinedNamespaceMeta.__getitem__   s    4yt9%7&tW55IICyy$vdV3GyPQ%RSSTF"?~N  wwt}r"   c                 $    | j                  |      S r*   r.   )r   r'   s     r   r5   z DefinedNamespaceMeta.__getattr__   s    t$$r"   c                 "    d| j                    dS )NzNamespace("z"))rV   r   s    r   r:   zDefinedNamespaceMeta.__repr__   s    SWWIR((r"   c                 ,    t        | j                        S r*   )r   rV   rg   s    r   __str__zDefinedNamespaceMeta.__str__   s    377|r"   c                 $    | j                  |      S r*   re   )r   others     r   __add__zDefinedNamespaceMeta.__add__   s    u%%r"   c                 .    t        |      j                  d      rt           |      S j                  t         j                              r!t        t         j                              d t         fd j                         D              S )zGDetermine whether a URI or an individual item belongs to this namespacer2   Nc              3      K   | ]_  }t        |t              rM|j                  v xs; |j                  v xs+ j                  xr d    dk(  xr dd j                          a yw)r   _   N)
issubclassDefinedNamespace__annotations__rY   rZ   isdigit).0cr   item_strs     r   	<genexpr>z4DefinedNamespaceMeta.__contains__.<locals>.<genexpr>   sz      
 !-.	 ))) W199$W##Us(:Ux|?S?S?UW
s   A%A()r   r3   r9   rA   rV   lenanymro)r   itemrw   r<   s   ` @r   rA   z!DefinedNamespaceMeta.__contains__   s|    t9t$7'--s377|,CL 1 34H 
 WWY	
 
 	
r"   r*   )rB   rC   rD   rE   r   rs   rW   boolrX   rY   r   r   rZ   r.   r5   r:   ri   rl   rA   rG   rH   s   @r   rU   rU      s^    
 
NE4E4GT#Y!OT!%)&
 
r"   rU   c                       e Zd ZdZd Zy)rr   z
    A Namespace with an enumerated list of members.
    Warnings are emitted if unknown members are referenced if _warn is True
    c                     t        d      )Nz!namespace may not be instantiated)	TypeErrorr    s    r   __init__zDefinedNamespace.__init__   s    ;<<r"   N)rB   rC   rD   rE   r    r"   r   rr   rr      s    
=r"   rr   )	metaclassc                   \     e Zd ZdZ fdZed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Z xZS )r   zf
    A namespace with a closed list of members

    Trying to create terms not listed is an error
    c                 v    t         |   | |      }|D ci c]  }|t        ||z          c}|_        |S c c}w r*   )r9   r   r   _ClosedNamespace__uris)r   uritermsr   tr<   s        r   r   zClosedNamespace.__new__   s<    W_S#&0561QrAv&6		 7s   6c                     t        |       S r*   )r   r    s    r   r   zClosedNamespace.uri   s    4yr"   c                 d    | j                   j                  |      }|t        d| d|  d      |S )Nr\   r]   r^   )r   getKeyError)r!   r'   r   s      r   r(   zClosedNamespace.term  s9    kkood#;VD6)=dV1EFF
r"   c                 $    | j                  |      S r*   r+   r,   s     r   r.   zClosedNamespace.__getitem__  r/   r"   c                     |j                  d      rt        	 | j                  |      S # t        $ r}t        |      d }~ww xY wr1   )r3   r4   r(   r   )r!   r'   es      r   r5   zClosedNamespace.__getattr__  sB    ??4   (yy& ($Q''(s   * 	A>Ac                 f    | j                    d| j                  j                   dt        |       dS )N.(r7   )rC   r<   rB   r   r    s    r   r:   zClosedNamespace.__repr__  s/    //"!DNN$;$;#<Ac$i]!LLr"   c                 ,    t        | j                        S r*   )listr   r    s    r   __dir__zClosedNamespace.__dir__  s    DKK  r"   c                 :    || j                   j                         v S r*   )r   valuesr?   s     r   rA   zClosedNamespace.__contains__  s    4;;%%''	
r"   c                     t        |       S r*   )dirr    s    r   _ipython_key_completions_z)ClosedNamespace._ipython_key_completions_  s    4yr"   )rB   rC   rD   rE   r   rF   r   r(   r.   r5   r:   r   rA   r   rG   rH   s   @r   r   r      sE    
  (M!

r"   r   z$http://www.w3.org/XML/1998/namespacec                   p    e Zd ZdZd Zd Zd Zed        Zd Z	d Z
defd	Zdd
ZddZddZd ZddZy)r   at  

    Class for managing prefix => namespace mappings

    Sample usage from FuXi ...

    .. code-block:: python

        ruleStore = N3RuleStore(additionalBuiltins=additionalBuiltins)
        nsMgr = NamespaceManager(Graph(ruleStore))
        ruleGraph = Graph(ruleStore,namespace_manager=nsMgr)


    and ...

    .. code-block:: pycon

        >>> import rdflib
        >>> from rdflib import Graph
        >>> from rdflib.namespace import Namespace, NamespaceManager
        >>> exNs = Namespace('http://example.com/')
        >>> namespace_manager = NamespaceManager(Graph())
        >>> namespace_manager.bind('ex', exNs, override=False)
        >>> g = Graph()
        >>> g.namespace_manager = namespace_manager
        >>> all_ns = [n for n in g.namespace_manager.namespaces()]
        >>> assert ('ex', rdflib.term.URIRef('http://example.com/')) in all_ns
        >>>

    c                 j   || _         i | _        i | _        d | _        i | _        i | _        | j                         D ]$  \  }}t        | j
                  t        |             & | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  dt               | j                  dt                | j                  dt"               | j                  d	t$               | j                  d
t&               | j                  dt(               | j                  dt*               | j                  dt,               | j                  dt.               | j                  dt0               | j                  dt2               | j                  dt4               | j                  dt6               | j                  dt8               | j                  dt:               | j                  dt<               | j                  dt>               | j                  dt@               | j                  dtB               | j                  dtD               | j                  dtF               | j                  dtH               y )Nbrickcsvwdcdcatdcmitypedctermsdcamdoapfoafodrlorgowlprofprovqbrdfrdfsschemashskossosassntimevannvoidxsdxml)%graph_NamespaceManager__cache_NamespaceManager__cache_strict_NamespaceManager__log_NamespaceManager__strie_NamespaceManager__trie
namespacesinsert_trier   bindBRICKCSVWDCDCATDCMITYPEDCTERMSDCAMDOAPFOAFODRL2ORGOWLPROFPROVQBRDFRDFSSDOSHSKOSSOSASSNTIMEVANNVOIDXSDXMLNS)r!   r   pns       r   r   zNamespaceManager.__init__F  s   
 
OO% 	-DAqSV,	- 			'5!		&$		$		&$		*h'		)W%		&$		&$		&$		&% 		%		%		&$		&$		$		%		&$		(C 		$		&$		&$		%		&$		&$		&$		% 			%r"   c                 H    t        fd| j                         D              S )Nc              3   F   K   | ]  \  }}j                  |        y wr*   r>   )ru   prefixnsr@   s      r   rx   z0NamespaceManager.__contains__.<locals>.<genexpr>t  s     J*&"3>>"%Js   !)rz   r   r?   s    `r   rA   zNamespaceManager.__contains__o  s    
 J8IJJJr"   c                     i | _         i | _        i | _        | j                         D ]$  \  }}t	        | j                  t        |             & y r*   )r   r   r   r   r   r   )r!   r   r   s      r   resetzNamespaceManager.resetv  sC    OO% 	-DAqSV,	-r"   c                 .    | j                   j                  S r*   )r   storer    s    r   r   zNamespaceManager.store}  s    zzr"   c                 `    | j                  |      \  }}}|dk(  r|S dj                  ||f      S Nr$   :)compute_qnamejoinr!   r   r   	namespacer'   s        r   qnamezNamespaceManager.qname  s8    "&"4"4S"9	4R<K88VTN++r"   c                 `    | j                  |      \  }}}|dk(  r|S dj                  ||f      S r   )compute_qname_strictr   r   s        r   qname_strictzNamespaceManager.qname_strict  s8    "&";";C"@	4R<K88VTN++r"   returnc                    	 t        |      \  }}|| j                  vr*t        | j                  | j                  t	        |             t        t	        |            }| j                  j                  |      }|t        |t              rd|z  S |d|z  S | j                  |      }dj                  |d   |d   g      S #  t        |t              rd|z  cY S d|z  cY S xY w)z
        Takes an RDF Term and 'normalizes' it into a QName (using the
        registered prefix) or (unlike compute_qname) the Notation 3
        form for URIs: <...URI...>
        z?%sz<%s>r   r   )r   r   insert_strier   r   r   r%   r	   r   r   r   r   )r!   rdfTermr   r'   r   
qNamePartss         r   normalizeUrizNamespaceManager.normalizeUri  s    		('0OIt,T\\4;;IGs9~.I ""9->j(;7?"^G##++G4J88Z]JrN;<<	('8,w&''s   AB? ?CCc                    t        |      st        dj                  |            || j                  vr
	 t	        |      \  }}|| j                  vr!t        | j                  | j                  |       | j                  |   r+t        | j                  |   |      }||}|t        |      d  }t        |      }| j                  j                  |      }|X|st        dj                  |            d}	 d|z  }| j                  j                  |      sn|dz  }'| j                  ||       ||f| j                  |<   | j                  |   S # t        $ r5}t        |      }| j                  j                  |      }|s|Y d }~Cd }~ww xY w)NzY"{}" does not look like a valid URI, cannot serialize this. Did you want to urlencode it?)No known prefix for {} and generate=Falserp   ns%s)r
   
ValueErrorrR   r   r   r   r   r   r   r   r   get_longest_namespacery   r   r   r   )	r!   r   generater   r'   r   r   pl_namespacenums	            r   r   zNamespaceManager.compute_qname  s   S!krr  dll""+C.	4 ,T\\4;;	B||I&4T\\)5LcR+ ,Is9~/0Dy)IZZ&&y1F~"CJJ9U  #c\F:://71HC	 
 		&),!'D 9DLL||C  =  "3K	**95G s   E 	F*FFc                    | j                  |      \  }}}t        t        |            r|||fS || j                  vr	 t	        |t
              \  }}|| j                  vr!t        | j                  | j                  |       t        |      }| j                  j                  |      }|X|st        dj                  |            d}	 d|z  }| j                  j                  |      sn|dz  }'| j!                  ||       |||f| j                  |<   | j                  |   S # t        $ r!}dj                  |      }t        |      d }~ww xY w)Nz^This graph cannot be serialized to a strict format because there is no valid way to shorten {}r   rp   r   )r   r   r   r   r   NAME_START_CATEGORIESr   rR   r   r   r   r   r   r   r   r   r   )	r!   r   r   r   r   r'   r   messager   s	            r   r   z%NamespaceManager.compute_qname_strict  sg    #'"4"4S"9	4SY9d**$---.&/5J&KOIt DLL0 t{{IF #9-	** >#&GNN ) 
 C!'##zz33F;!q	 
 IIfi0,2It+D##C(&&s++W " .FFLfSk  %W--.s   D! !	E*EEc                 :   t        t        |            }|d}nd|v rt        d      | j                  j	                  |      }|rt        |      }|r||k7  r|r<| j                  j                  ||       t        | j                  t        |             y|sd}d}	 ||}| j                  j	                  |      }|r|t        |      k(  ry| j                  j	                  |      sn|dz  }T| j                  j                  ||       no| j                  j                  |      }	|	| j                  j                  ||       n5|	|k(  rn/|s|	j                  d      r| j                  j                  ||       t        | j                  t        |             y)zbind a given namespace to the prefix

        if override, rebind, even if the given namespace is already
        bound to another prefix.

        if replace, replace any existing prefix with the new namespace

        Nr$    z Prefixes may not contain spaces.rc   rp   ro   )
r   r   r   r   r   r   r   r   r   r3   )
r!   r   r   overridereplacebound_namespacer   
new_prefix
tnamespacebound_prefixs
             r   r   zNamespaceManager.bind  sl    3y>*	>FF]=>>**..v6
 $_5O);

	2DKKY8 "C'-s3
!ZZ11*=
)vj/A"A zz++J7q  JJOOJ	2::,,Y7L#

	2'|66s;JJOOFI6DKKY0r"   c              #   r   K   | j                   j                         D ]  \  }}t        |      }||f  y wr*   )r   r   r   )r!   r   r   s      r   r   zNamespaceManager.namespacesH  s<     !%!6!6!8 	$FIy)I)##	$s   57c                     t        j                         j                         }t        d|z  ||       }|rt	        |      d   }|s|r|d   dk(  r|d   dk7  rd|z  }t        |      S )Nz%s/)allow_fragmentsr   r   #z%s#)r   cwdas_urir   r   r   )r!   r   defragbaseresults        r   
absolutizezNamespaceManager.absolutizeM  sj    xxz  "sJGv&q)Fs2w#~&**;f~r"   N)T)TF)rp   )rB   rC   rD   rE   r   rA   r   rF   r   r   r   r   r   r   r   r   r   r  r   r"   r   r   r   &  s^    >' RK-    ,,=s =2*!X6,p:1x$
r"   r   )LlLuLoLtNlNd)McMeMnLmr  )   ·u   ·-r   ro   %r   r7   c                     | rW| d   }|dk(  st        |      t        v r<t        dt        |             D ]#  }| |   }t        |      t        vs|t
        v r# y yy)Nr   ro   rp   )r   r  rangery   NAME_CATEGORIESALLOWED_NAME_CHARS)r'   firstirv   s       r   r   r     sg    QC<8E?.CC1c$i( G{o5..  r"   c                    | j                  t              rt        | j                  t              d   fS t        |       }t	        d|      D ]i  }| | dz
     }t        |      t        vs|t        v r't	        d|z
  |      D ]0  }t        | |         |v s	| |   dk(  s| d | }|s n| |d  }||fc c S   n t        dj                  |             )Nrp   r   r   ro   zCan't split '{}')
r3   r   splitry   r$  r   r%  r&  r   rR   )r   split_startlengthr(  rv   jr   lns           r   r   r     s    
~~esyy'*++XF1f QK{o-&&266* $CF#{2c!fmRaBQRB8O$  '..s3
44r"   c                 J   || v r| |   S d}t        | j                               D ]o  }t        |      t        |      kD  r"|j                  |      rt	        | |   |      c S |j                  |      sN|si | |<   d}| j                  |      }|| |   |<   q || vri | |<   | |   S )zInsert a value into the trie if it is not already contained in the trie.
    Return the subtree for the value regardless of whether it is a new value
    or not.FT)tuplekeysry   r3   r   pop)trier   multi_checkr-   dict_s        r   r   r     s     }E{KTYY[! 
%u:C U%5%5c%:tCy%00^^E" U"HHE  %DK
% DU;r"   c                 ,    || vrt        ||      | |<   y y r*   )r   )strier3  r   s      r   r   r     s    E"4/e r"   c                 f    | D ],  }|j                  |      st        | |   |      }||c S |c S  y r*   )r3   r   )r3  r   r-   outs       r   r   r     sC     C 'S	59C{

 r"   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )r   )[loggingra   typingr   unicodedatar   pathlibr   urllib.parser   r   rdflib.termr   r	   r
   rE   __all__	getLoggerrB   loggerr   r   rJ   typerU   rr   r   r   objectr   r  SPLIT_START_CATEGORIESr%  r&  r   r   r   r   r   rdflib.namespace._BRICKr   rdflib.namespace._CSVWr   rdflib.namespace._DCr   rdflib.namespace._DCATr   rdflib.namespace._DCMITYPEr   rdflib.namespace._DCTERMSr   rdflib.namespace._DCAMr   rdflib.namespace._DOAPr   rdflib.namespace._FOAFr   rdflib.namespace._ODRL2r   rdflib.namespace._ORGr   rdflib.namespace._OWLr   rdflib.namespace._PROFr   rdflib.namespace._PROVr   rdflib.namespace._QBr   rdflib.namespace._RDFr   rdflib.namespace._RDFSr   rdflib.namespace._SDOr   rdflib.namespace._SHr   rdflib.namespace._SKOSr   rdflib.namespace._SOSAr   rdflib.namespace._SSNr   rdflib.namespace._TIMEr   rdflib.namespace._VANNr   rdflib.namespace._VOIDr   rdflib.namespace._XSDr   r   r"   r   <module>r`     s3         "   7 7DL Y			8	$;$ ;$|3 3:3
4 3
l=!5 =.i .b 	89ov oh
 7 .$7 '*HHG &  6 5*.0
 * ' # ' / - ' ' ' ) % % ' ' # % ' % # ' ' % ' ' ' %r"   