| 1 |
|
#----------------------------------------------------------------------------- |
| 2 |
|
# Name: fraggleEngine.py |
| 3 |
|
# Purpose: Does the main (non-gui) work |
| 4 |
|
# |
| 5 |
|
# Author: joko |
| 6 |
|
# |
| 7 |
|
# Created: 2004/30/08 |
| 8 |
|
# RCS-ID: $Id$ |
| 9 |
|
# Copyright: (c) 2004 netfrag.org |
| 10 |
|
# Licence: GPL |
| 11 |
|
#----------------------------------------------------------------------------- |
| 12 |
|
|
| 13 |
|
#----------------------------------------------------------------------------- |
| 14 |
|
# $Log$ |
| 15 |
|
# Revision 1.13 2004/09/21 18:07:09 xabbu |
| 16 |
|
# + added functions listItems and getContent |
| 17 |
|
# |
| 18 |
|
# Revision 1.12 2004/09/17 09:42:30 xabbu |
| 19 |
|
# U function listItems() places a query to the server now and returns its results |
| 20 |
|
# |
| 21 |
|
# Revision 1.11 2004/09/15 22:27:06 xabbu |
| 22 |
|
# +/- function fraggleSync replaced by function syncTopics which places a query |
| 23 |
|
# + function query has simple eval of error return code now |
| 24 |
|
# |
| 25 |
|
# Revision 1.10 2004/09/13 21:07:38 xabbu |
| 26 |
|
# + function query added for testing of the nql / rpc interface |
| 27 |
|
# |
| 28 |
|
# Revision 1.9 2004/09/01 21:43:30 xabbu |
| 29 |
|
# +Moved funtion getDefaultDir from FraggleCtlPreferences to FraggleEngine for more convenience |
| 30 |
|
# +FraggleCtlModules class creation for handling content modules |
| 31 |
|
# |
| 32 |
|
# Revision 1.8 2004/08/31 16:48:19 joko |
| 33 |
|
# + def authenticate |
| 34 |
|
# |
| 35 |
|
# Revision 1.7 2004/08/31 09:34:10 joko |
| 36 |
|
# + def query_remote: don't do "getTopicById" here anymore |
| 37 |
|
# |
| 38 |
|
# Revision 1.6 2004/08/31 02:23:07 joko |
| 39 |
|
# U changes to (dummy) topics metadata (FraggleXml) |
| 40 |
|
# + def query_remote: wrapper for making remote xmlrpc call |
| 41 |
|
# |
| 42 |
|
# Revision 1.5 2004/08/30 13:03:13 joko |
| 43 |
|
# - def getDefaultDir now in fraggleCtlPreferences |
| 44 |
|
# + def getTopicById |
| 45 |
|
# |
| 46 |
|
# Revision 1.4 2004/08/27 03:18:30 joko |
| 47 |
|
# new methods: fraggleSync, getTopics |
| 48 |
|
# |
| 49 |
|
# Revision 1.3 2004/08/26 15:25:04 joko |
| 50 |
|
# added cvs headers |
| 51 |
|
#----------------------------------------------------------------------------- |
| 52 |
|
|
| 53 |
import os |
import os |
| 54 |
|
from fraggleConstants import * |
| 55 |
|
import FraggleXMLRPC |
| 56 |
|
import fraggleCtlPreferences |
| 57 |
|
import FraggleCtlModules |
| 58 |
|
import fraggleParserXML |
| 59 |
|
|
| 60 |
class FraggleEngine: |
class FraggleEngine: |
| 61 |
|
"""Back-end doing the work.""" |
| 62 |
|
def __init__(self): |
| 63 |
|
self.settings = {} |
| 64 |
|
self.topics = {} |
| 65 |
|
self.modules = FraggleCtlModules.create(self) |
| 66 |
|
self.preferences = fraggleCtlPreferences.create(self) |
| 67 |
|
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
| 68 |
|
|
| 69 |
|
def setSetting(self, settingname, value): |
| 70 |
|
"""Sets settingname to value in self.settings.""" |
| 71 |
|
self.settings[settingname] = value |
| 72 |
|
|
| 73 |
|
def getSetting(self, settingname, default=None): |
| 74 |
|
"""Returns settingname from self.settings. If not found |
| 75 |
|
and default!=None, the default value is return *and* |
| 76 |
|
saved to the settings.""" |
| 77 |
|
if self.settings.has_key(settingname): |
| 78 |
|
return self.settings[settingname] |
| 79 |
|
else: |
| 80 |
|
if default!=None: |
| 81 |
|
self.setSetting(settingname, default) |
| 82 |
|
return default |
| 83 |
|
else: |
| 84 |
|
return None |
| 85 |
|
|
| 86 |
|
def syncTopics(self): |
| 87 |
|
query = "GET * FROM contenttypes" |
| 88 |
|
topicData = self.queryCms(query) |
| 89 |
|
topictypes = topicData['1'] |
| 90 |
|
|
| 91 |
|
for i in topictypes: |
| 92 |
|
self.topics[i] = topictypes[i]['1'] |
| 93 |
|
|
| 94 |
|
#print self.topics |
| 95 |
|
|
| 96 |
|
def getTopics(self): |
| 97 |
|
return self.topics |
| 98 |
|
|
| 99 |
|
def getTopicById(self, id): |
| 100 |
|
return self.topics[id] |
| 101 |
|
|
| 102 |
|
def listItems(self,contentkey): |
| 103 |
|
print contentkey |
| 104 |
|
result = self.queryCms(str("GET creator_id, language_id, description, id FROM contents WITH type "+contentkey)) |
| 105 |
|
return result |
| 106 |
|
|
| 107 |
|
def getContent(self,contentkey): |
| 108 |
|
query = str("GET content, creator_id, description, id FROM contents WITH id "+contentkey) |
| 109 |
|
result = self.queryCms(query) |
| 110 |
|
print query |
| 111 |
|
return result |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
def queryCms(self,nqlquery): |
| 115 |
|
|
| 116 |
|
from xmlrpclib import Server,Error |
| 117 |
|
rpc = Server('http://netfrag.org/nfo/netfraggle.php') |
| 118 |
|
result = rpc.query(nqlquery) |
| 119 |
|
if result['0']['error'] == '0': |
| 120 |
|
#print result |
| 121 |
|
return result |
| 122 |
|
|
| 123 |
|
#else: |
| 124 |
|
#print result['0']['error'] |
| 125 |
|
#return result |
| 126 |
|
|
| 127 |
|
def query_remote(self, topicmeta): |
| 128 |
|
|
| 129 |
|
if topicmeta['target']['type'] == 'XMLRPC': |
| 130 |
|
# TODO: make FraggleXMLRPC do all stuff |
| 131 |
|
# (call methods dynamically) |
| 132 |
|
from xmlrpclib import Server, Error |
| 133 |
|
rpc = Server(topicmeta['target']['url']) |
| 134 |
|
#try: |
| 135 |
|
if topicmeta['result'] == 'item': |
| 136 |
|
topicdata = {} |
| 137 |
|
try: |
| 138 |
|
#topicdata = rpc.getContent('Home') |
| 139 |
|
#topicdata = rpc.getContent(tuple(topicmeta['target']['arguments'])) |
| 140 |
|
topicdata = rpc.getContent(topicmeta['target']['arguments']) |
| 141 |
|
#print topicdata |
| 142 |
|
except Error, v: |
| 143 |
|
print "ERROR", v |
| 144 |
|
elif topicmeta['result'] == 'list': |
| 145 |
|
topicdata = [] |
| 146 |
|
try: |
| 147 |
|
arg = {'type': 'xmlpage'} |
| 148 |
|
topicdata = rpc.listTopics(arg) |
| 149 |
|
#print topicdata |
| 150 |
|
#self.styledTextContent.SetText(repr(topicdata)) |
| 151 |
|
except Error, v: |
| 152 |
|
print "ERROR", v |
| 153 |
|
return topicdata |
| 154 |
|
|
| 155 |
|
def authenticate(self, server, username, password): |
| 156 |
|
from xmlrpclib import Server, Error |
| 157 |
|
rpc = Server(server) |
| 158 |
|
try: |
| 159 |
|
return rpc.authenticate({'user': username, 'pass': password}) |
| 160 |
|
except Error, v: |
| 161 |
|
print "ERROR", v |
| 162 |
|
|
| 163 |
def getDefaultDir(self): |
def getDefaultDir(self): |
| 164 |
"""Gets location of default dir and creates it |
"""Gets location of default dir and creates it |
| 165 |
if necessary. ($HOME/.pears/)""" |
if necessary. ($HOME/.pears/)""" |
| 178 |
if not os.path.exists(savedir): |
if not os.path.exists(savedir): |
| 179 |
os.makedirs(savedir) |
os.makedirs(savedir) |
| 180 |
return savedir |
return savedir |
| 181 |
|
|
| 182 |
|
class urlOpener(object): |
| 183 |
|
"""Opens urls.""" |
| 184 |
|
def __init__(self): |
| 185 |
|
pass |
| 186 |
|
def open(self, url="", command=None): |
| 187 |
|
"""Opens the url with the method defined in the settings. |
| 188 |
|
If command==None, it is retreived from the settings. |
| 189 |
|
""" |
| 190 |
|
if command==None: |
| 191 |
|
command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT) |
| 192 |
|
if command==BROWSERDEFAULT.strip(): |
| 193 |
|
# use default opening method |
| 194 |
|
import os |
| 195 |
|
if os.environ.has_key("BROWSER") and \ |
| 196 |
|
os.environ["BROWSER"]=='kfmclient openProfile webbrowsing': |
| 197 |
|
print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"] |
| 198 |
|
os.environ["BROWSER"] = 'konqueror' # set it to konqueror |
| 199 |
|
import webbrowser # MUST be imported only AFTER os.environ has been modified |
| 200 |
|
webbrowser.open(url, 1) |
| 201 |
|
#webbrowser.open_new(url) |
| 202 |
|
else: #otherwise use user-defined command line |
| 203 |
|
# replase %URL with actual url |
| 204 |
|
command = command.replace("%URL", url) |
| 205 |
|
import os |
| 206 |
|
os.system(command) |
| 207 |
|
|