mirror of
https://github.com/yweber/lodel2.git
synced 2026-03-24 03:52:01 +01:00
Ajouts
This commit is contained in:
parent
ee795f539d
commit
48165e0fcb
3 changed files with 47 additions and 9 deletions
|
|
@ -55,7 +55,7 @@ class SettingsLoader(object):
|
|||
# @param mandatory bool
|
||||
# @return the option
|
||||
def getoption(self,section,keyname,validator,default_value=None,mandatory=False):
|
||||
conf=copy.copy(self.__conf)
|
||||
conf=self.__conf
|
||||
if section in conf:
|
||||
sec=conf[section]
|
||||
if keyname in sec:
|
||||
|
|
@ -68,11 +68,15 @@ class SettingsLoader(object):
|
|||
return option
|
||||
elif default_value is None and mandatory:
|
||||
raise SettingsError("Default value mandatory for option %s" % keyname)
|
||||
sec['keyname']=dict()
|
||||
sec['keyname']['value'] = default_value
|
||||
sec['keyname']['file'] = 'default_value'
|
||||
sec[keyname]=dict()
|
||||
sec[keyname]['value'] = default_value
|
||||
sec[keyname]['file'] = 'default_value'
|
||||
return default_value
|
||||
else:
|
||||
conf[section]=dict()
|
||||
conf[section][keyname]=dict()
|
||||
conf[section][keyname]['value'] = default_value
|
||||
conf[section][keyname]['file'] = 'default_value'
|
||||
return default_value
|
||||
##@brief Sets option in a config section. Writes in the conf file
|
||||
# @param section str : name of the section
|
||||
|
|
@ -82,16 +86,19 @@ class SettingsLoader(object):
|
|||
# @return the option
|
||||
def setoption(self,section,keyname,value,validator):
|
||||
f_conf=copy.copy(self.__conf[section][keyname]['file'])
|
||||
if f_conf == 'default_value':
|
||||
f_conf = self.__conf_path + '/generated.ini'
|
||||
|
||||
conf=self.__conf
|
||||
conf[section][keyname] = value
|
||||
if f_conf == 'default_value':
|
||||
f_conf = self.conf_path + '/generated.ini'
|
||||
config = configparser.ConfigParser()
|
||||
config.read(f_conf)
|
||||
config[section]={}
|
||||
config[section][keyname] = validator(value)
|
||||
|
||||
with open(f_conf, 'w') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
##@brief Saves new partial configuration. Writes in the conf files corresponding
|
||||
# @param sections dict
|
||||
# @param validators dict of callable : takes one argument value and raises validation fail
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
[lodel2.foo.bar]
|
||||
foo = 42
|
||||
foobar = hello world
|
||||
foo_bar = foobar
|
||||
foo.bar = barfoo
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
#-*- coding: utf-8 -*-
|
||||
|
||||
import unittest
|
||||
import os.path
|
||||
|
||||
from lodel.settings.utils import *
|
||||
from lodel.settings.settings_loader import SettingsLoader
|
||||
|
||||
|
||||
|
||||
#A dummy validator that only returns the value
|
||||
def dummy_validator(value): return value
|
||||
#A dummy validator that always fails
|
||||
|
|
@ -86,8 +89,8 @@ class SettingsLoaderTestCase(unittest.TestCase):
|
|||
value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
|
||||
self.assertEqual(value, 'hello 42')
|
||||
# for non existing section in file
|
||||
# value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
|
||||
# self.assertEqual(value, 'hello 42')
|
||||
value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
|
||||
self.assertEqual(value, 'hello 42')
|
||||
|
||||
def test_getoption_complex(self):
|
||||
""" Testing behavior of getoption with less simple files & confs """
|
||||
|
|
@ -226,3 +229,26 @@ class SettingsLoaderTestCase(unittest.TestCase):
|
|||
self.assertEqual(option,'toto')
|
||||
option=loader.getoption('lodel2.A.e','a',dummy_validator)
|
||||
self.assertEqual(option,'ft')
|
||||
|
||||
def test_setoption_default_value(self):
|
||||
loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
|
||||
|
||||
# for non existing keys in file
|
||||
value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
|
||||
self.assertEqual(value, 'hello 42')
|
||||
# for non existing section in file
|
||||
value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
|
||||
self.assertEqual(value, 'hello 42')
|
||||
|
||||
loader.setoption('lodel2.foo.bar', 'foofoofoo', 'test ok', dummy_validator)
|
||||
loader.setoption('lodel2.foofoo', 'foofoofoo', 'test ok', dummy_validator)
|
||||
self.assertTrue(os.path.isfile('tests/settings/settings_examples/conf_setdef.d/generated.ini'))
|
||||
|
||||
loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
|
||||
value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator)
|
||||
self.assertEqual(value, 'test ok')
|
||||
value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator)
|
||||
self.assertEqual(value, 'test ok')
|
||||
|
||||
os.remove('tests/settings/settings_examples/conf_setdef.d/generated.ini')
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue