o
    i                     @  s  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 ejeeej jj d dlmZ d dlmZmZmZmZ d dlmZ erTd dlmZ d<ddZd=ddZd>ddZd?ddZ e!d kre" Z#e#j$d!ed"d# e#j$ded$d# e#j$ded%d# e#j$d&d'dd(d)d* e#j$d+dd,d- e#j%d.d/ e#& Z'ee'j(Z)e)du re*d0 e+d1 e j,e'j(e)d2Z-ee'j. ee'j/ e*d3e'j/ d4e'j. d5 e*d6e0e- d7 e*d8 ee-e'j.Z1e 2 d9e'j/ d: Z3e*d;e3  e e3e'j.e1d1 e'j/e'j4 dS dS )@    )annotationsN)Path)TYPE_CHECKINGIterableListTuple)Graph)DCTERMSOWLRDFSSKOS)guess_format)	ResultRow	namespacestrreturnNonec                 C  s   |  ds	tdd S )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)r    r   a/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/tools/defined_namespace_creator.pyvalidate_namespace   s   
r   	object_idc                 C  s   | D ]
}|  stdqd S )Nz5The supplied object_id must be an all-capitals string)isupperr   )r   cr   r   r   validate_object_id$   s
   r   gr   target_namespace'Tuple[List[Tuple[str, str]], List[str]]c           	      C  s   t tttd}dd|}g }| j||dD ]}tr!t|ts!J |	t
|d t
|d f q|jdd d	 g }|D ]}|d d
d}|	d|d |d d| d
 q=||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")
        }
        GROUP BY ?s
        xxx)initNsr      c                 S  s   | d S )Nr   r   )tupr   r   r   <lambda>g   s    z/get_target_namespace_elements.<locals>.<lambda>)key
 z     z: URIRef  # )r	   r
   r   r   replacequeryr   
isinstancer   appendr   sort)	r   r   
namespacesqelementsrelements_strsedescr   r   r   get_target_namespace_elementsK   s&    r:   output_file_namer   r7   Iterable[str]failboolc                 C  s   d| dt j   d}t| d2}|| |d |d| d |d |r6|d	 |d || W d    d S 1 sFw   Y  d S )
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)r;   r   r7   r   r=   headerfr   r   r   make_dn_files   s   	




"rG   __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actionrK   z	--no-failstore_false)rM   rN   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    )r;   r   r   r   r7   r<   r   r   r=   r>   r   r   )5
__future__r   argparser@   syspathlibr   typingr   r   r   r   pathr1   r   __file__parentabsoluterdflib.graphr   rdflib.namespacer	   r
   r   r   rdflib.utilr   rdflib.queryr   r   r   r:   rG   __name__ArgumentParserparseradd_argumentset_defaults
parse_argsargsrI   fmtprintexitparser   r   r   lenr5   cwdr;   r=   r   r   r   r   <module>   s    


'
(




