
    4Ysh4                        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	 dd	l
mZ dd
l
mZ ddl
mZ ddl
m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 g dZd Zd Zd6dZd Zd7dZd8de fdZ!d Z"d Z#d Z$d Z%d  Z&d! Z'd9d"Z(d# Z)d$d$d$d%d&d'd(d)d)d)d*d+d,d,d,d-Z*d7d.Z+d:d/Z,d7d0Z-d1 ddd2fd3Z.d4 Z/e0d5k(  r e/        yy);aT  
Some utility functions.

Miscellaneous utilities

* list2set
* first
* uniq
* more_than

Term characterisation and generation

* to_term
* from_n3

Date/time utilities

* date_time
* parse_date_time

Statement and component type checkers

* check_context
* check_subject
* check_predicate
* check_object
* check_statement
* check_pattern

    )timegm)altzone)gmtime)	localtime)time)timezone)splitext)ContextTypeError)ObjectTypeError)PredicateTypeError)SubjectTypeErrorN)	Namespace)NamespaceManager)BNode)Literal)URIRef)sign)list2setfirstuniq	more_thanto_termfrom_n3	date_timeparse_date_timecheck_contextcheck_subjectcheck_predicatecheck_objectcheck_statementcheck_patternguess_format
find_rootsget_treec                 n    t               }| D cg c]  }||vs|j                  |      r| c}S c c}w )zX
    Return a new list without duplicates.
    Preserves the order, unlike set(seq)
    )setadd)seqseenxs      k/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/util.pyr   r   L   s/    
 5D@!atmDHHQKA@@@s   	222c                     | D ]  }|c S  y)z_
    return the first element in a python sequence
    for graphs, use graph.value instead
    N )r(   results     r+   r   r   U   s    
      c                 @    |rt        d | D              S t        |       S )z,removes duplicate strings from the sequence.c              3   <   K   | ]  }|j                           y wN)strip).0ss     r+   	<genexpr>zuniq.<locals>.<genexpr>b   s     /1779/s   )r&   )sequencer3   s     r+   r   r   _   s     /h///8}r/   c                 .    d}| D ]  }|dz  }||kD  s y y)z>Returns 1 if sequence has more items than number and 0 if not.r      r-   )r7   numberiitems       r+   r   r   g   s.    	A 	Qv: r/   c                 "   | s|S | j                  d      r| j                  d      rt        | dd       S | j                  d      r| j                  d      rt        | dd       S | j                  d      rt	        |       S d| z  }t        |      )a  
    Creates and returns an Identifier of type corresponding
    to the pattern of the given positional argument string ``s``:

    '' returns the ``default`` keyword argument value or ``None``

    '<s>' returns ``URIRef(s)`` (i.e. without angle brackets)

    '"s"' returns ``Literal(s)`` (i.e. without doublequotes)

    '_s' returns ``BNode(s)`` (i.e. without leading underscore)

    <>r9   "_zUnrecognised term syntax: '%s')
