From: Stefan Pluecken Date: Thu, 11 Jun 2009 10:46:49 +0000 (+0200) Subject: add a format converter, i.e. to convert from lamedb to sat.xml files X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=commitdiff_plain;h=510fe63aa7bf232fcca7f2a3d0fdab2c45307b78;ds=sidebyside add a format converter, i.e. to convert from lamedb to sat.xml files the main destination format of this converter is a sat.xml file easily extendable with further datasources --- diff --git a/tools/host_tools/FormatConverter/README b/tools/host_tools/FormatConverter/README new file mode 100644 index 0000000..8584b59 --- /dev/null +++ b/tools/host_tools/FormatConverter/README @@ -0,0 +1,7 @@ +extendable format converter + +Can be used to convert lamedb to a satellites.xml file, including TSID/ONID entries for example to check diseqc setups with the enigma2 diseqc checker plugin. + +Just start lamedb2satxml for usage information. + +A more general usage interface is present in main.py, where you can call the different modules directly via a console user interface. diff --git a/tools/host_tools/FormatConverter/datasource.py b/tools/host_tools/FormatConverter/datasource.py new file mode 100644 index 0000000..6969d3b --- /dev/null +++ b/tools/host_tools/FormatConverter/datasource.py @@ -0,0 +1,109 @@ +from input import inputChoices + +class datasource: + def __init__(self): + self.clear() + + def setDatasources(self, datasources): + self.datasources = datasources + + def getCapabilities(self): + return [] + + def getName(self): + return "N/A" + + def getStatus(self): + text = str(len(self.transponderlist.keys())) + " Satellites" + "\n" + return text + + def printAll(self): + for sat in self.transponderlist.keys(): + print "***********" + print "sat:", sat, self.satnames[sat] + for transponder in self.transponderlist[sat]: + print transponder + + def clear(self): + self.transponderlist = {} + self.satnames = {} + + def read(self): + pass + + def write(self): + pass + + def addSat(self, satname, satpos): + if not self.transponderlist.has_key(satpos): + self.transponderlist[satpos] = [] + self.satnames[satpos] = satname + + def addTransponder(self, satpos, transponder): + if len(transponder.keys()) >= 6: + self.transponderlist[satpos].append(transponder) + +class genericdatasource(datasource): + def __init__(self): + datasource.__init__(self) + self.source = self.destination = None + + def getName(self): + return "Generic Datasource" + + def getCapabilities(self): + return [("copy data from one source to another", self.copy), ("merge data from one source into another", self.merge)] + + def copy(self): + self.copymerge(action = "copy") + + def merge(self): + self.copymerge(action = "merge") + + def copymerge(self, action = "copy"): + choice = -1 + while choice is not None: + choice = inputChoices(["select source", "select destination", "copy now!"]) + if choice == 0: + print "\nselect source:" + self.source = self.selectDatasource() + elif choice == 1: + print "\nselect destination" + self.destination = self.selectDatasource() + elif choice == 2: + self.docopymerge(action) + + def docopymerge(self, action = "copy"): + if self.source is None: + print "select a source first!" + elif self.destination is None: + print "select a destination first!" + else: + if action == "copy": + print "copying ", + elif action == "merge": + print "merging ", + print "from %s to %s" % (self.source.getName(), self.destination.getName()) + countsat = 0 + counttransponder = 0 + if action == "copy": + self.destination.clear() + for satpos in self.source.transponderlist.keys(): + countsat += 1 + self.destination.addSat(self.source.satnames[satpos], satpos) + for transponder in self.source.transponderlist[satpos]: + counttransponder += 1 + self.destination.addTransponder(satpos, transponder) + print "copied %d sats with %d transponders" % (countsat, counttransponder) + + def selectDatasource(self): + list = [] + sources = [] + for source in self.datasources: + if source != self: + list.append(source.getName() + (" (%d sats)" % len(source.transponderlist.keys()))) + sources.append(source) + choice = inputChoices(list) + if choice is None: + return None + return sources[choice] \ No newline at end of file diff --git a/tools/host_tools/FormatConverter/datasource.pyc b/tools/host_tools/FormatConverter/datasource.pyc new file mode 100644 index 0000000..e9d90ab Binary files /dev/null and b/tools/host_tools/FormatConverter/datasource.pyc differ diff --git a/tools/host_tools/FormatConverter/input.py b/tools/host_tools/FormatConverter/input.py new file mode 100644 index 0000000..7a20627 --- /dev/null +++ b/tools/host_tools/FormatConverter/input.py @@ -0,0 +1,23 @@ +import sys + +def inputText(): + input = sys.stdin.readline() + return input.strip() + +def inputChoices(list, backcmd = "b", backtext = "back"): + repeat = True + while(repeat): + repeat = False + count = 0 + for item in list: + print count, "-", item + count += 1 + print backcmd, "-", backtext + input = inputText() + if input == backcmd: + return None + + action = int(input) + if action >= len(list): + repeat = True + return action \ No newline at end of file diff --git a/tools/host_tools/FormatConverter/input.pyc b/tools/host_tools/FormatConverter/input.pyc new file mode 100644 index 0000000..081fff6 Binary files /dev/null and b/tools/host_tools/FormatConverter/input.pyc differ diff --git a/tools/host_tools/FormatConverter/lamedb.py b/tools/host_tools/FormatConverter/lamedb.py new file mode 100644 index 0000000..1d8d2ec --- /dev/null +++ b/tools/host_tools/FormatConverter/lamedb.py @@ -0,0 +1,81 @@ +from datasource import datasource + +class lamedb(datasource): + def __init__(self, filename = "lamedb"): + datasource.__init__(self) + self.setFilename(filename) + + def setFilename(self, filename): + self.filename = filename + + def getName(self): + return "lamedb" + + def getCapabilities(self): + return [("read file", self.read), ("print all", self.printAll)] + + def read(self): + inputfile = open(self.filename, "r") + lines = inputfile.readlines() + inputfile.close() + + versionstring = lines[0].split('/') + version = int(versionstring[1]) + if 3 > version or 4 < version: + print "unsupported lamedb version" + return + + transpondersreading = False + sats = {} + transponders = {} + for line in lines: + if line.strip() == "transponders": + transpondersreading = True + continue + if line.strip() == "services": + transpondersreading = False + continue + if transpondersreading: + if ord(line[0]) == 9: + transponder = line.strip().split(' ')[1].split(':') + sat = transponder[4] + if not sats.has_key(sat): + sats[sat] = [] + sats[sat].append((transponder, tsid, onid)) + tsid = None + onid = None + elif line.strip() != "/" and line.strip() != "end": + data = line.strip().split(":") + tsid = str(int(data[1], 16)) + onid = str(int(data[2], 16)) + satlist = sats.keys() + satlist.sort() + + for sat in satlist: + print sat + self.addSat(sat, sat) + transponders = sats[sat] + transponders.sort(key = lambda a: a[0]) + for transpondertuple in transponders: + transponder = transpondertuple[0] + tsid = transpondertuple[1] + onid = transpondertuple[2] + print transponder, tsid, onid + tmp_transponder = {} + tmp_transponder["frequency"] = transponder[0] + tmp_transponder["symbol_rate"] = transponder[1] + tmp_transponder["polarization"] = transponder[2] + tmp_transponder["fec"] = transponder[3] + if version == 3: + if len(transponder) > 6: + tmp_transponder["system"] = transponder[6] + tmp_transponder["modulation"] = transponder[7] + elif version == 4: + if len(transponder) > 7: + tmp_transponder["system"] = transponder[7] + tmp_transponder["modulation"] = transponder[8] + if (tsid != "1" or onid != "1"): + tmp_transponder["tsid"] = transponder[0] + tmp_transponder["onid"] = transponder[0] + self.addTransponder(sat, tmp_transponder) + \ No newline at end of file diff --git a/tools/host_tools/FormatConverter/lamedb.pyc b/tools/host_tools/FormatConverter/lamedb.pyc new file mode 100644 index 0000000..ee06dbd Binary files /dev/null and b/tools/host_tools/FormatConverter/lamedb.pyc differ diff --git a/tools/host_tools/FormatConverter/lamedb2satxml.py b/tools/host_tools/FormatConverter/lamedb2satxml.py new file mode 100755 index 0000000..d6adc1f --- /dev/null +++ b/tools/host_tools/FormatConverter/lamedb2satxml.py @@ -0,0 +1,21 @@ +#!/usr/bin/python +from datasource import genericdatasource +from satxml import satxml +from lamedb import lamedb + +import sys + +if len(sys.argv) != 3: + print "usage: %s " % sys.argv[0] + sys.exit() + +gen = genericdatasource() +db = lamedb(sys.argv[1]) +xml = satxml(sys.argv[2]) + +db.read() +gen.source = db +gen.destination = xml +gen.docopymerge(action = "copy") +xml.write() + diff --git a/tools/host_tools/FormatConverter/main.py b/tools/host_tools/FormatConverter/main.py new file mode 100755 index 0000000..5a36b5f --- /dev/null +++ b/tools/host_tools/FormatConverter/main.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +from datasource import genericdatasource +from satxml import satxml +from lamedb import lamedb +from input import * +import sys +import os + +maindata = genericdatasource() + +sources = [satxml, lamedb] + +datasources = [maindata] + +for source in sources: + datasources.append(source()) + +for source in datasources: + source.setDatasources(datasources) + +while(True): + os.system("/usr/bin/clear") + list = [] + for index in range(len(datasources)): + list.append(datasources[index].getName() + (" (%d sats)" % len(datasources[index].transponderlist.keys()))) + index = inputChoices(list, "q", "quit") + if index is None: + break + + while(True): + print datasources[index].getStatus() + list = [] + for action in datasources[index].getCapabilities(): + list.append(action[0]) + action = inputChoices(list) + if action is None: + break + + datasources[index].getCapabilities()[action][1]() + #except: + # print sys.exc_info() + # print "sorry, could not execute that command" + diff --git a/tools/host_tools/FormatConverter/satxml.py b/tools/host_tools/FormatConverter/satxml.py new file mode 100644 index 0000000..5e2069e --- /dev/null +++ b/tools/host_tools/FormatConverter/satxml.py @@ -0,0 +1,87 @@ +import os +from datasource import datasource +from xml.dom import minidom +from xml.dom.minidom import Document +from input import inputText + +class satxml(datasource): + def __init__(self, filename = "satellites.xml"): + self.filename = filename + datasource.__init__(self) + + if not os.path.isfile(filename): + print "File %s doesn't exist. Creating it." % filename + + def getStatus(self): + text = datasource.getStatus(self) + return text + + def getCapabilities(self): + return [("set filename", self.setFilename), ("read file", self.read), ("write file", self.write), ("print all", self.printAll)] + + def getName(self): + return "satellites.xml" + + def setFilename(self): + print "Please give a filename :" + filename = inputText() + if filename == "": + self.filename = "satellites.xml" + else: + self.filename = filename + print "Filename set to %s" % self.filename + + def read(self): + basicsatxml = minidom.parse(self.filename) + + for sat in basicsatxml.firstChild.childNodes: + if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat": + print sat.localName + satname = str(sat.getAttribute("name")) + satpos = str(sat.getAttribute("position")) + self.addSat(satname, satpos) + for transponder in sat.childNodes: + if transponder.nodeType == transponder.ELEMENT_NODE and transponder.localName == "transponder": + parameters = {} + paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"] + for param in paramlist: + entry = str(transponder.getAttribute(param)) + if entry != "": + parameters[param] = entry + if len(parameters.keys()) > 1: + self.addTransponder(satpos, parameters) + print self.transponderlist + + def write(self): + satxml = Document() + satellites = satxml.createElement("satellites") + satxml.appendChild(satellites) + satlist = self.transponderlist.keys() + print self.transponderlist + satlist.sort() + + for sat in satlist: + xmlsat = satxml.createElement("sat") + xmlsat.setAttribute("name", self.satnames[sat]) + xmlsat.setAttribute("flags", "1") + xmlsat.setAttribute("position", sat) + satellites.appendChild(xmlsat) + transponders = self.transponderlist[sat] + transponders.sort(key = lambda a: a["frequency"]) + + for transponder in transponders: + xmltransponder = satxml.createElement("transponder") + paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"] + for param in paramlist: + if transponder.has_key(param): + xmltransponder.setAttribute(param, transponder[param]) + xmlsat.appendChild(xmltransponder) + prettyxml = satxml.toprettyxml() + print prettyxml + file = open(self.filename, "w") + file.write(prettyxml) + file.close() + + + + \ No newline at end of file diff --git a/tools/host_tools/FormatConverter/satxml.pyc b/tools/host_tools/FormatConverter/satxml.pyc new file mode 100644 index 0000000..b5a6b0d Binary files /dev/null and b/tools/host_tools/FormatConverter/satxml.pyc differ