|  | 
| 1 | 1 | import json | 
| 2 |  | -import os | 
|  | 2 | + | 
|  | 3 | +# import os | 
| 3 | 4 | import re | 
| 4 | 5 | from uuid import UUID | 
| 5 | 6 | 
 | 
|  | 7 | +import mwclient | 
| 6 | 8 | from pyld import jsonld | 
| 7 | 9 | from rdflib import Graph | 
| 8 | 10 | 
 | 
| 9 | 11 | import osw.model.entity as model | 
|  | 12 | + | 
|  | 13 | +# from osw.auth import CredentialManager | 
| 10 | 14 | from osw.core import OSW | 
| 11 | 15 | from osw.wtsite import WtSite | 
| 12 | 16 | 
 | 
| 13 | 17 | # create/update the password file under examples/accounts.pwd.yaml | 
| 14 |  | -pwd_file_path = os.path.join( | 
| 15 |  | -    os.path.dirname(os.path.abspath(__file__)), "accounts.pwd.yaml" | 
| 16 |  | -) | 
| 17 |  | -wtsite = WtSite.from_domain("onto-wiki.eu", pwd_file_path) | 
|  | 18 | +# pwd_file_path = os.path.join( | 
|  | 19 | +#     os.path.dirname(os.path.abspath(__file__)), "accounts.pwd.yaml" | 
|  | 20 | +# ) | 
|  | 21 | +# cm = CredentialManager(cred_filepath=pwd_file_path) | 
|  | 22 | +# wtsite = WtSite(WtSite.WtSiteConfig( | 
|  | 23 | +#     iri="http://localhost:18081", | 
|  | 24 | +#     cred_mngr=cm | 
|  | 25 | +# )) | 
|  | 26 | + | 
|  | 27 | +# or use a hardocded login | 
|  | 28 | +site = mwclient.Site(scheme="http", host="localhost:18081", path="/w/") | 
|  | 29 | +site.login("Admin", "change_me123123") | 
|  | 30 | +wtsite = WtSite(WtSite.WtSiteLegacyConfig(site=site)) | 
|  | 31 | + | 
| 18 | 32 | osw = OSW(site=wtsite) | 
| 19 | 33 | 
 | 
| 20 |  | -# load the EmmoTerm schema => run this code only once | 
| 21 |  | -# osw.fetch_schema( | 
| 22 |  | -#    osw.FetchSchemaParam( | 
| 23 |  | -#        schema_title="Category:OSW57beed5e1294434ba77bb6516e461456", mode="replace" # EmmoTerm | 
| 24 |  | -#    ) | 
| 25 |  | -# ) | 
|  | 34 | + | 
|  | 35 | +# load the EmmoTerm schema => run the script a second time after the schema was loaded | 
|  | 36 | +try: | 
|  | 37 | +    model.EmmoTerm | 
|  | 38 | +except AttributeError: | 
|  | 39 | +    # name 'EmmoTerm' is not defined | 
|  | 40 | +    osw.fetch_schema( | 
|  | 41 | +        osw.FetchSchemaParam( | 
|  | 42 | +            schema_title="Category:OSW57beed5e1294434ba77bb6516e461456", | 
|  | 43 | +            mode="replace",  # EmmoTerm | 
|  | 44 | +        ) | 
|  | 45 | +    ) | 
| 26 | 46 | 
 | 
| 27 | 47 | # load the ontology | 
| 28 | 48 | g = Graph() | 
| 29 |  | -# g.parse("http://www.w3.org/People/Berners-Lee/card") | 
| 30 |  | -# g.parse("https://raw.githubusercontent.com/emmo-repo/domain-battery/master/battery.ttl", format="n3") | 
| 31 |  | -g.parse(r"BVCO_inferred.ttl") | 
|  | 49 | +g.parse( | 
|  | 50 | +    "https://raw.githubusercontent.com/emmo-repo/domain-battery/master/battery.ttl", | 
|  | 51 | +    format="n3", | 
|  | 52 | +) | 
|  | 53 | +# g.parse("https://github.com/Battery-Value-Chain-Ontology/ontology/releases/download/v0.3.0/BVCO_inferred.ttl", format="n3") | 
|  | 54 | +# g.parse(r"BVCO_inferred.ttl") | 
| 32 | 55 | 
 | 
