-
Notifications
You must be signed in to change notification settings - Fork 4
/
xml_upload.py
1197 lines (1066 loc) · 53.6 KB
/
xml_upload.py
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module handles the import of XML data into the DSP platform.
"""
from __future__ import annotations
import base64
import copy
import json
import os
import sys
import uuid
from collections import namedtuple
from datetime import datetime
from pathlib import Path
from typing import Any, Optional, Union, cast
from urllib.parse import quote_plus
import pandas as pd
import regex
from lxml import etree
from dsp_tools.models.connection import Connection
from dsp_tools.models.exceptions import BaseError, UserError
from dsp_tools.models.permission import Permissions
from dsp_tools.models.projectContext import ProjectContext
from dsp_tools.models.resource import KnoraStandoffXmlEncoder, ResourceInstance, ResourceInstanceFactory
from dsp_tools.models.sipi import Sipi
from dsp_tools.models.value import KnoraStandoffXml
from dsp_tools.models.xmlpermission import XmlPermission
from dsp_tools.models.xmlproperty import XMLProperty
from dsp_tools.models.xmlresource import XMLResource
from dsp_tools.utils.logging import get_logger
from dsp_tools.utils.shared import login, try_network_action, validate_xml_against_schema
MetricRecord = namedtuple("MetricRecord", ["res_id", "filetype", "filesize_mb", "event", "duration_ms", "mb_per_sec"])
logger = get_logger(__name__)
def _transform_server_url_to_foldername(server: str) -> str:
"""
Take the server URL and transform it so that it can be used as foldername.
Args:
server: server, e.g. "https://api.test.dasch.swiss/" or "http://0.0.0.0:3333"
Returns:
simplified version, e.g. "test.dasch.swiss" or "localhost"
"""
server_substitutions = {
r"https?://": "",
r"^api\.": "",
r":\d{2,5}/?$": "",
r"/$": "",
r"0.0.0.0": "localhost",
}
for pattern, repl in server_substitutions.items():
server = regex.sub(pattern, repl, server)
return server
def _determine_save_location_of_diagnostic_info(
server: str,
proj_shortcode: str,
onto_name: str,
) -> tuple[Path, str, str]:
"""
Determine the save location for diagnostic info that will be used if the xmlupload is interrupted.
They are going to be stored in ~/.dsp-tools/xmluploads/server/shortcode/ontoname.
This path is computed and created.
Args:
server: URL of the DSP server where the data is uploaded to
proj_shortcode: 4-digit hexadecimal shortcode of the project
onto_name: name of the ontology that the data belongs to
Returns:
a tuple consisting of the absolute full path to the storage location,
a version of the server URL that can be used as foldername,
and the timestamp string that can be used as component of file names
(so that different diagnostic files of the same xmlupload have the same timestamp)
"""
server_as_foldername = _transform_server_url_to_foldername(server)
timestamp_str = datetime.now().strftime("%Y-%m-%d_%H%M%S")
save_location = Path.home() / Path(".dsp-tools") / "xmluploads" / server_as_foldername / proj_shortcode / onto_name
save_location.mkdir(parents=True, exist_ok=True)
return save_location, server_as_foldername, timestamp_str
def _write_id2iri_mapping_and_metrics(
id2iri_mapping: dict[str, str],
metrics: Optional[list[MetricRecord]],
failed_uploads: list[str],
input_file: Union[str, Path, etree._ElementTree[Any]],
timestamp_str: str,
server_as_foldername: str,
) -> bool:
"""
Writes the id2iri mapping and the metrics into the current working directory,
and prints the failed uploads (if applicable).
Args:
id2iri_mapping: mapping of ids from the XML file to IRIs in DSP (initially empty, gets filled during the upload)
metrics: list with the metric records collected until now (gets filled during the upload)
failed_uploads: ids of resources that could not be uploaded (initially empty, gets filled during the upload)
input_file: path to the XML file or parsed ElementTree
timestamp_str: timestamp for the name of the log files
server_as_foldername: simplified version of the server URL that can be used as folder name
Returns:
True if there are no failed_uploads, False otherwise
"""
# determine names of files
if isinstance(input_file, (str, Path)):
id2iri_filename = f"{Path(input_file).stem}_id2iri_mapping_{timestamp_str}.json"
metrics_filename = f"{timestamp_str}_metrics_{server_as_foldername}_{Path(input_file).stem}.csv"
else:
id2iri_filename = f"{timestamp_str}_id2iri_mapping.json"
metrics_filename = f"{timestamp_str}_metrics_{server_as_foldername}.csv"
# write files and print info
success = True
with open(id2iri_filename, "x", encoding="utf-8") as f:
json.dump(id2iri_mapping, f, ensure_ascii=False, indent=4)
print(f"The mapping of internal IDs to IRIs was written to {id2iri_filename}")
logger.info(f"The mapping of internal IDs to IRIs was written to {id2iri_filename}")
if failed_uploads:
print(f"\nWARNING: Could not upload the following resources: {failed_uploads}\n")
logger.warning(f"Could not upload the following resources: {failed_uploads}")
success = False
if metrics:
os.makedirs("metrics", exist_ok=True)
df = pd.DataFrame(metrics)
df.to_csv(f"metrics/{metrics_filename}")
print(f"Total time of xmlupload: {sum(int(record.duration_ms) for record in metrics) / 1000:.1f} seconds")
return success
def _remove_circular_references(
resources: list[XMLResource],
verbose: bool,
) -> tuple[
list[XMLResource],
dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]],
dict[XMLResource, dict[XMLProperty, list[str]]],
]:
"""
Temporarily removes problematic resource-references from a list of resources.
A reference is problematic if it creates a circle (circular references).
Args:
resources: list of resources that possibly contain circular references
verbose: verbose output if True
Raises:
BaseError
Returns:
list: list of cleaned resources
stashed_xml_texts: dict with the stashed XML texts
stashed_resptr_props: dict with the stashed resptr-props
"""
if verbose:
print("Checking resources for unresolvable references...")
logger.info("Checking resources for unresolvable references...")
stashed_xml_texts: dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]] = {}
stashed_resptr_props: dict[XMLResource, dict[XMLProperty, list[str]]] = {}
# sort the resources according to outgoing resptrs
ok_resources: list[XMLResource] = []
nok_resources: list[XMLResource] = []
ok_res_ids: list[str] = []
cnt = 0
nok_len = 9999999
while len(resources) > 0 and cnt < 10000:
for resource in resources:
resptrs = resource.get_resptrs()
resptrs = [x for x in resptrs if not regex.search(r"https?://rdfh.ch/[a-fA-F0-9]{4}/\w{22}", x)]
if len(resptrs) == 0:
ok_resources.append(resource)
ok_res_ids.append(resource.id)
else:
ok = True
for resptr in resptrs:
if resptr not in ok_res_ids:
ok = False
if ok:
ok_resources.append(resource)
ok_res_ids.append(resource.id)
else:
nok_resources.append(resource)
resources = nok_resources
if len(nok_resources) == nok_len:
# there are circular references. go through all problematic resources, and stash the problematic references.
(
nok_resources,
ok_res_ids,
ok_resources,
stashed_xml_texts,
stashed_resptr_props,
) = _stash_circular_references(
nok_resources=nok_resources,
ok_res_ids=ok_res_ids,
ok_resources=ok_resources,
stashed_xml_texts=stashed_xml_texts,
stashed_resptr_props=stashed_resptr_props,
)
nok_len = len(nok_resources)
nok_resources = []
cnt += 1
if verbose:
print(f"{cnt}. ordering pass finished.")
logger.info(f"{cnt}. ordering pass finished.")
return ok_resources, stashed_xml_texts, stashed_resptr_props
def _stash_circular_references(
nok_resources: list[XMLResource],
ok_res_ids: list[str],
ok_resources: list[XMLResource],
stashed_xml_texts: dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]],
stashed_resptr_props: dict[XMLResource, dict[XMLProperty, list[str]]],
) -> tuple[
list[XMLResource],
list[str],
list[XMLResource],
dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]],
dict[XMLResource, dict[XMLProperty, list[str]]],
]:
"""
Raises:
BaseError
"""
for res in nok_resources.copy():
for link_prop in res.get_props_with_links():
if link_prop.valtype == "text":
for value in link_prop.values:
if value.resrefs and not all(_id in ok_res_ids for _id in value.resrefs):
# stash this XML text, replace it by its hash, and remove the
# problematic resrefs from the XMLValue's resrefs list
value_hash = str(hash(f"{value.value}{datetime.now()}"))
if res not in stashed_xml_texts:
stashed_xml_texts[res] = {link_prop: {value_hash: cast(KnoraStandoffXml, value.value)}}
elif link_prop not in stashed_xml_texts[res]:
stashed_xml_texts[res][link_prop] = {value_hash: cast(KnoraStandoffXml, value.value)}
else:
stashed_xml_texts[res][link_prop][value_hash] = cast(KnoraStandoffXml, value.value)
value.value = KnoraStandoffXml(value_hash)
value.resrefs = [_id for _id in value.resrefs if _id in ok_res_ids]
elif link_prop.valtype == "resptr":
for value in link_prop.values.copy():
if value.value not in ok_res_ids:
# value.value is the id of the target resource. stash it, then delete it
if res not in stashed_resptr_props:
stashed_resptr_props[res] = {}
stashed_resptr_props[res][link_prop] = [str(value.value)]
else:
if link_prop not in stashed_resptr_props[res]:
stashed_resptr_props[res][link_prop] = [str(value.value)]
else:
stashed_resptr_props[res][link_prop].append(str(value.value))
link_prop.values.remove(value)
else:
logger.error("ERROR in remove_circular_references(): link_prop.valtype is neither text nor resptr.")
raise BaseError("ERROR in remove_circular_references(): link_prop.valtype is neither text nor resptr.")
if len(link_prop.values) == 0:
# if all values of a link property have been stashed, the property needs to be removed
res.properties.remove(link_prop)
ok_resources.append(res)
ok_res_ids.append(res.id)
nok_resources.remove(res)
return nok_resources, ok_res_ids, ok_resources, stashed_xml_texts, stashed_resptr_props
def _convert_ark_v0_to_resource_iri(ark: str) -> str:
"""
Converts an ARK URL from salsah.org (ARK version 0) of the form ark:/72163/080c-779b9990a0c3f-6e
to a DSP resource IRI of the form http://rdfh.ch/080C/Ef9heHjPWDS7dMR_gGax2Q
This method is needed for the migration of projects from salsah.org to DSP.
Resources need to be created with an existing ARK,
so the IRI needs to be extracted from that ARK
in order for the ARK URL to be still valid after the migration.
Args:
ark: an ARK version 0 of the form ark:/72163/080c-779b9990a0c3f-6e,
'72163' being the Name Assigning Authority number,
'080c' being the project shortcode,
'779b9990a0c3f' being an ID derived from the object's Salsah ID and
'6e' being check digits
Raises:
BaseError: if the ARK is invalid
Returns:
Resource IRI (str) of the form http://rdfh.ch/080C/Ef9heHjPWDS7dMR_gGax2Q
"""
# create the DaSCH namespace to create version 5 UUIDs
generic_namespace_url = uuid.NAMESPACE_URL
dasch_uuid_ns = uuid.uuid5(generic_namespace_url, "https://dasch.swiss") # cace8b00-717e-50d5-bcb9-486f39d733a2
# get the salsah resource ID from the ARK and convert it to a UUID version 5 (base64 encoded)
if ark.count("-") != 2:
logger.error(f"while converting ARK '{ark}'. The ARK seems to be invalid")
raise BaseError(f"while converting ARK '{ark}'. The ARK seems to be invalid")
project_id, resource_id, _ = ark.split("-")
_, project_id = project_id.rsplit("/", 1)
project_id = project_id.upper()
if not regex.match("^[0-9a-fA-F]{4}$", project_id):
logger.error(f"while converting ARK '{ark}'. Invalid project shortcode '{project_id}'")
raise BaseError(f"while converting ARK '{ark}'. Invalid project shortcode '{project_id}'")
if not regex.match("^[0-9A-Za-z]+$", resource_id):
logger.error(f"while converting ARK '{ark}'. Invalid Salsah ID '{resource_id}'")
raise BaseError(f"while converting ARK '{ark}'. Invalid Salsah ID '{resource_id}'")
# make a UUID v5 from the namespace created above (which is a UUID itself) and the resource ID
# and encode it to base64
dsp_uuid = base64.urlsafe_b64encode(uuid.uuid5(dasch_uuid_ns, resource_id).bytes).decode("utf-8")
dsp_uuid = dsp_uuid[:-2]
# use the new UUID to create the resource IRI
return "http://rdfh.ch/" + project_id + "/" + dsp_uuid
def parse_xml_file(input_file: Union[str, Path, etree._ElementTree[Any]]) -> etree._Element:
"""
Parse an XML file with DSP-conform data,
remove namespace URI from the elements' names,
and transform the special tags <annotation>, <region>, and <link>
to their technically correct form
<resource restype="Annotation">, <resource restype="Region">, and <resource restype="LinkObj">.
Args:
input_file: path to the XML file, or parsed ElementTree
Returns:
the root element of the parsed XML file
"""
# remove comments and processing instructions (commented out properties break the XMLProperty constructor)
if isinstance(input_file, (str, Path)):
parser = etree.XMLParser(remove_comments=True, remove_pis=True)
tree = etree.parse(source=input_file, parser=parser)
else:
tree = copy.deepcopy(input_file)
for c in tree.xpath("//comment()"):
c.getparent().remove(c)
for c in tree.xpath("//processing-instruction()"):
c.getparent().remove(c)
# remove namespace URI from the elements' names and transform the special tags to their technically correct form
for elem in tree.iter():
elem.tag = etree.QName(elem).localname # remove namespace URI in the element's name
if elem.tag == "annotation":
elem.attrib["restype"] = "Annotation"
elem.tag = "resource"
elif elem.tag == "link":
elem.attrib["restype"] = "LinkObj"
elem.tag = "resource"
elif elem.tag == "region":
elem.attrib["restype"] = "Region"
elem.tag = "resource"
return tree.getroot()
def _check_if_onto_name_exists(
resclass_name_2_type: dict[str, type],
ontoname: str,
shortcode: str,
) -> None:
"""
Check if the "default-ontology" of the <knora> tag of the XML file exists on the DSP server.
Args:
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
ontoname: name of the ontology as referenced in the XML file
shortcode: shortcode of the project as referenced in the XML file
Raises:
UserError: if the ontology does not exist on the DSP server
"""
existing_onto_names = {x.split(":")[0] for x in resclass_name_2_type}
existing_onto_names.remove("knora-api")
if ontoname not in existing_onto_names:
err_msg = (
f"ERROR: The <knora> tag of your XML file references the default-ontology '{ontoname}', "
f"but the project {shortcode} on the DSP server contains only the ontologies {existing_onto_names}"
)
logger.error(err_msg)
raise UserError(err_msg)
def _check_consistency_with_ontology(
resources: list[XMLResource],
resclass_name_2_type: dict[str, type],
shortcode: str,
ontoname: str,
verbose: bool = False,
) -> None:
"""
Checks if the "default-ontology" of the <knora> tag of the XML file exists on the DSP server,
and if the resource types and property types in the XML are consistent with the ontology.
Args:
resources: a list of parsed XMLResources
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
shortcode: the shortcode of the project
as referenced in the attribute "shortcode" of the <knora> tag of the XML file
ontoname: the name of the ontology
as referenced in the attribute "default-ontology" of the <knora> tag of the XML file
verbose: verbose switch
Raises:
UserError: if there is an inconsistency between the ontology and the data
"""
if verbose:
print("Check if the resource types and properties are consistent with the ontology...")
logger.info("Check if the resource types and properties are consistent with the ontology...")
_check_if_onto_name_exists(
resclass_name_2_type=resclass_name_2_type,
ontoname=ontoname,
shortcode=shortcode,
)
_check_if_resource_types_exist(resources=resources, resclass_name_2_type=resclass_name_2_type)
_check_if_property_types_exist(resources=resources, resclass_name_2_type=resclass_name_2_type)
def _check_if_resource_types_exist(
resources: list[XMLResource],
resclass_name_2_type: dict[str, type],
) -> None:
"""
Check if the resource types in the XML file are consistent with the ontology.
Args:
resources: a list of parsed XMLResources
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
Raises:
UserError: if there is an inconsistency between the ontology and the data
"""
for resource in resources:
# check that the resource type is consistent with the ontology
if resource.restype not in resclass_name_2_type:
res_syntaxes = [
'DSP-API internals: <resource restype="restype">',
'current ontology: <resource restype=":restype">',
'other ontology: <resource restype="other:restype">',
]
res_explanations = [
'will be interpreted as "knora-api:restype"',
'"restype" must be defined in the "resources" section of your ontology',
'not yet implemented: "other" must be defined in the same JSON project file as your ontology',
]
err_msg = (
f"=========================\n"
f"ERROR: Resource '{resource.label}' (ID: {resource.id}) "
f"has an invalid resource type '{resource.restype}'. "
"Is your syntax correct? Remember the rules:\n"
)
for res_syntax, res_explanation in zip(res_syntaxes, res_explanations):
err_msg += f" - {res_syntax:<55} ({res_explanation})\n"
logger.error(err_msg)
raise UserError(err_msg)
def _check_if_property_types_exist(
resources: list[XMLResource],
resclass_name_2_type: dict[str, type],
) -> None:
"""
Check if the property types in the XML file are either a DSP base property
or a property that was defined for this specific resource (not every resource can have every property).
Args:
resources: a list of parsed XMLResources
resclass_name_2_type: infos about the resource classes that exist on the DSP server for the current ontology
Raises:
UserError: if there is an inconsistency between the ontology and the data
"""
knora_properties = resclass_name_2_type[resources[0].restype].knora_properties # type: ignore[attr-defined]
for resource in resources:
# check that the property types are consistent with the ontology
resource_properties = resclass_name_2_type[resource.restype].properties.keys() # type: ignore[attr-defined]
for propname in [prop.name for prop in resource.properties]:
if propname not in knora_properties and propname not in resource_properties:
prop_syntaxes = [
'DSP-API internals: <text-prop name="propname">',
'current ontology: <text-prop name=":propname">',
'other ontology: <text-prop name="other:propname">',
]
prop_explanations = [
'will be interpreted as "knora-api:propname"',
'"propname" must be defined in the "properties" section of your ontology',
'not yet implemented: "other" must be defined in the same JSON project file as your ontology',
]
err_msg = (
f"=========================\n"
f"ERROR: Resource '{resource.label}' (ID: {resource.id}) has an invalid property '{propname}'. "
f"Is your syntax correct? Remember the rules:\n"
)
for prop_syntax, prop_explanation in zip(prop_syntaxes, prop_explanations):
err_msg += f" - {prop_syntax:<55} ({prop_explanation})\n"
logger.error(err_msg)
raise UserError(err_msg)
print("Resource types and properties are consistent with the ontology.")
logger.info("Resource types and properties are consistent with the ontology.")
def _check_if_bitstreams_exist(
root: etree._Element,
imgdir: str,
) -> None:
"""
Make sure that all bitstreams referenced in the XML file exist in the imgdir.
Args:
root: parsed XML file
imgdir: folder where the bitstreams are stored
Raises:
UserError: if a bitstream does not exist in the imgdir
"""
multimedia_resources = [x for x in root if any((y.tag == "bitstream" for y in x.iter()))]
for res in multimedia_resources:
pth = [Path(x.text) for x in res.iter() if x.tag == "bitstream" and x.text][0]
if not Path(imgdir / pth).is_file():
raise UserError(
f"Bitstream '{pth!s}' of resource '{res.attrib['label']}' does not exist in the imgdir '{imgdir}'."
)
def xml_upload(
input_file: Union[str, Path, etree._ElementTree[Any]],
server: str,
user: str,
password: str,
imgdir: str,
sipi: str,
verbose: bool = False,
dump: bool = False,
save_metrics: bool = False,
preprocessing_done: bool = False,
) -> bool:
"""
This function reads an XML file and imports the data described in it onto the DSP server.
Args:
input_file: path to the XML file or parsed ElementTree
server: the DSP server where the data should be imported
user: the user (e-mail) with which the data should be imported
password: the password of the user with which the data should be imported
imgdir: the image directory
sipi: the sipi instance to be used
verbose: verbose option for the command, if used more output is given to the user
dump: if true, dumps the XML file to the current working directory
save_metrics: if true, saves time measurements into a "metrics" folder in the current working directory
preprocessing_done: if set, all multimedia files referenced in the XML file must already be on the server
Raises:
BaseError: in case of permanent network or software failure
UserError: in case of permanent network or software failure, or if the XML file is invalid
Returns:
True if all resources could be uploaded without errors; False if one of the resources could not be
uploaded because there is an error in it
"""
# parse the XML file
validate_xml_against_schema(input_file=input_file)
root = parse_xml_file(input_file=input_file)
if not preprocessing_done:
_check_if_bitstreams_exist(root=root, imgdir=imgdir)
shortcode = root.attrib["shortcode"]
default_ontology = root.attrib["default-ontology"]
logger.info(f"Validated and parsed the XML file. Shortcode='{shortcode}' and default_ontology='{default_ontology}'")
# determine save location that will be used for diagnostic info if the xmlupload is interrupted
save_location, server_as_foldername, timestamp_str = _determine_save_location_of_diagnostic_info(
server=server,
proj_shortcode=shortcode,
onto_name=default_ontology,
)
logger.info(f"save_location='{save_location}'")
# start metrics
metrics: list[MetricRecord] = []
preparation_start = datetime.now()
# establish connection to DSP server
con = login(server=server, user=user, password=password, dump=dump)
sipi_server = Sipi(sipi, con.get_token())
# get the project context
try:
proj_context = try_network_action(lambda: ProjectContext(con=con))
except BaseError:
logger.error("Unable to retrieve project context from DSP server", exc_info=True)
raise UserError("Unable to retrieve project context from DSP server") from None
# make Python object representations of the XML file
resources: list[XMLResource] = []
permissions: dict[str, XmlPermission] = {}
for child in root:
if child.tag == "permissions":
permission = XmlPermission(child, proj_context)
permissions[permission.id] = permission
elif child.tag == "resource":
resources.append(XMLResource(child, default_ontology))
# get the project information and project ontology from the server
try:
res_inst_factory = try_network_action(lambda: ResourceInstanceFactory(con, shortcode))
except BaseError:
logger.error(f"A project with shortcode {shortcode} could not be found on the DSP server", exc_info=True)
raise UserError(f"A project with shortcode {shortcode} could not be found on the DSP server") from None
permissions_lookup: dict[str, Permissions] = {s: perm.get_permission_instance() for s, perm in permissions.items()}
resclass_name_2_type: dict[str, type] = {
s: res_inst_factory.get_resclass_type(s) for s in res_inst_factory.get_resclass_names()
}
# check if the data in the XML is consistent with the ontology
_check_consistency_with_ontology(
resources=resources,
resclass_name_2_type=resclass_name_2_type,
shortcode=shortcode,
ontoname=default_ontology,
verbose=verbose,
)
# temporarily remove circular references
resources, stashed_xml_texts, stashed_resptr_props = _remove_circular_references(resources, verbose)
preparation_duration = datetime.now() - preparation_start
preparation_duration_ms = preparation_duration.seconds * 1000 + int(preparation_duration.microseconds / 1000)
metrics.append(MetricRecord("", "", "", "xml upload preparation", preparation_duration_ms, ""))
# upload all resources, then update the resources with the stashed XML texts and resptrs
id2iri_mapping: dict[str, str] = {}
failed_uploads: list[str] = []
nonapplied_resptr_props = {}
nonapplied_xml_texts = {}
try:
id2iri_mapping, failed_uploads, metrics = _upload_resources(
resources=resources,
imgdir=imgdir,
sipi_server=sipi_server,
permissions_lookup=permissions_lookup,
resclass_name_2_type=resclass_name_2_type,
id2iri_mapping=id2iri_mapping,
con=con,
failed_uploads=failed_uploads,
metrics=metrics,
preprocessing_done=preprocessing_done,
)
if stashed_xml_texts:
nonapplied_xml_texts = _upload_stashed_xml_texts(
verbose=verbose,
id2iri_mapping=id2iri_mapping,
con=con,
stashed_xml_texts=stashed_xml_texts,
)
if stashed_resptr_props:
nonapplied_resptr_props = _upload_stashed_resptr_props(
verbose=verbose,
id2iri_mapping=id2iri_mapping,
con=con,
stashed_resptr_props=stashed_resptr_props,
)
if nonapplied_resptr_props or nonapplied_xml_texts:
msg = "Some stashed resptrs or XML texts could not be reapplied to their resources on the DSP server."
logger.error(msg)
raise BaseError(msg)
except BaseException as err: # pylint: disable=broad-except
# The forseeable errors are already handled by the variables
# failed_uploads, nonapplied_xml_texts, and nonapplied_resptr_props.
# Here we catch the unforseeable exceptions, hence BaseException (=the base class of all exceptions)
_handle_upload_error(
err=err,
id2iri_mapping=id2iri_mapping,
failed_uploads=failed_uploads,
stashed_xml_texts=nonapplied_xml_texts or stashed_xml_texts,
stashed_resptr_props=nonapplied_resptr_props or stashed_resptr_props,
save_location=save_location,
timestamp_str=timestamp_str,
)
# write id2iri mapping, metrics, and print some final info
success = _write_id2iri_mapping_and_metrics(
id2iri_mapping=id2iri_mapping,
failed_uploads=failed_uploads,
metrics=metrics if save_metrics else None,
input_file=input_file,
timestamp_str=timestamp_str,
server_as_foldername=server_as_foldername,
)
if success:
print("All resources have successfully been uploaded.")
logger.info("All resources have successfully been uploaded.")
return success
def _upload_resources(
resources: list[XMLResource],
imgdir: str,
sipi_server: Sipi,
permissions_lookup: dict[str, Permissions],
resclass_name_2_type: dict[str, type],
id2iri_mapping: dict[str, str],
con: Connection,
failed_uploads: list[str],
metrics: list[MetricRecord],
preprocessing_done: bool,
) -> tuple[dict[str, str], list[str], list[MetricRecord]]:
"""
Iterates through all resources and tries to upload them to DSP.
If a temporary exception occurs, the action is repeated until success,
and if a permanent exception occurs, the resource is skipped.
Args:
resources: list of XMLResources to upload to DSP
imgdir: folder containing the multimedia files
sipi_server: Sipi instance
permissions_lookup: maps permission strings to Permission objects
resclass_name_2_type: maps resource class names to their types
id2iri_mapping: mapping of ids from the XML file to IRIs in DSP (initially empty, gets filled during the upload)
con: connection to DSP
failed_uploads: ids of resources that could not be uploaded (initially empty, gets filled during the upload)
metrics: list with the metric records collected until now (gets filled during the upload)
preprocessing_done: if set, all multimedia files referenced in the XML file must already be on the server
Returns:
id2iri_mapping, failed_uploads, metrics
"""
# If there are multimedia files: calculate their total size
bitstream_all_sizes_mb = [
Path(Path(imgdir) / Path(res.bitstream.value)).stat().st_size / 1000000
if res.bitstream and not preprocessing_done
else 0.0
for res in resources
]
if sum(bitstream_all_sizes_mb) > 0:
bitstream_size_total_mb = round(sum(bitstream_all_sizes_mb), 1)
bitstream_size_uploaded_mb = 0.0
print(f"This xmlupload contains multimedia files with a total size of {bitstream_size_total_mb} MB.")
logger.info(f"This xmlupload contains multimedia files with a total size of {bitstream_size_total_mb} MB.")
else: # make Pylance happy
bitstream_size_total_mb = 0.0
bitstream_size_uploaded_mb = 0.0
for i, resource in enumerate(resources):
resource_start = datetime.now()
filetype = ""
filesize = round(bitstream_all_sizes_mb[i], 1)
bitstream_duration_ms = None
resource_iri = resource.iri
if resource.ark:
resource_iri = _convert_ark_v0_to_resource_iri(resource.ark)
# in case of a multimedia resource: upload the multimedia file
resource_bitstream = None
if preprocessing_done and resource.bitstream:
resource_bitstream = resource.get_bitstream(resource.bitstream.value, permissions_lookup)
elif resource.bitstream:
pth = resource.bitstream.value
try:
bitstream_start = datetime.now()
filetype = Path(pth).suffix[1:]
img: Optional[dict[Any, Any]] = try_network_action(
sipi_server.upload_bitstream,
filepath=str(Path(imgdir) / Path(pth)),
)
bitstream_duration = datetime.now() - bitstream_start
bitstream_duration_ms = bitstream_duration.seconds * 1000 + int(bitstream_duration.microseconds / 1000)
mb_per_sec = round((filesize / bitstream_duration_ms) * 1000, 1)
metrics.append(
MetricRecord(resource.id, filetype, filesize, "bitstream upload", bitstream_duration_ms, mb_per_sec)
)
except BaseError as err:
err_msg = err.orig_err_msg_from_api or err.message
msg = f"Unable to upload file '{pth}' of resource '{resource.label}' ({resource.id})"
print(f"WARNING: {msg}: {err_msg}")
logger.warning(msg, exc_info=True)
failed_uploads.append(resource.id)
continue
bitstream_size_uploaded_mb += bitstream_all_sizes_mb[i]
msg = f"Uploaded file '{pth}' ({bitstream_size_uploaded_mb:.1f} MB / {bitstream_size_total_mb} MB)"
print(msg)
logger.info(msg)
internal_file_name_bitstream = img["uploadedFiles"][0]["internalFilename"] # type: ignore[index]
resource_bitstream = resource.get_bitstream(internal_file_name_bitstream, permissions_lookup)
# create the resource in DSP
resclass_type = resclass_name_2_type[resource.restype]
properties = resource.get_propvals(id2iri_mapping, permissions_lookup)
try:
resource_instance: ResourceInstance = resclass_type(
con=con,
label=resource.label,
iri=resource_iri,
permissions=permissions_lookup.get(str(resource.permissions)),
creation_date=resource.creation_date,
bitstream=resource_bitstream,
values=properties,
)
resource_creation_start = datetime.now()
created_resource: ResourceInstance = try_network_action(resource_instance.create)
resource_creation_duration = datetime.now() - resource_creation_start
resource_creation_duration_ms = resource_creation_duration.seconds * 1000 + int(
resource_creation_duration.microseconds / 1000
)
metrics.append(
MetricRecord(resource.id, filetype, filesize, "resource creation", resource_creation_duration_ms, "")
)
except BaseError as err:
err_msg = err.orig_err_msg_from_api or err.message
print(f"WARNING: Unable to create resource '{resource.label}' ({resource.id}): {err_msg}")
log_msg = (
f"Unable to create resource '{resource.label}' ({resource.id})\n"
f"Resource details:\n{vars(resource)}\n"
f"Property details:\n" + "\n".join([str(vars(prop)) for prop in resource.properties])
)
logger.warning(log_msg, exc_info=True)
failed_uploads.append(resource.id)
continue
id2iri_mapping[resource.id] = created_resource.iri
resource_designation = f"'{created_resource.label}' (ID: '{resource.id}', IRI: '{created_resource.iri}')"
print(f"Created resource {i+1}/{len(resources)}: {resource_designation}")
logger.info(f"Created resource {i+1}/{len(resources)}: {resource_designation}")
resource_duration = datetime.now() - resource_start
resource_duration_ms = resource_duration.seconds * 1000 + int(resource_duration.microseconds / 1000)
looping_overhead_ms = resource_duration_ms - resource_creation_duration_ms - (bitstream_duration_ms or 0)
metrics.append(MetricRecord(resource.id, filetype, filesize, "looping overhead", looping_overhead_ms, ""))
return id2iri_mapping, failed_uploads, metrics
def _upload_stashed_xml_texts(
verbose: bool,
id2iri_mapping: dict[str, str],
con: Connection,
stashed_xml_texts: dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]],
) -> dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]]:
"""
After all resources are uploaded, the stashed xml texts must be applied to their resources in DSP.
Args:
verbose: bool
id2iri_mapping: mapping of ids from the XML file to IRIs in DSP
con: connection to DSP
stashed_xml_texts: all xml texts that have been stashed
Returns:
nonapplied_xml_texts: the xml texts that could not be uploaded
"""
print("Upload the stashed XML texts...")
logger.info("Upload the stashed XML texts...")
nonapplied_xml_texts = stashed_xml_texts.copy()
for resource, link_props in stashed_xml_texts.items():
if resource.id not in id2iri_mapping:
# resource could not be uploaded to DSP, so the stash cannot be uploaded either
# no action necessary: this resource will remain in nonapplied_xml_texts,
# which will be handled by the caller
continue
res_iri = id2iri_mapping[resource.id]
try:
existing_resource = try_network_action(con.get, route=f"/v2/resources/{quote_plus(res_iri)}")
except BaseError as err:
# print the message to keep track of the cause for the failure. Apart from that, no action is necessary:
# this resource will remain in nonapplied_xml_texts, which will be handled by the caller
orig_err_msg = err.orig_err_msg_from_api or err.message
err_msg = (
f"Unable to upload XML texts of resource '{resource.id}', "
"because the resource cannot be retrieved from the DSP server."
)
print(f" WARNING: {err_msg} Original error message: {orig_err_msg}")
logger.warning(err_msg, exc_info=True)
continue
print(f' Upload XML text(s) of resource "{resource.id}"...')
logger.info(f' Upload XML text(s) of resource "{resource.id}"...')
for link_prop, hash_to_value in link_props.items():
existing_values = existing_resource[link_prop.name]
if not isinstance(existing_values, list):
existing_values = [existing_values]
for existing_value in existing_values:
old_xmltext = existing_value.get("knora-api:textValueAsXml")
if not old_xmltext:
# no action necessary: this property will remain in nonapplied_xml_texts,
# which will be handled by the caller
continue
# strip all xml tags from the old xmltext, so that the pure text itself remains
pure_text = regex.sub(r"(<\?xml.+>\s*)?<text>\s*(.+)\s*<\/text>", r"\2", old_xmltext)
# if the pure text is a hash, the replacement must be made. This hash originates from
# _stash_circular_references(), and identifies the XML texts
if pure_text not in hash_to_value:
# no action necessary: this property will remain in nonapplied_xml_texts,
# which will be handled by the caller
continue
new_xmltext = hash_to_value[pure_text]
# replace the outdated internal ids by their IRI
for _id, _iri in id2iri_mapping.items():
new_xmltext.regex_replace(f'href="IRI:{_id}:IRI"', f'href="{_iri}"')
# prepare API call
jsonobj = {
"@id": res_iri,
"@type": resource.restype,
link_prop.name: {
"@id": existing_value["@id"],
"@type": "knora-api:TextValue",
"knora-api:textValueAsXml": new_xmltext,
"knora-api:textValueHasMapping": {"@id": "http://rdfh.ch/standoff/mappings/StandardMapping"},
},
"@context": existing_resource["@context"],
}
jsondata = json.dumps(jsonobj, indent=4, separators=(",", ": "), cls=KnoraStandoffXmlEncoder)
# execute API call
try:
try_network_action(con.put, route="/v2/values", jsondata=jsondata)
except BaseError as err:
# print the message to keep track of the cause for the failure.
# Apart from that, no action is necessary:
# this resource will remain in nonapplied_xml_texts, which will be handled by the caller
orig_err_msg = err.orig_err_msg_from_api or err.message
err_msg = f"Unable to upload the xml text of '{link_prop.name}' of resource '{resource.id}'."
print(f" WARNING: {err_msg} Original error message: {orig_err_msg}")
logger.warning(err_msg, exc_info=True)
continue
nonapplied_xml_texts[resource][link_prop].pop(pure_text)
if verbose:
print(f' Successfully uploaded xml text of "{link_prop.name}"\n')
logger.info(f' Successfully uploaded xml text of "{link_prop.name}"\n')
# make a purged version of nonapplied_xml_texts, without empty entries
nonapplied_xml_texts = _purge_stashed_xml_texts(
stashed_xml_texts=nonapplied_xml_texts,
id2iri_mapping=id2iri_mapping,
)
return nonapplied_xml_texts
def _purge_stashed_xml_texts(
stashed_xml_texts: dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]],
id2iri_mapping: dict[str, str],
) -> dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]]:
"""
Accepts a stash of XML texts and purges it of resources that could not be uploaded (=don't exist in DSP), and of
resources that had all their XML texts reapplied. It returns a new dict with only the resources that exist in DSP,
but whose XML texts could not all be uploaded.
Args:
stashed_xml_texts: the stash to purge
id2iri_mapping: used to check if a resource could be uploaded
Returns:
a purged version of stashed_xml_text
"""
# remove resources that couldn't be uploaded. If they don't exist in DSP, it's not worth caring about their xmltexts
stashed_xml_texts = {res: propdict for res, propdict in stashed_xml_texts.items() if res.id in id2iri_mapping}
# remove resources that don't have stashed xmltexts (=all xmltexts had been reapplied)
nonapplied_xml_texts: dict[XMLResource, dict[XMLProperty, dict[str, KnoraStandoffXml]]] = {}
for res, propdict in stashed_xml_texts.items():
for prop, xmldict in propdict.items():
if len(xmldict) > 0:
if res not in nonapplied_xml_texts:
nonapplied_xml_texts[res] = {}
nonapplied_xml_texts[res][prop] = xmldict
return nonapplied_xml_texts
def _upload_stashed_resptr_props(
verbose: bool,
id2iri_mapping: dict[str, str],
con: Connection,
stashed_resptr_props: dict[XMLResource, dict[XMLProperty, list[str]]],
) -> dict[XMLResource, dict[XMLProperty, list[str]]]:
"""
After all resources are uploaded, the stashed resptr props must be applied to their resources in DSP.
Args:
verbose: bool
id2iri_mapping: mapping of ids from the XML file to IRIs in DSP
con: connection to DSP
stashed_resptr_props: all resptr props that have been stashed