From 29c4b6aee1c687c2b1529011382ddb26080043b8 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Thu, 5 Jul 2007 19:35:59 +0000 Subject: [PATCH] Just some updates.. Probably going to change to DBUS instead of Pyro in next revision. --- deluge/src/common.py | 54 ++++++++++++++++++++++++ deluge/src/config.py | 98 ++++++++++++++++++++++++++++++++++++++++++++ deluge/src/core.py | 11 ++++- deluge/src/daemon.py | 10 +++-- deluge/src/main.py | 22 +++++----- deluge/src/ui.py | 12 +++--- 6 files changed, 186 insertions(+), 21 deletions(-) create mode 100644 deluge/src/common.py create mode 100644 deluge/src/config.py diff --git a/deluge/src/common.py b/deluge/src/common.py new file mode 100644 index 000000000..d0d04510d --- /dev/null +++ b/deluge/src/common.py @@ -0,0 +1,54 @@ +# +# common.py +# +# Copyright (C) Andrew Resch 2007 +# +# 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") + diff --git a/deluge/src/config.py b/deluge/src/config.py new file mode 100644 index 000000000..4b7af2d1d --- /dev/null +++ b/deluge/src/config.py @@ -0,0 +1,98 @@ +# +# config.py +# +# Copyright (C) Andrew Resch 2007 +# +# 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 diff --git a/deluge/src/core.py b/deluge/src/core.py index 9e73020df..6019f67c1 100644 --- a/deluge/src/core.py +++ b/deluge/src/core.py @@ -31,13 +31,22 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. -# Instantiate the logger import logging + +from deluge.config import Config +import deluge.common + +# Get the logger log = logging.getLogger("deluge") +DEFAULT_PREFS = { +} + class Core: def __init__(self): log.debug("Core init..") + + self.config = Config("core.conf", DEFAULT_PREFS) def test(self): print "test" diff --git a/deluge/src/daemon.py b/deluge/src/daemon.py index 3158938f0..0a756a485 100644 --- a/deluge/src/daemon.py +++ b/deluge/src/daemon.py @@ -31,20 +31,22 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. -# Instantiate the logger import logging -log = logging.getLogger("deluge") import Pyro.core + from deluge.core import Core +# Get the logger +log = logging.getLogger("deluge") + class Daemon: def __init__(self): # Instantiate the Manager class self.core = Core() # Initialize the Pyro core and daemon Pyro.core.initServer(banner=0) - log.info("Pyro server initiliazed..") + log.debug("Pyro server initiliazed..") self.daemon = Pyro.core.Daemon() # Connect the Manager to the Pyro server obj = Pyro.core.ObjBase() @@ -56,6 +58,6 @@ class Daemon: # Start the main loop for the pyro daemon self.daemon.requestLoop() - def getURI(self): + def get_uri(self): # Return the URI for the Pyro server return self.uri diff --git a/deluge/src/main.py b/deluge/src/main.py index 13d8b680e..9f5110d6b 100644 --- a/deluge/src/main.py +++ b/deluge/src/main.py @@ -1,9 +1,6 @@ -#!/usr/bin/env python # # main.py # -# Copyright (C) Zach Tibbitts 2006 -# Copyright (C) Alon Zakai 2006 # Copyright (C) Andrew Resch 2007 # # Deluge is free software. @@ -37,16 +34,16 @@ # The main starting point for the program. This function is called when the # user runs the command 'deluge'. +import logging import os import signal - from optparse import OptionParser -import deluge.common + from deluge.daemon import Daemon from deluge.ui import Ui +import deluge.common # Setup the logger -import logging logging.basicConfig( level=logging.DEBUG, format="[%(levelname)-8s] %(name)s:%(module)s:%(lineno)d %(message)s" @@ -55,10 +52,10 @@ logging.basicConfig( log = logging.getLogger("deluge") def main(): - log.info("Starting Deluge..") - # 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", metavar="DAEMON", action="store_true", default=False) parser.add_option("--ui", dest="ui", help="Start Deluge UI", @@ -66,10 +63,12 @@ def main(): # Get the options and args from the OptionParser (options, args) = parser.parse_args() + + log.info("Deluge %s", deluge.common.get_version()) log.debug("options: %s", options) log.debug("args: %s", args) - + daemon = None pid = None uri = None @@ -78,8 +77,9 @@ def main(): if options.daemon: log.info("Starting daemon..") daemon = Daemon() - uri = daemon.getURI() + uri = daemon.get_uri() # We need to fork() the process to run it in the background... + # FIXME: We cannot use fork() on Windows pid = os.fork() if not pid: daemon.start() diff --git a/deluge/src/ui.py b/deluge/src/ui.py index ef8113bc2..1d16110a7 100644 --- a/deluge/src/ui.py +++ b/deluge/src/ui.py @@ -31,18 +31,20 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. -# Instantiate the logger import logging -log = logging.getLogger("deluge") import Pyro.core +# Get the logger +log = logging.getLogger("deluge") + class Ui: def __init__(self, core_uri): log.debug("Ui init..") log.debug("core_uri: %s", core_uri) # Get the core manager from the Pyro server - self.core = Pyro.core.getProxyForURI(core_uri) - # Test - self.core.test() + if core_uri != None: + self.core = Pyro.core.getProxyForURI(core_uri) + # Test + self.core.test()