1 |
joko |
1.5 |
#----------------------------------------------------------------------------- |
2 |
|
|
# Name: fraggleEngine.py |
3 |
|
|
# Purpose: Does the main (non-gui) work |
4 |
|
|
# |
5 |
|
|
# Author: joko |
6 |
|
|
# |
7 |
|
|
# Created: 2004/30/08 |
8 |
xabbu |
1.12 |
# RCS-ID: $Id: fraggleEngine.py,v 1.11 2004/09/15 22:27:06 xabbu Exp $ |
9 |
joko |
1.5 |
# Copyright: (c) 2004 netfrag.org |
10 |
|
|
# Licence: GPL |
11 |
|
|
#----------------------------------------------------------------------------- |
12 |
joko |
1.2 |
|
13 |
joko |
1.5 |
#----------------------------------------------------------------------------- |
14 |
joko |
1.4 |
# $Log: fraggleEngine.py,v $ |
15 |
xabbu |
1.12 |
# 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 |
xabbu |
1.11 |
# Revision 1.10 2004/09/13 21:07:38 xabbu |
20 |
|
|
# + function query added for testing of the nql / rpc interface |
21 |
|
|
# |
22 |
xabbu |
1.10 |
# 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 |
xabbu |
1.9 |
# Revision 1.8 2004/08/31 16:48:19 joko |
27 |
|
|
# + def authenticate |
28 |
|
|
# |
29 |
joko |
1.8 |
# Revision 1.7 2004/08/31 09:34:10 joko |
30 |
|
|
# + def query_remote: don't do "getTopicById" here anymore |
31 |
|
|
# |
32 |
joko |
1.7 |
# 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 |
joko |
1.6 |
# Revision 1.5 2004/08/30 13:03:13 joko |
37 |
|
|
# - def getDefaultDir now in fraggleCtlPreferences |
38 |
|
|
# + def getTopicById |
39 |
|
|
# |
40 |
joko |
1.5 |
# Revision 1.4 2004/08/27 03:18:30 joko |
41 |
|
|
# new methods: fraggleSync, getTopics |
42 |
|
|
# |
43 |
joko |
1.4 |
# Revision 1.3 2004/08/26 15:25:04 joko |
44 |
|
|
# added cvs headers |
45 |
joko |
1.5 |
#----------------------------------------------------------------------------- |
46 |
joko |
1.3 |
|
47 |
joko |
1.1 |
import os |
48 |
joko |
1.2 |
from fraggleConstants import * |
49 |
joko |
1.4 |
import FraggleXMLRPC |
50 |
joko |
1.5 |
import fraggleCtlPreferences |
51 |
xabbu |
1.9 |
import FraggleCtlModules |
52 |
xabbu |
1.10 |
import fraggleParserXML |
53 |
joko |
1.1 |
|
54 |
|
|
class FraggleEngine: |
55 |
joko |
1.2 |
"""Back-end doing the work.""" |
56 |
|
|
def __init__(self): |
57 |
|
|
self.settings = {} |
58 |
joko |
1.4 |
self.topics = {} |
59 |
xabbu |
1.9 |
self.modules = FraggleCtlModules.create(self) |
60 |
joko |
1.5 |
self.preferences = fraggleCtlPreferences.create(self) |
61 |
|
|
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
62 |
joko |
1.2 |
|
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 |
joko |
1.4 |
|
80 |
xabbu |
1.11 |
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 |
joko |
1.4 |
|
90 |
|
|
def getTopics(self): |
91 |
|
|
return self.topics |
92 |
joko |
1.5 |
|
93 |
|
|
def getTopicById(self, id): |
94 |
|
|
return self.topics[id] |
95 |
xabbu |
1.10 |
|
96 |
xabbu |
1.12 |
def listItems(self,contentkey): |
97 |
|
|
result = self.queryCms(str("GET creator_id, language_id, description FROM contents WITH type "+contentkey)) |
98 |
|
|
return result |
99 |
xabbu |
1.10 |
|
100 |
|
|
def queryCms(self,nqlquery): |
101 |
|
|
|
102 |
|
|
from xmlrpclib import Server,Error |
103 |
|
|
rpc = Server('http://netfrag.org/nfo/netfraggle.php') |
104 |
xabbu |
1.11 |
result = rpc.query(nqlquery) |
105 |
|
|
if result['0']['error'] == '0': |
106 |
xabbu |
1.12 |
#print result |
107 |
|
|
return result |
108 |
|
|
|
109 |
xabbu |
1.11 |
#else: |
110 |
|
|
#print result['0']['error'] |
111 |
|
|
#return result |
112 |
|
|
|
113 |
joko |
1.7 |
def query_remote(self, topicmeta): |
114 |
joko |
1.2 |
|
115 |
joko |
1.6 |
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 |
joko |
1.8 |
|
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 |
xabbu |
1.9 |
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 |
joko |
1.2 |
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 |
|
|
|