
    4Ysh                        d Z ddlZddlmZ ddlZddlZej                  j                   e ee	      j                  j                         j                  j                               ddlmZ ddlmZmZmZmZ ddlmZ d Zd Zd	 Zd
 Zedk(  r ej4                         Zej9                  ded       ej9                  ded       ej9                  ded       ej9                  ddddd       ej9                  ddd       ej;                  d       ej=                         Z eej@                        Z!e! e"d        e#d        e       jI                  ej@                  e!       Z% eejL                          eejN                          e"d!ejN                   d"ejL                   d#        e"d$ e(e%       d%        e"d&        ee%ejL                        Z) e       jU                         d'ejN                   d(z  Z+ e"d)e+         ee+ejL                  e)d   ejN                  ejX                         yy)*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
    N)Path)Graph)DCTERMSOWLRDFSSKOS)guess_formatc                 <    | j                  d      st        d      y )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)	namespaces    /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/tools/defined_namespace_creator.pyvalidate_namespacer      s!    j)JKK *    c                 H    | D ]  }|j                         rt        d       y )Nz5The supplied object_id must be an all-capitals string)isupperr   )	object_idcs     r   validate_object_idr      s(     Vyy{TUUVr   c                    t         t        t        t        d}dj	                  d|      }g }| j                  ||      D ]-  }|j                  t        |d         t        |d         f       / |j                  d        g }|D ]P  }|d   j	                  d	d
      }|j                  d|d   j	                  t        j                  d       d| d	       R ||fS )N)dctermsowlrdfsskosa  
        SELECT DISTINCT ?s ?def
        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")
        }
        xxx)initNsr      c                     | d   S )Nr    )tups    r   <lambda>z/get_target_namespace_elements.<locals>.<lambda>\   s
    #a& r   )key
 z     z: URIRef  # )r   r   r   r   replacequeryappendstrsortargstarget_namespace)	gr.   
namespacesqelementsrelements_strsedescs	            r   get_target_namespace_elementsr7   E   s    $S$MJ	 G " HWWQzW* 0QqTC!I./0 MM(M)M 
t||D#&1Q4<< 5 5r:;<vRP	

 ]""r   c                    d| dt         j                   j                          d}t        | d      5 }|j                  |       |j                  d       |j                  d| d       |j                  d       |r"|j                  d	       |j                  d       |j	                  |       d d d        y # 1 sw Y   y xY w)
Nz`from rdflib.term import URIRef
from rdflib.namespace import DefinedNamespace, Namespace


class zo(DefinedNamespace):
    """
    DESCRIPTION_EDIT_ME_!

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

z    _fail = True)datetimeutcnowopenwrite
writelines)output_file_namer.   r4   r   failheaderfs          r   make_dn_filerC   h   s     k 
 ##%
& 'F 
	$ $			'(8'9<=	GG&'GGFO	]#$ $ $s   A>B99C__main__ontology_filez<Path to the RDF ontology to extract a DefinedNamespace from.)typehelpr.   zQThe namespace within the ontology that you want to create a DefinedNamespace for.r   zEThe RDFlib object ID of the DefinedNamespace, e.g. GEO for GeoSPARQL.z-fz--failr@   
store_truezRWhether (true) or not (false) to mimic ClosedNamespace and fail on non-element use)destactionrG   z	--no-failstore_false)rI   rJ   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 )-__doc__syspathlibr   argparser:   pathr*   r+   __file__parentabsoluterdflibr   rdflib.namespacer   r   r   r   rdflib.utilr	   r   r   r7   rC   __name__ArgumentParserparseradd_argumentset_defaults
parse_argsr-   rE   fmtprintexitparser/   r.   r   lenr2   cwdr?   r@   r!   r   r   <module>rf      sV       DN))224;;BBC D  5 5 $L
VN #F$0 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$//!dnndiiq r   