#!/usr/bin/python
"""
.. module:: owl2rdflib

owl2rdflib
******

:Description: owl2rdflib

    Generates a python file with a RDFlib Namespace object with the classes and attributes of a owl file

:Authors:
    bejar

:Version:

:Date:  16/12/2020
"""

__author__ = 'bejar'

import argparse

from rdflib import Graph, Namespace
from rdflib import RDFS, RDF, OWL, URIRef
import time


def chop(uriref):
    if '#' in uriref:
        return uriref.toPython().split("#")[-1]
    elif '/' in uriref:
        return uriref.toPython().split("/")[-1]
    else:
        return uriref


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', required=True, help='Input file')
    parser.add_argument('--output', default=None, help='Output file')
    parser.add_argument('--format', required=True, choices=['xml', 'turtle'], help='File format')

    args = parser.parse_args()
    out = args.output + '.py'

    g = Graph()

    g.parse(args.input, format=args.format)

    cls = [c for c in g.subjects(RDF.type, OWL.Class) if c != OWL.Thing]
    opr = [c for c in g.subjects(RDF.type, OWL.ObjectProperty)]
    dpr = [c for c in g.subjects(RDF.type, OWL.DatatypeProperty)]

    names = set()
    for v in cls + opr + dpr:
        if isinstance(v, URIRef):
            names.add(v)
    names = list(names)

    onto = [c for c in g.subjects(RDF.type, OWL.Ontology)]

    f = open(out, 'w')
    f.write("\"\"\"\n")
    f.write(f".. module:: {args.output}\n")
    f.write(f"\n Translated by owl2rflib\n")
    f.write(f"\n Translated to RDFlib from ontology {onto[0].toPython()}\n")
    f.write(f"\n :Date {time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())}\n")

    f.write("\"\"\"\n")
    f.write("from rdflib import URIRef\n")
    f.write("from rdflib.namespace import ClosedNamespace\n")

    f.write("\n")
    f.write(f"{args.output.upper()} =  ClosedNamespace(\n")
    f.write(f"    uri=URIRef('{onto[0].toPython()}'),\n")
    f.write(f"    terms=[\n")
    for n in names[:-1]:
        f.write(f"        '{chop(n)}',\n")
    f.write(f"        '{chop(names[-1])}'\n")
    f.write(f"    ]\n")
    f.write(f")\n")

    f.close()
