#!/usr/bin/env python
__version__="dev0.3"
__author__="AGE"
__date__="02004-08-06"

import filmListXML

class MyFilms:
    """
    Container for filmdata.
    This is 'des Pudels Kern' - the core object, around which the hole
    code is designed :>

    It also handles to/from xml conversion 
    """
    xmlfile = ""
    filmlist = ""

    def __init__(self):
	### hier landen alle daten in leicht bearbeitbarer form
	# key 0 dient als platzhalter und zum abchecken der richtigen reihenfolge
	self.filmlist = {0:["title","language","codec","cd","comments"]}
	#self.setXMLFile(xmlfile)
	#self.fillfromXML()

    def setXMLFile(self, xmlfile):
	"""
	Set file from which to read or write to.

	@param String xmlfile : Name for file that is used for xml reading/writing
	"""
	self.xmlfile = xmlfile

    def fillfromXML(self):
	"""
	Fill dictionary 'filmlist' using an external method.
	"""
	filmtool = filmListXML.FilmListXML()
	# damit wird self.filmlist gefuellt (da self uebergeben wurde)
	filmtool.readInXML(self, self.xmlfile, printer=0)

    def saveXMLfromJoerchs(self, htmlfile, debug=0):
	"""
	Read Joerchs -> convert to xml -> save xml -> read xml into 'filmlist'
	After all, 'filmlist' is hopefully filled with the films from
	Joerchs html file.

	'hopefully' because if an error occurs, you are really out of luck.
	    (es ging irgendwo irgendwas irgendwie schief ;)
	In most cases you have to edit the html file yourself.
	Fortunately you the get the content read so far. If there
	seems anything corrupt delete/change the correspondig part in the
	html file and try again. This sucks but it's not worth more
	effort ;)

	TODO: write more general code, that reads other html files as well

	@return String : 
	"""
	self.filmlist = {0:["title","language","codec","cd","comments"]}
	filmtool = filmListXML.FilmListXML()
	debugout = filmtool.convertJoerchs2XML(self, htmlfile, debug)
	#gegen korrupte htmls hilft folgender output
	if debug>0:
	    temp = debugout
	    debugout = "\thtmlfilmlist:"
	    for i in temp:
		debugout += "\n"+str(i)
	filmtool.saveXMLList(self.xmlfile)
	#check it out
	#uebergibt wieder 'self', was gut gefuellt zurueck kommen sollte
	filmtool.readInXML(self, self.xmlfile, printer=0)
	return str(debugout)

    def saveToXMLFile(self):
	"""
	Give self to external method which saves 'filmlist' in a xml
	file.
	"""
	filmsafe = filmListXML.FilmListXML()
	filmsafe.myFilmsToXMLFile(self, self.xmlfile)
	
    def getFilm(self, id):
	"""
	Return one film of 'filmlist'.

	@param Int id : Number for the requested film
	@return List/String : List of one film's data / errormessage
	"""
	if self.filmlist.has_key(id):
	    return self.filmlist[id]
	else:
	    return "no film found with id: %s" % id

    def addFilm(self, data):
	"""
	Add a new film to 'filmlist'

	@param List data : Data of one film
	"""
	filmadd = filmListXML.FilmListXML()
	# naechst groesste id finden
	id = max(self.filmlist.keys())+1
	self.filmlist[id] = data
	filmadd.setSingleXMLData(self.filmlist)
	#filmadd.addToXMLList()
	#filmadd.saveXMLList("foo")

    def changeFilm(self, id):
	"""
	Overwrite the film responding to the given id
	(Is now obsolete: was used for module tests..)

	@param Int id : Number for the requested Film
	"""
	if self.filmlist.has_key(id):
	    filmadd = filmListXML.FilmListXML()
	    title = raw_input("name eingeben: ")
	    lang = raw_input("sprache eingeben: ")
	    codec = raw_input("codec eingeben: ")
	    cd = raw_input("cds eingeben: ")
	    comment = raw_input("comment eingeben: ")
	    filmadd.setAllXMLData(self.filmlist)
	    self.filmlist[id] = [title,lang,codec,cd,comment]
	else:
	    print "no film found with id: %s" % id
	    return


if __name__ == "__main__":
    ###local testing
    xmlfile = "myFilms.xml"
    mf = MyFilms(xmlfile)
    #foo = mf.getFilm(666)
    #print "%s" % foo
    #mf.changeFilm(2)
    list = ["foo","asd","123","bar","bla"]
    mf.addFilm(list)
    mf.setXMLFile("fee.new")
    mf.saveToXMLFile()