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.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 |
41 |
|
# new methods: fraggleSync, getTopics |
42 |
|
# |
43 |
|
# Revision 1.3 2004/08/26 15:25:04 joko |
44 |
|
# added cvs headers |
45 |
|
#----------------------------------------------------------------------------- |
46 |
|
|
47 |
import os |
import os |
48 |
from fraggleConstants import * |
from fraggleConstants import * |
49 |
|
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 = {} |
59 |
|
self.modules = FraggleCtlModules.create(self) |
60 |
|
self.preferences = fraggleCtlPreferences.create(self) |
61 |
|
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
62 |
|
|
63 |
|
def setSetting(self, settingname, value): |
64 |
|
"""Sets settingname to value in self.settings.""" |
65 |
|
self.settings[settingname] = value |
66 |
|
|
67 |
|
def getSetting(self, settingname, default=None): |
68 |
|
"""Returns settingname from self.settings. If not found |
69 |
|
and default!=None, the default value is return *and* |
70 |
|
saved to the settings.""" |
71 |
|
if self.settings.has_key(settingname): |
72 |
|
return self.settings[settingname] |
73 |
|
else: |
74 |
|
if default!=None: |
75 |
|
self.setSetting(settingname, default) |
76 |
|
return default |
77 |
|
else: |
78 |
|
return None |
79 |
|
|
80 |
|
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 |
|
|
90 |
|
def getTopics(self): |
91 |
|
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): |
def getDefaultDir(self): |
150 |
"""Gets location of default dir and creates it |
"""Gets location of default dir and creates it |
151 |
if necessary. ($HOME/.pears/)""" |
if necessary. ($HOME/.pears/)""" |
164 |
if not os.path.exists(savedir): |
if not os.path.exists(savedir): |
165 |
os.makedirs(savedir) |
os.makedirs(savedir) |
166 |
return savedir |
return savedir |
167 |
|
|
|
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 |
|
|
|
|
|
|
|
168 |
class urlOpener(object): |
class urlOpener(object): |
169 |
"""Opens urls.""" |
"""Opens urls.""" |
170 |
def __init__(self): |
def __init__(self): |