-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
748 lines (611 loc) · 29.9 KB
/
Copy pathviews.py
File metadata and controls
748 lines (611 loc) · 29.9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
import datetime
import json
import hashlib
import traceback
import html
from .models import ArcUser
from .utilities import _get_arc_context
from rest_framework.decorators import api_view
from manager.utilities import _get_context, get_scholar_corpus, _contains, _clean
from django.shortcuts import render, HttpResponse, redirect
from django.http import Http404
from django.utils.text import slugify
from elasticsearch_dsl import A
from bson.objectid import ObjectId
@api_view(['GET'])
def query(request, corpus_id):
context = _get_context(request)
content = {}
corpus, role = get_scholar_corpus(corpus_id, context['scholar'])
if corpus and 'ArcArtifact' in corpus.content_types:
aggs = {}
aggs['ArcFederation'] = A('nested', path='federations')
aggs['ArcFederation'].bucket('names', 'terms', size=10000, field='federations.id')
aggs['ArchiveParent'] = A('nested', path='archive')
aggs['ArchiveParent'].bucket('names', 'terms', size=10000, field='archive.parent_path')
aggs['ArcArchive'] = A('nested', path='archive')
aggs['ArcArchive'].bucket('names', 'terms', size=10000, field='archive.id')
aggs['ArcType'] = A('nested', path='types')
aggs['ArcType'].bucket('names', 'terms', size=10000, field='types.id')
aggs['ArcGenre'] = A('nested', path='genres')
aggs['ArcGenre'].bucket('names', 'terms', size=10000, field='genres.id')
aggs['ArcDiscipline'] = A('nested', path='disciplines')
aggs['ArcDiscipline'].bucket('names', 'terms', size=10000, field='disciplines.id')
aggs['decades'] = A('histogram', field='years', interval=10)
if context['search']:
context['search']['aggregations'] = aggs
content = corpus.search_content(content_type='ArcArtifact', excludes=['full_text_contents'], **context['search'])
else:
content = corpus.search_content(content_type='ArcArtifact', excludes=['full_text_contents'], aggregations=aggs, general_query="*")
else:
raise Http404("You are not authorized to access this endpoint.")
return HttpResponse(
json.dumps(content),
content_type='application/json'
)
@api_view(['GET', 'POST'])
def api_arc_user_auth(request, corpus_id, arc_username):
context, corpus = _get_arc_context(request, corpus_id)
pwd = _clean(request.POST, 'password', None)
response = {
'message': 'invalid username or password',
'user_auth_token': ''
}
if request.method == 'POST' and corpus and arc_username and pwd:
try:
arc_user = ArcUser.objects.get(username=arc_username)
except:
arc_user = None
if _contains(request.POST, ['register', 'password', 'email', 'fullname']):
if arc_user:
response['message'] = 'Username already exists!'
arc_user = None
else:
email = _clean(request.POST, 'email')
fullname = _clean(request.POST, 'fullname')
arc_user = ArcUser()
arc_user.username = arc_username
hash_obj = hashlib.sha1(str.encode(pwd))
phash = hash_obj.hexdigest()
arc_user.password_hash = phash
arc_user.email = email
arc_user.fullname = fullname
arc_user.save()
if arc_user:
hash_obj = hashlib.sha1(str.encode(pwd))
phash = hash_obj.hexdigest()
if phash == arc_user.password_hash:
response['user_auth_token'] = str(ObjectId())
cache_key = 'arc_user_cache_{0}'.format(response['user_auth_token'])
cache_value = {
'ip': request.META.get('HTTP_X_REAL_IP', '0.0.0.0'),
'userid': arc_user.userid,
'username': arc_username,
'fullname': arc_user.fullname,
'email': arc_user.email,
'institution': arc_user.institution,
'link': arc_user.link,
'about': arc_user.about_me
}
corpus.redis_cache.set(cache_key, json.dumps(cache_value), ex=3600)
arc_user.last_login = datetime.datetime.now()
arc_user.save()
response['message'] = 'user authenticated'
else:
response['message'] = ''
elif request.method == 'GET' and 'arc-user-token' in request.GET and 'arc_user' in context and 'logout' in request.GET:
corpus.redis_cache.delete(f"arc_user_cache_{request.GET['arc-user-token']}")
response['message'] = 'user logged out'
return HttpResponse(
json.dumps(response),
content_type='application/json'
)
@api_view(['GET'])
def api_arc_user_info(request, corpus_id):
context, corpus = _get_arc_context(request, corpus_id)
response = {
'message': 'invalid user token',
'info': {}
}
if corpus and 'arc_user' in context:
response['info'] = context['arc_user']
response['message'] = 'basic info retrieved'
return HttpResponse(
json.dumps(response),
content_type='application/json'
)
@api_view(['GET', 'POST'])
def api_arc_user_collection(request, corpus_id):
context, corpus = _get_arc_context(request, corpus_id)
response = {
'message': 'invalid user token',
'collection': {}
}
if corpus and 'arc_user' in context:
arc_userid = context['arc_user']['userid']
if request.method == 'GET':
context['search']['fields_filter'] = {
'arc_userid': f"{arc_userid}",
}
if 'page' not in context['search']:
context['search']['page'] = 1
if 'page_size' not in context['search']:
context['search']['page_size'] = 50
context['search']['es_debug'] = True
response['collection'] = corpus.search_content(content_type='ArcCollection', **context['search'])
response['message'] = f"successfully retrieved page {context['search']['page']}"
elif request.method == 'POST':
if 'artifact-id' in request.POST:
artifact_id = _clean(request.POST, 'artifact-id').strip()
artifact = corpus.get_content('ArcArtifact', artifact_id)
if artifact and 'annotation' in request.POST:
annotation = _clean(request.POST, 'annotation').strip()
collection = corpus.get_content('ArcCollection')
collection.artifact_uri = artifact.external_uri
collection.arc_userid = context['arc_user']['userid']
if annotation:
collection.annotation = annotation
collection.date_collected = datetime.datetime.now()
collection.save()
response['message'] = "artifact collected successfully"
elif artifact and 'delete' in request.POST:
collection = corpus.get_content('ArcCollection', {'artifact_uri': artifact.external_uri, 'arc_userid': context['arc_user']['userid']}, single_result=True)
if collection:
collection.delete()
response['message'] = "artifact uncollected successfully"
return HttpResponse(
json.dumps(response),
content_type='application/json'
)
def bigdiva(request, corpus_id):
response = _get_context(request)
corpus, role = get_scholar_corpus(corpus_id, response['scholar'])
return render(
request,
'bigdiva.html',
{
'corpus_id': corpus_id,
'role': role,
'response': response,
}
)
def uri_ascription(request, corpus_id, content_type, content_id):
context = _get_context(request)
corpus, role = get_scholar_corpus(corpus_id, context['scholar'])
ascription = None
if corpus:
content_uri = '/corpus/{0}/{1}/{2}'.format(
corpus_id,
content_type,
content_id
)
try:
ascription = corpus.get_content('UriAscription', {'corpora_uri': content_uri})[0]
except:
ascription = None
return render(
request,
'AscriptionWidget.html',
{
'corpus_id': corpus_id,
'popup': True,
'role': role,
'attribution': ascription,
'response': context,
}
)
def lincs_ttl(request, corpus_id, content_type, content_id):
context = _get_context(request)
corpus, role = get_scholar_corpus(corpus_id, context['scholar'])
arts = []
ttl = ''
if corpus and content_id and content_type == 'ArcArtifact' and 'ArcArtifact' in corpus.content_types:
arts.append(corpus.get_content('ArcArtifact', content_id, single_result=True))
elif corpus and content_id and content_type == 'ArcArchive' and _contains(corpus.content_types, ['ArcArchive', 'ArcArtifact']):
skip = request.GET.get('skip', '0')
limit = request.GET.get('limit', None)
arts = corpus.get_content('ArcArtifact', {'archive': content_id})
if limit and limit.isdigit() and skip.isdigit():
arts = arts.skip(int(skip)).limit(int(limit))
print(f"generating TTL for {arts.count(True)} artifacts.")
ttl = _generate_lincs_ttl(arts)
return HttpResponse(
ttl,
content_type='text/turtle'
)
def _generate_lincs_ttl(artifacts):
globals = {}
tab = ' '
arc_uri_template = 'http://ar-c.org/uri/{ct}/{id}'
role_labels = {
'ART': 'Visual Artist',
'AUT': 'Author',
'EDT': 'Editor',
'PBL': 'Publisher',
'TRL': 'Translator',
'CRE': 'Creator',
'ETR': 'Etcher',
'EGR': 'Engraver',
'OWN': 'Owner',
'ARC': 'Architect',
'BND': 'Binder',
'BKD': 'Book designer',
'BKP': 'Book producer',
'CLL': 'Calligrapher',
'CTG': 'Cartographer',
'COL': 'Collector',
'CLR': 'Colorist',
'CWT': 'Commentator',
'COM': 'Compiler',
'CMT': 'Compositor',
'DUB': 'Dubious author',
'FAC': 'Facsimilist',
'ILU': 'Illuminator',
'ILL': 'Illustrator',
'LTG': 'Lithographer',
'PRT': 'Printer',
'POP': 'Printer of plates',
'PRM': 'Printmaker',
'RPS': 'Repository',
'RBR': 'Rubricator',
'SCR': 'Scribe',
'SCL': 'Sculptor',
'TYD': 'Type designer',
'TYG': 'Typographer',
'WDE': 'Wood engraver',
'WDC': 'Wood cutter'
}
# this ttl string will be built to contain the turtle representation of this artifact
ttl = '''@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix crm: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix crmpc: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix frbroo: <http://iflastandards.info/ns/fr/frbr/frbroo/> .
@prefix crmdig: <http://www.ics.forth.gr/isl/CRMdig/> .
@prefix cwrc: <http://id.lincsproject.ca/cwrc#> .\n\n'''
for art in artifacts:
if art and art.external_uri and art.title and art.agents and art.years:
# todo: replace quote chars; vet use of has_note for accounting for art.description; how to rep relateds; how to represent ocr, full_text, full_text_url, full_text_contents; whether to include labels for things like <full_image> (present in examples but not spec sheet); how to represent subjects and coverages, ie ("Activism and involvement", "World War II -- Concentration camps -- Living conditions") from URI http://ddr.densho.org/ddr-csujad-29-59-1/
#################################
# MAIN STUB OF ARTIFACT #
#################################
# main URI declaration
ttl += '''<{uri}> a frbroo:F2_Expression ;\n'''.format(uri=art.external_uri.strip())
# artifact "creation" node declaration (will specify agents and years later)
ttl += '''{tab}crm:P94i_was_created_by <{id}_creation> ;\n'''.format(
tab=tab,
id=arc_uri_template.format(ct="artifact", id=art.id)
)
# browser URL
ttl += '''{tab}crm:P1_is_identified_by <{id}_id> , <{url}> , <{id}_title> ;\n'''.format(
tab=tab,
id=arc_uri_template.format(ct="artifact", id=art.id),
url=art.url.strip()
)
# label
ttl += '''{tab}rdfs:label "{label}" ;\n'''.format(tab=tab, label=html.unescape(art.label.strip()))
# archive node declaration
ttl += '''{tab}crm:P16i_was_used_for <{id}_contribution> ;\n'''.format(
tab=tab,
id=arc_uri_template.format(ct="artifact", id=art.id)
)
# alternative title node declaration
if art.alt_title:
ttl += '''{tab}crm:E33_E41_Linguistic_Appellation <{id}_alt_title> ;\n'''.format(
tab=tab,
id=arc_uri_template.format(ct="artifact", id=art.id)
)
# conflate ARC type, genres, disciplines, and freeculture indicator into LINCS "types" and declare nodes
types = []
for t in art.types:
types.append(t.name.strip().replace(' ', '_'))
for g in art.genres:
types.append(g.name.strip().replace(' ', '_'))
for d in art.disciplines:
types.append(d.name.strip().replace(' ', '_'))
if art.free_culture:
types.append("Open_Access")
if types:
types = ['<' + t.strip() + '>' for t in types]
ttl += '''{tab}crm:P2_has_type {types} ;\n'''.format(
tab=tab,
types=",\n{tab}{tab}".format(tab=tab).join(types)
)
# subject node declaration(s)
if art.subjects:
for subj in art.subjects:
if subj:
subj_id = arc_uri_template.format(ct="subject", id=slugify(subj))
ttl += '''{tab}crm:P129_is_about <{subj_id}> ;\n'''.format(
tab=tab,
subj_id=subj_id
)
globals['''<{subj_id}> a skos:Concept ;
rdfs:label "{subject}" .'''.format(subj_id=subj_id, subject=subj)] = True
# coverage node declaration(s)
if art.coverages:
for cov in art.coverages:
if cov:
cov_id = arc_uri_template.format(ct="coverage", id=slugify(cov))
ttl += '''{tab}crm:P67_refers_to <{cov_id}> ;\n'''.format(tab=tab, cov_id=cov_id)
globals['''<{cov_id}> a crm:E53_Place ;
rdfs:label "{coverage}" .'''.format(cov_id=cov_id, coverage=cov)] = True
# provenance node(s) declaration (handling art.sources)
if art.sources:
prov_nodes = []
for prov_num in range(0, len(art.sources)):
prov_nodes.append(
"<{id}_provenance_{num}>".format(
id=arc_uri_template.format(ct="artifact", id=art.id),
num=prov_num + 1
)
)
ttl += '''{tab}crm:P67i_is_referred_to_by {provs} ;\n'''.format(
tab=tab,
provs=",\n{tab}{tab}".format(tab=tab).join(prov_nodes)
)
# visual representations of artifact like image, thumbnail node declarations
if art.image_url:
ttl += '''{tab}crm:P138i_has_representation <{image}> ;\n'''.format(tab=tab, image=art.image_url.strip())
if art.thumbnail_url:
ttl += '''{tab}crm:P138i_has_representation <{thumb}> ;\n'''.format(tab=tab, thumb=art.thumbnail_url.strip())
# markup representations of artifact like XML, HTML, SGML node declarations
if art.source_xml:
ttl += '''{tab}crm:P67i_is_referred_to_by <{xml}> ;\n'''.format(tab=tab, xml=art.source_xml.strip())
if art.source_html:
ttl += '''{tab}crm:P67i_is_referred_to_by <{html}> ;\n'''.format(tab=tab, html=art.source_html.strip())
if art.source_sgml:
ttl += '''{tab}crm:P67i_is_referred_to_by <{sgml}> ;\n'''.format(tab=tab, sgml=art.source_sgml.strip())
# language node declaration
if art.language:
ttl += '''{tab}crm:P72_has_language <{lang}> ;\n'''.format(tab=tab, lang=art.language.strip())
# description
if art.description:
ttl += '''{tab}crm:P3_has_note "{desc}" ;\n'''.format(tab=tab, desc=art.description.strip())
# has_parts
if art.has_parts:
parts = ['<' + p.strip() + '>' for p in art.has_parts]
ttl += '''{tab}crm:P148_has_component {has_parts} ;\n'''.format(
tab=tab,
has_parts=",\n{tab}{tab}".format(tab=tab).join(parts)
)
# is_part_ofs
if art.is_part_ofs:
parts = ['<' + p.strip() + '>' for p in art.is_part_ofs]
ttl += '''{tab}crm:P148i_is_component_of {is_part_ofs} ;\n'''.format(
tab=tab,
is_part_ofs=",\n{tab}{tab}".format(tab=tab).join(parts)
)
# federation, date of edition, review date node declaration (expressed in terms of digital surrogacy)
ttl += '''{tab}crm:P129i_is_subject_of <{id}_digital_surrogate> .\n\n'''.format(
tab=tab,
id=arc_uri_template.format(ct="artifact", id=art.id)
)
ttl += '''<{id}_digital_surrogate> a crm:E42_Identifier ;
rdfs:label "ARC digital surrogate of {label}" ;
crm:P2_has_type <ARC_digital_surrogate>, <http://vocab.getty.edu/aat/300379790> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip())
)
if art.date_of_edition:
ttl += '''<{id}_digital_surrogate> crm:P94i_was_created_by <{id}_digital_surrogate_creation> .
<{id}_digital_surrogate_creation> a crm:E65_Creation ;
rdfs:label "Creation of ARC digital surrogate of {label}" ;
crm:P2_has_type <https://www.wikidata.org/wiki/Q99231516> ;
crm:P4_has_time-span <{id}_digital_surrogate_creation_timespan> .
<{id}_digital_surrogate_creation_timespan> a crm:E52_Time-Span ;
rdfs:label "Datetime of creation of ARC digital surrogate of {label}" ;
crm:P82_at_some_time_within "{year}" ;
crm:P82a_begin_of_the_begin "{year}-01-01T00:00:00"^^xsd:dateTime ;
crm:P82b_end_of_the_end "{year}-12-31T23:59:59"^^xsd:dateTime .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip()),
year=art.date_of_edition
)
if art.date_of_review:
ttl += '''<{id}_digital_surrogate> crm:P16i_was_used_for <{id}_digital_surrogate_review>, <{id}_digital_surrogate_ingestion> .
<{id}_digital_surrogate_review> a crm:E7_Activity ;
rdfs:label "Review of ARC digital surrogate of {label}" ;
crm:P2_has_type <Review> ;
crm:P9i_forms_part_of <{id}_digital_surrogate_ingestion> ;
crm:P4_has_time-span <{id}_digital_surrogate_review_timespan> .
<{id}_digital_surrogate_review_timespan> a crm:E52_Time-Span ;
rdfs:label "Datetime of review of ARC digital surrogate of {label}" ;
crm:P82_at_some_time_within "{year}" ;
crm:P82a_begin_of_the_begin "{year}-01-01T00:00:00"^^xsd:dateTime ;
crm:P82b_end_of_the_end "{year}-12-31T23:59:59"^^xsd:dateTime ;
crm:P14_carried_out_by <{federation}> .
<{id}_digital_surrogate_ingestion> a crm:E7_Activity ;
rdfs:label "Ingestion of ARC digital surrogate of {label}" ;
crm:P2_has_type <Ingestion> ;
crm:P14_carried_out_by <ARC> ;
crm:P4_has_time-span <{id}_digital_surrogate_ingestion_timespan> .
<{id}_digital_surrogate_ingestion_timespan> a crm:E52_Time-Span ;
rdfs:label "Datetime of ingestion of ARC digital surrogate of {label}" ;
crm:P82_at_some_time_within "{year}" ;
crm:P82a_begin_of_the_begin "{year}-01-01T00:00:00"^^xsd:dateTime ;
crm:P82b_end_of_the_end "{year}-12-31T23:59:59"^^xsd:dateTime .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip()),
federation=art.federations[0].handle,
year=art.date_of_review
)
globals['''<Review> a crm:E55_Type .'''] = True
globals['''<Ingestion> a crm:E55_Type .'''] = True
globals['''<ARC> a crm:E39_Actor .'''] = True
globals['''<{federation}> a crm:E39_Actor .'''.format(federation=art.federations[0].handle)] = True
globals['''<ARC_digital_surrogate> a crm:E55_Type .'''] = True
#################################
# DEPENDENT NODES #
#################################
# archive
ttl += '''<{id}_contribution> a crm:E7_Activity ;
rdfs:label "Contribution of {label} to ARC" ;
crm:P2_has_type <Contribution_to_ARC> ;
crm:P14_carried_out_by <{archive}> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip()),
# archive=art.archive.handle
archive=arc_uri_template.format(ct="archive", id=art.archive.id)
)
globals['''<Contribution_to_ARC> a crm:E55_Type ;
rdfs:label "Contribution to ARC" .'''] = True
globals['''<{archive}> a crm:E39_Actor ;
rdfs:label "{archive_label}" .'''.format(
# archive=art.archive.handle,
archive=arc_uri_template.format(ct="archive", id=art.archive.id),
archive_label=art.archive.name.strip() if art.archive.name else art.archive.handle
)] = True
# title
ttl += '''<{id}_title> a crm:E33_E41_Linguistic_Appellation ;
rdfs:label "{title}" ;
crm:P190_has_symbolic_content "{title}" ;
crm:P2_has_type <main_title> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
title=art.title.strip()
)
globals['''<main_title> a crm:E55_Type ;
rdfs:label "Main title" .'''] = True
# alt title
if art.alt_title:
ttl += '''<{id}_alt_title> a crm:E35_Title ;
rdfs:label "{alt_title}" ;
crm:P190_has_symbolic_content "{alt_title}" ;
crm:P2_has_type <alternative_title> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
alt_title=art.alt_title.strip()
)
globals['''<alternative_title> a crm:E55_Type ;
rdfs:label "Alternative title" .'''] = True
# LINCS types (ARC type, genres, disciplines, freeculture)
if types:
for t in types:
globals['''{type} a crm:E55_Type ;
rdfs:label "{type_label}" .'''.format(
type=t,
type_label=t.replace('<', '').replace('>', '').replace('_', ' ')
)] = True
# provenance(s)
if art.sources:
for prov_num in range(0, len(art.sources)):
ttl += '''<{id}_provenance_{prov_num}> a crm:E33_Linguistic_Object ;
rdfs:label "Provenance statement about {label}" ;
crm:P190_has_symbolic_content "{prov}" ;
crm:P2_has_type <Provenance_note> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
prov_num=prov_num + 1,
label=html.unescape(art.label.strip()),
prov=art.sources[prov_num]
)
globals['''<Provenance_note> a crn:E55_Type ;
rdfs:label "Provenance note" .'''] = True
# visual representations
if art.image_url:
ttl += '''<{image}> a crm:E36_Visual_Item ;
rdfs:label "Full image of {label}" ;
crm:P2_has_type <full_image> .\n\n'''.format(
image=art.image_url.strip(),
label=html.unescape(art.label.strip())
)
globals['''<full_image> a crm:E55_Type ;
rdfs:label "full image" .'''] = True
if art.thumbnail_url:
ttl += '''<{thumb}> a crm:E36_Visual_Item ;
rdfs:label "Thumbnail image of {label}" ;
crm:P2_has_type <thumbnail_image> .\n\n'''.format(
thumb=art.thumbnail_url.strip(),
label=html.unescape(art.label.strip())
)
globals['''<thumbnail_image> a crm:E55_Type ;
rdfs:label "thumbnail image" .'''] = True
# markup representations
if art.source_xml:
ttl += '''<{xml}> a crm:E73_Information_Object ;
rdfs:label "XML source code for data of {label}" ;
crm:P2_has_type <xml> .\n\n'''.format(
xml=art.source_xml.strip(),
label=html.unescape(art.label.strip())
)
globals['''<xml> a crm:E55_Type ;
rdfs:label "XML document" .'''] = True
if art.source_html:
ttl += '''<{html}> a crm:E73_Information_Object ;
rdfs:label "XML source code for data of {label}" ;
crm:P2_has_type <html> .\n\n'''.format(
html=art.source_html.strip(),
label=html.unescape(art.label.strip())
)
globals['''<html> a crm:E55_Type ;
rdfs:label "HTML document" .'''] = True
if art.source_sgml:
ttl += '''<{sgml}> a crm:E73_Information_Object ;
rdfs:label "SGML source code for data of {label}" ;
crm:P2_has_type <sgml> .\n\n'''.format(
sgml=art.source_sgml.strip(),
label=html.unescape(art.label.strip())
)
globals['''<sgml> a crm:E55_Type ;
rdfs:label "SGML document" .'''] = True
# language
if art.language:
globals['''<{lang}> a crm:E56_Language .'''.format(lang=art.language.strip())] = True
# agents and dates (the creation node)
# TODO: per convo with Zac: any time the entity's name shows up, it should be replaced with authoritative URI if present.
# need to chat about nuance of this since technically we're dealing with _agents_ that have roles
# agents = ["<{entity}_{role}>".format(entity=a.entity.name.strip(), role=a.role.name.strip()) for a in art.agents]
agents = ["<{id}>".format(id=arc_uri_template.format(ct="agent", id=a.id)) for a in art.agents]
ttl += '''<{id}_creation> a crm:E65_Creation ;
rdfs:label "Creation of {label}" ;
crm:P2_has_type cwrc:ProductionEvent, cwrc:PublishingEvent ;
crmpc:P01i_is_domain_of {agents} ;
crm:P4_has_time-span <{id}_creation_timespan> .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip()),
agents=",\n{tab}{tab}".format(tab=tab).join(agents)
)
for agent in art.agents:
entity_uri = arc_uri_template.format(ct="entity", id=agent.entity.id)
if agent.entity.external_uri and agent.entity.external_uri_verified:
entity_uri = agent.entity.external_uri
if entity_uri.endswith('/'):
entity_uri = entity_uri[:-1]
if 'wikidata.org' in entity_uri:
entity_uri = entity_uri.replace('/wiki/', '/entity/')
globals['''<{agent_id}> a crmpc:PC14_carried_out_by ;
rdfs:label "{name} in the role of {role_desc}" ;
crmpc:P02_has_range <{entity_id}> ;
crmpc:P14.1_in_the_role_of <{role_id}> .'''.format(
agent_id=arc_uri_template.format(ct="agent", id=agent.id),
entity_id=entity_uri,
role_id=arc_uri_template.format(ct="role", id=agent.role.id),
name=agent.entity.name.strip(),
role_desc=role_labels.get(agent.role.name.strip(), 'Unknown Role')
)] = True
globals['''<{entity_id}> a crm:E39_Actor ;
rfs:label "{name}" .'''.format(
entity_id=entity_uri,
name=agent.entity.name.strip()
)] = True
globals['''<{role_id}> a crm:E55_Type ;
rdfs:label "{role_desc}" .'''.format(
role_id=agent.role.id,
role_desc=role_labels.get(agent.role.name.strip(), 'Unknown Role')
)] = True
ttl += '''<{id}_creation_timespan> a crm:E52_Time-Span ;
rdfs:label "Datetime of creation of {label}" ;
crm:P82_at_some_time_within "{date_value}" ;
crm:P82a_begin_of_the_begin "{first_year}-01-01T00:00:00"^^xsd:dateTime ;
crm:P82b_end_of_the_end "{last_year}-12-31T23:59:59"^^xsd:dateTime .\n\n'''.format(
id=arc_uri_template.format(ct="artifact", id=art.id),
label=html.unescape(art.label.strip()),
date_value=art.date_value,
first_year=art.years[0],
last_year=art.years[-1]
)
else:
print(f"Artifact {art.id} does not meet the requirements.")
#################################
# Globals #
#################################
for statement in globals.keys():
ttl += statement + '\n\n'
return ttl