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