Skip to content
Snippets Groups Projects
Commit 782baec5 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

ENH: New functions in settings module, primarily to make testing easier

parent a37e0499
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,16 @@ the following functions can be called at the module-level: ...@@ -25,6 +25,16 @@ the following functions can be called at the module-level:
Settings.clear Settings.clear
Some functions are also available to replace the module-level :class:`Settings`
instance:
.. autosummary::
:nosignatures:
set
use
These functions will have no effect before :func:`initialise` is called. These functions will have no effect before :func:`initialise` is called.
Two types of configuration data are available: Two types of configuration data are available:
...@@ -67,15 +77,9 @@ storing configuration files. ...@@ -67,15 +77,9 @@ storing configuration files.
""" """
def initialise(*args, **kwargs): def set(settings):
"""Initialise the ``settings`` module. This function creates a """Set the module-level :class:`Settings` instance. """
:class:`Settings` instance, and enables the module-level mod = sys.modules[__name__]
functions. All settings are passed through to :meth:`Settings.__init__`.
"""
mod = sys.modules[__name__]
settings = Settings(*args, **kwargs)
mod.settings = settings mod.settings = settings
mod.read = settings.read mod.read = settings.read
mod.write = settings.write mod.write = settings.write
...@@ -89,6 +93,31 @@ def initialise(*args, **kwargs): ...@@ -89,6 +93,31 @@ def initialise(*args, **kwargs):
mod.clear = settings.clear mod.clear = settings.clear
def initialise(*args, **kwargs):
"""Initialise the ``settings`` module. This function creates a
:class:`Settings` instance, and enables the module-level
functions. All settings are passed through to :meth:`Settings.__init__`.
"""
set(Settings(*args, **kwargs))
@contextlib.contextmanager
def use(settings):
"""Temporarily replace the module-level :class:`Settings` object
with the given one.
"""
mod = sys.modules[__name__]
old = getattr(mod, 'settings', None)
try:
set(settings)
yield
finally:
if old is not None:
set(old)
# These are all overwritten by # These are all overwritten by
# the initialise function. # the initialise function.
def read(name, default=None): def read(name, default=None):
...@@ -391,7 +420,7 @@ class Settings(object): ...@@ -391,7 +420,7 @@ class Settings(object):
try: try:
with open(configFile, 'wb') as f: with open(configFile, 'wb') as f:
pickle.dump(config, f, protocol=2) pickle.dump(config, f, protocol=2)
except (IOError, pickle.PicklingError, EOFError): except (FileNotFoundError, IOError, pickle.PicklingError, EOFError):
log.warning('Unable to save {} configuration file ' log.warning('Unable to save {} configuration file '
'{}'.format(self.__configID, configFile), '{}'.format(self.__configID, configFile),
exc_info=True) exc_info=True)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment