|
|
@ -1,10 +1,9 @@
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import copy
|
|
|
|
|
|
|
|
from .p3 import configparser
|
|
|
|
from .p3 import configparser
|
|
|
|
|
|
|
|
|
|
|
|
# constant stuff (DFT = DEFAULT)
|
|
|
|
# constant stuff (DFT = DEFAULT)
|
|
|
|
|
|
|
|
|
|
|
|
MAIN_SECTION = 'papers'
|
|
|
|
MAIN_SECTION = 'papers'
|
|
|
|
DFT_CONFIG_PATH = os.path.expanduser('~/.papersrc')
|
|
|
|
DFT_CONFIG_PATH = os.path.expanduser('~/.papersrc')
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
DFT_EDIT_CMD = os.environ['EDITOR']
|
|
|
|
DFT_EDIT_CMD = os.environ['EDITOR']
|
|
|
@ -32,16 +31,19 @@ BOOLEANS = {'import_copy', 'import_move', 'color', 'version_warning'}
|
|
|
|
# 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(section = MAIN_SECTION):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def config(section=MAIN_SECTION):
|
|
|
|
if _config is None:
|
|
|
|
if _config is None:
|
|
|
|
raise ValueError('not config instanciated yet')
|
|
|
|
raise ValueError('not config instanciated yet')
|
|
|
|
_config._section = section
|
|
|
|
_config._section = section
|
|
|
|
return _config
|
|
|
|
return _config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config(object):
|
|
|
|
class Config(object):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
object.__setattr__(self, '_section', MAIN_SECTION) # active section
|
|
|
|
object.__setattr__(self, '_section', MAIN_SECTION) # active section
|
|
|
|
object.__setattr__(self, '_cfg', configparser.SafeConfigParser())
|
|
|
|
object.__setattr__(self, '_cfg', configparser.SafeConfigParser())
|
|
|
|
|
|
|
|
|
|
|
|
self._cfg.add_section(self._section)
|
|
|
|
self._cfg.add_section(self._section)
|
|
|
@ -55,11 +57,11 @@ class Config(object):
|
|
|
|
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)
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def save(self, path = DFT_CONFIG_PATH):
|
|
|
|
def save(self, path=DFT_CONFIG_PATH):
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
self._cfg.write(f)
|
|
|
|
self._cfg.write(f)
|
|
|
|
|
|
|
|
|
|
|
@ -77,7 +79,7 @@ class Config(object):
|
|
|
|
value = str2bool(value)
|
|
|
|
value = str2bool(value)
|
|
|
|
return value
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, name, default = None):
|
|
|
|
def get(self, name, default=None):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return self.__getattr__(name)
|
|
|
|
return self.__getattr__(name)
|
|
|
|
except (configparser.NoOptionError, configparser.NoSectionError):
|
|
|
|
except (configparser.NoOptionError, configparser.NoSectionError):
|
|
|
@ -89,5 +91,6 @@ class Config(object):
|
|
|
|
value = str2bool(value)
|
|
|
|
value = str2bool(value)
|
|
|
|
yield name, value
|
|
|
|
yield name, value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def str2bool(s):
|
|
|
|
def str2bool(s):
|
|
|
|
return str(s).lower() in ('yes', 'true', 't', 'y', '1')
|
|
|
|
return str(s).lower() in ('yes', 'true', 't', 'y', '1')
|
|
|
|