2006-12-08 19:06:49 +00:00
|
|
|
# dcommon.py
|
2006-11-28 22:28:37 +00:00
|
|
|
#
|
2006-12-08 19:06:49 +00:00
|
|
|
# Copyright (C) Zach Tibbitts 2006 <zach@collegegeek.org>
|
2007-01-08 19:38:19 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2, or (at your option)
|
2006-12-08 19:06:49 +00:00
|
|
|
# any later version.
|
|
|
|
#
|
2007-01-08 19:38:19 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
2006-12-08 19:06:49 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-01-08 19:38:19 +00:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2006-12-08 19:06:49 +00:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-01-08 19:38:19 +00:00
|
|
|
# along with this program. If not, write to:
|
2006-12-08 19:06:49 +00:00
|
|
|
# The Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor
|
|
|
|
# Boston, MA 02110-1301, USA.
|
2006-11-28 22:28:37 +00:00
|
|
|
|
2007-02-07 23:49:31 +00:00
|
|
|
import sys, os, os.path, webbrowser
|
2006-11-28 22:28:37 +00:00
|
|
|
|
2006-12-04 23:59:45 +00:00
|
|
|
PROGRAM_NAME = "Deluge"
|
2007-02-05 23:51:11 +00:00
|
|
|
PROGRAM_VERSION = "0.4.9.0"
|
2006-11-28 22:28:37 +00:00
|
|
|
DELUGE_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))
|
2007-02-08 00:31:18 +00:00
|
|
|
PREFIX = sys.prefix
|
|
|
|
GLADE_DIR = PREFIX + "/share/deluge/glade"
|
|
|
|
PIXMAP_DIR = PREFIX + "/share/deluge/pixmaps"
|
2006-11-28 22:28:37 +00:00
|
|
|
|
2007-02-06 20:11:46 +00:00
|
|
|
class DelugePreferences:
|
|
|
|
def __init__(self):
|
|
|
|
self.pref = {}
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
self.pref[key] = value
|
|
|
|
|
2007-02-09 21:41:58 +00:00
|
|
|
def get(self, key, kind=None):
|
|
|
|
result = self.pref[key]
|
|
|
|
if kind == None:
|
|
|
|
return result
|
|
|
|
elif kind == bool:
|
|
|
|
# Python interprets bool("False") as True, so we must compensate for this
|
|
|
|
if isinstance(result, str):
|
|
|
|
return (result.lower() == "true")
|
|
|
|
elif isinstance(result, int):
|
|
|
|
return not (result == 0)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
elif kind == str:
|
|
|
|
return str(result)
|
|
|
|
elif kind == int:
|
|
|
|
try:
|
|
|
|
return int(result)
|
|
|
|
except ValueError:
|
|
|
|
return int(float(result))
|
|
|
|
elif kind == float:
|
|
|
|
return float(result)
|
|
|
|
else:
|
|
|
|
return result
|
2007-02-06 20:11:46 +00:00
|
|
|
|
2007-02-07 23:49:31 +00:00
|
|
|
def keys(self):
|
|
|
|
return self.pref.keys()
|
|
|
|
|
2007-02-06 20:11:46 +00:00
|
|
|
def load_from_file(self, filename):
|
|
|
|
f = open(filename, mode='r')
|
|
|
|
for line in f:
|
2007-02-09 21:41:58 +00:00
|
|
|
try:
|
|
|
|
(key, value) = line.split("=")
|
|
|
|
key = key.strip(" \n")
|
|
|
|
value = value.strip(" \n")
|
|
|
|
self.pref[key] = value
|
|
|
|
print 'Read option %s with a value of %s'%(key, value)
|
|
|
|
except ValueError:
|
|
|
|
print "Empty Line"
|
2007-02-06 20:11:46 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
def save_to_file(self, filename):
|
|
|
|
f = open(filename, mode='w')
|
2007-02-09 21:41:58 +00:00
|
|
|
f.write('#%s preferences file\n\n'%PROGRAM_NAME)
|
2007-02-06 20:11:46 +00:00
|
|
|
for key in self.pref.keys():
|
|
|
|
f.write(key)
|
|
|
|
f.write(' = ')
|
2007-02-09 21:41:58 +00:00
|
|
|
f.write(str(self.pref[key]))
|
2007-02-06 20:11:46 +00:00
|
|
|
f.write('\n')
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
|
2007-02-06 21:21:09 +00:00
|
|
|
# Returns formatted string describing filesize
|
|
|
|
# fsize_b should be in bytes
|
|
|
|
# Returned value will be in either KB, MB, or GB
|
|
|
|
def fsize(fsize_b):
|
|
|
|
fsize_kb = float (fsize_b / 1024.0)
|
|
|
|
if fsize_kb < 1000:
|
|
|
|
return '%.1f KB'%fsize_kb
|
|
|
|
fsize_mb = float (fsize_kb / 1024.0)
|
|
|
|
if fsize_mb < 1000:
|
|
|
|
return '%.1f MB'%fsize_mb
|
|
|
|
fsize_gb = float (fsize_mb / 1024.0)
|
|
|
|
return '%.1f GB'%fsize_gb
|
|
|
|
|
|
|
|
# Returns a formatted string representing a percentage
|
|
|
|
def fpcnt(dec):
|
|
|
|
return '%.2f%%'%(dec * 100)
|
|
|
|
|
|
|
|
# Returns a formatted string representing transfer rate
|
|
|
|
def frate(bps):
|
|
|
|
return '%s/s'%(fsize(bps))
|
|
|
|
|
|
|
|
def fseed(state):
|
|
|
|
return str(str(state['num_seeds']) + " (" + str(state['total_seeds']) + ")")
|
|
|
|
|
|
|
|
def fpeer(state):
|
|
|
|
return str(str(state['num_peers']) + " (" + str(state['total_peers']) + ")")
|
|
|
|
|
|
|
|
|
2006-11-28 22:28:37 +00:00
|
|
|
def get_glade_file(fname):
|
|
|
|
return GLADE_DIR + "/" + fname
|
|
|
|
|
|
|
|
def get_pixmap(fname):
|
|
|
|
return PIXMAP_DIR + "/" + fname
|
|
|
|
|
|
|
|
def open_url_in_browser(dialog, link):
|
|
|
|
try:
|
|
|
|
webbrowser.open(link)
|
|
|
|
except webbrowser.Error:
|
2007-02-06 21:21:09 +00:00
|
|
|
print "Error: no webbrowser found"
|