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