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