1 |
joko |
1.2 |
#!/usr/bin/env python |
2 |
|
|
|
3 |
joko |
1.4 |
# $Id: fraggleEngine.py,v 1.3 2004/08/26 15:25:04 joko Exp $ |
4 |
|
|
# $Log: fraggleEngine.py,v $ |
5 |
|
|
# Revision 1.3 2004/08/26 15:25:04 joko |
6 |
|
|
# added cvs headers |
7 |
|
|
# |
8 |
joko |
1.3 |
|
9 |
joko |
1.1 |
import os |
10 |
joko |
1.2 |
from fraggleConstants import * |
11 |
joko |
1.4 |
import FraggleXMLRPC |
12 |
joko |
1.1 |
|
13 |
|
|
class FraggleEngine: |
14 |
joko |
1.2 |
"""Back-end doing the work.""" |
15 |
|
|
def __init__(self): |
16 |
|
|
self.settings = {} |
17 |
joko |
1.4 |
self.topics = {} |
18 |
joko |
1.1 |
|
19 |
|
|
def getDefaultDir(self): |
20 |
|
|
"""Gets location of default dir and creates it |
21 |
|
|
if necessary. ($HOME/.pears/)""" |
22 |
|
|
try: |
23 |
|
|
import pearsdebug |
24 |
|
|
savedir = pearsdebug.savedir |
25 |
|
|
except: |
26 |
|
|
dir = '.netfraggle' |
27 |
|
|
savedir = os.path.expanduser(os.path.join('~', dir)) |
28 |
|
|
if len(savedir)<=len("c:\\/" + dir): |
29 |
|
|
# problem that might occur on Win2k (no $HOME environment variable) |
30 |
|
|
temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir) |
31 |
|
|
if temp > len("c:\\/" + dir): |
32 |
|
|
savedir = temp |
33 |
|
|
# create dir if it doesn't exist |
34 |
|
|
if not os.path.exists(savedir): |
35 |
|
|
os.makedirs(savedir) |
36 |
|
|
return savedir |
37 |
joko |
1.2 |
|
38 |
|
|
def setSetting(self, settingname, value): |
39 |
|
|
"""Sets settingname to value in self.settings.""" |
40 |
|
|
self.settings[settingname] = value |
41 |
|
|
|
42 |
|
|
def getSetting(self, settingname, default=None): |
43 |
|
|
"""Returns settingname from self.settings. If not found |
44 |
|
|
and default!=None, the default value is return *and* |
45 |
|
|
saved to the settings.""" |
46 |
|
|
if self.settings.has_key(settingname): |
47 |
|
|
return self.settings[settingname] |
48 |
|
|
else: |
49 |
|
|
if default!=None: |
50 |
|
|
self.setSetting(settingname, default) |
51 |
|
|
return default |
52 |
|
|
else: |
53 |
|
|
return None |
54 |
joko |
1.4 |
|
55 |
|
|
def fraggleSync(self, config): |
56 |
|
|
|
57 |
|
|
# v1 - demo |
58 |
|
|
self.topics = [ |
59 |
|
|
{ |
60 |
|
|
'name': 'TWiki-NetFraggleTest-ViaTWiki', |
61 |
|
|
'mode': 'push', |
62 |
|
|
'target': { |
63 |
|
|
'url': 'http://netfrag.org/twiki/bin/view/Main/NetFraggleTest', |
64 |
|
|
'type': 'TWiki', |
65 |
|
|
} |
66 |
|
|
}, |
67 |
|
|
{ |
68 |
|
|
'name': 'TWiki-NetFraggleTest-ViaXMLRPC', |
69 |
|
|
'mode': 'push', |
70 |
|
|
'target': { |
71 |
|
|
'method': 'appendToPage', |
72 |
|
|
'arguments': ['NetFraggleTest'], |
73 |
|
|
'type': 'XMLRPC', |
74 |
|
|
} |
75 |
|
|
}, |
76 |
|
|
] |
77 |
|
|
return |
78 |
|
|
|
79 |
|
|
# v2 - live |
80 |
|
|
|
81 |
|
|
# 1. load payload from remote side |
82 |
|
|
xml_rpc = FraggleXMLRPC.create(self, config) |
83 |
|
|
fragglexml = xml_rpc.FraggleSync() |
84 |
|
|
|
85 |
|
|
# 2. (todo) unmarshal to self.topics (hash) |
86 |
|
|
# self.topics = unmarshal(fragglexml) |
87 |
|
|
|
88 |
|
|
def getTopics(self): |
89 |
|
|
return self.topics |
90 |
joko |
1.2 |
|
91 |
|
|
|
92 |
|
|
class urlOpener(object): |
93 |
|
|
"""Opens urls.""" |
94 |
|
|
def __init__(self): |
95 |
|
|
pass |
96 |
|
|
def open(self, url="", command=None): |
97 |
|
|
"""Opens the url with the method defined in the settings. |
98 |
|
|
If command==None, it is retreived from the settings. |
99 |
|
|
""" |
100 |
|
|
if command==None: |
101 |
|
|
command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT) |
102 |
|
|
if command==BROWSERDEFAULT.strip(): |
103 |
|
|
# use default opening method |
104 |
|
|
import os |
105 |
|
|
if os.environ.has_key("BROWSER") and \ |
106 |
|
|
os.environ["BROWSER"]=='kfmclient openProfile webbrowsing': |
107 |
|
|
print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"] |
108 |
|
|
os.environ["BROWSER"] = 'konqueror' # set it to konqueror |
109 |
|
|
import webbrowser # MUST be imported only AFTER os.environ has been modified |
110 |
|
|
webbrowser.open(url, 1) |
111 |
|
|
#webbrowser.open_new(url) |
112 |
|
|
else: #otherwise use user-defined command line |
113 |
|
|
# replase %URL with actual url |
114 |
|
|
command = command.replace("%URL", url) |
115 |
|
|
import os |
116 |
|
|
os.system(command) |
117 |
|
|
|