Just some updates.. Probably going to change to DBUS instead of Pyro in

next revision.
This commit is contained in:
Andrew Resch 2007-07-05 19:35:59 +00:00
commit 29c4b6aee1
6 changed files with 186 additions and 21 deletions

54
deluge/src/common.py Normal file
View file

@ -0,0 +1,54 @@
#
# common.py
#
# Copyright (C) Andrew Resch 2007 <andrewresch@gmail.com>
#
# Deluge is free software.
#
# You may 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 of the License, or (at your option)
# any later version.
#
# deluge 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 deluge. 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.
import logging
import pkg_resources
import xdg, xdg.BaseDirectory
import os.path
# Get the logger
log = logging.getLogger("deluge")
def get_version():
"""Returns the program version from the egg metadata"""
return pkg_resources.require("Deluge")[0].version
def get_config_dir(filename=None):
""" Returns the CONFIG_DIR path if no filename is specified
Returns the CONFIG_DIR + filename as a path if filename is specified
"""
if filename != None:
return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename)
else:
return xdg.BaseDirectory.save_config_path("deluge")

98
deluge/src/config.py Normal file
View file

@ -0,0 +1,98 @@
#
# config.py
#
# Copyright (C) Andrew Resch 2007 <andrewresch@gmail.com>
#
# Deluge is free software.
#
# You may 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 of the License, or (at your option)
# any later version.
#
# deluge 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 deluge. 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.
import logging
import pickle
import deluge.common
# Get the logger
log = logging.getLogger("deluge")
class Config:
def __init__(self, filename, defaults=None):
log.debug("Config created with filename: %s", filename)
log.debug("Config defaults: %s", defaults)
self.config = {}
# If defaults is not None then we need to use "defaults".
if defaults != None:
self.config = defaults
# Load the config from file in the config_dir
self.config_file = deluge.common.get_config_dir(filename)
self.load(self.config_file)
def load(self, filename=None):
# Use self.config_file if filename is None
if filename is None:
filename = self.config_file
try:
# Un-pickle the file and update the config dictionary
log.debug("Opening pickled file for load..")
pkl_file = open(filename, "rb")
filedump = pickle.load(pkl_file)
self.config.update(filedump)
pkl_file.close()
except IOError:
log.warning("IOError: Unable to load file '%s'", filename)
except EOFError:
log.debug("Closing pickled file..")
pkl_file.close()
def save(self, filename=None):
# Saves the config dictionary
if filename is None:
filename = self.config_file
try:
log.debug("Opening pickled file for save..")
pkl_file = open(filename, "wb")
pickle.dump(self.config, pkl_file)
log.debug("Closing pickled file..")
pkl_file.close()
except IOError:
log.warning("IOError: Unable to save file '%s'", filename)
def set(self, key, value):
# Sets the "key" with "value" in the config dict
log.debug("Setting '%s' to %s", key, value)
self.config[key] = value
def get(self, key):
# Attempts to get the "key" value and returns None if the key is invalid
try:
value = self.config[key]
log.debug("Getting '%s' as %s", key, value)
return value
except KeyError:
log.warning("Key does not exist, returning None")
return None

View file

@ -31,13 +31,22 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# Instantiate the logger
import logging import logging
from deluge.config import Config
import deluge.common
# Get the logger
log = logging.getLogger("deluge") log = logging.getLogger("deluge")
DEFAULT_PREFS = {
}
class Core: class Core:
def __init__(self): def __init__(self):
log.debug("Core init..") log.debug("Core init..")
self.config = Config("core.conf", DEFAULT_PREFS)
def test(self): def test(self):
print "test" print "test"

View file

