1 |
joko |
1.2 |
#!/usr/bin/env python |
2 |
|
|
|
3 |
joko |
1.3 |
# $Id$ |
4 |
|
|
# $Log$ |
5 |
|
|
|
6 |
joko |
1.1 |
import os |
7 |
joko |
1.2 |
from fraggleConstants import * |
8 |
joko |
1.1 |
|
9 |
|
|
class FraggleEngine: |
10 |
joko |
1.2 |
"""Back-end doing the work.""" |
11 |
|
|
def __init__(self): |
12 |
|
|
self.settings = {} |
13 |
joko |
1.1 |
|
14 |
|
|
def getDefaultDir(self): |
15 |
|
|
"""Gets location of default dir and creates it |
16 |
|
|
if necessary. ($HOME/.pears/)""" |
17 |
|
|
try: |
18 |
|
|
import pearsdebug |
19 |
|
|
savedir = pearsdebug.savedir |
20 |
|
|
except: |
21 |
|
|
dir = '.netfraggle' |
22 |
|
|
savedir = os.path.expanduser(os.path.join('~', dir)) |
23 |
|
|
if len(savedir)<=len("c:\\/" + dir): |
24 |
|
|
# problem that might occur on Win2k (no $HOME environment variable) |
25 |
|
|
temp = os.path.join(os.path.expandvars('$USERPROFILE'), dir) |
26 |
|
|
if temp > len("c:\\/" + dir): |
27 |
|
|
savedir = temp |
28 |
|
|
# create dir if it doesn't exist |
29 |
|
|
if not os.path.exists(savedir): |
30 |
|
|
os.makedirs(savedir) |
31 |
|
|
return savedir |
32 |
joko |
1.2 |
|
33 |
|
|
def setSetting(self, settingname, value): |
34 |
|
|
"""Sets settingname to value in self.settings.""" |
35 |
|
|
self.settings[settingname] = value |
36 |
|
|
|
37 |
|
|
def getSetting(self, settingname, default=None): |
38 |
|
|
"""Returns settingname from self.settings. If not found |
39 |
|
|
and default!=None, the default value is return *and* |
40 |
|
|
saved to the settings.""" |
41 |
|
|
if self.settings.has_key(settingname): |
42 |
|
|
return self.settings[settingname] |
43 |
|
|
else: |
44 |
|
|
if default!=None: |
45 |
|
|
self.setSetting(settingname, default) |
46 |
|
|
return default |
47 |
|
|
else: |
48 |
|
|
return None |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
class urlOpener(object): |
52 |
|
|
"""Opens urls.""" |
53 |
|
|
def __init__(self): |
54 |
|
|
pass |
55 |
|
|
def open(self, url="", command=None): |
56 |
|
|
"""Opens the url with the method defined in the settings. |
57 |
|
|
If command==None, it is retreived from the settings. |
58 |
|
|
""" |
59 |
|
|
if command==None: |
60 |
|
|
command = FraggleEngine().getSetting(BROWSERCOMMAND, BROWSERDEFAULT) |
61 |
|
|
if command==BROWSERDEFAULT.strip(): |
62 |
|
|
# use default opening method |
63 |
|
|
import os |
64 |
|
|
if os.environ.has_key("BROWSER") and \ |
65 |
|
|
os.environ["BROWSER"]=='kfmclient openProfile webbrowsing': |
66 |
|
|
print "Invalid browser detected : %s\nResetting to konqueror." % os.environ["BROWSER"] |
67 |
|
|
os.environ["BROWSER"] = 'konqueror' # set it to konqueror |
68 |
|
|
import webbrowser # MUST be imported only AFTER os.environ has been modified |
69 |
|
|
webbrowser.open(url, 1) |
70 |
|
|
#webbrowser.open_new(url) |
71 |
|
|
else: #otherwise use user-defined command line |
72 |
|
|
# replase %URL with actual url |
73 |
|
|
command = command.replace("%URL", url) |
74 |
|
|
import os |
75 |
|
|
os.system(command) |
76 |
|
|
|