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