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