o
    mi?                     @  s  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	rFdd	lmZ d@ddZdAddZdBddZdCd!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/d0 ej!d1d2 e" Z#ee#j$Z%e%du re&d3 e'd4 e j(e#j$e%d5Z)ee#j* ee#j+ e&d6e#j+ d7e#j* d8 e&d9e,e) d: e&d; ee)e#j*Z-e . d<e#j+ d= Z/e&d>e/  ee/e#j*e-d4 e-d? e#j+e#j0 dS dS )Da/  
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                 C  s   |  ds	tdd S )N)/#z/The supplied namespace must end with '/' or '#')endswith
ValueError)r    r   `/home/kim/smarthome/.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_namespace2Tuple[List[Tuple[str, str]], List[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 }g }|D ]4}|d |d
}	|d dd}
|	 rgt|	sg|	d|	 d|
 d q?|	d|	 d|
 d q?|||fS )N)ZdctermsowlZrdfsZ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
        Zxxx)Z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  # 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nameZdescr   r   r   get_target_namespace_elementsJ   s*    
r9   output_file_namer   r5   Iterable[str]r6   	List[str]failboolc                 C  s  d| dt j   d}t| dh}|| |d |d| d |d |r6|d	 |d || t|d
krq|d |d |d |d |d || |d |d W d    d S W d    d S 1 s|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	
    """
wr'   z    _NS = Namespace("z")z

z    _fail = Truer   z"    # Valid non-python identifiersz    _extras = [z    ])datetimeutcnowopenwrite
writelineslen)r:   r   r5   r6   r   r=   headerfr   r   r   make_dn_filev   s4   	













"rH   __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actionrL   z	--no-failstore_false)rN   rO   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   r5   r;   r6   r<   r   r   r=   r>   r   r   )1__doc__
__future__r   argparser@   r/   pathlibr   typingr   r   r   r   Zrdflib.graphr   Zrdflib.namespacer	   r
   r   r   Zrdflib.utilr   Zrdflib.queryr   r   r   r9   rH   __name__ArgumentParserparseradd_argumentr   set_defaults
parse_argsargsrJ   fmtprintexitparser   r   r   rE   r3   cwdr:   r=   r   r   r   r   <module>   s    


'
,
)



