-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGNtoURIfolder.py
99 lines (74 loc) · 2.65 KB
/
TGNtoURIfolder.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 os
key = ''
isPath = True
try:
############################
### Importpath xml files ###
############################
# Test if path contains xmlFile
while isPath:
# Import
importFolder = Path(input('Enter input folder: '))
if '.xml' in str(importFolder):
print('Please enter a folder path, not a file path. For further information see README.md.')
else:
isPath = False
isPath = True
# Save filenames
files = os.listdir(importFolder)
###########################
### Exportpath xml file ###
###########################
# Export
while isPath:
exportFolder = Path(input('Enter different output folder: '))
if '.xml' in str(exportFolder):
print('Please enter a folder path, not a file path. For further information see README.md.')
elif importFolder == exportFolder:
print('Please enter different folder.')
else:
isPath = False
except FileNotFoundError as fnfError:
print(fnfError)
except ValueError as valueError:
print(valueError)
else:
##############
### Import ###
##############
for file in files:
if '.xml' in file:
try:
tree = ET.parse(importFolder / file)
root = tree.getroot()
except FileNotFoundError as fnfError:
print(fnfError)
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
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)
###########################
### Export new xml file ###
###########################
try:
# write new xml document
exportFile = str(exportFolder / file)
except FileNotFoundError as fnfError:
print(fnfError)
else:
tree.write(exportFile)
print('Status: ' + file + ' exported', datetime.datetime.now())