1 |
#!/usr/bin/env python |
#----------------------------------------------------------------------------- |
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.9 2004/09/01 21:43:30 xabbu |
16 |
|
# +Moved funtion getDefaultDir from FraggleCtlPreferences to FraggleEngine for more convenience |
17 |
|
# +FraggleCtlModules class creation for handling content modules |
18 |
|
# |
19 |
|
# Revision 1.8 2004/08/31 16:48:19 joko |
20 |
|
# + def authenticate |
21 |
|
# |
22 |
|
# Revision 1.7 2004/08/31 09:34:10 joko |
23 |
|
# + def query_remote: don't do "getTopicById" here anymore |
24 |
|
# |
25 |
|
# Revision 1.6 2004/08/31 02:23:07 joko |
26 |
|
# U changes to (dummy) topics metadata (FraggleXml) |
27 |
|
# + def query_remote: wrapper for making remote xmlrpc call |
28 |
|
# |
29 |
|
# Revision 1.5 2004/08/30 13:03:13 joko |
30 |
|
# - def getDefaultDir now in fraggleCtlPreferences |
31 |
|
# + def getTopicById |
32 |
|
# |
33 |
|
# Revision 1.4 2004/08/27 03:18:30 joko |
34 |
|
# new methods: fraggleSync, getTopics |
35 |
|
# |
36 |
|
# Revision 1.3 2004/08/26 15:25:04 joko |
37 |
|
# added cvs headers |
38 |
|
#----------------------------------------------------------------------------- |
39 |
|
|
40 |
import os |
import os |
41 |
from fraggleConstants import * |
from fraggleConstants import * |
42 |
|
import FraggleXMLRPC |
43 |
|
import fraggleCtlPreferences |
44 |
|
import FraggleCtlModules |
45 |
|
|
46 |
class FraggleEngine: |
class FraggleEngine: |
47 |
"""Back-end doing the work.""" |
"""Back-end doing the work.""" |
48 |
def __init__(self): |
def __init__(self): |
49 |
self.settings = {} |
self.settings = {} |
50 |
|
self.topics = {} |
51 |
|
self.modules = FraggleCtlModules.create(self) |
52 |
|
self.preferences = fraggleCtlPreferences.create(self) |
53 |
|
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
54 |
|
|
55 |
|
def setSetting(self, settingname, value): |
56 |
|
"""Sets settingname to value in self.settings.""" |
57 |
|
self.settings[settingname] = value |
58 |
|
|
59 |
|
def getSetting(self, settingname, default=None): |
60 |
|
"""Returns settingname from self.settings. If not found |
61 |
|
and default!=None, the default value is return *and* |
62 |
|
saved to the settings.""" |
63 |
|
if self.settings.has_key(settingname): |
64 |
|
return self.settings[settingname] |
65 |
|
else: |
66 |
|
if default!=None: |
67 |
|
self.setSetting(settingname, default) |
68 |
|
return default |
69 |
|
else: |
70 |
|
return None |
71 |
|
|
72 |
|
def fraggleSync(self): |
73 |
|
|
74 |
|
# v1 - demo |
75 |
|
self.topics = [ |
76 |
|
{ |
77 |
|
'name': 'iNFO-Page:Test-ViaXMLRPC', |
78 |
|
'mode': 'pull', |
79 |
|
'result': 'item', |
80 |
|
'target': { |
81 |
|
'url': 'http://netfrag.org/nfo/netfraggle.php', |
82 |
|
'method': 'nfo.getContent', |
83 |
|
'arguments': 'Test', |
84 |
|
'type': 'XMLRPC', |
85 |
|
} |
86 |
|
}, |
87 |
|
{ |
88 |
|
'name': 'iNFO-Page:Home-ViaXMLRPC', |
89 |
|
'mode': 'pull', |
90 |
|
'result': 'item', |
91 |
|
'target': { |
92 |
|
'url': 'http://netfrag.org/nfo/netfraggle.php', |
93 |
|
'method': 'nfo.getContent', |
94 |
|
'arguments': 'Home', |
95 |
|
'type': 'XMLRPC', |
96 |
|
} |
97 |
|
}, |
98 |
|
{ |
99 |
|
'name': 'iNFO-PageIndex', |
100 |
|
'mode': 'pull', |
101 |
|
'result': 'list', |
102 |
|
'target': { |
103 |
|
'url': 'http://netfrag.org/nfo/netfraggle.php', |
104 |
|
'method': 'nfo.listTopics', |
105 |
|
'arguments': ['xmlpage'], |
106 |
|
'type': 'XMLRPC', |
107 |
|
} |
108 |
|
}, |
109 |
|
{ |
110 |
|
'name': 'TWiki-Test-ViaTWiki', |
111 |
|
'mode': 'push', |
112 |
|
'target': { |
113 |
|
'url': 'http://netfrag.org/twiki/bin/view/Main/NetFraggleTest', |
114 |
|
'type': 'TWiki', |
115 |
|
} |
116 |
|
}, |
117 |
|
{ |
118 |
|
'name': 'TWiki-Test-ViaXMLRPC', |
119 |
|
'mode': 'push', |
120 |
|
'target': { |
121 |
|
'method': 'appendToPage', |
122 |
|
'arguments': ['NetFraggleTest'], |
123 |
|
'type': 'XMLRPC', |
124 |
|
} |
125 |
|
}, |
126 |
|
] |
127 |
|
return |
128 |
|
|
129 |
|
# v2 - live |
130 |
|
|
131 |
|
# 1. load payload from remote side |
132 |
|
fragglexml = self.rpc.FraggleSync() |
133 |
|
|
134 |
|
# 2. (todo) unmarshal to self.topics (hash) |
135 |
|
# self.topics = unmarshal(fragglexml) |
136 |
|
|
137 |
|
def getTopics(self): |
138 |
|
return self.topics |
139 |
|
|
140 |
|
def getTopicById(self, id): |
141 |
|
return self.topics[id] |
142 |
|
|
143 |
|
def query_remote(self, topicmeta): |
144 |
|
|
145 |
|
if topicmeta['target']['type'] == 'XMLRPC': |
146 |
|
# TODO: make FraggleXMLRPC do all stuff |
147 |
|
# (call methods dynamically) |
148 |
|
from xmlrpclib import Server, Error |
149 |
|
rpc = Server(topicmeta['target']['url']) |
150 |
|
#try: |
151 |
|
if topicmeta['result'] == 'item': |
152 |
|
topicdata = {} |
153 |
|
try: |
154 |
|
#topicdata = rpc.getContent('Home') |
155 |
|
#topicdata = rpc.getContent(tuple(topicmeta['target']['arguments'])) |
156 |
|
topicdata = rpc.getContent(topicmeta['target']['arguments']) |
157 |
|
#print topicdata |
158 |
|
except Error, v: |
159 |
|
print "ERROR", v |
160 |
|
elif topicmeta['result'] == 'list': |
161 |
|
topicdata = [] |
162 |
|
try: |
163 |
|
arg = {'type': 'xmlpage'} |
164 |
|
topicdata = rpc.listTopics(arg) |
165 |
|
#print topicdata |
166 |
|
#self.styledTextContent.SetText(repr(topicdata)) |
167 |
|
except Error, v: |
168 |
|
print "ERROR", v |
169 |
|
return topicdata |
170 |
|
|
171 |
|
def authenticate(self, server, username, password): |
172 |
|
from xmlrpclib import Server, Error |
173 |
|
rpc = Server(server) |
174 |
|
try: |
175 |
|
return rpc.authenticate({'user': username, 'pass': password}) |
176 |
|
except Error, v: |
177 |
|
print "ERROR", v |
178 |
|
|
179 |
def getDefaultDir(self): |
def getDefaultDir(self): |
180 |
"""Gets location of default dir and creates it |
"""Gets location of default dir and creates it |
181 |
if necessary. ($HOME/.pears/)""" |
if necessary. ($HOME/.pears/)""" |
194 |
if not os.path.exists(savedir): |
if not os.path.exists(savedir): |
195 |
os.makedirs(savedir) |
os.makedirs(savedir) |
196 |
return savedir |
return savedir |
197 |
|
|
|
def setSetting(self, settingname, value): |
|
|
"""Sets settingname to value in self.settings.""" |
|
|
self.settings[settingname] = value |
|
|
|
|
|
def getSetting(self, settingname, default=None): |
|
|
"""Returns settingname from self.settings. If not found |
|
|
and default!=None, the default value is return *and* |
|
|
saved to the settings.""" |
|
|
if self.settings.has_key(settingname): |
|
|
return self.settings[settingname] |
|
|
else: |
|
|
if default!=None: |
|
|
self.setSetting(settingname, default) |
|
|
return default |
|
|
else: |
|
|
return None |
|
|
|
|
|
|
|
198 |
class urlOpener(object): |
class urlOpener(object): |
199 |
"""Opens urls.""" |
"""Opens urls.""" |
200 |
def __init__(self): |
def __init__(self): |