def sparqlontoparser_main():

    end_list = None
    voc_list = None
    propsofi = None

    try:

        #http://stackoverflow.com/questions/7165749/open-file-in-a-relative-location-in-python
        script_dir = os.path.dirname(__file__)

        end_list = csv2list(script_dir + '/data/input_data/Endpoint2Voc.csv')
        voc_list = csv2list(script_dir +
                            '/data/input_data/VocabularyLocation.csv')
        propsofi = csv2list(script_dir + '/data/input_data/PropertyHooks.csv')

        if end_list and voc_list and propsofi:

            if isinstance(end_list, list) and isinstance(
                    voc_list, list) and isinstance(propsofi, list):

                for end in end_list:

                    if isinstance(end, dict):

                        if "source_nick" in end.keys():

                            if isinstance(end["source_nick"], (str, unicode)):

                                if len(end["source_nick"]) > 0:

                                    if end["source_nick"][0] != "#":

                                        endpoint = None

                                        if "endpointORfilepath" in end.keys():

                                            if isinstance(
                                                    end["endpointORfilepath"],
                                                (str, unicode)):

                                                if len(end["endpointORfilepath"]
                                                       ) > 0:

                                                    if end["endpointORfilepath"][
                                                            0] != "#":

                                                        endpoint = end[
                                                            "endpointORfilepath"]

                                        if endpoint:

                                            source_type = None

                                            if "type" in end.keys():

                                                if isinstance(
                                                        end["type"],
                                                    (str, unicode)):

                                                    if len(end["type"]) > 0:

                                                        if end["type"][
                                                                0] != "#":

                                                            source_type = end[
                                                                "type"]

                                            apikey = None

                                            if "apikey" in end.keys():

                                                if isinstance(
                                                        end["apikey"],
                                                    (str, unicode)):

                                                    if len(end["apikey"]) > 0:

                                                        if end["apikey"][
                                                                0] != "#":

                                                            apikey = end[
                                                                "apikey"]

                                            if None in end.keys():

                                                v_list = None

                                                if isinstance(
                                                        end[None],
                                                    (str, unicode)):

                                                    if len(end[None]) > 0:

                                                        v_list = []
                                                        v_list.append(
                                                            end[None])

                                                elif isinstance(
                                                        end[None], list):

                                                    v_list = end[None]

                                                if v_list:

                                                    for v in v_list:

                                                        if isinstance(
                                                                v,
                                                            (str, unicode)):

                                                            if len(v) > 0:

                                                                if v[0] != "#":

                                                                    from_uri = None

                                                                    for voc in voc_list:

                                                                        if isinstance(
                                                                                voc,
                                                                                dict
                                                                        ):

                                                                            if "vocab_acronym" in voc.keys(
                                                                            ) and "vocab_uri" in voc.keys(
                                                                            ):

                                                                                if isinstance(
                                                                                        voc["vocab_acronym"],
                                                                                    (str,
                                                                                     unicode
                                                                                     )
                                                                                ) and isinstance(
                                                                                        voc["vocab_uri"],
                                                                                    (str,
                                                                                     unicode
                                                                                     )
                                                                                ):

                                                                                    if len(
                                                                                            voc["vocab_acronym"]
                                                                                    ) > 0 and len(
                                                                                            voc["vocab_uri"]
                                                                                    ) > 0:

                                                                                        if voc["vocab_acronym"][
                                                                                                0] != "#" and voc[
                                                                                                    "vocab_uri"][
                                                                                                        0] != "#":

                                                                                            if v == voc[
                                                                                                    "vocab_acronym"]:

                                                                                                from_uri = voc[
                                                                                                    "vocab_uri"]

                                                                    pids = None

                                                                    print "\n\n", "# " * 15

                                                                    print "[" + source_type + "]", endpoint, "\t", from_uri, "\t", apikey

                                                                    pids = obtainPropertyIDs(
                                                                        propsofi,
                                                                        endpoint,
                                                                        from_uri,
                                                                        apikey,
                                                                        source_type
                                                                    )

                                                                    if pids:

                                                                        container = None

                                                                        if source_type and source_type == "api":
                                                                            # API based
                                                                            pids_swk_wmeta = reformatpidsForAPIuse(
                                                                                pids,
                                                                                propsofi
                                                                            )

                                                                            container = apiGatherPropertySetValuesFromAllClasses(
                                                                                from_uri,
                                                                                apikey,
                                                                                pids_swk_wmeta
                                                                            )

                                                                    #     else:
                                                                    #         # SPARQL based
                                                                    #         container = sparqlGatherPropertySetvaluesFromAllClasses(endpoint, pids, from_uri, apikey, source_type)

                                                                        if container:

                                                                            if isinstance(
                                                                                    container,
                                                                                    list
                                                                            ):
                                                                                # num_item = min(2, len(container))
                                                                                # for item in range(num_item):
                                                                                #     print container[item]

                                                                                print "\n", len(
                                                                                    container
                                                                                )

                                                                                #write_to_solr(container, 'http://localhost:8983/solr/restest_032016/')
                                                                                #http://stackoverflow.com/questions/26197494/authenticating-connection-in-pysolr
                                                                                #http://superuser.com/questions/259481/reverse-scp-over-ssh-connection/259493#259493
                                                                                write_to_solr(
                                                                                    container,
                                                                                    'https://b2note.bsc.es/solr/b2note_testing/'
                                                                                )

                                                                                print "\n", "# " * 15

    except:

        print "Could not proceed with sparqlontoparser_main execution."
