-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGNtoURI.py
99 lines (73 loc) · 2.42 KB
/
TGNtoURI.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
import re
from pathlib import Path
import xml.etree.ElementTree as ET
import datetime
##############
### Import ###
##############
print('Status: Program started', datetime.datetime.now())
key = ''
isXML = True
isPath = True
try:
###########################
### Importpath xml file ###
###########################
# Test if path contains xmlFile
while isXML:
# Import file
importFile = Path(input('Enter path to import file: '))
if '.xml' in str(importFile):
isXML = False
else:
print('No xml File detected, please try again. For further information see README.md.')
###########################
### Exportpath xml file ###
###########################
# Test if folder path or file path
while isPath:
# Export path
exportFolder = Path(input('Enter path to output folder: '))
if '.xml' in str(exportFolder):
print('Please enter a folder path, not a file path. For further information see README.md.')
else:
isPath = False
# Export file
exportFile = input('Enter output filename: ')
# if filename does not contain data format, add .xml
if '.xml' not in str(exportFile):
exportFile += '.xml'
# Open xml file
tree = ET.parse(importFile)
root = tree.getroot()
except FileNotFoundError as fnfError:
print(fnfError)
except ValueError as valueError:
print(valueError)
else:
#################
### Parse xml ###
#################
# find all name-nodes
for element in root.findall('.//*[@key]'):
# test if key = tgn no
if 'tgn' in element.get('key', ''):
# extract getty no from key attribute
# '\d' does not work?!
key = re.search(r'(tgn,)(.*)', element.get('key', '')).group(2)
# create getty URI
key = 'http://vocab.getty.edu/tgn/' + key
# add getty URI als ref attribute
element.set('ref', key)
# matches places; match function not supported
# //name[matches(@key, \'tgn.*\')]
# matches key value of places
# //name[matches(@key, 'tgn.*')]/string(@key)
###########################
### Export new xml file ###
###########################
try:
tree.write(exportFolder / exportFile)
print('Status: ' + exportFile + ' exported ', datetime.datetime.now())
except FileNotFoundError as fnfError:
print(fnfError)