From: <umg...@us...> - 2007-02-28 21:37:21
|
Revision: 358 http://svn.sourceforge.net/pybridge/?rev=358&view=rev Author: umgangee Date: 2007-02-28 13:37:21 -0800 (Wed, 28 Feb 2007) Log Message: ----------- Move Settings class out of UI, to enable reuse. Modified Paths: -------------- trunk/pybridge/pybridge/ui/utils.py Added Paths: ----------- trunk/pybridge/pybridge/settings.py Added: trunk/pybridge/pybridge/settings.py =================================================================== --- trunk/pybridge/pybridge/settings.py (rev 0) +++ trunk/pybridge/pybridge/settings.py 2007-02-28 21:37:21 UTC (rev 358) @@ -0,0 +1,53 @@ +# PyBridge -- online contract bridge made easy. +# Copyright (C) 2004-2007 PyBridge Project. +# +# 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 +# of the License, 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. + + +import ConfigParser + + +class Settings: + """A wrapper for ConfigParser.""" + + + def __init__(self, path, sections): + """Make section key/value pairs into attributes of this object. + + @param path: the location of configuration file to load. + @param sections: a list of section names to be made available. + """ + self._path = path + self._config = ConfigParser.SafeConfigParser() + self._config.read(path) + + for section in sections: + # Create sections if they do not exist. + if not self._config.has_section(section): + self._config.add_section(section) + # Make items in section available as + items = {} + for key, value in self._config.items(section): + items[key] = value + setattr(self, section.lower(), items) # self.<section> = items + + + def save(self): + """Writes contents of section/item dicts back to file.""" + for section in self._config.sections(): + for key, value in getattr(self, section.lower()).items(): + self._config.set(section, key, value) + self._config.write(file(self._path, 'w')) + Modified: trunk/pybridge/pybridge/ui/utils.py =================================================================== --- trunk/pybridge/pybridge/ui/utils.py 2007-02-28 18:33:16 UTC (rev 357) +++ trunk/pybridge/pybridge/ui/utils.py 2007-02-28 21:37:21 UTC (rev 358) @@ -16,8 +16,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -import pybridge.environment as env - # Set up client with UI event handler. from pybridge.network.client import client from eventhandler import eventhandler @@ -26,54 +24,13 @@ PORT = 5040 # Default port for PyBridge. +import pybridge.environment as env +from pybridge.settings import Settings +file = env.find_config_client('client.cfg') +settings = Settings(file, ['Connection', 'General']) -import ConfigParser - -class Settings: - """""" - - connection = {} - general = {} - - - def __init__(self, filename): - self.config = ConfigParser.SafeConfigParser() - self.filename = filename - self.read() - - - def read(self): - """""" - self.config.read(self.filename) - - # Create sections if they do not exist. - for section in ('Connection', 'General'): - if not self.config.has_section(section): - self.config.add_section(section) - self.write() - - for key, value in self.config.items('Connection'): - self.connection[key] = value - for key, value in self.config.items('General'): - self.general[key] = value - - - def write(self): - """""" - for key, value in self.connection.items(): - self.config.set('Connection', key, value) - for key, value in self.general.items(): - self.config.set('General', key, value) - self.config.write(file(self.filename, 'w')) - - -settings = Settings(env.find_config_client('client.cfg')) - - - - import imp from UserDict import UserDict @@ -123,7 +80,7 @@ def quit(): """Shutdown gracefully.""" client.disconnect() - settings.write() # Save settings. + settings.save() # Save settings. reactor.stop() gtk.main_quit() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |