-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteNodesFolder.py
181 lines (140 loc) · 5.7 KB
/
deleteNodesFolder.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
import datetime
from lxml import etree as ET
from pathlib import Path
import os
##############################
### Methode to delete node ###
##############################
def strip_node(element):
text = element.xpath('string()')
# Add tail to text
text += (element.tail or '')
if text is not None:
# get parent node
parent = element.getparent()
# get previous node
previous = element.getprevious()
if previous is not None:
if previous.tail is not None:
previous.tail += text
else:
previous.tail = text
else:
parent.text = (parent.text or '') + text
# removes node
parent.remove(element)
print('Status: Program started, ', datetime.datetime.now())
text = ''
isXML = True
isPath = True
rightChoice = True
deleteByName = False
deleteByAttribute = False
deleteByBoth = False
try:
###########################
### Importpath xml file ###
###########################
try:
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)
for file in files:
if '.xml' in file:
try:
# Open xml file
tree = ET.parse(str(importFolder / file))
root = tree.getroot()
except FileNotFoundError as fnfError:
print(fnfError)
except OSError as osError:
print(osError)
while rightChoice:
# Choice if delete node by node name or by attribute
choice = input('Do you want to delete a node by name, by attribute or by both? Enter \'1\' for node, \'2\' for attribute and \'3\' for both: ')
# Delete by name
if choice == '1':
rightChoice = False
deleteByName = True
nodeName = input('Enter node name: ')
addValue = input('Filter by node value? (y/n): ')
if addValue == 'y':
nodeValue = input('Enter node value to filter (part of value possible): ')
# Delete by attribute
elif choice == '2':
rightChoice = False
deleteByAttribute = True
attributeName = input('Enter attribute name: ')
addValue = input('Filter by attribute value? (y/n): ')
if addValue == 'y':
attributeValue = input('Enter attribute value to filter (part of value possible): ')
# Both
elif choice == '3':
rightChoice = False
deleteByBoth = True
nodeName = input('Enter node name: ')
attributeName = input('Enter attribute name: ')
withoutAttr = input('Node with attribute (1) or without the attribute (2): ')
addValue = input('Filter by attribute value? (y/n): ')
if addValue == 'y':
attributeValue = input('Enter attribute value to filter: ')
else:
print('Please enter \'1\', \'2\' or \'3\'. ')
# delete by name
if deleteByName:
for element in tree.xpath('.//' + nodeName):
# attribute name and value
if addValue == 'y':
if nodeValue in element.text():
strip_node(element)
else:
strip_node(element)
# delete by attribute
elif deleteByAttribute:
# attribute name
for element in tree.xpath('.//*[@' + attributeName + ']'):
# attribute name and value
if addValue == 'y':
if attributeValue in element.get(attributeName, ''):
strip_node(element)
else:
strip_node(element)
# delete by name and attribute
elif deleteByBoth:
for element in tree.xpath('.//'+nodeName+'[not(@'+attributeName+')]'):
if addValue == 'y':
if attributeValue in element.get(attributeName, ''):
strip_node(element)
else:
strip_node(element)
###########################
### Export new xml file ###
###########################
try:
tree.write(str(exportFolder / file))
print('Status: ' + file + ' exported ', datetime.datetime.now())
except FileNotFoundError as fnfError:
print(fnfError)
except ValueError as valueError:
print(valueError)