def test_set_config_param(config_default): # should autosave config.set_config_param('labstate.filepath', 'blah') assert config.get_config_param('labstate.filepath') == 'blah' config.set_config_param('test_section.test_option', 'blah1') config.set_config_param('test_section.test_option2', 'blah2') assert config.get_config_param('test_section.test_option') == 'blah1' assert config.get_config_param('test_section.test_option2') == 'blah2' config.reset_config_param('test_section.test_option2') # back to default # reset value of test_option2 assert config.get_config_param('test_section.test_option2') == 'test_value2' config.reset_config_param('test_section') assert config.get_config_param('test_section.test_option') == 'test_value'
def test_exceptions(config_default): with pytest.raises(config.InvalidSection): config.get_config_param('invalid_section') config.print_config_param('invalid_section') first_section = next(iter(default_config.keys())) with pytest.raises(config.InvalidOption): config.get_config_param(f"{first_section}.invalid_option") with pytest.raises(config.InvalidOption): config.print_config_param(f"{first_section}.invalid_option") with pytest.raises(config.InvalidOption): config.get_config_param(f"{first_section}") # just the section
import getpass from datetime import datetime from json import JSONDecodeError from pathlib import Path import shutil import os def timestamp_string(): """ Returns timestamp in iso format (e.g. 2018-03-25T18:30:55.328389)""" return str(datetime.now().isoformat()) json = jsonpickle.json # _filename = Path("/home/jupyter/labstate.json") _filename = os.path.expanduser(config.get_config_param( 'labstate.filepath')) # resolve '~' to home folder try: _filename = Path(_filename).resolve() # resolve symlinks if any can_write = True if not os.path.isfile(_filename): # file does not exist # try to make directory os.makedirs(_filename.parent, exist_ok=True) # check if directory is writable can_write = os.access(_filename.parent, os.W_OK) else:
from datetime import datetime from json import JSONDecodeError from pathlib import Path import shutil import os def timestamp_string(): """ Returns timestamp in iso format (e.g. 2018-03-25T18:30:55.328389)""" return str(datetime.now().isoformat()) json = jsonpickle.json # _filename = Path("/home/jupyter/labstate.json") _filename = os.path.expanduser( config.get_config_param('labstate.filepath')) # resolve '~' to home folder try: _filename = Path(_filename).resolve() # resolve symlinks if any can_write = True if not os.path.isfile(_filename): # file does not exist # try to make directory os.makedirs(_filename.parent, exist_ok=True) # check if directory is writable can_write = os.access(_filename.parent, os.W_OK) else: # file exists