-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspar.py
More file actions
276 lines (243 loc) · 9.67 KB
/
spar.py
File metadata and controls
276 lines (243 loc) · 9.67 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'essepuntato'
import web
import os
from hashformat import process_hashformat
from ldd import LinkedDataDirector
from operator import itemgetter
from wl import WebLogger
from oh import OntologyHandler
import re
# Vars
base_path = "spar" + os.sep
ontology_base_path = base_path + os.sep + "ontology_descriptions" + os.sep
documentation_base_path = base_path + os.sep + "ontology_documentations" + os.sep
example_base_path = base_path + os.sep + "ontology_examples" + os.sep
news_base_path = base_path + os.sep + "ontology_news" + os.sep
publications_base_path = base_path + os.sep + "ontology_publications" + os.sep
publication_list_path = publications_base_path + "publication_list.txt"
citing_paper_list_path = publications_base_path + "citing_list.txt"
projects_list_path = publications_base_path + "projects.txt"
acronyms_path = publications_base_path + "acronyms.txt"
mediatype_base_path = "mediatype" + os.sep
mediatype_base_url = "https://w3id.org/spar/mediatype/"
ontologies_base_url = "http://www.sparontologies.net/ontologies/"
# ontologies_base_url = "http://localhost:8181/ontologies/"
# mediatype_base_url = "http://localhost:8080/mediatype/"
tmp_dir_for_copying_rdf = os.path.expanduser("~")
src_fragment = "/source"
# For redirecting
urls = (
"/", "Home",
"/static/(.*)", "Static",
"/robots.txt", "Robots",
"/ontologies/?(.*)", "Ontologies",
"/examples/?", "Examples",
"/publications/?", "Publications",
"/uptake/?", "Uptake",
"/contacts/?", "Contacts",
"/about/?", "About",
"/news/?", "News",
"/mediatype/(.+)", "MediaType",
"/mediatype/?", "MediaType"
)
# For rendering
render = web.template.render(base_path)
# Page URLs and their name
pages = [
{
"url": "/ontologies",
"title": "Ontologies"
},
{
"url": "/examples",
"title": "Examples"
},
{
"url": "/publications",
"title": "Publications"
},
{
"url": "/uptake",
"title": "Uptake"
},
{
"url": "/contacts",
"title": "Contacts"
},
{
"url": "/about",
"title": "About"
},
{
"url": "/news",
"title": "News"
}
]
web_logger = WebLogger("sparontologies.net", "sparontologies_log.txt", [
"REMOTE_ADDR", # The IP address of the visitor
"HTTP_USER_AGENT", # The browser type of the visitor
"HTTP_REFERER", # The URL of the page that called your program
"HTTP_HOST", # The hostname of the page being attempted
"REQUEST_URI" # The interpreted pathname of the requested document
# or CGI (relative to the document root)
],
{"REMOTE_ADDR": ["212.47.249.17"]} # uncomment this for real app
# {"REMOTE_ADDR": ["127.0.0.1"]} # uncomment this for test
)
# Local web application
app = web.application(urls, globals())
# Gunicorn WSGI application
application = app.wsgifunc()
class Home:
def GET(self):
web_logger.mes()
return render.home("SPAR Ontologies - Home", pages)
class Static:
def GET(self, name):
"""Serve static files"""
static_dir = "static"
file_path = os.path.join(static_dir, name)
if not os.path.exists(file_path):
raise web.notfound()
# Content types
ext = os.path.splitext(name)[1]
content_types = {
'.css': 'text/css',
'.js': 'application/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
}
web.header('Content-Type', content_types.get(ext, 'application/octet-stream'))
with open(file_path, 'rb') as f:
return f.read()
class Robots:
def GET(self):
raise web.seeother('/static/robots.txt')
class Publications:
def GET(self):
spar_acronym = "spar"
spar_title = "Semantic Publishing and Referencing (SPAR) Ontologies"
acronyms_dict = process_hashformat(acronyms_path)[0]
acron_list = sorted(acronyms_dict.keys())
acron_list.insert(0, "spar")
acronyms_dict[spar_acronym] = spar_title
web_logger.mes()
return render.publications(
"SPAR Ontologies - Publications", pages,
acron_list,
acronyms_dict,
process_hashformat(publication_list_path))
class Uptake:
def GET(self):
web_logger.mes()
return render.uptake(
"SPAR Ontologies - Uptake", pages,
sorted(process_hashformat(projects_list_path), key=itemgetter('name')),
sorted(process_hashformat(citing_paper_list_path), key=itemgetter('title')))
class Contacts:
def GET(self):
web_logger.mes()
return render.contacts("SPAR Ontologies - Contacts", pages)
class News:
def GET(self):
all_news = []
for cur_dir, cur_subdir, cur_files in os.walk(news_base_path, topdown=True):
for cur_file in cur_files:
if cur_file.endswith(".txt"):
cur_news_list = process_hashformat(cur_dir + cur_file)
all_news += cur_news_list
web_logger.mes()
return render.news("SPAR Ontologies - News", pages, all_news)
class About:
def GET(self):
web_logger.mes()
return render.about("SPAR Ontologies - About", pages)
class Examples:
def GET(self):
all_examples = []
for cur_dir, cur_subdir, cur_files in os.walk(example_base_path):
for cur_file in cur_files:
if cur_file.endswith(".txt"):
cur_example_list = process_hashformat(cur_dir + cur_file)
all_examples += cur_example_list
web_logger.mes()
return render.examples("SPAR Ontologies - Examples", pages, all_examples)
class Ontologies:
def GET(self, onto_acronym):
if onto_acronym is not None and onto_acronym.strip() != "":
if re.search("%s(\.xml|\.ttl|\.json|\.html|\.nt)?" % src_fragment, onto_acronym):
web.seeother('https://w3id.org/spar/' + onto_acronym.replace(src_fragment, ""))
else:
ontology_path = ontology_base_path + onto_acronym + ".txt"
example_path = example_base_path + onto_acronym + ".txt"
if os.path.exists(ontology_path) and os.path.exists(example_path):
cur_ontology_dict = process_hashformat(ontology_path)[0]
cur_example_list = process_hashformat(example_path)
# handle empty DOI
doi_value = cur_ontology_dict["doi"]
if doi_value.lower() == "not defined":
doi_value = "It will be soon available"
web_logger.mes()
return render.ontology("SPAR Ontologies - ", pages,
cur_ontology_dict["name"],
cur_ontology_dict["acronym"],
cur_ontology_dict["url"],
doi_value,
cur_ontology_dict["documentation"],
cur_ontology_dict["repository"],
cur_ontology_dict["description"],
cur_example_list,
process_hashformat(publication_list_path),
onto_acronym)
else:
raise web.notfound()
else: # Load home
web_logger.mes()
return render.ontologies(
"SPAR Ontologies - Ontologies", pages,
process_hashformat(publication_list_path),
"spar")
class MediaType:
def GET(self, file_path=None):
director = LinkedDataDirector(
mediatype_base_path, mediatype_base_url,
label_conf={
"http://purl.org/spar/pso/holdsStatusInTime": "status in time",
"http://purl.org/spar/cito/isDescribedBy": "described",
"http://purl.org/spar/cito/isDocumentedBy": "rfc",
"http://purl.org/dc/terms/relation": "related",
"http://purl.org/dc/terms/MediaType": "media type",
"http://purl.org/dc/terms/contributor": "contributor",
"http://xmlns.com/foaf/0.1/Agent": "agent",
"http://xmlns.com/foaf/0.1/name": "name",
"http://xmlns.com/foaf/0.1/homepage": "homepage",
"http://purl.org/spar/pso/withStatus": "status",
"http://purl.org/spar/pso/Status": "status",
"http://purl.org/spar/pso/StatusInTime": "status in time",
"http://purl.org/dc/terms/title": "title",
"http://purl.org/spar/fabio/Expression": "expression",
"http://purl.org/dc/terms/license": "license",
"http://creativecommons.org/licenses/by/4.0/":
"Creative Commons Attribution 4.0 International License (CC BY 4.0)"},
tmp_dir=tmp_dir_for_copying_rdf)
if file_path is None:
web_logger.mes()
return director.get_render().index()
else:
cur_page = director.redirect(file_path)
if cur_page is None:
raise web.notfound()
else:
web_logger.mes()
return cur_page
if __name__ == "__main__":
app.run()