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.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 * |
from fraggleConstants import * |
38 |
|
import FraggleXMLRPC |
39 |
|
import fraggleCtlPreferences |
40 |
|
|
41 |
class FraggleEngine: |
class FraggleEngine: |
42 |
"""Back-end doing the work.""" |
"""Back-end doing the work.""" |
43 |
def __init__(self): |
def __init__(self): |
44 |
self.settings = {} |
self.settings = {} |
45 |
|
self.topics = {} |
46 |
def getDefaultDir(self): |
self.preferences = fraggleCtlPreferences.create(self) |
47 |
"""Gets location of default dir and creates it |
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
|
if necessary. ($HOME/.pears/)""" |
|
|
try: |
|
|
import pearsdebug |
|
|
savedir = pearsdebug.savedir |
|
|
except: |
|
|
dir = '.netfraggle' |
|
|
savedir = os.path.expanduser(os.path.join('~', dir)) |
|
|
if len(savedir)<=len("c:\\/" + dir): |
|
|
# problem that might occur on Win2k (no $HOME environment variable) |
|
|
temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir) |
|
|
if temp > len("c:\\/" + dir): |
|
|
savedir = temp |
|
|
# create dir if it doesn't exist |
|
|
if not os.path.exists(savedir): |
|
|
os.makedirs(savedir) |
|
|
return savedir |
|
48 |
|
|
49 |
def setSetting(self, settingname, value): |
def setSetting(self, settingname, value): |
50 |
"""Sets settingname to value in self.settings.""" |
"""Sets settingname to value in self.settings.""" |
63 |
else: |
else: |
64 |
return None |
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 |
|
# v2 - live |
124 |
|
|
125 |
|
# 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: |
169 |
|
return rpc.authenticate({'user': username, 'pass': password}) |
170 |
|
except Error, v: |
171 |
|
print "ERROR", v |
172 |
|
|
173 |
|
|
174 |
class urlOpener(object): |
class urlOpener(object): |
175 |
"""Opens urls.""" |
"""Opens urls.""" |
176 |
def __init__(self): |
def __init__(self): |