startswithendswithr   r   r   	Exception)r5   defaultmsgs      r+   r   r   q   s     	
c	qzz#a"g	
c	qzz#q2w	
c	Qx.2nr/   r5   c                    | s|S | j                  d      r,t        | dd j                  d      j                  d            S | j                  d      r| j                  d      rd}nd}| j	                  |d      \  }}|t        |      d }d}d}|j                  d	      }	|	d
k\  rt        ||	dz   d |||      }n|j                  d      r|dd }|j                  dd      }|j                  dd      }|j                  d      j                  d      }t        |||      S | dk(  s| dk(  rt        | dk(        S | j                         rt        t        |             S | j                  d      r.t        | dd       }
t        j                  j                  ||
      S | j                  d      r.t        | dd       }
t        j                  j                  ||
      S | j                  d      rt!        | dd       S d| v rh|'t#        t        j                  j                               }| j%                  dd      \  }}t'        |j)                               |   }t+        |      |   S t!        |       S )a  
    Creates the Identifier corresponding to the given n3 string.

        >>> from_n3('<http://ex.com/foo>') == URIRef('http://ex.com/foo')
        True
        >>> from_n3('"foo"@de') == Literal('foo', lang='de')
        True
        >>> from_n3('"""multi\nline\nstring"""@en') == Literal(
        ...     'multi\nline\nstring', lang='en')
        True
        >>> from_n3('42') == Literal(42)
        True
        >>> from_n3(Literal(42).n3()) == Literal(42)
        True
        >>> from_n3('"42"^^xsd:integer') == Literal(42)
        True
        >>> from rdflib import RDFS
        >>> from_n3('rdfs:label') == RDFS['label']
        True
        >>> nsm = NamespaceManager(rdflib.graph.Graph())
        >>> nsm.bind('dbpedia', 'http://dbpedia.org/resource/')
        >>> berlin = URIRef('http://dbpedia.org/resource/Berlin')
        >>> from_n3('dbpedia:Berlin', nsm=nsm) == berlin
        True

    r>   r9   r@   zraw-unicode-escapezunicode-escaperA   z"""Nz^^r      @z\"z\xz\\xtruefalse{[z_::)rC   r   encodedecodersplitlenrfindr   replacer   isdigitintrdflibgraphQuotedGraphGraphr   r   splitdict
namespacesr   )r5   rF   backendnsmquotesvaluerestdatatypelanguagedtoffset
identifierprefix	last_partnss                 r+   r   r      sD   6 ||C a"gnn%9:AABRSTT	
c	<<FFhhvq)tc&km$ ::d#q=
 tHqLN3WgsKHs#8eS) eV, 1299:JKuh11	
fWqF{##	
s1v	
c	QqW%
||''<<	
c	QqW%
||!!':66	
d	QqrU|	;"6<<#5#5#78CGGCO	#.."#F+}Y''Qxr/   c                 x    t        | t              s*t        | t              st        | dt	        |             y y )NrO   )
isinstancer   r   r
   type)cs    r+   r   r      s1    q&!Z5%9!T!W566 &:!r/   c                 \    t        | t              st        | t              st        |       yy)z*Test that s is a valid subject identifier.N)rl   r   r   r   )r5   s    r+   r   r      s(    q&!Z5%9q!! &:!r/   c                 :    t        | t              st        |       y)z,Test that p is a valid predicate identifier.N)rl   r   r   )ps    r+   r   r      s    a  ## !r/   c                 ~    t        | t              s-t        | t              st        | t              st	        |       yyy)z)Test that o is a valid object identifier.N)rl   r   r   r   r   )os    r+   r   r      s6    q&!Z7%;z!U?Sa   @T%;!r/   c                    | \  }}}t        |t              st        |t              st        |      t        |t              st	        |      t        |t              s-t        |t
              st        |t              st        |      y y y r2   rl   r   r   r   r   r   r   tripler5   rq   rs   s       r+   r    r       st    IQ1q&!Z5%9q!!a  ##q&!Z7%;z!U?Sa   @T%;!r/   c                 $   | \  }}}|r+t        |t              st        |t              st        |      |rt        |t              st	        |      |r>t        |t              s-t        |t
              st        |t              st        |      y y y y r2   ru   rv   s       r+   r!   r!     s    IQ1*Q':a+?q!!Av& ##1fAw!7:a;Oa   <P!7 	r/   c           	          | 
t               } |r1t        |       }|d   r
t        dz  }n	t        dz  }d|dz  |dz  fz  }nt	        |       }d}|\	  }}}}}	}
}}}d|||||	|
|fz  }|S )ah  http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z

    >>> date_time(1126482850)
    '2005-09-11T23:54:10Z'

    @@ this will change depending on where it is run
    #>>> date_time(1126482850, local_time_zone=True)
    #'2005-09-11T19:54:10-04:00'

    >>> date_time(1)
    '1970-01-01T00:00:01Z'

    >>> date_time(0)
    '1970-01-01T00:00:00Z'
       <   z
-%02d:%02dZz!%0004d-%02d-%02dT%02d:%02d:%02d%s)r   r   r   r   r   )tlocal_time_zone
time_tupletz_minstzdyearmonthdayhhmmsswdyzr5   s                  r+   r   r     s      	yFq\
a=mG"nGgmWr\::AY
-7*D%b"b"a+tUCRS.QQAHr/   c                    d| vr| dz  } | j                  d      \  }}|dd |dd }}|r|dk(  r|dd }d}n6t        |dd       }t        |d	d
       }t        |      |z  |dz  z   dz  }| }|j                  d      \  }	}
}|j                  d      \  }}}t        t        |	      t        |
      t        |      t        |      t        |      t        |      dddf	      }||z   }|S )a  always returns seconds in UTC

    # tests are written like this to make any errors easier to understand
    >>> parse_date_time('2005-09-11T23:54:10Z') - 1126482850.0
    0.0

    >>> parse_date_time('2005-09-11T16:54:10-07:00') - 1126482850.0
    0.0

    >>> parse_date_time('1970-01-01T00:00:01Z') - 1.0
    0.0

    >>> parse_date_time('1970-01-01T00:00:00Z') - 0.0
    0.0
    >>> parse_date_time("2005-09-05T10:42:00") - 1125916920.0
    0.0
    Tz
T00:00:00Zr   rz   Nr|   r@            r{   -rO   )r\   rW   r   r   )valymdr   hmstz_str	tz_offset
signed_hrsminssecsr   r   r   hourminutesecondr}   s                   r+   r   r   6  s	   & #~|		#ICq)T!"XCVs]CRy	_
6!A;Z 4'*r/9R?E	yy~D%99S>D&&	TCJC#d)S[#f+qRSUVW	A 	
IAHr/   xmln3turtlenttrixrdfanquadstrigjson-ld)r   rdfowlr   ttlr   r   xhtmlhtmlsvgnqr   jsonjsonldr   c                     |xs t         }|j                  t        |             xs |j                  | j                               S )a9  
    Guess RDF serialization based on file suffix. Uses
    ``SUFFIX_FORMAT_MAP`` unless ``fmap`` is provided. Examples:

        >>> guess_format('path/to/file.rdf')
        'xml'
        >>> guess_format('path/to/file.owl')
        'xml'
        >>> guess_format('path/to/file.ttl')
        'turtle'
        >>> guess_format('path/to/file.json')
        'json-ld'
        >>> guess_format('path/to/file.xhtml')
        'rdfa'
        >>> guess_format('path/to/file.svg')
        'rdfa'
        >>> guess_format('path/to/file.xhtml', {'xhtml': 'grddl'})
        'grddl'

    This also works with just the suffixes, with or without leading dot, and
    regardless of letter case::

        >>> guess_format('.rdf')
        'xml'
        >>> guess_format('rdf')
        'xml'
        >>> guess_format('RDF')
        'xml'
    )SUFFIX_FORMAT_MAPget_get_extlower)fpathfmaps     r+   r"   r"   u  s6    < $$D88HUO$?(??r/   c                     t        |       d   }|dk(  r| j                  d      r| }|r|j                         }|j                  d      r|dd }|S )a)  
    Gets the file extension from a file(path); stripped of leading '.' and in
    lower case. Examples:

        >>> _get_ext("path/to/file.txt")
        'txt'
        >>> _get_ext("OTHER.PDF")
        'pdf'
        >>> _get_ext("noext")
        ''
        >>> _get_ext(".rdf")
        'rdf'
    r@    .r9   N)r	   rC   r   )r   r   exts      r+   r   r     sU     5/"
C
byU%%c*iik
~~c!"gJr/   c                     t               }|
t               }| j                  |      D ]A  \  }}|j                  |       ||v r|j                  |       ||vs1|j                  |       C |S )a  
    Find the roots in some sort of transitive hierarchy.

    find_roots(graph, rdflib.RDFS.subClassOf)
    will return a set of all roots of the sub-class hierarchy

    Assumes triple of the form (child, prop, parent), i.e. the direction of
    RDFS.subClassOf or SKOS.broader

    )r&   subject_objectsr'   remove)rY   proproots	non_rootsr*   r   s         r+   r#   r#     sm     I}%%d+ 1a:LLOIIIaL Lr/   c                     | S r2   r-   )r*   s    r+   <lambda>r     s     r/   downc           
          |
t               }||v ry|j                  |       g }|dk(  r| j                  ||      }n| j                  ||      }|D ]'  }	t	        | |	|||||      }
|
s|j                  |
       )  ||      t        ||      fS )a  
    Return a nested list/tuple structure representing the tree
    built by the transitive property given, starting from the root given

    i.e.

    get_tree(graph,
       rdflib.URIRef("http://xmlns.com/foaf/0.1/Person"),
       rdflib.RDFS.subClassOf)

    will return the structure for the subClassTree below person.

    dir='down' assumes triple of the form (child, prop, parent),
    i.e. the direction of RDFS.subClassOf or SKOS.broader
    Any other dir traverses in the other direction

    Nr   )key)r&   r'   subjectsobjectsr$   appendsorted)rY   rootr   mappersortkeydonedirtreebranchesbranchr}   s              r+   r$   r$     s    * |ut|HHTND
f}>>$-==t, UFD&'4EKKN
 4L&7344r/   c                  ,    dd l } | j                          y )Nr   )doctesttestmod)r   s    r+   testr     s    OOr/   __main__)r   r2   )NNN)NF)T)1__doc__calendarr   r   r   r   r   r   os.pathr	   rdflib.exceptionsr
   r   r   r   rdflib.graphrX   rdflib.namespacer   r   rdflib.termr   r   r   rdflib.compatr   __all__r   r   r   r   r   strr   r   r   r   r   r    r!   r   r   r   r"   r   r#   r$   r   __name__r-   r/   r+   <module>r      s  >        . - 0 .  & -    *A6Ss Sl7
"$!	!! F)Z 


 &@D02 *4d&5R z 	F r/   