
    01i?                    <   S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	J
r
JrJr  SSKJr  SSKJrJrJrJr  SSKJr  \	(       a  SS	KJr  S.S
 jrS/S jr      S0S jr              S1S jr\S:X  Ga  \R:                  " 5       r\R?                  S\ SS9  \R?                  S\ SS9  \R?                  S\ SS9  \R?                  SSSSSS9  \R?                  SSSS9  \RC                  SS 9  \RE                  5       r#\" \#RH                  5      r%\%c  \&" S!5        \'" S"5        \" 5       RQ                  \#RH                  \%S#9r)\" \#RT                  5        \" \#RV                  5        \&" S$\#RV                   S%\#RT                   S&35        \&" S'\," \)5       S(35        \&" S)5        \" \)\#RT                  5      r-\" 5       R]                  5       S*\#RV                   S+3-  r/\&" S,\/ 35        \" \/\#RT                  \-S"   \-S-   \#RV                  \#R`                  5        gg)2a/  
This rdflib Python script creates a DefinedNamespace Python file from a given RDF file

It is a very simple script: it finds all things defined in the RDF file within a given
namespace:

    <thing> a ?x

    where ?x is anything and <thing> starts with the given namespace

Nicholas J. Car, Dec, 2021
    )annotationsN)Path)TYPE_CHECKINGIterableListTuple)Graph)DCTERMSOWLRDFSSKOS)guess_format)	ResultRowc                F    U R                  S5      (       d  [        S5      eg )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)	namespaces    `/home/james-whalen/.local/lib/python3.13/site-packages/rdflib/tools/defined_namespace_creator.pyvalidate_namespacer      s$    j))JKK *    	object_idc                V    U  H#  nUR                  5       (       a  M  [        S5      e   g )Nz5The supplied object_id must be an all-capitals string)isupperr   )r   cs     r   validate_object_idr   #   s#    yy{{TUU r   target_namespacec                x   [         [        [        [        S.nSR	                  SU5      n/ nU R                  X2S9 HP  n[        (       a  [        U[        5      (       d   eUR                  [        US   5      [        US   5      45        MR     UR                  S S9  / n/ nU H  nUS   R	                  US	5      n	US   R	                  S
S5      n
U	R                  5       (       a5  [        R                  " U	5      (       d  UR                  SU	 SU
 S
35        Mw  UR                  SU	 SU
 S
35        M     XFU4$ )N)dctermsowlrdfsskosa  
        SELECT ?s (GROUP_CONCAT(DISTINCT STR(?def)) AS ?defs)
        WHERE {
            # all things in the RDF data (anything RDF.type...)
            ?s a ?o .

            # get any definitions, if they have one
            OPTIONAL {
                ?s dcterms:description|rdfs:comment|skos:definition ?def
            }

            # only get results for the target namespace (supplied by user)
            FILTER STRSTARTS(STR(?s), "xxx")
            FILTER (STR(?s) != "xxx")
        }
        GROUP BY ?s
        xxx)initNsr      c                    U S   $ )Nr    )tups    r   <lambda>/get_target_namespace_elements.<locals>.<lambda>g   s    #a&r   )key 
 z    z: URIRef  # z	        "z",  # )r
   r   r   r   replacequeryr   
isinstancer   appendstrsortisidentifierkeyword	iskeyword)gr   
namespacesqelementsrelements_strsnon_python_elements_strsenamedescs              r   get_target_namespace_elementsrC   J   s9    %S$MJ	  G! & ')HWWQW*=a++++QqTC!I./ +
 MM(M)!M*,t||,b1t||D#&w'8'8'>'>  4v\$r!BC$++	$vdV2,PQ  $<<<r   failc                   SU S[         R                   R                  5        S3n[        U S5       nUR                  U5        UR                  S5        UR                  SU S35        UR                  S5        U(       a"  UR                  S	5        UR                  S5        UR	                  U5        [        U5      S
:  a  UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR	                  U5        UR                  S5        UR                  S5        S S S 5        g ! , (       d  f       g = f)Nz`from rdflib.namespace import DefinedNamespace, Namespace
from rdflib.term import URIRef


class zo(DefinedNamespace):
    """
    DESCRIPTION_EDIT_ME_!

    Generated from: SOURCE_RDF_FILE_EDIT_ME_!
    Date: z	
    """
wr.   z    _NS = Namespace("z")z

z    _fail = Truer   z"    # Valid non-python identifiersz    _extras = [z    ])datetimeutcnowopenwrite
writelineslen)output_file_namer   r>   r?   r   rD   headerfs           r   make_dn_filerP   v   s    k 
 ##%
& 'F 
	$			'(8'9<=	GG&'GGFO	]#'(1,GGDMGG89GGDMGG%&GGDMLL12GGGGGDM% 
%	$	$s   DE
E#__main__ontology_filez<Path to the RDF ontology to extract a DefinedNamespace from.)typehelpzQThe namespace within the ontology that you want to create a DefinedNamespace for.zEThe RDFlib object ID of the DefinedNamespace, e.g. GEO for GeoSPARQL.z-fz--fail
store_truezRWhether (true) or not (false) to mimic ClosedNamespace and fail on non-element use)destactionrT   z	--no-failstore_false)rV   rW   F)featurez2The format of the file you've supplied is unknown.r&   )formatzCreating DefinedNamespace file z for z...zOntology with z triples loaded...z!Getting all namespace elements..._z.pyz&Creating DefinedNamespace Python file    )r   r4   returnNone)r   r4   r]   r^   )r9   r	   r   r4   r]   z2Tuple[List[Tuple[str, str]], List[str], List[str]])rM   r   r   r4   r>   zIterable[str]r?   z	List[str]r   r4   rD   boolr]   r^   )1__doc__
__future__r   argparserG   r7   pathlibr   typingr   r   r   r   rdflib.graphr	   rdflib.namespacer
   r   r   r   rdflib.utilr   rdflib.queryr   r   r   rC   rP   __name__ArgumentParserparseradd_argumentr4   set_defaults
parse_argsargsrR   fmtprintexitparser9   r   r   rL   r<   cwdrM   rD   r(   r   r   <module>ru      s   #     7 7  5 5 $&L
VN)=)= #)=7)=X&&& !& (	&
 & & 
&R z$$&F
K       T      &G
&D
t))
*C
{BCQd((5At,,-t~~&	
)$..)9 :$$%S	* 
N3q6("4
56	
-.,Q0E0EFHvzz|$..)9&==	23C2D
EF		q r   