General Functions
ListLookup
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_new
Creates a list lookup based on list labels in a specified language and returning list node names. Works for all lists in a project.json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_json_path
|
str | Path
|
path to a JSON project file (a.k.a. ontology) |
required |
language_of_label
|
str
|
label language used for the list |
required |
default_ontology
|
str
|
ontology prefix which is defined as default in the XML file |
required |
Returns:
Type | Description |
---|---|
ListLookup
|
|
Examples:
list_lookup = xmllib.ListLookup.create_new(
project_json_path="project.json",
language_of_label="en",
default_ontology="default-onto",
)
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
get_node_via_list_name
Returns the list node name based on a label.
The language of the label was specified when creating the ListLookup
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
list_name
|
str
|
name of the list |
required |
node_label
|
str
|
label of the node |
required |
Returns:
Type | Description |
---|---|
str
|
node name |
Examples:
node_name = list_lookup.get_node_via_list_name(
list_name="list1",
node_label="Label 1" # or: "label 1" (capitalisation is not relevant)
)
# node_name == "node1"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
get_list_name_and_node_via_property
Returns the list name and the node name based on a property that is used with the list and the label of a node.
The language of the label was specified when creating the ListLookup
.
The list name needs to be referenced in the XML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prop_name
|
str
|
name of the list |
required |
node_label
|
str
|
label of the node |
required |
Returns:
Type | Description |
---|---|
tuple[str, str]
|
list name and node name |
Examples:
list_name, node_name = list_lookup.get_list_name_and_node_via_property(
prop_name=":hasList", # or: "default-onto:hasList"
node_label="label 1"
)
# list_name == "list1"
# node_name == "node1"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
get_list_name_via_property
Returns the list name as specified in the ontology for a property. The list name needs to be referenced in the XML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prop_name
|
str
|
name of the property |
required |
Returns:
Type | Description |
---|---|
str
|
Name of the list |
Examples:
list_name = list_lookup.get_list_name_via_property(
prop_name=":hasList", # or: "default-onto:hasList"
)
# list_name == "list1"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_footnote_string
Takes the text for a footnote, and returns a string with the correct formatting.
You can use this if you want to add the footnote to a string.
Currently, the newline replacement options are restricted to LINEBREAK
and NONE
.
The reserved characters <
, >
and &
will be escaped temporarily,
but they will be correctly displayed in DSP-APP.
Attention
- The text in the footnote may be richtext, i.e. contain XML tags.
- Not all tags supported in ordinary richtext are currently implemented.
- The allowed tags are:
<br>
(break line)<strong>
(bold)<em>
(italic)<u>
(underline)<strike>
(strike through)<a href="URI">
(link to a URI)<a class="salsah-link" href="Knora IRI">
(link to a resource)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
footnote_text
|
str
|
Text for the footnote |
required |
newline_replacement_option
|
NewlineReplacement
|
options to replace newlines |
LINEBREAK
|
Returns:
Type | Description |
---|---|
str
|
The footnote as a string |
Examples:
result = xmllib.create_footnote_string("Text")
# result == '<footnote content="Text"/>'
result = xmllib.create_footnote_string("Text\nSecond Line")
# result == '<footnote content="Text<br/>Second Line"/>'
result = xmllib.create_footnote_string("Already escaped <>")
# already escaped characters will not be escaped again
# result == '<footnote content="Already escaped <>"/>'
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_footnote_element
Takes the text for a footnote, and returns an etree.Element
.
You can use this if you are working with lxml
.
Currently, the newline replacement options are restricted to LINEBREAK
and NONE
.
Attention
- The text in the footnote may be richtext, i.e. contain XML tags.
- Not all tags supported in ordinary richtext are currently implemented.
- The allowed tags are:
<br>
(break line)<strong>
(bold)<em>
(italic)<u>
(underline)<strike>
(strike through)<a href="URI">
(link to a URI)<a class="salsah-link" href="Knora IRI">
(link to a resource)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
footnote_text
|
str
|
Text for the footnote |
required |
newline_replacement_option
|
NewlineReplacement
|
options to replace newlines |
LINEBREAK
|
Returns:
Type | Description |
---|---|
_Element
|
The footnote as a string |
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_standoff_link_to_resource
Creates a standoff link to a resource.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resource_id
|
str
|
ID of the resource that is linked |
required |
displayed_text
|
str
|
text to display for the embedded link |
required |
Returns:
Type | Description |
---|---|
str
|
A standoff link in string form. |
Examples:
result = xmllib.create_standoff_link_to_resource("resource_id", "Text")
# result == '<a class="salsah-link" href="IRI:resource_id:IRI">Text</a>'
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_standoff_link_to_uri
Creates a standoff link to a URI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
the target URI that should be linked to |
required |
displayed_text
|
str
|
text to display for the embedded link |
required |
Returns:
Type | Description |
---|---|
str
|
A standoff link in string form. |
Examples:
result = xmllib.create_standoff_link_to_uri("https://www.dasch.swiss/", "This is DaSCH")
# result == '<a href="https://www.dasch.swiss/">This is DaSCH</a>'
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
get_list_nodes_from_string_via_list_name
Resolves list labels to node names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_with_list_labels
|
str
|
the string containing list labels |
required |
label_separator
|
str
|
separator in the string that contains the labels |
required |
list_name
|
str
|
name of the list |
required |
list_lookup
|
ListLookup
|
|
required |
Returns:
Type | Description |
---|---|
list[str]
|
A list of node names. If the string is empty, it returns an empty list. |
Examples:
string_with_list_labels = "Label 1; Label 2"
nodes = xmllib.get_list_nodes_from_string_via_list_name(
string_with_list_labels=string_with_list_labels,
label_separator=";",
list_name="list1",
list_lookup=list_lookup,
)
# nodes == ["node1", "node2"]
string_with_list_labels = ""
nodes = xmllib.get_list_nodes_from_string_via_list_name(
string_with_list_labels=string_with_list_labels,
label_separator=";",
list_name="list1",
list_lookup=list_lookup,
)
# nodes == []
string_with_list_labels = pd.NA
nodes = xmllib.get_list_nodes_from_string_via_list_name(
string_with_list_labels=string_with_list_labels,
label_separator=";",
list_name="list1",
list_lookup=list_lookup,
)
# nodes == []
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
get_list_nodes_from_string_via_property
Takes a string containing list labels, the separator by which they can be split, a property name and the list lookup. Resolves the labels and returns the list name to be referenced in the XML file and a list of node names. If the string is empty, it returns an empty list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_with_list_labels
|
str
|
the string containing the labels |
required |
label_separator
|
str
|
separator in the string that contains the labels |
required |
property_name
|
str
|
name of the property |
required |
list_lookup
|
ListLookup
|
|
required |
Returns:
Type | Description |
---|---|
tuple[str, list[str]]
|
The name of the list and a list of node names. |
Examples:
string_with_list_labels = "Label 1; Label 2"
list_name, nodes = xmllib.get_list_nodes_from_string_via_property(
string_with_list_labels=string_with_list_labels,
label_separator=";",
property_name=":hasList",
list_lookup=list_lookup,
)
# list_name == "list1"
# nodes == ["node1", "node2"]
string_with_list_labels = ""
list_name, nodes = xmllib.get_list_nodes_from_string_via_property(
string_with_list_labels=string_with_list_labels,
label_separator=";",
property_name=":hasList",
list_lookup=list_lookup,
)
# list_name == ""
# nodes == []
string_with_list_labels = pd.NA
list_name, nodes = xmllib.get_list_nodes_from_string_via_property(
string_with_list_labels=string_with_list_labels,
label_separator=";",
property_name=":hasList",
list_lookup=list_lookup,
)
# list_name == ""
# nodes == []
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
escape_reserved_xml_characters
From richtext strings (encoding="xml"), escape the reserved characters <
, >
and &
,
but only if they are not part of a standard standoff tag or escape sequence.
See the documentation for the standard standoff tags allowed by DSP-API, which will not be escaped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
the richtext string to be escaped |
required |
Returns:
Type | Description |
---|---|
str
|
The escaped richtext string |
Examples:
result = xmllib.escape_reserved_xml_characters("Text <unknownTag>")
# result == "Text <unknownTag>"
result = xmllib.escape_reserved_xml_characters("Text <br/> text after")
# result == "Text <br/> text after"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
make_xsd_compatible_id
An xsd:ID may not contain all types of special characters,
and it must start with a letter or underscore.
Replace illegal characters with _
, and prepend a leading _
if necessary.
The string must contain at least one Unicode letter (matching the regex \p{L}
),
_
, !
, ?
, or number, but must not be None
, <NA>
, N/A
, or -
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_value
|
str | float | int
|
input value |
required |
Returns:
Type | Description |
---|---|
str
|
An xsd ID compatible string based on the input value |
Examples:
result = xmllib.make_xsd_compatible_id("0_Universität_Basel")
# result == "_0_Universit_t_Basel"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
make_xsd_compatible_id_with_uuid
An xsd:ID may not contain all types of special characters,
and it must start with a letter or underscore.
Replace illegal characters with _
, and prepend a leading _
if necessary.
Additionally, add a UUID at the end.
The UUID will be different each time the function is called.
The string must contain at least one Unicode letter (matching the regex \p{L}
),
_
, !
, ?
, or number, but must not be None
, <NA>
, N/A
, or -
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_value
|
str | float | int
|
input value |
required |
Returns:
Type | Description |
---|---|
str
|
an xsd ID based on the input value, with a UUID attached. |
Examples:
result = xmllib.make_xsd_compatible_id_with_uuid("Universität_Basel")
# result == "Universit_t_Basel_88f5cd0b-f333-4174-9030-65900b17773d"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_list_from_string
Attention
This function is deprecated, use the new function called 'create_list_from_input' instead.
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
607 608 609 610 611 612 613 614 615 616 617 |
|
create_list_from_input
Create a list of strings from the input value, using the provided separator. If the input is empty it returns an empty list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_value
|
Any
|
input value to check and convert |
required |
separator
|
str
|
The character that separates the different values in the string. For example, a comma or newline. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
The list that results from splitting the input string. |
Examples:
result = xmllib.create_list_from_input(" one, two, three", ",")
# result == ["one", "two", "three"]
result = xmllib.create_list_from_input(1, "-")
# result == ["1"]
result = xmllib.create_list_from_input(" \n ", "\n")
# result == []
result = xmllib.create_list_from_input(None, ",")
# result == []
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
create_non_empty_list_from_string
Creates a list from a string. Trailing and leading whitespaces are removed from the list items.
If the resulting list is empty it will raise an XmllibInputError
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
input string |
required |
separator
|
str
|
The character that separates the different values in the string. For example, a comma or newline. |
required |
resource_id
|
str | None
|
If the ID of the resource is provided, a better error message can be composed |
None
|
prop_name
|
str | None
|
If the name of the property is provided, a better error message can be composed |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
The list that results from splitting the input string. |
Examples:
result = xmllib.create_non_empty_list_from_string("One\nTwo ", "\n")
# result == ["One", "Two"]
result = xmllib.create_non_empty_list_from_string(" \n/ ", "/")
# raises XmllibInputError
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
clean_whitespaces_from_string
Remove redundant whitespaces (space, \n
, \t
, etc.) and replace them with a single space.
If the resulting string is empty, a warning will be printed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
input string |
required |
Returns:
Type | Description |
---|---|
str
|
The cleaned string. |
Examples:
result = xmllib.clean_whitespaces_from_string("\t Text\nafter newline")
# result == "Text after newline"
result = xmllib.clean_whitespaces_from_string(" \n\t ")
# result == ""
# warns that the string is now empty
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|
find_license_in_string
Checks if a string contains a license, and returns it. Returns None if no license was found. The case (upper case/lower case) is ignored.
Look out: Your string should contain no more than 1 license. If it contains more, there is no guarantee which one will be returned.
See recommended licenses for details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
string to check |
required |
Returns:
Type | Description |
---|---|
License | None
|
|
Examples:
result = xmllib.find_license_in_string("CC BY")
# result == LicenseRecommended.CC.BY
result = xmllib.find_license_in_string("Creative Commons Developing Nations 2.0 Generic Deed")
# result == None
Currently supported license formats
- "AI" -> LicenseRecommended.DSP.AI_GENERATED
- "KI" -> LicenseRecommended.DSP.AI_GENERATED
- "IA" -> LicenseRecommended.DSP.AI_GENERATED
- "public domain" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "gemeinfrei" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "frei von Urheberrechten" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "urheberrechtsbefreit" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "libre de droits" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "domaine public" -> LicenseRecommended.DSP.PUBLIC_DOMAIN
- "unknown" -> LicenseRecommended.DSP.UNKNOWN
- "unbekannt" -> LicenseRecommended.DSP.UNKNOWN
- "inconnu" -> LicenseRecommended.DSP.UNKNOWN
- "CC BY" -> LicenseRecommended.CC.BY
- "Creative Commons BY 4.0" -> LicenseRecommended.CC.BY
- "CC 0 1.0" -> LicenseOther.Public.CC_0_1_0
- "CC PDM 1.0" -> LicenseOther.Public.CC_PDM_1_0
- "BORIS Standard License" -> LicenseOther.Various.BORIS_STANDARD
- "LICENCE OUVERTE 2.0" -> LicenseOther.Various.FRANCE_OUVERTE
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/general_functions.py
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 |
|