-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLFile.py
58 lines (45 loc) · 1.92 KB
/
XMLFile.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
import os
import shutil
import xml.etree.ElementTree as ET
from Directory import Directory
class XMLFile(object):
"""A Directory will have path and name with
following properties:
Attributes:
file_name: A string representing the directory name.
file_path: path where the directory will be created.
"""
def __init__(self, json_data, file_path=os.getcwd()):
self.json_data = json_data
self.set_file_name()
self.set_file_path(file_path)
self.set_file_root()
def set_file_name(self):
self.file_name = self.json_data.xml_file_name
def set_file_path(self, file_path):
self.file_path = os.path.join(file_path, self.file_name)
def set_file_root(self):
with open(self.file_path) as f:
tree = ET.parse(f)
self.file_xml_root = tree.getroot()
def get_file_path(self):
return self.file_path
def get_file_name(self):
return self.file_name
def get_xml_root(self):
return self.file_xml_root
def read_xml_file(self):
for elem in self.file_xml_root.getiterator():
if self.json_data.tag == elem.tag:
if self.json_data.attribute == "":
text_xml_to_search = elem.text
else:
text_xml_to_search = elem.get(self.json_data.attribute)
if text_xml_to_search in self.json_data.search_text_list:
output_inside_dir = Directory(text_xml_to_search, self.json_data.output_dir.get_path())
self.copydwf(output_inside_dir)
def copydwf(self, output_inside_dir):
path_list = self.file_path.split('\\')
src = os.path.join( self.json_data.input_dir.get_path(), path_list[-1] )
dst = os.path.join( output_inside_dir.get_path(), path_list[-1])
shutil.copyfile(src, dst)