From ae36d5b4959262916f6ba6ceeb63f5756bcb38bc Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Sat, 20 Feb 2010 14:16:21 +0100 Subject: [PATCH] properly coerce Murmur's config variable to Python types and handle None --- pyweb/mumble/models.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/pyweb/mumble/models.py b/pyweb/mumble/models.py index 96bc0de..310a837 100644 --- a/pyweb/mumble/models.py +++ b/pyweb/mumble/models.py @@ -26,17 +26,26 @@ from mumble.mmobjects import mmChannel, mmPlayer from mumble.mctl import MumbleCtlBase -def mk_config_property( field, doc="" ): +def mk_config_property( field, doc="", get_coerce=None, get_none=None, set_coerce=str, set_none='' ): """ Create a property for the given config field. """ def get_field( self ): if self.id is not None: - return self.getConf( field ) - else: - return None + val = self.getConf( field ); + if val is None or val == '': + return get_none + if callable(get_coerce): + return get_coerce( val ) + return val + return None def set_field( self, value ): - self.setConf( field, str(value) ) + if value is None: + self.setConf( field, set_none ) + elif callable(set_coerce): + self.setConf( field, set_coerce(value) ) + else: + self.setConf( field, value ) return property( get_field, set_field, doc=doc ) @@ -108,13 +117,13 @@ class Mumble( models.Model ): url = mk_config_property( "registerurl", "Website URL" ) motd = mk_config_property( "welcometext", "Welcome Message" ) passwd = mk_config_property( "password", "Server Password" ) - users = mk_config_property( "users", "Max. Users" ) - bwidth = mk_config_property( "bandwidth", "Bandwidth [Bps]" ) + users = mk_config_property( "users", "Max. Users", get_coerce=int ) + bwidth = mk_config_property( "bandwidth", "Bandwidth [Bps]", get_coerce=int ) sslcrt = mk_config_property( "certificate", "SSL Certificate" ) sslkey = mk_config_property( "key", "SSL Key" ) player = mk_config_property( "username", "Player name regex" ) channel = mk_config_property( "channelname", "Channel name regex" ) - defchan = mk_config_property( "defaultchannel", "Default channel" ) + defchan = mk_config_property( "defaultchannel", "Default channel", get_coerce=int ) obfsc = property( lambda self: ( self.getConf( "obfuscate" ) == "true" ) if self.id is not None else None,