forked from CESNET/mad_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_operations.py
94 lines (83 loc) · 2.58 KB
/
xml_operations.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
import time
import xml.etree.ElementTree as ET
from shutil import copyfile
import os
import logging
import random
class XmlOperator:
def __init__(self):
pass
logging.debug("XmlOperator object created")
self.output_base_directory_name = (
"xml_output-"
+ str(round(time.time()))
+ "-"
+ str(random.randint(1, 1000000))
)
if not os.path.exists("xml_outputs/" + self.output_base_directory_name):
os.mkdir("xml_outputs/" + self.output_base_directory_name)
logging.debug(
"xml_outputs/" + self.output_base_directory_name + " was created"
)
def output(self, instance):
type = instance.__class__.__name__.lower()
template_file_destination = "xml_templates/" + type + ".xml"
output_file_name = (
type
+ "_output_"
+ str(instance.id)
+ "-"
+ str(round(time.time()))
+ ".xml"
)
output_directory_name = type + "_outputs"
output_file_destination = (
"xml_outputs/"
+ self.output_base_directory_name
+ "/"
+ output_directory_name
+ "/"
+ output_file_name
)
if (
os.path.exists(
"xml_outputs/"
+ self.output_base_directory_name
+ "/"
+ output_directory_name
)
== False
):
os.mkdir(
"xml_outputs/"
+ self.output_base_directory_name
+ "/"
+ output_directory_name
)
logging.debug(
"xml_outputs/"
+ self.output_base_directory_name
+ "/"
+ output_directory_name
+ " was created"
)
copyfile(template_file_destination, output_file_destination)
logging.debug(
"Template file copied: "
+ template_file_destination
+ " to "
+ output_file_destination
)
tree = ET.parse(output_file_destination)
root = tree.getroot()
for key, value in instance.__dict__.items():
# changing the elem text
for elem in root.iter(key.upper()):
elem.text = str(value)
break
tree.write(output_file_destination)
logging.debug(
output_file_destination
+ " generated. Attributes changed: "
+ str(instance.__dict__)
)