PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / tools / host_tools / FormatConverter / satxml.py
1 import os
2 from datasource import datasource
3 from xml.dom import minidom
4 from xml.dom.minidom import Document
5 from input import inputText
6
7 class satxml(datasource):
8         def __init__(self, filename = "satellites.xml"):
9                 self.filename = filename
10                 datasource.__init__(self)
11
12                 if not os.path.isfile(filename):
13                         print "File %s doesn't exist. Creating it." % filename
14
15         def getStatus(self):
16                 text = datasource.getStatus(self)
17                 return text
18
19         def getCapabilities(self):
20                 return [("set filename", self.setFilename), ("read file", self.read), ("write file", self.write), ("print all", self.printAll)]
21
22         def getName(self):
23                 return "satellites.xml"
24
25         def setFilename(self):
26                 print "Please give a filename <satellites.xml>:"
27                 filename = inputText()
28                 if filename == "":
29                         self.filename = "satellites.xml"
30                 else:
31                         self.filename = filename
32                 print "Filename set to %s" % self.filename
33
34         def read(self):
35                 basicsatxml = minidom.parse(self.filename)
36
37                 for sat in basicsatxml.firstChild.childNodes:
38                         if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat":
39                                 print sat.localName
40                                 satname = str(sat.getAttribute("name"))
41                                 satpos = str(sat.getAttribute("position"))
42                                 self.addSat(satname, satpos)
43                                 for transponder in sat.childNodes:
44                                         if transponder.nodeType == transponder.ELEMENT_NODE and transponder.localName == "transponder":
45                                                 parameters = {}
46                                                 paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"]
47                                                 for param in paramlist:
48                                                         entry = str(transponder.getAttribute(param))
49                                                         if entry != "":
50                                                                 parameters[param] = entry
51                                                 if len(parameters.keys()) > 1:
52                                                         self.addTransponder(satpos, parameters)
53                 print self.transponderlist
54
55         def write(self):
56                 satxml = Document()
57                 satellites = satxml.createElement("satellites")
58                 satxml.appendChild(satellites)
59                 satlist = self.transponderlist.keys()
60                 print self.transponderlist
61                 satlist.sort()
62
63                 for sat in satlist:
64                         xmlsat = satxml.createElement("sat")
65                         xmlsat.setAttribute("name", self.satnames[sat])
66                         xmlsat.setAttribute("flags", "1")
67                         xmlsat.setAttribute("position", sat)
68                         satellites.appendChild(xmlsat)
69                         transponders = self.transponderlist[sat]
70                         transponders.sort(key = lambda a: a["frequency"])
71
72                         for transponder in transponders:
73                                 xmltransponder = satxml.createElement("transponder")
74                                 paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"]
75                                 for param in paramlist:
76                                         if transponder.has_key(param):
77                                                 xmltransponder.setAttribute(param, transponder[param])
78                                 xmlsat.appendChild(xmltransponder)
79                 prettyxml = satxml.toprettyxml()
80                 print prettyxml
81                 file = open(self.filename, "w")
82                 file.write(prettyxml)
83                 file.close()