
    i?                    P   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZmZ ddlmZ e	rdd	lmZ d.d
Zd/dZ	 	 	 	 	 	 d0dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1dZedk(  r ej:                         Zej?                  de d       ej?                  de d       ej?                  de d       ej?                  ddddd       ej?                  ddd       ejC                  d        ejE                         Z# ee#jH                        Z%e% e&d!        e'd"        e       jQ                  e#jH                  e%#      Z) ee#jT                          ee#jV                          e&d$e#jV                   d%e#jT                   d&        e&d' e,e)       d(        e&d)        ee)e#jT                        Z- e       j]                         d*e#jV                   d+z  Z/ e&d,e/         ee/e#jT                  e-d"   e-d-   e#jV                  e#j`                         yy)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                <    | j                  d      st        d      y )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)	namespaces    UC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/tools/defined_namespace_creator.pyvalidate_namespacer      s!    j)JKK *    	object_idc                H    | D ]  }|j                         rt        d       y )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                T   t         t        t        t        d}dj	                  d|      }g }| j                  ||      D ]E  }t        rt        |t              sJ |j                  t        |d         t        |d         f       G |j                  d        g }g }|D ]  }|d   j	                  |d	      }	|d   j	                  d
d      }
|	j                         r.t        j                  |	      s|j                  d|	 d|
 d
       k|j                  d|	 d|
 d
        |||fS )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                    | d   S )Nr    )tups    r   <lambda>z/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_elementsrB   J   s2    %S$MJ	  G! & ')HWWQzW*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                   d| dt         j                   j                          d}t        | d      5 }|j                  |       |j                  d       |j                  d| d       |j                  d       |r"|j                  d	       |j                  d       |j	                  |       t        |      d
kD  r|j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j	                  |       |j                  d       |j                  d       d d d        y # 1 sw Y   y xY w)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   rC   headerfs           r   make_dn_filerO   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actionrS   z	--no-failstore_false)rU   rV   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   r3   returnNone)r   r3   r\   r]   )r8   r	   r   r3   r\   z2Tuple[List[Tuple[str, str]], List[str], List[str]])rL   r   r   r3   r=   zIterable[str]r>   z	List[str]r   r3   rC   boolr\   r]   )1__doc__
__future__r   argparserF   r6   pathlibr   typingr   r   r   r   rdflib.graphr	   rdflib.namespacer
   r   r   r   rdflib.utilr   rdflib.queryr   r   r   rB   rO   __name__ArgumentParserparseradd_argumentr3   set_defaults
parse_argsargsrQ   fmtprintexitparser8   r   r   rK   r;   cwdrL   rC   r(   r   r   <module>rt      s   #     7 7  5 5 $&L
VN)=)= #)=7)=X&&& !& (	&
 & & 
&R z$X$$&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   