summaryrefslogtreecommitdiff
path: root/utils/ontology/service2rdf-xml.py
blob: c385f74c225fe269cccbfa415d3f3c6e0ea3a256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#
# Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import configparser, os
import sys
import getopt

def usage():
  print("Usage: python service2rdf-xml.py --metadata=ONTOLOGY.metadata --service=ONTOLOGY.service [--uri=URI]")

def main():
  try:
     uri = ""
     metadataf = ""
     servicef = ""

     opts, args = getopt.getopt(sys.argv[1:], "hu:va:vm:vs:v", ["help", "uri=", "metadata=", "service="])

     for o, a in opts:
         if o in ("-u", "--uri"):
           uri = a
         elif o in ("-m", "--metadata"):
           metadataf = a
         elif o in ("-s", "--service"):
           servicef = a
         elif o in ("-h", "--help"):
           usage ()
           sys.exit()

     if uri == "":
       uri = "http://live.gnome.org/Tracker/XMLSchema"

     if metadataf == "" or servicef == "":
       usage ()
       sys.exit ()

     service = configparser.ConfigParser()
     service.readfp(open(servicef))

     metadata = configparser.ConfigParser()
     metadata.readfp(open(metadataf))

     print("<rdf:RDF")
     print("  xmlns:nid3=\"http://tracker.api.gnome.org/ontology/v3/nid3#\"")
     print("  xmlns:nfo=\"http://tracker.api.gnome.org/ontology/v3/nfo#\"")
     print("  xmlns:nmo=\"http://tracker.api.gnome.org/ontology/v3/nmo#\"")
     print("  xmlns:nie=\"http://tracker.api.gnome.org/ontology/v3/nie#\"")
     print("  xmlns:exif=\"http://www.kanzaki.com/ns/exif#\"")
     print("  xmlns:nao=\"http://tracker.api.gnome.org/ontology/v3/nao#\"")
     print("  xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"")
     print("  xmlns:protege=\"http://protege.stanford.edu/system#\"")
     print("  xmlns:dcterms=\"http://purl.org/dc/terms/\"")
     print("  xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"")
     print("  xmlns:ncal=\"http://tracker.api.gnome.org/ontology/v3/ncal#\"")
     print("  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"")
     print("  xmlns:nrl=\"http://tracker.api.gnome.org/ontology/v3/nrl#\"")
     print("  xmlns:pimo=\"http://www.semanticdesktop.org/ontologies/2007/11/01/pimo#\"")
     print("  xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"")
     print("  xmlns:tmo=\"http://www.semanticdesktop.org/ontologies/2008/05/20/tmo#\"")
     print("  xmlns:dc=\"http://purl.org/dc/elements/1.1/\"")
     print("  xmlns:nco=\"http://tracker.api.gnome.org/ontology/v3/nco#\"")
     print("  xmlns:nexif=\"http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#\">")

     print("")

     for klass in service.sections():
        splitted = klass.split (":")
        print("\t<rdfs:Class rdf:about=\"" + uri + "/" + splitted[0] + "#" + splitted[1] + "\">")
        print("\t\t<rdfs:label>" + splitted[1] + "</rdfs:label>")

        for name, value in service.items (klass):
           if name == "SuperClasses":
             vsplit = value.split (";")
             for val in vsplit:
               vvsplit = val.split (":");
               print("\t\t<rdfs:subClassOf>")
               print("\t\t\t<rdfs:Class rdf:about=\"" + uri + "/" +vvsplit[0] + "#" + vvsplit[1] + "\"/>")
               print("\t\t</rdfs:subClassOf>")
        print("\t</rdfs:Class>")

     for mdata in metadata.sections():
        splitted = mdata.split (":")
        print("\t<rdf:Property rdf:about=\"" + uri + "#" + splitted[1] + "\">")
        print("\t\t<rdfs:label>" + splitted[1] + "</rdfs:label>")

        for name, value in metadata.items (mdata):
           if name == "datatype":
             print("\t\t<rdfs:range rdf:resource=\"" + uri + "#" + value + "\"/>")

           if name == "domain":
            vvsplit = value.split (":")
            print("\t\t<rdfs:domain rdf:resource=\"" + uri + "/" +vvsplit[0] + "#" + vvsplit[1] + "\"/>")

           if name == "parent":
            print("\t\t<rdfs:subPropertyOf rdf:resource=\"" + uri + "#" + value.split (":")[1] + "\"/>")

           if name == "weight":
            print("\t\t<rdfs:comment>Weight is " + value + "</rdfs:comment>")

        print("\t</rdf:Property>")

     print("</rdf:RDF>")
  except getopt.GetoptError as err:
     print(str(err))
     usage ()
     sys.exit(2)

if __name__ == "__main__":
    main()