Return proper setting type
This commit is contained in:
1 parent
1dd130e164
commit
12ce6bc7ab
2 files changed
+34
-16
No files matched your search
@@ -72,7 +72,7 @@ class Loader:
|
|||||||
options = {}
|
options = {}
|
||||||
for group in section['groups']:
|
for group in section['groups']:
|
||||||
for option in group['options']:
|
for option in group['options']:
|
||||||
options[option['name']] = option.get('default', '')
|
options[option['name']] = option
|
||||||
fireEvent('settings.register', section_name = section['name'], options = options, save = save)
|
fireEvent('settings.register', section_name = section['name'], options = options, save = save)
|
||||||
return True
|
return True
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from couchpotato.api import addApiView
|
from couchpotato.api import addApiView
|
||||||
from couchpotato.core.event import addEvent
|
from couchpotato.core.event import addEvent
|
||||||
from couchpotato.core.helpers.encoding import isInt
|
from couchpotato.core.helpers.encoding import isInt, toUnicode
|
||||||
from couchpotato.core.helpers.request import getParams, jsonified
|
from couchpotato.core.helpers.request import getParams, jsonified
|
||||||
from couchpotato.core.helpers.variable import mergeDicts
|
from couchpotato.core.helpers.variable import mergeDicts
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
@@ -10,8 +10,8 @@ import time
|
|||||||
|
|
||||||
class Settings():
|
class Settings():
|
||||||
|
|
||||||
bool = {'true':True, 'false':False}
|
|
||||||
options = {}
|
options = {}
|
||||||
|
types = {}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
@@ -42,32 +42,44 @@ class Settings():
|
|||||||
|
|
||||||
def registerDefaults(self, section_name, options = {}, save = True):
|
def registerDefaults(self, section_name, options = {}, save = True):
|
||||||
self.addSection(section_name)
|
self.addSection(section_name)
|
||||||
for option, value in options.iteritems():
|
for option_name, option in options.iteritems():
|
||||||
self.setDefault(section_name, option, value)
|
self.setDefault(section_name, option_name, option.get('default', ''))
|
||||||
|
|
||||||
#self.log.debug('Defaults for "%s": %s' % (section_name, options))
|
if option.get('type'):
|
||||||
|
self.setType(section_name, option_name, option.get('type'))
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
self.save(self)
|
self.save(self)
|
||||||
|
|
||||||
def set(self, section, option, value):
|
def set(self, section, option, value):
|
||||||
return self.p.set(section, option, self.cleanValue(value))
|
return self.p.set(section, option, value)
|
||||||
|
|
||||||
def get(self, option = '', section = 'core', default = ''):
|
def get(self, option = '', section = 'core', default = ''):
|
||||||
try:
|
try:
|
||||||
value = self.p.get(section, option)
|
|
||||||
return self.cleanValue(value)
|
try: type = self.types[section][option]
|
||||||
|
except: type = 'unicode'
|
||||||
|
|
||||||
|
if hasattr(self, 'get%s' % type.capitalize()):
|
||||||
|
return getattr(self, 'get%s' % type.capitalize())(section, option)
|
||||||
|
else:
|
||||||
|
return self.getUnicode(section, option)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def cleanValue(self, value):
|
def getEnabler(self, section, option):
|
||||||
if(isInt(value)):
|
return self.p.getboolean(section, option)
|
||||||
return int(value)
|
|
||||||
|
|
||||||
if str(value).lower() in self.bool:
|
def getBool(self, section, option):
|
||||||
return self.bool.get(str(value).lower())
|
return self.p.getboolean(section, option)
|
||||||
|
|
||||||
return value.strip()
|
def getInt(self, section, option):
|
||||||
|
return self.p.getint(section, option)
|
||||||
|
|
||||||
|
def getUnicode(self, section, option):
|
||||||
|
value = self.p.get(section, option)
|
||||||
|
return toUnicode(value.strip())
|
||||||
|
|
||||||
def getValues(self):
|
def getValues(self):
|
||||||
values = {}
|
values = {}
|
||||||
@@ -75,7 +87,7 @@ class Settings():
|
|||||||
values[section] = {}
|
values[section] = {}
|
||||||
for option in self.p.items(section):
|
for option in self.p.items(section):
|
||||||
(option_name, option_value) = option
|
(option_name, option_value) = option
|
||||||
values[section][option_name] = self.cleanValue(option_value)
|
values[section][option_name] = self.get(option_name, section)
|
||||||
return values
|
return values
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
@@ -92,6 +104,12 @@ class Settings():
|
|||||||
if not self.p.has_option(section, option):
|
if not self.p.has_option(section, option):
|
||||||
self.p.set(section, option, value)
|
self.p.set(section, option, value)
|
||||||
|
|
||||||
|
def setType(self, section, option, type):
|
||||||
|
if not self.types.get(section):
|
||||||
|
self.types[section] = {}
|
||||||
|
|
||||||
|
self.types[section][option] = type
|
||||||
|
|
||||||
def addOptions(self, section_name, options):
|
def addOptions(self, section_name, options):
|
||||||
|
|
||||||
if not self.options.get(section_name):
|
if not self.options.get(section_name):
|
||||||
|
|||||||
Reference in new issue
Block a user