#!/usr/bin/python
import urllib2
from xml.dom import minidom, Node
def get_xml():
"""fetches xml file and parses it"""
#url_info = urllib2.urlopen('http://rennmaus.gysel.at/gerbils.xml')
url_info = urllib2.urlopen('file:gerbils.xml')
xmldoc = minidom.parse(url_info)
return xmldoc
def list_all():
"""called by function get_xml and read_nodes to list all xml entries in ordered form"""
rootNode = get_xml().documentElement
for node in rootNode.childNodes:
read_nodes(node, rootNode)
def list_single(name):
"""calls function search to display a single entry"""
rootNode = get_xml().documentElement
for node in rootNode.childNodes:
suche(node, name)
def help():
"""help function - called with "help", displays all program commands """
print "Hilfe zu diesem Programm:\n"
print "help oder ? - gibt diese Hilfe aus"
print "list - gibt alle verfuegbaren Rennmaeuse und all ihre Daten aus"
print "names - listet die Namen aller verfuegbaren Rennmaeuse auf"
print "quit / q - beendet das Programm"
print "search Name - sucht nach Maus die mit Name beginnt"
print "todo - zeigt TODO - Liste"
print "version - zeigt die aktuelle Version an"
def main():
""" This is the main function, works as a prompt to execute certain commands """
while 1:
command = raw_input("rennmaus> ")
if command == "list":
list_all()
elif command == "quit" or command == "q":
print "Bye!"
return;
elif command == "help" or command == "?":
help()
elif command == "version":
version()
elif command == "todo":
todo()
elif command.startswith('search'):
name = command[6:].lstrip()#killt die ersten 6 zeichen + alle spaces
print name
list_single(name)
elif command == "names":
read_names()
elif command == "add":
add_mouse()
else: print "Ungueltige Eingabe!"
def read_names():
"""lists all gerbils names"""
rootNode = get_xml().documentElement
print "All gerbil's names:"
for node in rootNode.childNodes:
if (node.nodeName == "gerbil"):
for gerbil_node in node.childNodes:
print_node(gerbil_node, node, "name", "Name: ")
def read_nodes(node, rootNode):
""" read_nodes function iterates over all nodes and subnodes - calls print_node to display them"""
if (node.nodeName == "gerbil"):
for gerbil_node in node.childNodes:
print_node(gerbil_node, node, "name", "Name: ")
print_node(gerbil_node, node, "born", "Geboren: ")
print_node(gerbil_node, node, "clan", "Zucht: ")
print_node(gerbil_node, node, "genetics", "Gencode: ")
print_node(gerbil_node, node, "colour", "Farbe: ")
if (gerbil_node.nodeName == "eps"):
print "Epilepsiedaten: \n"
for eps_node in gerbil_node.childNodes:
print_node(eps_node, node, "date", "Datum: ")
print_node(eps_node, node, "intensity", "Intensitaet: ")
print_node(eps_node, node, "comment", "Kommentar: ")
print "____________\n"
def suche(node, name):
"""Searches the XML database for a particular gerbil's data"""
if (node.nodeName == "gerbil"):
found = False
for gerbil_node in node.childNodes:
if (found == True or get_node(gerbil_node, node, "name").startswith(name)):
found = True
print_node(gerbil_node, node, "name", "Name: ")
print_node(gerbil_node, node, "born", "Geboren: ")
print_node(gerbil_node, node, "clan", "Zucht: ")
print_node(gerbil_node, node, "genetics", "Gencode: ")
print_node(gerbil_node, node,"colour","Farbe: ")
if(gerbil_node.nodeName == "eps"):
print "Epilepsiedaten: \n"
for eps_node in gerbil_node.childNodes:
print_node(eps_node, node, "date", "Datum: ")
print_node(eps_node, node, "intensity", "Intensitaet: ")
print_node(eps_node, node, "comment", "Kommentar: ")
print "\n"
def get_node(for_node, node, string):
"""get_node gets a node below gerbil_node"""
if (for_node.nodeName == string):
ret = ""
for text_node in for_node.childNodes:
if (text_node.nodeType == node.TEXT_NODE):
ret += text_node.nodeValue
else: return ""
return ret
def print_node(for_node, node, string, beschreibung):
"""print_node selects and reads a single node "string", checks its length, if the node isn't empty, get_node prints it"""
what = get_node(for_node, node, string)
if len(what) > 0:
print beschreibung + what
def add_mouse():
dom = get_xml()
gerbil = dom.createElement("gerbil") # creates
gerbil.appendChild(append_node(dom, "id", "100"))
gerbil.appendChild(append_node(dom, "name", raw_input("Name: ")))
gerbil.appendChild(append_node(dom, "clan", raw_input("Zucht: ")))
gerbil.appendChild(append_node(dom, "colour", raw_input("Farbe: ")))
dom.childNodes[0].appendChild(gerbil) # appends at end of 1st child's children
print dom.toxml()
save2file(dom.toxml())
def append_node(dom, name, func):
node = dom.createElement(name)
txt = dom.createTextNode(func)
node.appendChild(txt)
return node
def save2file(content):
file = open("gerbils.xml", "w")
file.writelines(content)
file.close()
def version():
""" prints actual version and version history"""
print "Version 0.00000001 - marula\n"
print "Version 0.00000002 - search enabled"
def todo():
"""todo list"""
print "TODO-LISTE"
print 10*"_"
print "\n"
print "Change gerbil data"
print "add new gerbils"
print "create family trees"
print "make menu"
print "indented notations"
print "Sometimes: PDF - Output of family trees"
main()