/[cvs]/nfo/projects/netfraggle/bin/fraggleEngine.py
ViewVC logotype

Contents of /nfo/projects/netfraggle/bin/fraggleEngine.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Tue Sep 21 18:07:09 2004 UTC (20 years, 2 months ago) by xabbu
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +13 -2 lines
File MIME type: text/x-python
+ added functions listItems and getContent

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: fraggleEngine.py,v 1.12 2004/09/17 09:42:30 xabbu Exp $
9 # Copyright: (c) 2004 netfrag.org
10 # Licence: GPL
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # $Log: fraggleEngine.py,v $
15 # Revision 1.12 2004/09/17 09:42:30 xabbu
16 # U function listItems() places a query to the server now and returns its results
17 #
18 # Revision 1.11 2004/09/15 22:27:06 xabbu
19 # +/- function fraggleSync replaced by function syncTopics which places a query
20 # + function query has simple eval of error return code now
21 #
22 # Revision 1.10 2004/09/13 21:07:38 xabbu
23 # + function query added for testing of the nql / rpc interface
24 #
25 # Revision 1.9 2004/09/01 21:43:30 xabbu
26 # +Moved funtion getDefaultDir from FraggleCtlPreferences to FraggleEngine for more convenience
27 # +FraggleCtlModules class creation for handling content modules
28 #
29 # Revision 1.8 2004/08/31 16:48:19 joko
30 # + def authenticate
31 #
32 # Revision 1.7 2004/08/31 09:34:10 joko
33 # + def query_remote: don't do "getTopicById" here anymore
34 #
35 # Revision 1.6 2004/08/31 02:23:07 joko
36 # U changes to (dummy) topics metadata (FraggleXml)
37 # + def query_remote: wrapper for making remote xmlrpc call
38 #
39 # Revision 1.5 2004/08/30 13:03:13 joko
40 # - def getDefaultDir now in fraggleCtlPreferences
41 # + def getTopicById
42 #
43 # Revision 1.4 2004/08/27 03:18:30 joko
44 # new methods: fraggleSync, getTopics
45 #
46 # Revision 1.3 2004/08/26 15:25:04 joko
47 # added cvs headers
48 #-----------------------------------------------------------------------------
49
50 import os
51 from fraggleConstants import *
52 import FraggleXMLRPC
53 import fraggleCtlPreferences
54 import FraggleCtlModules
55 import fraggleParserXML
56
57 class FraggleEngine:
58 """Back-end doing the work."""
59 def __init__(self):
60 self.settings = {}
61 self.topics = {}
62 self.modules = FraggleCtlModules.create(self)
63 self.preferences = fraggleCtlPreferences.create(self)
64 self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig())
65
66 def setSetting(self, settingname, value):
67 """Sets settingname to value in self.settings."""
68 self.settings[settingname] = value
69
70 def getSetting(self, settingname, default=None):
71 """Returns settingname from self.settings. If not found
72 and default!=None, the default value is return *and*
73 saved to the settings."""
74 if self.settings.has_key(settingname):
75 return self.settings[settingname]
76 else:
77 if default!=None:
78 self.setSetting(settingname, default)
79 return default
80 else:
81 return None
82
83 def syncTopics(self):
84 query = "GET * FROM contenttypes"
85 topicData = self.queryCms(query)
86 topictypes = topicData['1']
87
88 for i in topictypes:
89 self.topics[i] = topictypes[i]['1']
90
91 #print self.topics
92
93 def getTopics(self):
94 return self.topics
95
96 def getTopicById(self, id):
97 return self.topics[id]
98
99 def listItems(self,contentkey):
100 print contentkey
101 result = self.queryCms(str("GET creator_id, language_id, description, id FROM contents WITH type "+contentkey))
102 return result
103
104 def getContent(self,contentkey):
105 query = str("GET content, creator_id, description, id FROM contents WITH id "+contentkey)
106 result = self.queryCms(query)
107 print query
108 return result
109
110
111 def queryCms(self,nqlquery):
112
113 from xmlrpclib import Server,Error
114 rpc = Server('http://netfrag.org/nfo/netfraggle.php')
115 result = rpc.query(nqlquery)
116 if result['0']['error'] == '0':
117 #print result
118 return result
119
120 #else:
121 #print result['0']['error']
122 #return result
123
124 def query_remote(self, topicmeta):
125
126 if topicmeta['target']['type'] == 'XMLRPC':
127 # TODO: make FraggleXMLRPC do all stuff
128 # (call methods dynamically)
129 from xmlrpclib import Server, Error
130 rpc = Server(topicmeta['target']['url'])
131 #try:
132 if topicmeta['result'] == 'item':
133 topicdata = {}
134 try:
135 #topicdata = rpc.getContent('Home')
136 #topicdata = rpc.getContent(tuple(topicmeta['target']['arguments']))
137 topicdata = rpc.getContent(topicmeta['target']['arguments'])
138 #print topicdata
139 except Error, v:
140 print "ERROR", v
141 elif topicmeta['result'] == 'list':
142 topicdata = []
143 try:
144 arg = {'type': 'xmlpage'}
145 topicdata = rpc.listTopics(arg)
146 #print topicdata
147 #self.styledTextContent.SetText(repr(topicdata))
148 except Error, v:
149 print "ERROR", v
150 return topicdata
151
152 def authenticate(self, server, username, password):
153 from xmlrpclib import Server, Error
154 rpc = Server(server)
155 try:
156 return rpc.authenticate({'user': username, 'pass': password})
157 except Error, v:
158 print "ERROR", v
159
160 def getDefaultDir(self):
161 """Gets location of default dir and creates it
162 if necessary. ($HOME/.pears/)"""
163 try:
164 import pearsdebug
165 savedir = pearsdebug.savedir
166 except:
167 dir = '.netfraggle'
168 savedir = os.path.expanduser(os.path.join('~', dir))
169 if len(savedir)<=len("c:\\/" + dir):
170 # problem that might occur on Win2k (no $HOME environment variable)
171 temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir)
172 if temp > len("c:\\/" + dir):
173 savedir = temp
174 # create dir if it doesn't exist
175 if not os.path.exists(savedir):
176 os.makedirs(savedir)
177 return savedir
178
179 class urlOpener(object):
180 """Opens urls."""
181 def __init__(self):
182 pass
183 def open(self, url="", command=None):
184 """Opens the url with the method defined in the settings.
185 If command==None, it is retreived from the settings.
186 """
187 if command==None:
188 command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT)
189 if command==BROWSERDEFAULT.strip():
190 # use default opening method
191 import os
192 if os.environ.has_key("BROWSER") and \
193 os.environ["BROWSER"]=='kfmclient openProfile webbrowsing':
194 print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"]
195 os.environ["BROWSER"] = 'konqueror' # set it to konqueror
196 import webbrowser # MUST be imported only AFTER os.environ has been modified
197 webbrowser.open(url, 1)
198 #webbrowser.open_new(url)
199 else: #otherwise use user-defined command line
200 # replase %URL with actual url
201 command = command.replace("%URL", url)
202 import os
203 os.system(command)
204

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed