-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm_generator.py
182 lines (144 loc) · 6.75 KB
/
alarm_generator.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
182
from xml.dom.minidom import Document, DOMImplementation
import pandas as pd
import os
import sys
import codecs
from xml.dom import minidom
import numpy as np
if not os.path.exists("topics/reference_alarms"):
os.makedirs("topics/reference_alarms")
def prepare_xml (group):
alarmIdNetAct = group ["alarmId NetAct"].iloc[0]
imp = DOMImplementation ()
doctype = imp.createDocumentType ("concept", "-//OASIS//DTD DITA Concept//EN", "concept.dtd")
doc = Document ()
doc.appendChild (doctype)
concept = doc.createElement ("concept")
concept.setAttribute ("id", f"_{alarmIdNetAct}")
title = doc.createElement ("title")
title_text = doc.createTextNode (f"{alarmIdNetAct}")
title.appendChild(title_text)
concept.appendChild(title)
conbody = doc.createElement ("conbody")
for key in group.columns:
p = doc.createElement ("p")
p_text = doc.createTextNode (key)
p.appendChild (p_text)
conbody.appendChild(p)
if len (group[key].unique()) > 1:
for value in group[key].unique ():
codeblock = doc.createElement ("codeblock")
if pd.isna(value):
codeblock_text = doc.createTextNode ("")
codeblock.appendChild (codeblock_text)
elif isinstance (value, (float, np.floating)) and value.is_integer():
codeblock_text = doc.createTextNode (str(int(value)))
codeblock.appendChild (codeblock_text)
else:
codeblock_text = doc.createTextNode (str(value))
codeblock.appendChild (codeblock_text)
conbody.appendChild (codeblock)
else:
codeblock = doc.createElement ("codeblock")
if pd.isna (group[key].iloc[0]):
codeblock_text = doc.createTextNode ("")
codeblock.appendChild(codeblock_text)
elif isinstance (group[key].iloc[0], (float, np.floating)) and group[key].iloc[0].is_integer():
codeblock_text = doc.createTextNode(str(int(group[key].iloc[0])))
else:
codeblock_text = doc.createTextNode(str(group[key].iloc[0]))
codeblock.appendChild(codeblock_text)
conbody.appendChild(codeblock)
concept.appendChild(conbody)
doc.appendChild(concept)
with codecs.open(f"topics/reference_alarms/alarms_{alarmIdNetAct}.xml", 'w', encoding='UTF-8') as f:
doc.writexml (f,indent = "\t", addindent = '\t', newl = '\n', encoding = 'UTF-8')
f.close()
def read_excel_data (file_name):
try:
df = pd.read_excel (file_name, sheet_name = "Alarm_list")
except ValueError:
raise ValueError ("'Alarm_list' sheet not found in file. Please check the sheet name or check if it exists!")
df = None
return df
if __name__ == '__main__':
try:
file_name = sys.argv[1]
df = read_excel_data (file_name)
except IndexError:
print("Please provide a filename as a command-line argument!")
sys.exit(1)
except FileNotFoundError:
print(f"File '{file_name}' does not exist. Please recheck for any mistypes and if the file exists at all!")
sys.exit(1)
except ValueError as e:
print (str(e))
sys.exit(1)
if df is not None:
for alarmIdNetAct, group in df.groupby ("alarmId NetAct"):
prepare_xml (group)
#How to create a DITA Map
def prepare_ditamap(alarmIdNetAct):
imp = DOMImplementation()
doctype = imp.createDocumentType ("map", "-//OASIS//DTD DITA Map//EN", "map.dtd")
doc = minidom.Document()
doc.appendChild(doctype)
map = doc.createElement ("map")
map.setAttribute ('\n'"base",
""" pdm-category(Core Network) pdm-subcategory(Mobile Circuit Core) pdm-product(Open Mobile Softswitch) """)
map.setAttribute ('\n'"id","troubleshoot_mss_alarms")
map.setAttribute ("outputclass", "sans-numbering")
doc.appendChild(map)
title = doc.createElement ("title")
title_text = doc.createTextNode ("Alarm List for CNCS")
title.appendChild (title_text)
map.appendChild(title)
topicmeta = doc.createElement ("topicmeta")
othermeta = doc.createElement ("othermeta")
othermeta.setAttribute ("content", "reference_alarms_DN1000111289")
othermeta.setAttribute ("name", "Filename")
topicmeta.appendChild (othermeta)
othermeta = doc.createElement ("othermeta")
othermeta.setAttribute ("content", "document")
othermeta.setAttribute ("name", "maptype")
topicmeta.appendChild (othermeta)
othermeta = doc.createElement ("othermeta")
othermeta.setAttribute ("content", "DN1000111289")
othermeta.setAttribute ("name", "docnumber")
topicmeta.appendChild (othermeta)
othermeta = doc.createElement ("othermeta")
othermeta.setAttribute ("content", "270291757")
othermeta.setAttribute ("name", "chronicleid")
topicmeta.appendChild (othermeta)
othermeta = doc.createElement ("othermeta")
othermeta.setAttribute ("content", "1-0-0")
othermeta.setAttribute ("name", "issue")
topicmeta.appendChild (othermeta)
map.appendChild(topicmeta)
topicref1 = doc.createElement ("topicref")
topicref1.setAttribute ("type", "concept")
topicref1.setAttribute ("href", "topics/reference_alarms/alarm_introduction.xml")
topicref1.setAttribute ("keys", "alarm_intro")
topicref2 = doc.createElement ("topicref")
topicref2.setAttribute ("type", "concept")
topicref2.setAttribute ("href", "topics/reference_alarms/alarm_description.xml")
topicref2.setAttribute ("keys", "alarm_desc")
topicref1.appendChild(topicref2)
map.appendChild(topicref1)
topicref3 = doc.createElement ("topicref")
topicref3.setAttribute ("type", "concept")
topicref3.setAttribute ("href", "topics/reference_alarms/alarm_list.xml")
topicref3.setAttribute ("keys", "alarm_list")
map.appendChild(topicref3)
for alarmIdNetAct, group in df.groupby("alarmId NetAct"):
topicref = doc.createElement("topicref")
topicref.setAttribute("type", "concept")
topicref.setAttribute("keys", f"alarms_{alarmIdNetAct}")
topicref.setAttribute("href", f"topics/reference_alarms/alarms_{alarmIdNetAct}.xml")
topicref3.appendChild(topicref)
map.appendChild(topicref3)
doc.appendChild(map)
with codecs.open(f"reference_alarms.ditamap", 'w', encoding='UTF-8') as f:
doc.writexml(f, indent="\t", addindent='\t', newl='\n', encoding='UTF-8')
f.close()
prepare_ditamap(alarmIdNetAct="alarmId NetAct")