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