deluge/plugins/WebUi/webserver_common.py

164 lines
5.0 KiB
Python
Raw Normal View History

2007-10-05 21:16:46 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) Martijn Voncken 2007 <mvoncken@gmail.com>
#
# 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)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
2007-10-28 01:35:27 +00:00
"""
initializes config,render and proxy.
contains all hacks to support running in process0.5 ,run inside-gtk0.5 and
run in process0.6
"""
2007-10-05 21:16:46 +00:00
import os
import deluge
import random
import pickle
2007-10-28 01:35:27 +00:00
import sys
2007-10-05 21:16:46 +00:00
from webpy022 import template
random.seed()
2007-10-28 01:35:27 +00:00
path = os.path.dirname(__file__)
try:
_('translate something')
except:
import gettext
gettext.install('~/') #no translations :(
try:
config_dir = deluge.common.CONFIG_DIR
except:
config_dir = os.path.expanduser("~/.config/deluge")
config_file = os.path.join(config_dir,'webui.conf')
session_file = os.path.join(config_dir,'webui.sessions')
class subclassed_render(object):
"""
try to use the html template in configured dir.
not available : use template in /deluge/
"""
def __init__(self, template_dirname, cache=False):
self.base_template = template.render(
os.path.join(path, 'templates/deluge/'),
cache=cache)
self.sub_template = template.render(
os.path.join(path, 'templates/%s/' % template_dirname),
cache=cache)
def __getattr__(self, attr):
if hasattr(self.sub_template, attr):
return getattr(self.sub_template, attr)
else:
return getattr(self.base_template, attr)
def init_process():
globals()['config'] = pickle.load(open(config_file))
globals()['render'] = subclassed_render(config.get('template'),
config.get('cache_templates'))
2007-10-05 21:16:46 +00:00
2007-10-28 01:35:27 +00:00
def init_06():
import deluge.ui.client as proxy
proxy.set_core_uri('http://localhost:58846') #How to configure this?
2007-10-05 21:16:46 +00:00
2007-10-28 01:35:27 +00:00
init_process()
globals()['proxy'] = proxy
def init_05():
2007-10-05 21:16:46 +00:00
import dbus
2007-10-28 01:35:27 +00:00
init_process()
2007-10-05 21:16:46 +00:00
bus = dbus.SessionBus()
proxy = bus.get_object("org.deluge_torrent.dbusplugin"
, "/org/deluge_torrent/DelugeDbusPlugin")
2007-10-28 01:35:27 +00:00
globals()['proxy'] = proxy
2007-10-05 21:16:46 +00:00
2007-10-28 01:35:27 +00:00
def init_gtk_05():
2007-10-05 21:16:46 +00:00
#appy possibly changed config-vars, only called in when runing inside gtk.
from dbus_interface import get_dbus_manager
globals()['proxy'] = get_dbus_manager()
globals()['config'] = deluge.pref.Preferences(config_file, False)
2007-10-28 01:35:27 +00:00
globals()['render'] = subclassed_render(config.get('template'),
config.get('cache_templates'))
2007-10-07 04:56:01 +00:00
2007-10-28 01:35:27 +00:00
#hacks to determine environment, TODO: clean up.
if 'env=0.5' in sys.argv:
init_05()
elif not hasattr(deluge, 'common'):
init_06()
elif not hasattr(deluge,'pref'):
init_05()
2007-10-05 21:16:46 +00:00
2007-10-28 01:35:27 +00:00
#constants
REVNO = open(os.path.join(os.path.dirname(__file__),'revno')).read()
2007-10-05 21:16:46 +00:00
VERSION = open(os.path.join(os.path.dirname(__file__),'version')).read()
TORRENT_KEYS = ['distributed_copies', 'download_payload_rate',
'download_rate', 'eta', 'is_seed', 'name', 'next_announce',
'num_files', 'num_peers', 'num_pieces', 'num_seeds', 'paused',
'piece_length','progress', 'ratio', 'total_done', 'total_download',
'total_payload_download', 'total_payload_upload', 'total_peers',
'total_seeds', 'total_size', 'total_upload', 'total_wanted',
'tracker_status', 'upload_payload_rate', 'upload_rate',
2007-10-07 04:56:01 +00:00
'uploaded_memory','tracker','state','queue_pos']
2007-10-05 21:16:46 +00:00
STATE_MESSAGES = (_("Queued"),
_("Checking"),
_("Connecting"),
_("Downloading Metadata"),
_("Downloading"),
_("Finished"),
_("Seeding"),
_("Allocating"))
2007-10-28 01:35:27 +00:00
SPEED_VALUES = [
(-1, 'Unlimited'),
(5, '5.0 Kib/sec'),
(10, '10.0 Kib/sec'),
(15, '15.0 Kib/sec'),
(25, '25.0 Kib/sec'),
(30, '30.0 Kib/sec'),
(50, '50.0 Kib/sec'),
(80, '80.0 Kib/sec'),
(300, '300.0 Kib/sec'),
(500, '500.0 Kib/sec')
]
2007-10-29 20:01:37 +00:00
#try:
# SESSIONS = pickle.load(open(session_file))
#except:
SESSIONS = []
2007-10-28 01:35:27 +00:00