more tests for config, slightly changed implementation and interface.
This commit is contained in:
parent
e92c418d80
commit
f934e8c2ec
@ -18,33 +18,33 @@ DFT_IMPORT_MOVE = 'no'
|
|||||||
DFT_COLOR = 'yes'
|
DFT_COLOR = 'yes'
|
||||||
DFT_PLUGINS = 'texnote'
|
DFT_PLUGINS = 'texnote'
|
||||||
|
|
||||||
DFT_CONFIG = configparser.SafeConfigParser({
|
DFT_CONFIG = {'papers_dir' : DFT_PAPERS_DIR,
|
||||||
'papers_dir' : DFT_PAPERS_DIR,
|
'open_cmd' : DFT_OPEN_CMD,
|
||||||
'open_cmd' : DFT_OPEN_CMD,
|
'edit_cmd' : DFT_EDIT_CMD,
|
||||||
'edit_cmd' : DFT_EDIT_CMD,
|
'import_copy' : DFT_IMPORT_COPY,
|
||||||
'import_copy' : DFT_IMPORT_COPY,
|
'import_move' : DFT_IMPORT_MOVE,
|
||||||
'import_move' : DFT_IMPORT_MOVE,
|
'color' : DFT_COLOR,
|
||||||
'color' : DFT_COLOR,
|
'plugins' : DFT_PLUGINS
|
||||||
'plugins' : DFT_PLUGINS
|
}
|
||||||
})
|
|
||||||
|
|
||||||
BOOLEANS = {'import-copy', 'import-move', 'color'}
|
BOOLEANS = {'import-copy', 'import-move', 'color'}
|
||||||
|
|
||||||
DFT_CONFIG.add_section(MAIN_SECTION)
|
|
||||||
|
|
||||||
|
|
||||||
# package-shared config that can be accessed using :
|
# package-shared config that can be accessed using :
|
||||||
# from configs import config
|
# from configs import config
|
||||||
config = None
|
_config = None
|
||||||
|
def config():
|
||||||
|
return _config
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
object.__setattr__(self, '_cfg', copy.copy(DFT_CONFIG))
|
object.__setattr__(self, '_cfg', configparser.SafeConfigParser(DFT_CONFIG))
|
||||||
|
self._cfg.add_section(MAIN_SECTION)
|
||||||
|
|
||||||
def as_global(self):
|
def as_global(self):
|
||||||
global config
|
global _config
|
||||||
config = self
|
_config = self
|
||||||
|
|
||||||
def load(self, path = DFT_CONFIG_PATH):
|
def load(self, path = DFT_CONFIG_PATH):
|
||||||
self._cfg.read(path)
|
self._cfg.read(path)
|
||||||
|
@ -3,28 +3,42 @@ import unittest
|
|||||||
|
|
||||||
import testenv
|
import testenv
|
||||||
from papers import configs
|
from papers import configs
|
||||||
|
from papers.configs import config
|
||||||
|
|
||||||
class TestConfig(unittest.TestCase):
|
class TestConfig(unittest.TestCase):
|
||||||
|
|
||||||
def test_create_config(self):
|
def test_create_config(self):
|
||||||
a = configs.Config()
|
a = configs.Config()
|
||||||
a.as_global()
|
a.as_global()
|
||||||
from papers.configs import config
|
self.assertEqual(a, config())
|
||||||
self.assertEqual(a, config)
|
|
||||||
|
|
||||||
def test_config_content(self):
|
def test_config_content(self):
|
||||||
a = configs.Config()
|
a = configs.Config()
|
||||||
a.as_global()
|
a.as_global()
|
||||||
from papers.configs import config
|
self.assertEqual(config().papers_dir, configs.DFT_PAPERS_DIR)
|
||||||
self.assertEqual(config.papers_dir, configs.DFT_PAPERS_DIR)
|
self.assertEqual(config().color, configs.str2bool(configs.DFT_COLOR))
|
||||||
self.assertEqual(config.color, configs.str2bool(configs.DFT_COLOR))
|
|
||||||
|
|
||||||
def test_set(self):
|
def test_set(self):
|
||||||
a = configs.Config()
|
a = configs.Config()
|
||||||
a.as_global()
|
a.as_global()
|
||||||
from papers.configs import config
|
from papers.configs import config
|
||||||
config.color = 'no'
|
config().color = 'no'
|
||||||
self.assertEqual(config.color, False)
|
self.assertEqual(config().color, False)
|
||||||
# booleans type for new variables are memorized, but not saved.
|
# booleans type for new variables are memorized, but not saved.
|
||||||
config.bla = True
|
config().bla = True
|
||||||
self.assertEqual(config.bla, True)
|
self.assertEqual(config().bla, True)
|
||||||
|
|
||||||
|
def test_reload(self):
|
||||||
|
from papers.configs import config
|
||||||
|
|
||||||
|
a = configs.Config()
|
||||||
|
a.as_global()
|
||||||
|
a.color = False
|
||||||
|
a.bla = 'foo'
|
||||||
|
config.color = not configs.str2bool(configs.DFT_COLOR)
|
||||||
|
self.assertEqual(config().color, not configs.str2bool(configs.DFT_COLOR))
|
||||||
|
|
||||||
|
b = configs.Config()
|
||||||
|
b.as_global()
|
||||||
|
self.assertEqual(b, config())
|
||||||
|
self.assertEqual(config().color, configs.str2bool(configs.DFT_COLOR))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user