@ -31,20 +31,22 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# Instantiate the logger
import logging import logging
log = logging.getLogger("deluge")
import Pyro.core import Pyro.core
from deluge.core import Core from deluge.core import Core
# Get the logger
log = logging.getLogger("deluge")
class Daemon: class Daemon:
def __init__(self): def __init__(self):
# Instantiate the Manager class # Instantiate the Manager class
self.core = Core() self.core = Core()
# Initialize the Pyro core and daemon # Initialize the Pyro core and daemon
Pyro.core.initServer(banner=0) Pyro.core.initServer(banner=0)
log.info("Pyro server initiliazed..") log.debug("Pyro server initiliazed..")
self.daemon = Pyro.core.Daemon() self.daemon = Pyro.core.Daemon()
# Connect the Manager to the Pyro server # Connect the Manager to the Pyro server
obj = Pyro.core.ObjBase() obj = Pyro.core.ObjBase()
@ -56,6 +58,6 @@ class Daemon:
# Start the main loop for the pyro daemon # Start the main loop for the pyro daemon
self.daemon.requestLoop() self.daemon.requestLoop()
def getURI(self): def get_uri(self):
# Return the URI for the Pyro server # Return the URI for the Pyro server
return self.uri return self.uri

View file

@ -1,9 +1,6 @@
#!/usr/bin/env python
# #
# main.py # main.py
# #
# Copyright (C) Zach Tibbitts 2006 <zach@collegegeek.org>
# Copyright (C) Alon Zakai 2006 <kripkensteiner@gmail.com>
# Copyright (C) Andrew Resch 2007 <andrewresch@gmail.com> # Copyright (C) Andrew Resch 2007 <andrewresch@gmail.com>
# #
# Deluge is free software. # Deluge is free software.
@ -37,16 +34,16 @@
# The main starting point for the program. This function is called when the # The main starting point for the program. This function is called when the
# user runs the command 'deluge'. # user runs the command 'deluge'.
import logging
import os import os
import signal import signal
from optparse import OptionParser from optparse import OptionParser
import deluge.common
from deluge.daemon import Daemon from deluge.daemon import Daemon
from deluge.ui import Ui from deluge.ui import Ui
import deluge.common
# Setup the logger # Setup the logger
import logging
logging.basicConfig( logging.basicConfig(
level=logging.DEBUG, level=logging.DEBUG,
format="[%(levelname)-8s] %(name)s:%(module)s:%(lineno)d %(message)s" format="[%(levelname)-8s] %(name)s:%(module)s:%(lineno)d %(message)s"
@ -55,10 +52,10 @@ logging.basicConfig(
log = logging.getLogger("deluge") log = logging.getLogger("deluge")
def main(): def main():
log.info("Starting Deluge..")
# Setup the argument parser # Setup the argument parser
parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION) # FIXME: need to use deluge.common to fill in version
parser = OptionParser(usage="%prog [options] [actions]",
version=deluge.common.get_version())
parser.add_option("--daemon", dest="daemon", help="Start Deluge daemon", parser.add_option("--daemon", dest="daemon", help="Start Deluge daemon",
metavar="DAEMON", action="store_true", default=False) metavar="DAEMON", action="store_true", default=False)
parser.add_option("--ui", dest="ui", help="Start Deluge UI", parser.add_option("--ui", dest="ui", help="Start Deluge UI",
@ -66,10 +63,12 @@ def main():
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
log.info("Deluge %s", deluge.common.get_version())
log.debug("options: %s", options) log.debug("options: %s", options)
log.debug("args: %s", args) log.debug("args: %s", args)
daemon = None daemon = None
pid = None pid = None
uri = None uri = None
@ -78,8 +77,9 @@ def main():
if options.daemon: if options.daemon:
log.info("Starting daemon..") log.info("Starting daemon..")
daemon = Daemon() daemon = Daemon()
uri = daemon.getURI() uri = daemon.get_uri()
# We need to fork() the process to run it in the background... # We need to fork() the process to run it in the background...
# FIXME: We cannot use fork() on Windows
pid = os.fork() pid = os.fork()
if not pid: if not pid:
daemon.start() daemon.start()

View file

@ -31,18 +31,20 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# Instantiate the logger
import logging import logging
log = logging.getLogger("deluge")
import Pyro.core import Pyro.core
# Get the logger
log = logging.getLogger("deluge")
class Ui: class Ui:
def __init__(self, core_uri): def __init__(self, core_uri):
log.debug("Ui init..") log.debug("Ui init..")
log.debug("core_uri: %s", core_uri) log.debug("core_uri: %s", core_uri)
# Get the core manager from the Pyro server # Get the core manager from the Pyro server
self.core = Pyro.core.getProxyForURI(core_uri) if core_uri != None:
# Test self.core = Pyro.core.getProxyForURI(core_uri)
self.core.test() # Test
self.core.test()