示例#2
0
def sparqlontoparser_main():

    end_list = None
    voc_list = None
    propsofi = None

    try:

        #http://stackoverflow.com/questions/7165749/open-file-in-a-relative-location-in-python
        script_dir = os.path.dirname(__file__)

        end_list = csv2list( script_dir + '/data/input_data/Endpoint2Voc.csv' )
        voc_list = csv2list( script_dir + '/data/input_data/VocabularyLocation.csv' )
        propsofi = csv2list( script_dir + '/data/input_data/PropertyHooks.csv' )

        if end_list and voc_list and propsofi:

            if isinstance(end_list, list) and isinstance(voc_list, list) and isinstance(propsofi, list):

                for end in end_list:

                    if isinstance(end, dict):

                        if "source_nick" in end.keys():

                            if isinstance(end["source_nick"], (str, unicode)):

                                if len(end["source_nick"])>0:

                                    if end["source_nick"][0] != "#":

                                        endpoint = None

                                        if "endpointORfilepath" in end.keys():

                                            if isinstance(end["endpointORfilepath"], (str, unicode)):

                                                if len(end["endpointORfilepath"])>0:

                                                    if end["endpointORfilepath"][0] != "#":

                                                        endpoint = end["endpointORfilepath"]

                                        if endpoint:

                                            source_type = None

                                            if "type" in end.keys():

                                                if isinstance(end["type"], (str, unicode)):

                                                    if len(end["type"])>0:

                                                        if end["type"][0] != "#":

                                                            source_type = end["type"]

                                            apikey = None

                                            if "apikey" in end.keys():

                                                if isinstance(end["apikey"], (str, unicode)):

                                                    if len(end["apikey"])>0:

                                                        if end["apikey"][0] != "#":

                                                            apikey = end["apikey"]

                                            if None in end.keys():

                                                v_list = None

                                                if isinstance(end[None], (str, unicode)):

                                                    if len(end[None])>0:

                                                        v_list = []
                                                        v_list.append( end[None] )

                                                elif isinstance(end[None], list):

                                                    v_list = end[None]

                                                if v_list:

                                                    for v in v_list:

                                                        if isinstance(v, (str, unicode)):

                                                            if len(v)>0:

                                                                if v[0] != "#":

                                                                    from_uri = None

                                                                    for voc in voc_list:

                                                                        if isinstance(voc, dict):

                                                                            if "vocab_acronym" in voc.keys() and "vocab_uri" in voc.keys():

                                                                                if isinstance(voc["vocab_acronym"], (str, unicode)) and isinstance(voc["vocab_uri"], (str, unicode)):

                                                                                    if len(voc["vocab_acronym"])>0 and len(voc["vocab_uri"])>0:

                                                                                        if voc["vocab_acronym"][0] != "#" and voc["vocab_uri"][0] != "#":

                                                                                            if v == voc["vocab_acronym"]:

                                                                                                from_uri = voc["vocab_uri"]

                                                                    pids = None

                                                                    print "\n\n", "# " * 15

                                                                    print "[" + source_type + "]", endpoint, "\t", from_uri, "\t", apikey

                                                                    pids = obtainPropertyIDs( propsofi, endpoint, from_uri, apikey, source_type )

                                                                    if pids:

                                                                        container = None

                                                                        if source_type and source_type == "api":
                                                                            # API based
                                                                            pids_swk_wmeta  = reformatpidsForAPIuse(pids, propsofi)

                                                                            container = apiGatherPropertySetValuesFromAllClasses(from_uri, apikey, pids_swk_wmeta)

                                                                    #     else:
                                                                    #         # SPARQL based
                                                                    #         container = sparqlGatherPropertySetvaluesFromAllClasses(endpoint, pids, from_uri, apikey, source_type)

                                                                        if container:

                                                                            if isinstance(container, list):
                                                                                # num_item = min(2, len(container))
                                                                                # for item in range(num_item):
                                                                                #     print container[item]

                                                                                print "\n", len(container)

                                                                                #write_to_solr(container, 'http://localhost:8983/solr/restest_032016/')
                                                                                #http://stackoverflow.com/questions/26197494/authenticating-connection-in-pysolr
                                                                                #http://superuser.com/questions/259481/reverse-scp-over-ssh-connection/259493#259493
                                                                                write_to_solr(container, 'https://opseudat03.bsc.es:8983/solr/b2note_testing/')

                                                                                print "\n", "# " * 15

    except:

        print "Could not proceed with sparqlontoparser_main execution."
#below is a basic worflow to create the dictionary
# required to populate the solr index using pysolr
if __name__ == '__main__':

    if sys.argv[1:]:
        #if main method called get the ontology as parameter
        ontology_location = sys.argv[1:]
        print "Starting ontology population script..."

        #new ontology manager class
        manager = OntologyManager()
        #set uri properties for annotations you want to include such as synonym and label
        manager.synonym_uri = "http://www.vehicles/synonym"

        location = ontology_location[0]

        graph = manager.load_ontology(location)
        #or load locally if parameter has not been passed using:
        #graph = load_ontology('/Users/seppblatter/Desktop/fifa_expenses.owl')
        print "Ontology loading complete"
        #create the dictionary and store in a container
        # which is a list of OntologyClass objects
        container = manager.add_all_to_dictionary(graph=graph)
        #write this to solr using the solr_writer python script
        #pass the container and the location of the solr index
        write_to_solr(container, 'http://localhost:8983/solr/b2note_index/')
    else:
        print "no ontology to load - please specify a file or URL as ontology location"