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