| 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$ |
| 9 |
|
# Copyright: (c) 2004 netfrag.org |
| 10 |
|
# Licence: GPL |
| 11 |
|
#----------------------------------------------------------------------------- |
| 12 |
|
|
| 13 |
|
#----------------------------------------------------------------------------- |
| 14 |
|
# $Log$ |
| 15 |
|
# Revision 1.5 2004/08/30 13:03:13 joko |
| 16 |
|
# - def getDefaultDir now in fraggleCtlPreferences |
| 17 |
|
# + def getTopicById |
| 18 |
|
# |
| 19 |
|
# Revision 1.4 2004/08/27 03:18:30 joko |
| 20 |
|
# new methods: fraggleSync, getTopics |
| 21 |
|
# |
| 22 |
|
# Revision 1.3 2004/08/26 15:25:04 joko |
| 23 |
|
# added cvs headers |
| 24 |
|
#----------------------------------------------------------------------------- |
| 25 |
|
|
| 26 |
import os |
import os |
| 27 |
|
from fraggleConstants import * |
| 28 |
|
import FraggleXMLRPC |
| 29 |
|
import fraggleCtlPreferences |
| 30 |
|
|
| 31 |
class FraggleEngine: |
class FraggleEngine: |
| 32 |
|
"""Back-end doing the work.""" |
| 33 |
|
def __init__(self): |
| 34 |
|
self.settings = {} |
| 35 |
|
self.topics = {} |
| 36 |
|
self.preferences = fraggleCtlPreferences.create(self) |
| 37 |
|
self.rpc = FraggleXMLRPC.create(self, self.preferences.getConfig()) |
| 38 |
|
|
| 39 |
|
def setSetting(self, settingname, value): |
| 40 |
|
"""Sets settingname to value in self.settings.""" |
| 41 |
|
self.settings[settingname] = value |
| 42 |
|
|
| 43 |
|
def getSetting(self, settingname, default=None): |
| 44 |
|
"""Returns settingname from self.settings. If not found |
| 45 |
|
and default!=None, the default value is return *and* |
| 46 |
|
saved to the settings.""" |
| 47 |
|
if self.settings.has_key(settingname): |
| 48 |
|
return self.settings[settingname] |
| 49 |
|
else: |
| 50 |
|
if default!=None: |
| 51 |
|
self.setSetting(settingname, default) |
| 52 |
|
return default |
| 53 |
|
else: |
| 54 |
|
return None |
| 55 |
|
|
| 56 |
|
def fraggleSync(self): |
| 57 |
|
|
| 58 |
|
# v1 - demo |
| 59 |
|
self.topics = [ |
| 60 |
|
{ |
| 61 |
|
'name': 'iNFO-Test-ViaXMLRPC', |
| 62 |
|
'mode': 'pull', |
| 63 |
|
'target': { |
| 64 |
|
'url': 'http://netfrag.org/nfo/netfraggle.php', |
| 65 |
|
'method': 'nfo.getContent', |
| 66 |
|
'arguments': ['Test'], |
| 67 |
|
'type': 'XMLRPC', |
| 68 |
|
} |
| 69 |
|
}, |
| 70 |
|
{ |
| 71 |
|
'name': 'iNFO-Home-ViaXMLRPC', |
| 72 |
|
'mode': 'pull', |
| 73 |
|
'target': { |
| 74 |
|
'url': 'http://netfrag.org/nfo/netfraggle.php', |
| 75 |
|
'method': 'nfo.getContent', |
| 76 |
|
'arguments': ['Home'], |
| 77 |
|
'type': 'XMLRPC', |
| 78 |
|
} |
| 79 |
|
}, |
| 80 |
|
{ |
| 81 |
|
'name': 'TWiki-Test-ViaTWiki', |
| 82 |
|
'mode': 'push', |
| 83 |
|
'target': { |
| 84 |
|
'url': 'http://netfrag.org/twiki/bin/view/Main/NetFraggleTest', |
| 85 |
|
'type': 'TWiki', |
| 86 |
|
} |
| 87 |
|
}, |
| 88 |
|
{ |
| 89 |
|
'name': 'TWiki-Test-ViaXMLRPC', |
| 90 |
|
'mode': 'push', |
| 91 |
|
'target': { |
| 92 |
|
'method': 'appendToPage', |
| 93 |
|
'arguments': ['NetFraggleTest'], |
| 94 |
|
'type': 'XMLRPC', |
| 95 |
|
} |
| 96 |
|
}, |
| 97 |
|
] |
| 98 |
|
return |
| 99 |
|
|
| 100 |
|
# v2 - live |
| 101 |
|
|
| 102 |
|
# 1. load payload from remote side |
| 103 |
|
fragglexml = self.rpc.FraggleSync() |
| 104 |
|
|
| 105 |
|
# 2. (todo) unmarshal to self.topics (hash) |
| 106 |
|
# self.topics = unmarshal(fragglexml) |
| 107 |
|
|
| 108 |
|
def getTopics(self): |
| 109 |
|
return self.topics |
| 110 |
|
|
| 111 |
|
def getTopicById(self, id): |
| 112 |
|
return self.topics[id] |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
class urlOpener(object): |
| 116 |
|
"""Opens urls.""" |
| 117 |
|
def __init__(self): |
| 118 |
|
pass |
| 119 |
|
def open(self, url="", command=None): |
| 120 |
|
"""Opens the url with the method defined in the settings. |
| 121 |
|
If command==None, it is retreived from the settings. |
| 122 |
|
""" |
| 123 |
|
if command==None: |
| 124 |
|
command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT) |
| 125 |
|
if command==BROWSERDEFAULT.strip(): |
| 126 |
|
# use default opening method |
| 127 |
|
import os |
| 128 |
|
if os.environ.has_key("BROWSER") and \ |
| 129 |
|
os.environ["BROWSER"]=='kfmclient openProfile webbrowsing': |
| 130 |
|
print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"] |
| 131 |
|
os.environ["BROWSER"] = 'konqueror' # set it to konqueror |
| 132 |
|
import webbrowser # MUST be imported only AFTER os.environ has been modified |
| 133 |
|
webbrowser.open(url, 1) |
| 134 |
|
#webbrowser.open_new(url) |
| 135 |
|
else: #otherwise use user-defined command line |
| 136 |
|
# replase %URL with actual url |
| 137 |
|
command = command.replace("%URL", url) |
| 138 |
|
import os |
| 139 |
|
os.system(command) |
| 140 |
|
|
|
def getDefaultDir(self): |
|
|
"""Gets location of default dir and creates it |
|
|
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 |
|