Skip to content

Commit 7984438

Browse files
author
Thomas Hanke
committed
bit of cleanup
1 parent 024cc85 commit 7984438

3 files changed

Lines changed: 49 additions & 70 deletions

File tree

app.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,16 @@ async def create_mapper(request: Request):
158158
request.session["data_url"] = data_url
159159

160160
# Use method_url from form or use placeholder
161-
if start_form.method_url.data:
162-
method_url = start_form.method_url.data
161+
if start_form.template_url.data:
162+
template_url = start_form.template_url.data
163163
else:
164-
method_url = start_form.method_url.render_kw["placeholder"]
164+
template_url = start_form.template_url.render_kw["placeholder"]
165165
flash(
166166
request,
167-
"URL Method File empty: using placeholder value for demonstration",
167+
"URL Template File empty: using placeholder value for demonstration",
168168
"info",
169169
)
170-
request.session["method_url"] = method_url
170+
request.session["template_url"] = template_url
171171
request.session["use_template_rowwise"] = start_form.use_template_rowwise.data
172172
# entrys from advanced form - parse comma-separated values
173173
data_subject_types_str = start_form.advanced.data_subject_types.data or ""
@@ -179,22 +179,22 @@ async def create_mapper(request: Request):
179179
mapping_predicate_uri = start_form.advanced.mapping_predicate_uri.data
180180
request.session["mapping_predicate_uri"] = mapping_predicate_uri
181181

182-
method_object_types_str = start_form.advanced.method_object_types.data or ""
182+
template_object_types_str = start_form.advanced.template_object_types.data or ""
183183
mapping_object_types = [
184-
uri.strip() for uri in method_object_types_str.split(",") if uri.strip()
184+
uri.strip() for uri in template_object_types_str.split(",") if uri.strip()
185185
]
186186
request.session["mapping_object_types"] = mapping_object_types
187187

188188
try:
189189
mapper = maptomethod.Mapper(
190190
data_url=data_url,
191-
method_url=method_url,
191+
template_url=template_url,
192192
use_template_rowwise=request.session["use_template_rowwise"],
193193
mapping_predicate_uri=URIRef(mapping_predicate_uri),
194194
data_subject_types=[
195195
URIRef(uri) for uri in mapping_subject_types
196196
],
197-
method_object_types=[
197+
template_object_types=[
198198
URIRef(uri) for uri in mapping_object_types
199199
],
200200
authorization=authorization,
@@ -234,7 +234,7 @@ async def create_mapper(request: Request):
234234

235235
# Populate form fields from session to ensure badges persist
236236
start_form.advanced.data_subject_types.data = ",".join(mapping_subject_types)
237-
start_form.advanced.method_object_types.data = ",".join(mapping_object_types)
237+
start_form.advanced.template_object_types.data = ",".join(mapping_object_types)
238238
start_form.advanced.mapping_predicate_uri.data = mapping_predicate_uri
239239

240240
request.session["auth"] = authorization
@@ -265,7 +265,7 @@ async def map(request: Request):
265265
# logging.debug(request.data)
266266
formdata = await request.form()
267267
data_url = request.session.get("data_url", None)
268-
method_url = request.session.get("method_url", None)
268+
template_url = request.session.get("template_url", None)
269269
use_template_rowwise = request.session.get("use_template_rowwise", False)
270270
mapping_subject_types = request.session.get("mapping_subject_types", [])
271271
mapping_predicate_uri = request.session.get("mapping_predicate_uri", None)
@@ -279,18 +279,18 @@ async def map(request: Request):
279279
authorization
280280
)
281281
objects, _ = maptomethod.query_entities(
282-
method_url,
282+
template_url,
283283
[URIRef(uri) for uri in mapping_object_types],
284284
authorization
285285
)
286286

287287
# Create form and populate with session data
288288
start_form = forms.StartForm(
289-
request, data_url=data_url, method_url=method_url
289+
request, data_url=data_url, template_url=template_url
290290
)
291291
# Populate advanced fields with session data
292292
start_form.advanced.data_subject_types.data = ",".join(mapping_subject_types)
293-
start_form.advanced.method_object_types.data = ",".join(mapping_object_types)
293+
start_form.advanced.template_object_types.data = ",".join(mapping_object_types)
294294
start_form.advanced.mapping_predicate_uri.data = mapping_predicate_uri
295295
# entrys from advanced form
296296

@@ -308,13 +308,13 @@ async def map(request: Request):
308308
request.session["maplist"] = maplist
309309
with maptomethod.Mapper(
310310
data_url=data_url,
311-
method_url=method_url,
311+
template_url=template_url,
312312
use_template_rowwise=use_template_rowwise,
313313
mapping_predicate_uri=URIRef(mapping_predicate_uri),
314314
data_subject_types=[
315315
URIRef(uri) for uri in mapping_subject_types
316316
],
317-
method_object_types=[
317+
template_object_types=[
318318
URIRef(uri) for uri in mapping_object_types
319319
],
320320
maplist=maplist,
@@ -445,13 +445,13 @@ class Config:
445445
json_schema_extra = {
446446
"example": {
447447
"data_url": "https://github.com/Mat-O-Lab/CSVToCSVW/raw/main/examples/example-metadata.json",
448-
"method_url": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl",
448+
"template_url": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl",
449449
"data_super_classes": [
450450
maptomethod.OA.Annotation,
451451
maptomethod.CSVW.Column,
452452
],
453453
"predicate": maptomethod.ContentToBearingRelation,
454-
"method_super_classes": [
454+
"template_super_classes": [
455455
maptomethod.InformtionContentEntity,
456456
maptomethod.TemporalRegionClass,
457457
],
@@ -477,7 +477,7 @@ def mapping(request: MappingRequest, req: Request) -> StreamingResponse:
477477
result = maptomethod.Mapper(
478478
str(request.data_url),
479479
str(request.template_url),
480-
method_object_types=[
480+
template_object_types=[
481481
URIRef(str(uri)) for uri in request.template_types
482482
],
483483
mapping_predicate_uri=URIRef(str(request.predicate)),

examples/request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"data_url": "https://github.com/Mat-O-Lab/CSVToCSVW/raw/main/examples/example-metadata.json",
3-
"method_url": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl",
3+
"template_url": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl",
44
"map": {
55
"SpecimenID": "AktuelleProbe0",
66
"WidthMeasurement": "ProbenbreiteB03",

maptomethod.py

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,7 @@ def get_all_sub_classes(superclass: URIRef, authorization=None) -> List[URIRef]:
187187
return classes
188188

189189

190-
def get_methods() -> Dict:
191-
"""Get all ttl filenames and URLs in the MSEO methods folder.
192-
193-
Returns:
194-
Dict: Dict with method name aas key and url to file as value
195-
"""
196-
mseo_repo = github.Github().get_repo("Mat-O-Lab/MSEO")
197-
folder_index = mseo_repo.get_contents("methods")
198-
# print(folder_index)
199-
if folder_index:
200-
methods_urls = [
201-
method.download_url
202-
for method in folder_index
203-
if method.download_url and method.download_url.endswith("ttl")
204-
]
205-
methods = {
206-
re_search("[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))", url)[0].split(".")[0]: url
207-
for url in methods_urls
208-
}
209-
else:
210-
methods = {}
211-
logging.info("Following methods are available as select: {}".format(methods))
212-
return methods
190+
# Removed get_methods() function - templates are now provided directly by users
213191

214192

215193
# InformtionContentEntity = CCO.InformationContentEntity
@@ -222,9 +200,9 @@ class Mapper:
222200
def __init__(
223201
self,
224202
data_url: str,
225-
method_url: str,
203+
template_url: str,
226204
use_template_rowwise: bool,
227-
method_object_types: List[URIRef] = [
205+
template_object_types: List[URIRef] = [
228206
InformtionContentEntity,
229207
TemporalRegionClass,
230208
],
@@ -235,37 +213,38 @@ def __init__(
235213
maplist: List[Tuple[str, str]] = [],
236214
authorization=None,
237215
):
238-
"""Mapper Class for creating Rule based yarrrml mappings for data metadata to link to a knowledge graph.
216+
"""Mapper Class for creating Rule based yarrrml mappings for data metadata to link to a template knowledge graph.
239217
240218
Args:
241-
data_url (AnyUrl): Url to metadata describing the data to link
242-
method_url (AnyUrl): Url to knowledgegraph describing context to link data to.
243-
method_subject_super_class_uris (List[URIRef], optional): List of rdflib URIRef objects defining classes to query for as subjects of the mapping rules. Defaults to [InformtionContentEntity,TemporalRegionClass].
219+
data_url (str): URL to metadata describing the data to link
220+
template_url (str): URL to template knowledge graph describing context to link data to
221+
use_template_rowwise (bool): Whether to duplicate the template for each data row
222+
template_object_types (List[URIRef], optional): List of URIRef objects defining classes to query for as objects in the template graph. Defaults to [InformtionContentEntity,TemporalRegionClass].
244223
mapping_predicate_uri (URIRef, optional): Object property to use as predicate to link. Defaults to ContentToBearingRelation.
245-
data_object_super_class_uris (List[URIRef], optional): List of rdflib URIRef objects defining classes to query for as objects of the mapping rules. Defaults to [OA.Annotation,CSVW.Column].
246-
subjects (List[URIRef], optional): List of rdflib URIRef objects which are individuals in the data metadata. Defaults to [].
247-
objects (List[URIRef], optional): List of rdflib URIRef objects which are individuals in the knowledge grph. Defaults to [].
248-
maplist (List[Tuple[str, str]], optional): List of pairs of individual name of objects in knowledge graph and labels of indivuals in data metadata to create mapping rules for. Defaults to [].
249-
authorization (str): Json strint to use as Authorization Header on requests to external urls.
224+
data_subject_types (List[URIRef], optional): List of URIRef objects defining classes to query for as subjects in the data metadata. Defaults to [OA.Annotation,CSVW.Column].
225+
subjects (dict, optional): Dict of subject individuals from data metadata. Defaults to [].
226+
objects (dict, optional): Dict of object individuals from template knowledge graph. Defaults to [].
227+
maplist (List[Tuple[str, str]], optional): List of pairs mapping object names in template to subject IDs in data. Defaults to [].
228+
authorization (str, optional): Authorization Header value for requests to external URLs.
250229
"""
251230
logging.info(
252231
"Following Namespaces available to Mapper: {}".format(ontologies.keys())
253232
)
254233
self.data_url = data_url
255-
self.method_url = method_url
234+
self.template_url = template_url
256235
self.use_template_rowwise = use_template_rowwise
257236
self.mapping_predicate_uri = mapping_predicate_uri
258237
self.authorization = authorization
259238
logging.debug("checking objects and subjects populated")
260239
# file_data, file_name =open_file(data_url)
261240
if not objects:
262241
self.objects, base_ns_objects = query_entities(
263-
self.method_url, method_object_types, self.authorization
242+
self.template_url, template_object_types, self.authorization
264243
)
265244
self.base_ns_objects = base_ns_objects
266245
else:
267246
self.objects = objects
268-
self.base_ns_objects = self.method_url + "/"
247+
self.base_ns_objects = self.template_url + "/"
269248
logging.debug("namespace objects: " + self.base_ns_objects)
270249

271250
if not subjects:
@@ -280,7 +259,7 @@ def __init__(
280259

281260
self.maplist = maplist
282261

283-
# methods for context managers
262+
# templates for context managers
284263
def __enter__(self):
285264
return self
286265

@@ -293,7 +272,7 @@ def __str__(self) -> str:
293272
Returns:
294273
str: String representation
295274
"""
296-
return f"Mapper: {self.data_url} {self.method_url}"
275+
return f"Mapper: {self.data_url} {self.template_url}"
297276

298277
def to_yaml(self) -> dict:
299278
"""Return filename and yarrrml yaml file content
@@ -507,29 +486,29 @@ def get_mapping_output(
507486
data_url: str,
508487
use_template_rowwise: bool,
509488
data_ns: str,
510-
method_ns: str,
489+
template_ns: str,
511490
map_list: List,
512491
subjects_dict: dict,
513492
mapping_predicate_uri: URIRef,
514493
authorization=None,
515494
) -> dict:
516-
"""Get yaml definging the yarrrml mapping rules.
495+
"""Generate YARRRML mapping rules linking data to template.
517496
518497
Args:
519-
data_url (AnyUrl): Url to data metadata to use
520-
data_ns (AnyUrl): Namespace of the data entities to map
521-
method_ns (AnyUrl): Namespace of the method entities to relate to
522-
method_url (AnyUrl): Url to knowledge graph to use
523-
map_list (List): List of pairs of individual name of objects in knowledge graph and labels of indivuals in data metadata to create mapping rules for.
524-
subjects_dict (dict): Dict of subjects to create mapping rules for with short entity IRI as key
498+
data_url (str): URL to data metadata to use
499+
use_template_rowwise (bool): Whether to duplicate template for each row
500+
data_ns (str): Namespace of the data entities to map
501+
template_ns (str): Namespace of the template entities to relate to
502+
map_list (List): List of pairs mapping template object names to data subject IDs
503+
subjects_dict (dict): Dict of data subjects with short entity IRI as key
525504
mapping_predicate_uri (URIRef): Object property to use as predicate to link
526-
authorization: Authorization header for HTTP requests
505+
authorization (str, optional): Authorization header for HTTP requests
527506
528507
Returns:
529-
dict: Dict with key filename with value the sugested mapping filenaem and filedata with value the string content of the generated yarrrml yaml file.
508+
dict: Dict with 'filename' (suggested mapping filename) and 'filedata' (YARRRML yaml content)
530509
"""
531510
g = Graph(bind_namespaces="core")
532-
g.bind("template", Namespace(method_ns))
511+
g.bind("template", Namespace(template_ns))
533512
g.bind("data", Namespace(data_ns))
534513
g.bind("bfo", BFO)
535514
g.bind("csvw", CSVW)

0 commit comments

Comments
 (0)