new, simplified, easier to use config implementation

tests are provided, but the rest of the code has not be updated yet.
This commit is contained in:
Fabien Benureau 2013-07-01 13:14:53 +01:00
parent 0d16c6a5a3
commit e92c418d80
2 changed files with 88 additions and 42 deletions

View File

@ -1,59 +1,75 @@
import os import os
import ConfigParser import copy
from p3 import configparser
# constant stuff (DFT = DEFAULT)
MAIN_SECTION = 'papers' MAIN_SECTION = 'papers'
CONFIG_PATH = os.path.expanduser('~/.papersrc') DFT_CONFIG_PATH = os.path.expanduser('~/.papersrc')
DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers') DFT_PAPERS_DIR = os.path.expanduser('~/.papers')
DEFAULT_OPEN_CMD = 'open' DFT_OPEN_CMD = 'open'
try: try:
DEFAULT_EDIT_CMD = os.environ['EDITOR'] DFT_EDIT_CMD = os.environ['EDITOR']
except KeyError: except KeyError:
DEFAULT_EDIT_CMD = 'vi' DFT_EDIT_CMD = 'vi'
DEFAULT_IMPORT_COPY = 'yes' DFT_IMPORT_COPY = 'yes'
DEFAULT_IMPORT_MOVE = 'no' DFT_IMPORT_MOVE = 'no'
DEFAULT_COLOR = 'yes' DFT_COLOR = 'yes'
DEFAULT_PLUGINS = 'texnote' DFT_PLUGINS = 'texnote'
CONFIG = ConfigParser.SafeConfigParser({ DFT_CONFIG = configparser.SafeConfigParser({
'papers-directory': DEFAULT_PAPERS_DIRECTORY, 'papers_dir' : DFT_PAPERS_DIR,
'open-cmd': DEFAULT_OPEN_CMD, 'open_cmd' : DFT_OPEN_CMD,
'edit-cmd': DEFAULT_EDIT_CMD, 'edit_cmd' : DFT_EDIT_CMD,
'import-copy': DEFAULT_IMPORT_COPY, 'import_copy' : DFT_IMPORT_COPY,
'import-move': DEFAULT_IMPORT_MOVE, 'import_move' : DFT_IMPORT_MOVE,
'color': DEFAULT_COLOR, 'color' : DFT_COLOR,
'plugins': DEFAULT_PLUGINS}) 'plugins' : DFT_PLUGINS
CONFIG.add_section(MAIN_SECTION) })
BOOLEANS = {'import-copy', 'import-move', 'color'}
DFT_CONFIG.add_section(MAIN_SECTION)
def read_config(): # package-shared config that can be accessed using :
CONFIG.read(CONFIG_PATH) # from configs import config
return CONFIG config = None
class Config(object):
def add_and_write_option(section, option, value): def __init__(self):
cfg = ConfigParser.ConfigParser() object.__setattr__(self, '_cfg', copy.copy(DFT_CONFIG))
cfg.read(CONFIG_PATH)
if not cfg.has_section(section):
cfg.add_section(section)
cfg.set(section, option, value) def as_global(self):
global config
config = self
f = open(CONFIG_PATH, 'w') def load(self, path = DFT_CONFIG_PATH):
cfg.write(f) self._cfg.read(path)
f.close() return self
def save(self, path = DFT_CONFIG_PATH):
with open(path, 'w') as f:
self._cfg.write(f)
def get_plugins(cfg): def __setattr__(self, name, value):
return cfg.get(MAIN_SECTION, 'plugins').split() if type(value) is bool:
BOOLEANS.add(name)
self._cfg.set(MAIN_SECTION, name, str(value))
def __getattr__(self, name):
value = self._cfg.get(MAIN_SECTION, name)
if name in BOOLEANS:
value = str2bool(value)
return value
def get_boolean(value, default = False): def items(self):
value = str(value).lower() for name, value in self._cfg.items(MAIN_SECTION):
if value in ('yes', 'true', 't', 'y', '1'): if name in BOOLEANS:
return True value = str2bool(value)
elif value in ('no', 'false', 'f', 'n', '0'): yield name, value
return False
else: def str2bool(s):
return default return str(s).lower() in ('yes', 'true', 't', 'y', '1')

30
tests/test_config.py Normal file
View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
import unittest
import testenv
from papers import configs
class TestConfig(unittest.TestCase):
def test_create_config(self):
a = configs.Config()
a.as_global()
from papers.configs import config
self.assertEqual(a, config)
def test_config_content(self):
a = configs.Config()
a.as_global()
from papers.configs import config
self.assertEqual(config.papers_dir, configs.DFT_PAPERS_DIR)
self.assertEqual(config.color, configs.str2bool(configs.DFT_COLOR))
def test_set(self):
a = configs.Config()
a.as_global()
from papers.configs import config
config.color = 'no'
self.assertEqual(config.color, False)
# booleans type for new variables are memorized, but not saved.
config.bla = True
self.assertEqual(config.bla, True)