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 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'}
|
||||||
|
|
||||||
def read_config():
|
DFT_CONFIG.add_section(MAIN_SECTION)
|
||||||
CONFIG.read(CONFIG_PATH)
|
|
||||||
return CONFIG
|
|
||||||
|
|
||||||
|
|
||||||
def add_and_write_option(section, option, value):
|
# package-shared config that can be accessed using :
|
||||||
cfg = ConfigParser.ConfigParser()
|
# from configs import config
|
||||||
cfg.read(CONFIG_PATH)
|
config = None
|
||||||
if not cfg.has_section(section):
|
|
||||||
cfg.add_section(section)
|
|
||||||
|
|
||||||
cfg.set(section, option, value)
|
class Config(object):
|
||||||
|
|
||||||
f = open(CONFIG_PATH, 'w')
|
def __init__(self):
|
||||||
cfg.write(f)
|
object.__setattr__(self, '_cfg', copy.copy(DFT_CONFIG))
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
def as_global(self):
|
||||||
|
global config
|
||||||
|
config = self
|
||||||
|
|
||||||
def get_plugins(cfg):
|
def load(self, path = DFT_CONFIG_PATH):
|
||||||
return cfg.get(MAIN_SECTION, 'plugins').split()
|
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):
|
def __setattr__(self, name, value):
|
||||||
value = str(value).lower()
|
if type(value) is bool:
|
||||||
if value in ('yes', 'true', 't', 'y', '1'):
|
BOOLEANS.add(name)
|
||||||
return True
|
self._cfg.set(MAIN_SECTION, name, str(value))
|
||||||
elif value in ('no', 'false', 'f', 'n', '0'):
|
|
||||||
return False
|
def __getattr__(self, name):
|
||||||
else:
|
value = self._cfg.get(MAIN_SECTION, name)
|
||||||
return default
|
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