--- nfo/projects/netfraggle/bin/fraggleEngine.py 2004/08/25 18:19:31 1.1 +++ nfo/projects/netfraggle/bin/fraggleEngine.py 2004/09/21 18:07:09 1.13 @@ -1,7 +1,165 @@ +#----------------------------------------------------------------------------- +# Name: fraggleEngine.py +# Purpose: Does the main (non-gui) work +# +# Author: joko +# +# Created: 2004/30/08 +# RCS-ID: $Id: fraggleEngine.py,v 1.13 2004/09/21 18:07:09 xabbu Exp $ +# Copyright: (c) 2004 netfrag.org +# Licence: GPL +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# $Log: fraggleEngine.py,v $ +# Revision 1.13 2004/09/21 18:07:09 xabbu +# + added functions listItems and getContent +# +# Revision 1.12 2004/09/17 09:42:30 xabbu +# U function listItems() places a query to the server now and returns its results +# +# Revision 1.11 2004/09/15 22:27:06 xabbu +# +/- function fraggleSync replaced by function syncTopics which places a query +# + function query has simple eval of error return code now +# +# Revision 1.10 2004/09/13 21:07:38 xabbu +# + function query added for testing of the nql / rpc interface +# +# Revision 1.9 2004/09/01 21:43:30 xabbu +# +Moved funtion getDefaultDir from FraggleCtlPreferences to FraggleEngine for more convenience +# +FraggleCtlModules class creation for handling content modules +# +# Revision 1.8 2004/08/31 16:48:19 joko +# + def authenticate +# +# Revision 1.7 2004/08/31 09:34:10 joko +# + def query_remote: don't do "getTopicById" here anymore +# +# Revision 1.6 2004/08/31 02:23:07 joko +# U changes to (dummy) topics metadata (FraggleXml) +# + def query_remote: wrapper for making remote xmlrpc call +# +# Revision 1.5 2004/08/30 13:03:13 joko +# - def getDefaultDir now in fraggleCtlPreferences +# + def getTopicById +# +# Revision 1.4 2004/08/27 03:18:30 joko +# new methods: fraggleSync, getTopics +# +# Revision 1.3 2004/08/26 15:25:04 joko +# added cvs headers +#----------------------------------------------------------------------------- + import os +from fraggleConstants import * +import FraggleXMLRPC +import fraggleCtlPreferences +import FraggleCtlModules +import fraggleParserXML class FraggleEngine: + """Back-end doing the work.""" + def __init__(self): + self.settings = {} + self.topics = {} + self.modules = FraggleCtlModules.create(self) + self.preferences = fraggleCtlPreferences.create(self) + self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) + + def setSetting(self, settingname, value): + """Sets settingname to value in self.settings.""" + self.settings[settingname] = value + + def getSetting(self, settingname, default=None): + """Returns settingname from self.settings. If not found + and default!=None, the default value is return *and* + saved to the settings.""" + if self.settings.has_key(settingname): + return self.settings[settingname] + else: + if default!=None: + self.setSetting(settingname, default) + return default + else: + return None + def syncTopics(self): + query = "GET * FROM contenttypes" + topicData = self.queryCms(query) + topictypes = topicData['1'] + + for i in topictypes: + self.topics[i] = topictypes[i]['1'] + + #print self.topics + + def getTopics(self): + return self.topics + + def getTopicById(self, id): + return self.topics[id] + + def listItems(self,contentkey): + print contentkey + result = self.queryCms(str("GET creator_id, language_id, description, id FROM contents WITH type "+contentkey)) + return result + + def getContent(self,contentkey): + query = str("GET content, creator_id, description, id FROM contents WITH id "+contentkey) + result = self.queryCms(query) + print query + return result + + + def queryCms(self,nqlquery): + + from xmlrpclib import Server,Error + rpc = Server('http://netfrag.org/nfo/netfraggle.php') + result = rpc.query(nqlquery) + if result['0']['error'] == '0': + #print result + return result + + #else: + #print result['0']['error'] + #return result + + def query_remote(self, topicmeta): + + if topicmeta['target']['type'] == 'XMLRPC': + # TODO: make FraggleXMLRPC do all stuff + # (call methods dynamically) + from xmlrpclib import Server, Error + rpc = Server(topicmeta['target']['url']) + #try: + if topicmeta['result'] == 'item': + topicdata = {} + try: + #topicdata = rpc.getContent('Home') + #topicdata = rpc.getContent(tuple(topicmeta['target']['arguments'])) + topicdata = rpc.getContent(topicmeta['target']['arguments']) + #print topicdata + except Error, v: + print "ERROR", v + elif topicmeta['result'] == 'list': + topicdata = [] + try: + arg = {'type': 'xmlpage'} + topicdata = rpc.listTopics(arg) + #print topicdata + #self.styledTextContent.SetText(repr(topicdata)) + except Error, v: + print "ERROR", v + return topicdata + + def authenticate(self, server, username, password): + from xmlrpclib import Server, Error + rpc = Server(server) + try: + return rpc.authenticate({'user': username, 'pass': password}) + except Error, v: + print "ERROR", v + def getDefaultDir(self): """Gets location of default dir and creates it if necessary. ($HOME/.pears/)""" @@ -20,3 +178,30 @@ if not os.path.exists(savedir): os.makedirs(savedir) return savedir + +class urlOpener(object): + """Opens urls.""" + def __init__(self): + pass + def open(self, url="", command=None): + """Opens the url with the method defined in the settings. + If command==None, it is retreived from the settings. + """ + if command==None: + command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT) + if command==BROWSERDEFAULT.strip(): + # use default opening method + import os + if os.environ.has_key("BROWSER") and \ + os.environ["BROWSER"]=='kfmclient openProfile webbrowsing': + print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"] + os.environ["BROWSER"] = 'konqueror' # set it to konqueror + import webbrowser # MUST be imported only AFTER os.environ has been modified + webbrowser.open(url, 1) + #webbrowser.open_new(url) + else: #otherwise use user-defined command line + # replase %URL with actual url + command = command.replace("%URL", url) + import os + os.system(command) +