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 |
# $Id$ |
#----------------------------------------------------------------------------- |
14 |
# $Log$ |
# $Log$ |
15 |
|
# Revision 1.11 2004/09/15 22:27:06 xabbu |
16 |
|
# +/- function fraggleSync replaced by function syncTopics which places a query |
17 |
|
# + function query has simple eval of error return code now |
18 |
|
# |
19 |
|
# Revision 1.10 2004/09/13 21:07:38 xabbu |
20 |
|
# + function query added for testing of the nql / rpc interface |
21 |
|
# |
22 |
|
# Revision 1.9 2004/09/01 21:43:30 xabbu |
23 |
|
# +Moved funtion getDefaultDir from FraggleCtlPreferences to FraggleEngine for more convenience |
24 |
|
# +FraggleCtlModules class creation for handling content modules |
25 |
|
# |
26 |
|
# Revision 1.8 2004/08/31 16:48:19 joko |
27 |
|
# + def authenticate |
28 |
|
# |
29 |
|
# Revision 1.7 2004/08/31 09:34:10 joko |
30 |
|
# + def query_remote: don't do "getTopicById" here anymore |
31 |
|
# |
32 |
|
# Revision 1.6 2004/08/31 02:23:07 joko |
33 |
|
# U changes to (dummy) topics metadata (FraggleXml) |
34 |
|
# + def query_remote: wrapper for making remote xmlrpc call |
35 |
|
# |
36 |
|
# Revision 1.5 2004/08/30 13:03:13 joko |
37 |
|
# - def getDefaultDir now in fraggleCtlPreferences |
38 |
|
# + def getTopicById |
39 |
|
# |
40 |
# Revision 1.4 2004/08/27 03:18:30 joko |
# Revision 1.4 2004/08/27 03:18:30 joko |
41 |
# new methods: fraggleSync, getTopics |
# new methods: fraggleSync, getTopics |
42 |
# |
# |
43 |
# Revision 1.3 2004/08/26 15:25:04 joko |
# Revision 1.3 2004/08/26 15:25:04 joko |
44 |
# added cvs headers |
# added cvs headers |
45 |
# |
#----------------------------------------------------------------------------- |
46 |
|
|
47 |
import os |
import os |
48 |
from fraggleConstants import * |
from fraggleConstants import * |
49 |
import FraggleXMLRPC |
import FraggleXMLRPC |
50 |
|
import fraggleCtlPreferences |
51 |
|
import FraggleCtlModules |
52 |
|
import fraggleParserXML |
53 |
|
|
54 |
class FraggleEngine: |
class FraggleEngine: |
55 |
"""Back-end doing the work.""" |
"""Back-end doing the work.""" |
56 |
def __init__(self): |
def __init__(self): |
57 |
self.settings = {} |
self.settings = {} |
58 |
self.topics = {} |
self.topics = {} |
59 |
|
self.modules = FraggleCtlModules.create(self) |
60 |
def getDefaultDir(self): |
self.preferences = fraggleCtlPreferences.create(self) |
61 |
"""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 |
|
62 |
|
|
63 |
def setSetting(self, settingname, value): |
def setSetting(self, settingname, value): |
64 |
"""Sets settingname to value in self.settings.""" |
"""Sets settingname to value in self.settings.""" |
77 |
else: |
else: |
78 |
return None |
return None |
79 |
|
|
80 |
def fraggleSync(self, config): |
def syncTopics(self): |
81 |
|
query = "GET * FROM contenttypes" |
82 |
|
topicData = self.queryCms(query) |
83 |
|
topictypes = topicData['1'] |
84 |
|
|
85 |
|
for i in topictypes: |
86 |
|
self.topics[i] = topictypes[i]['1'] |
87 |
|
|
88 |
|
#print self.topics |
89 |
|
|
|
# v1 - demo |
|
|
self.topics = [ |
|
|
{ |
|
|
'name': 'TWiki-NetFraggleTest-ViaTWiki', |
|
|
'mode': 'push', |
|
|
'target': { |
|
|
'url': 'http://netfrag.org/twiki/bin/view/Main/NetFraggleTest', |
|
|
'type': 'TWiki', |
|
|
} |
|
|
}, |
|
|
{ |
|
|
'name': 'TWiki-NetFraggleTest-ViaXMLRPC', |
|
|
'mode': 'push', |
|
|
'target': { |
|
|
'method': 'appendToPage', |
|
|
'arguments': ['NetFraggleTest'], |
|
|
'type': 'XMLRPC', |
|
|
} |
|
|
}, |
|
|
] |
|
|
return |
|
|
|
|
|
# v2 - live |
|
|
|
|
|
# 1. load payload from remote side |
|
|
xml_rpc = FraggleXMLRPC.create(self, config) |
|
|
fragglexml = xml_rpc.FraggleSync() |
|
|
|
|
|
# 2. (todo) unmarshal to self.topics (hash) |
|
|
# self.topics = unmarshal(fragglexml) |
|
|
|
|
90 |
def getTopics(self): |
def getTopics(self): |
91 |
return self.topics |
return self.topics |
92 |
|
|
93 |
|
def getTopicById(self, id): |
94 |
|
return self.topics[id] |
95 |
|
|
96 |
|
def listItems(self,topicId): |
97 |
|
result = self.queryCms("GET creator_id, language_id, description FROM contents") |
98 |
|
#print result |
99 |
|
|
100 |
|
def queryCms(self,nqlquery): |
101 |
|
|
102 |
|
from xmlrpclib import Server,Error |
103 |
|
rpc = Server('http://netfrag.org/nfo/netfraggle.php') |
104 |
|
result = rpc.query(nqlquery) |
105 |
|
if result['0']['error'] == '0': |
106 |
|
#print result |
107 |
|
return result |
108 |
|
|
109 |
|
#else: |
110 |
|
#print result['0']['error'] |
111 |
|
#return result |
112 |
|
|
113 |
|
def query_remote(self, topicmeta): |
114 |
|
|
115 |
|
if topicmeta['target']['type'] == 'XMLRPC': |
116 |
|
# TODO: make FraggleXMLRPC do all stuff |
117 |
|
# (call methods dynamically) |
118 |
|
from xmlrpclib import Server, Error |
119 |
|
rpc = Server(topicmeta['target']['url']) |
120 |
|
#try: |
121 |
|
if topicmeta['result'] == 'item': |
122 |
|
topicdata = {} |
123 |
|
try: |
124 |
|
#topicdata = rpc.getContent('Home') |
125 |
|
#topicdata = rpc.getContent(tuple(topicmeta['target']['arguments'])) |
126 |
|
topicdata = rpc.getContent(topicmeta['target']['arguments']) |
127 |
|
#print topicdata |
128 |
|
except Error, v: |
129 |
|
print "ERROR", v |
130 |
|
elif topicmeta['result'] == 'list': |
131 |
|
topicdata = [] |
132 |
|
try: |
133 |
|
arg = {'type': 'xmlpage'} |
134 |
|
topicdata = rpc.listTopics(arg) |
135 |
|
#print topicdata |
136 |
|
#self.styledTextContent.SetText(repr(topicdata)) |
137 |
|
except Error, v: |
138 |
|
print "ERROR", v |
139 |
|
return topicdata |
140 |
|
|
141 |
|
def authenticate(self, server, username, password): |
142 |
|
from xmlrpclib import Server, Error |
143 |
|
rpc = Server(server) |
144 |
|
try: |
145 |
|
return rpc.authenticate({'user': username, 'pass': password}) |
146 |
|
except Error, v: |
147 |
|
print "ERROR", v |
148 |
|
|
149 |
|
def getDefaultDir(self): |
150 |
|
"""Gets location of default dir and creates it |
151 |
|
if necessary. ($HOME/.pears/)""" |
152 |
|
try: |
153 |
|
import pearsdebug |
154 |
|
savedir = pearsdebug.savedir |
155 |
|
except: |
156 |
|
dir = '.netfraggle' |
157 |
|
savedir = os.path.expanduser(os.path.join('~', dir)) |
158 |
|
if len(savedir)<=len("c:\\/" + dir): |
159 |
|
# problem that might occur on Win2k (no $HOME environment variable) |
160 |
|
temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir) |
161 |
|
if temp > len("c:\\/" + dir): |
162 |
|
savedir = temp |
163 |
|
# create dir if it doesn't exist |
164 |
|
if not os.path.exists(savedir): |
165 |
|
os.makedirs(savedir) |
166 |
|
return savedir |
167 |
|
|
168 |
class urlOpener(object): |
class urlOpener(object): |
169 |
"""Opens urls.""" |
"""Opens urls.""" |
170 |
def __init__(self): |
def __init__(self): |