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.7 2004/08/31 09:34:10 joko Exp $ |
9 |
# Copyright: (c) 2004 netfrag.org |
10 |
# Licence: GPL |
11 |
#----------------------------------------------------------------------------- |
12 |
|
13 |
#----------------------------------------------------------------------------- |
14 |
# $Log: fraggleEngine.py,v $ |
15 |
# Revision 1.7 2004/08/31 09:34:10 joko |
16 |
# + def query_remote: don't do "getTopicById" here anymore |
17 |
# |
18 |
# 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 |
# Revision 1.5 2004/08/30 13:03:13 joko |
23 |
# - def getDefaultDir now in fraggleCtlPreferences |
24 |
# + def getTopicById |
25 |
# |
26 |
# Revision 1.4 2004/08/27 03:18:30 joko |
27 |
# new methods: fraggleSync, getTopics |
28 |
# |
29 |
# Revision 1.3 2004/08/26 15:25:04 joko |
30 |
# added cvs headers |
31 |
#----------------------------------------------------------------------------- |
32 |
|
33 |
import os |
34 |
from fraggleConstants import * |
35 |
import FraggleXMLRPC |
36 |
import fraggleCtlPreferences |
37 |
|
38 |
class FraggleEngine: |
39 |
"""Back-end doing the work.""" |
40 |
def __init__(self): |
41 |
self.settings = {} |
42 |
self.topics = {} |
43 |
self.preferences = fraggleCtlPreferences.create(self) |
44 |
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
45 |
|
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 |
|
63 |
def fraggleSync(self): |
64 |
|
65 |
# v1 - demo |
66 |
self.topics = [ |
67 |
{ |
68 |
'name': 'iNFO-Page:Test-ViaXMLRPC', |
69 |
'mode': 'pull', |
70 |
'result': 'item', |
71 |
'target': { |
72 |
'url': 'http://netfrag.org/nfo/netfraggle.php', |
73 |
'method': 'nfo.getContent', |
74 |
'arguments': 'Test', |
75 |
'type': 'XMLRPC', |
76 |
} |
77 |
}, |
78 |
{ |
79 |
'name': 'iNFO-Page:Home-ViaXMLRPC', |
80 |
'mode': 'pull', |
81 |
'result': 'item', |
82 |
'target': { |
83 |
'url': 'http://netfrag.org/nfo/netfraggle.php', |
84 |
'method': 'nfo.getContent', |
85 |
'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 |
'type': 'XMLRPC', |
98 |
} |
99 |
}, |
100 |
{ |
101 |
'name': 'TWiki-Test-ViaTWiki', |
102 |
'mode': 'push', |
103 |
'target': { |
104 |
'url': 'http://netfrag.org/twiki/bin/view/Main/NetFraggleTest', |
105 |
'type': 'TWiki', |
106 |
} |
107 |
}, |
108 |
{ |
109 |
'name': 'TWiki-Test-ViaXMLRPC', |
110 |
'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 |
fragglexml = self.rpc.FraggleSync() |
124 |
|
125 |
# 2. (todo) unmarshal to self.topics (hash) |
126 |
# self.topics = unmarshal(fragglexml) |
127 |
|
128 |
def getTopics(self): |
129 |
return self.topics |
130 |
|
131 |
def getTopicById(self, id): |
132 |
return self.topics[id] |
133 |
|
134 |
def query_remote(self, topicmeta): |
135 |
|
136 |
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 |
|
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 |
|
171 |
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 |
|