
    Pi?                    d   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d>dZd?dZedk    r ej                    Ze                    d e d!"           e                    de d#"           e                    de d$"           e                    d%d&dd'd()           e                    d*dd+,           e!                    d-.           e"                                Z# ee#j$                  Z%e% e&d/            e'd0            e            (                    e#j$        e%1          Z) ee#j*                    ee#j+                    e&d2e#j+         d3e#j*         d4            e&d5 e,e)           d6            e&d7            ee)e#j*                  Z- e            .                                d8e#j+         d9z  Z/ e&d:e/             ee/e#j*        e-d0         e-d;         e#j+        e#j0                   dS dS )@a/  
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)	ResultRow	namespacestrreturnNonec                N    |                      d          st          d          d S )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)r   s    z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/tools/defined_namespace_creator.pyvalidate_namespacer      s4    j)) LJKKKL L    	object_idc                V    | D ]%}|                                 st          d          &d S )Nz5The supplied object_id must be an all-capitals string)isupperr   )r   cs     r   validate_object_idr    #   sE     V Vyy{{ 	VTUUU	VV Vr   gr	   target_namespace2Tuple[List[Tuple[str, str]], List[str], List[str]]c                   t           t          t          t          d}d                    d|          }g }|                     ||          D ]]}t          rt          |t                    sJ |	                    t          |d                   t          |d                   f           ^|                    d            g }g }|D ]}|d                             |d	          }	|d                             d
d          }
|	                                r1t          j        |	          s|	                    d|	 d|
 d
           |	                    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   appendr   sortisidentifierkeyword	iskeyword)r!   r"   
namespacesqelementsrelements_strsnon_python_elements_strsenamedescs              r   get_target_namespace_elementsrE   J   s    %S$MMJ	  G ! & ')HWWQzW** 0 0 	,a+++++QqTC!II.////MM((M)))!M*, R Rt||,b11t||D#&& 	Rw'8'>'> 	R  !B!B!B$!B!B!BCCCC$++,P$,P,Pd,P,P,PQQQQ]$<<<r   output_file_namer   r@   Iterable[str]rA   	List[str]failboolc                J   d| dt           j                                          d}t          | d          5 }|                    |           |                    d           |                    d| d           |                    d           |r*|                    d	           |                    d           |                    |           t          |          d
k    r|                    d           |                    d           |                    d           |                    d           |                    d           |                    |           |                    d           |                    d           d d d            d S # 1 swxY w Y   d S )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	
    """
wr2   z    _NS = Namespace("z")z

z    _fail = Truer   z"    # Valid non-python identifiersz    _extras = [z    ])datetimeutcnowopenwrite
writelineslen)rF   r"   r@   rA   r   rI   headerfs           r   make_dn_filerU   v   s    	  ##%%  F 
	$	$ 			<(8<<<===	 	GG&'''GGFOOO	]###'((1,,GGDMMMGG8999GGDMMMGG%&&&GGDMMMLL1222GGGGGDMMM%                 s   EFFF__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actionrY   z	--no-failstore_false)r[   r\   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   r   r   r   )r   r   r   r   )r!   r	   r"   r   r   r#   )rF   r   r"   r   r@   rG   rA   rH   r   r   rI   rJ   r   r   )1__doc__
__future__r   argparserM   r:   pathlibr   typingr   r   r   r   rdflib.graphr	   rdflib.namespacer
   r   r   r   rdflib.utilr   rdflib.queryr   r   r    rE   rU   __name__ArgumentParserparseradd_argumentr   set_defaults
parse_argsargsrW   fmtprintexitparser!   r"   r   rR   r>   cwdrF   rI   r-   r   r   <module>rw      s    # " " " " "          7 7 7 7 7 7 7 7 7 7 7 7       5 5 5 5 5 5 5 5 5 5 5 5 $ $ $ $ $ $ '&&&&&&L L L L
V V V VN)= )= )= )=X& & & &R z$X$&&F
K           T          &GGG
&&&D
,t)
*
*C
{BCCCQd(55At,---t~&&&	E	*$. 	* 	*$	* 	* 	*   
E
533q66
5
5
5666	E
-...,,Q0EFFHtvvzz||&=$.&=&=&==	E
E3C
E
EFFFL	    q r   