2008-01-03 20:59:36 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
this script fetches single nodes from a eventmanagement xml file
|
|
|
|
"""
|
2008-04-16 16:19:32 +02:00
|
|
|
from optparse import OptionParser
|
2008-01-03 20:59:36 +01:00
|
|
|
from xml.dom.minidom import parse
|
2008-04-19 11:51:31 +02:00
|
|
|
import os, mysql_connect, configobj
|
2008-04-16 16:19:32 +02:00
|
|
|
|
|
|
|
def main():
|
2008-04-19 11:51:31 +02:00
|
|
|
usage = "usage: %prog [options]"
|
2008-04-16 16:19:32 +02:00
|
|
|
parser = OptionParser(usage)
|
|
|
|
parser.add_option("-f", "--file", dest="filename",
|
|
|
|
help="read xml data from FILENAME")
|
|
|
|
parser.add_option("-c", "--config", dest="configfile",
|
|
|
|
help="read DB config from FILENAME")
|
2008-04-19 11:51:31 +02:00
|
|
|
parser.add_option("-v", "--verbose", dest="verbose",
|
|
|
|
help="change Loglevel (0-3)")
|
2008-04-16 16:19:32 +02:00
|
|
|
(options, args) = parser.parse_args()
|
2008-04-19 11:51:31 +02:00
|
|
|
if options.configfile:
|
|
|
|
print "getting config from: %s " % options.configfile
|
|
|
|
if not os.access(options.configfile, os.R_OK) or not os.path.isfile(options.configfile):
|
|
|
|
print "Error: could not read configuration file (%s)\n" % options.configfile
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
conffile = os.path.realpath(options.configfile)
|
|
|
|
prefs = get_preferences(conffile)
|
|
|
|
else:
|
|
|
|
print parser.print_help()
|
|
|
|
return
|
2008-04-16 16:19:32 +02:00
|
|
|
if options.filename:
|
|
|
|
print "reading XML file: %s " % options.filename
|
2008-04-19 11:51:31 +02:00
|
|
|
if not os.access(options.filename, os.R_OK) or not os.path.isfile(options.filename):
|
|
|
|
print "Error: could not read configuration file (%s)\n" % options.filename
|
|
|
|
return
|
2008-04-16 16:19:32 +02:00
|
|
|
else:
|
|
|
|
print parser.print_help()
|
|
|
|
return
|
2008-04-19 11:51:31 +02:00
|
|
|
|
|
|
|
loglevel = 0
|
|
|
|
if options.verbose: loglevel = int(options.verbose)
|
|
|
|
|
|
|
|
if loglevel >= 1: print prefs
|
|
|
|
|
2008-04-16 16:19:32 +02:00
|
|
|
dic = readXML(options.filename)
|
|
|
|
|
2008-04-19 11:51:31 +02:00
|
|
|
if "xmltags" in prefs.keys() :
|
|
|
|
xmltags = prefs["xmltags"]["tags"]
|
|
|
|
if loglevel >= 2: print xmltags
|
|
|
|
else:
|
|
|
|
print "Warning: using hardcoded XML structure.. "
|
|
|
|
xmltags = ["INFORMATION", "WANN_START_XML", "WANN_ENDE", "WO_XML", "WAS_XML", "RUBRIK_XML", "SONSTIGES"]
|
2008-04-16 16:19:32 +02:00
|
|
|
print
|
2008-04-19 11:51:31 +02:00
|
|
|
if loglevel >= 1: print "XML Quelle: ", dic["FMPDSORESULT"]["DATABASE"]
|
2008-04-16 16:19:32 +02:00
|
|
|
#print "Data 0", dic["FMPDSORESULT"]["ROW"][0]
|
|
|
|
#print "Data 1", dic["FMPDSORESULT"]["ROW"][1]
|
|
|
|
entry = 0
|
2008-04-19 11:51:31 +02:00
|
|
|
month = prefs["month"]["number"]
|
2008-04-16 16:19:32 +02:00
|
|
|
days = 0
|
|
|
|
data = []
|
|
|
|
test = mysql_connect.DataBase()
|
|
|
|
|
|
|
|
for item in dic["FMPDSORESULT"]["ROW"]:
|
|
|
|
entry += 1
|
|
|
|
if len(item["DATUM"]) > 1:
|
|
|
|
days +=1
|
|
|
|
text = "\tDatum: %s" % item["DATUM"]
|
|
|
|
#filename = "/tmp/szene"+str(days)
|
|
|
|
#f = open(filename,mode="a")
|
|
|
|
#f.write(text)
|
|
|
|
#print text
|
2008-04-19 11:51:31 +02:00
|
|
|
data.append(test.prepare_event(item, month, days))
|
2008-04-16 16:19:32 +02:00
|
|
|
'''
|
|
|
|
for content in xmltags:
|
|
|
|
text = ("%s: %s") % (content, item[content])
|
|
|
|
#f.write(text)
|
|
|
|
print text
|
|
|
|
'''
|
|
|
|
|
|
|
|
#f.close()
|
|
|
|
|
|
|
|
print "Tage durchlaufen: %i" % days
|
|
|
|
entries = len(dic["FMPDSORESULT"]["ROW"])
|
|
|
|
print "Eintraege insgesamt: %i" % entries
|
|
|
|
if entries != entry:
|
|
|
|
print "Es gibt %i Eintraege in der xml Datei aber nur %i wurden durchlaufen" % (entries, entry)
|
|
|
|
|
|
|
|
## finally put the mess into mysql db
|
|
|
|
test.connect()
|
|
|
|
print "Datenbank fuellen.."
|
|
|
|
#test.prepare_new_month("Mai")
|
|
|
|
#test.insert_event(data)
|
|
|
|
test.close()
|
|
|
|
|
2008-01-03 20:59:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NotTextNodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def getTextFromNode(node):
|
|
|
|
"""
|
|
|
|
scans through all children of node and gathers the
|
|
|
|
text. if node has non-text child-nodes, then
|
|
|
|
NotTextNodeError is raised.
|
|
|
|
"""
|
|
|
|
t = ""
|
|
|
|
for n in node.childNodes:
|
|
|
|
if n.nodeType == n.TEXT_NODE:
|
|
|
|
t += n.nodeValue
|
|
|
|
else:
|
|
|
|
raise NotTextNodeError
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
def nodeToDic(node):
|
|
|
|
"""
|
|
|
|
nodeToDic() scans through the children of node and makes a
|
|
|
|
dictionary from the content.
|
|
|
|
three cases are differentiated:
|
|
|
|
- if the node contains no other nodes, it is a text-node
|
|
|
|
and {nodeName:text} is merged into the dictionary.
|
|
|
|
- if there is more than one child with the same name
|
|
|
|
then these children will be appended to a list and this
|
|
|
|
list is merged to the dictionary in the form: {nodeName:list}.
|
|
|
|
- else, nodeToDic() will call itself recursively on
|
|
|
|
the nodes children (merging {nodeName:nodeToDic()} to
|
|
|
|
the dictionary).
|
|
|
|
"""
|
|
|
|
dic = {}
|
|
|
|
multlist = {} # holds temporary lists where there are multiple children
|
|
|
|
for n in node.childNodes:
|
|
|
|
multiple = False
|
|
|
|
if n.nodeType != n.ELEMENT_NODE:
|
|
|
|
continue
|
|
|
|
# find out if there are multiple records
|
|
|
|
if len(node.getElementsByTagName(n.nodeName)) > 1:
|
|
|
|
multiple = True
|
|
|
|
# and set up the list to hold the values
|
|
|
|
if not multlist.has_key(n.nodeName):
|
|
|
|
multlist[n.nodeName] = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
#text node
|
|
|
|
text = getTextFromNode(n).strip().encode('utf-8')
|
|
|
|
except NotTextNodeError:
|
|
|
|
if multiple:
|
|
|
|
# append to our list
|
|
|
|
multlist[n.nodeName].append(nodeToDic(n))
|
|
|
|
dic.update({n.nodeName:multlist[n.nodeName]})
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# 'normal' node
|
|
|
|
dic.update({n.nodeName:nodeToDic(n)})
|
|
|
|
continue
|
|
|
|
|
|
|
|
# text node
|
|
|
|
if multiple:
|
|
|
|
multlist[n.nodeName].append(text)
|
|
|
|
dic.update({n.nodeName:multlist[n.nodeName]})
|
|
|
|
else:
|
|
|
|
dic.update({n.nodeName:text})
|
|
|
|
return dic
|
|
|
|
|
|
|
|
|
2008-04-16 16:19:32 +02:00
|
|
|
def readXML(filename):
|
2008-01-03 20:59:36 +01:00
|
|
|
dom = parse(filename)
|
|
|
|
return nodeToDic(dom)
|
|
|
|
|
2008-04-19 11:51:31 +02:00
|
|
|
def get_preferences(config_file):
|
|
|
|
"""Load the CryptoBox configuration.
|
|
|
|
"""
|
|
|
|
import StringIO
|
|
|
|
try:
|
|
|
|
prefs = configobj.ConfigObj(config_file)
|
|
|
|
if prefs:
|
|
|
|
#print "found config: %s" % prefs.items()
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise CBConfigUnavailableError("failed to load the config file: %s" % config_file)
|
|
|
|
except IOError, err_msg:
|
|
|
|
raise CBConfigUnavailableError("unable to open the config file (%s): %s" % (config_file, err_msg))
|
|
|
|
except configobj.ConfigObjError, err_msg:
|
|
|
|
raise CBConfigError("failed to load config file (%s): %s" % (config_file, err_msg))
|
|
|
|
return prefs
|
2008-01-03 20:59:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2008-04-16 16:19:32 +02:00
|
|
|
main()
|
|
|
|
|