| 33 | 56 | # convert to json-ld dict | 
| 34 | 57 | g = json.loads(g.serialize(format="json-ld", auto_compact=True)) | 
|  | 
| 41 | 64 |     "xsd": "http://www.w3.org/2001/XMLSchema#", | 
| 42 | 65 |     "skos": "http://www.w3.org/2004/02/skos/core#", | 
| 43 | 66 |     "dc": "http://purl.org/dc/terms/", | 
| 44 |  | -    # "emmo": "http://emmo.info/emmo#", #keep values with full uri | 
|  | 67 | +    "emmo": "http://emmo.info/emmo#",  # keep values with full uri | 
| 45 | 68 |     "uri": {"@id": "@id"}, | 
| 46 | 69 |     "rdf_type": {"@id": "@type"}, | 
| 47 | 70 |     # "label": "rdfs:label", | 
| 48 | 71 |     "label": {"@id": "skos:prefLabel"}, | 
| 49 | 72 |     "altLabel": {"@id": "skos:altLabel"}, | 
| 50 | 73 |     "text": {"@id": "@value"}, | 
| 51 | 74 |     "lang": {"@id": "@language"}, | 
| 52 |  | -    "subClassOf": {"@id": "rdfs:subClassOf", "@type": "@id"}, | 
|  | 75 | +    "subclass_of": {"@id": "rdfs:subClassOf", "@type": "@id"}, | 
| 53 | 76 |     "source": "dc:source", | 
| 54 | 77 |     "disjointUnionOf": "owl:disjointUnionOf", | 
| 55 | 78 |     "disjointWith": "owl:disjointWith", | 
|  | 
| 88 | 111 |     "altLabel", | 
| 89 | 112 |     "comment", | 
| 90 | 113 |     "description", | 
| 91 |  | -    "subClassOf", | 
|  | 114 | +    "subclass_of", | 
| 92 | 115 | ] | 
| 93 |  | -map_uuid_uri = [] | 
| 94 |  | -remove_unnamed = ["subClassOf"]  # , 'equivalentClass'] | 
|  | 116 | +map_uuid_uri = ["subclass_of"] | 
|  | 117 | +remove_unnamed = ["subclass_of"]  # , 'equivalentClass'] | 
| 95 | 118 | 
 | 
| 96 | 119 | # postprocess json-ld | 
| 97 | 120 | for node in compacted["@graph"]: | 
|  | 
| 110 | 133 |     for key in ensure_array: | 
| 111 | 134 |         if key in node and not isinstance(node[key], list): | 
| 112 | 135 |             node[key] = [node[key]] | 
|  | 136 | +    for key in remove_unnamed: | 
|  | 137 | +        if key in node: | 
|  | 138 | +            if isinstance(node[key], list): | 
|  | 139 | +                node[key] = [value for value in node[key] if not value.startswith("_:")] | 
|  | 140 | +            elif isinstance(node[key], str) and node[key].startswith("_:"): | 
|  | 141 | +                del node[key] | 
| 113 | 142 |     for key in map_uuid_uri: | 
| 114 | 143 |         if key in node: | 
| 115 | 144 |             if isinstance(node[key], list): | 
| 116 | 145 |                 for i, val in enumerate(node[key]): | 
| 117 | 146 |                     node[key][i] = "Category:OSW" + str( | 
| 118 | 147 |                         UUID(re.sub(r"[^A-Fa-f0-9]", "", node[key][i])[-32:]) | 
| 119 |  | -                    ) | 
|  | 148 | +                    ).replace("-", "") | 
| 120 | 149 |             if isinstance(node[key], str): | 
| 121 | 150 |                 node[key][i] = "Category:OSW" + str( | 
| 122 | 151 |                     UUID(re.sub(r"[^A-Fa-f0-9]", "", node[key][i])[-32:]) | 
| 123 |  | -                ) | 
| 124 |  | -    for key in remove_unnamed: | 
| 125 |  | -        if key in node: | 
| 126 |  | -            if isinstance(node[key], list): | 
| 127 |  | -                node[key] = [value for value in node[key] if not value.startswith("_:")] | 
| 128 |  | -            elif isinstance(node[key], str) and node[key].startswith("_:"): | 
| 129 |  | -                del node[key] | 
|  | 152 | +                ).replace("-", "") | 
| 130 | 153 | 
 | 
| 131 |  | -    if "rdf_type" in node and node["rdf_type"] == "owl:Class": | 
|  | 154 | +    if ( | 
|  | 155 | +        "rdf_type" in node | 
|  | 156 | +        and node["rdf_type"] == "owl:Class" | 
|  | 157 | +        and not node["uri"].startswith("_:") | 
|  | 158 | +    ): | 
| 132 | 159 |         node["uuid"] = str(UUID(re.sub(r"[^A-Fa-f0-9]", "", node["uri"])[-32:])) | 
| 133 | 160 | 
 | 
| 134 | 161 |         if "prefLabel" in node: | 
|  | 
| 205 | 232 | ) | 
| 206 | 233 | 
 | 
| 207 | 234 | ontologies = [emmo, battinfo, electrochemistry, periodictable, gpo, bvco] | 
|  | 235 | +# ontologies = [battinfo] | 
| 208 | 236 | 
 | 
| 209 | 237 | # import ontologies | 
| 210 | 238 | osw.import_ontology(OSW.ImportOntologyParam(ontologies=ontologies, entities=entities)) | 
0 commit comments