#----------------------------------------------------------------------------- # Copyright (c) 2012 - 2018, Anaconda, Inc. and Intake contributors # All rights reserved. # # The full license is in the LICENSE file, distributed with this software. #----------------------------------------------------------------------------- from os.path import expanduser import logging import os import posixpath import yaml from intake.utils import yaml_load from .utils import make_path_posix logger = logging.getLogger('intake') confdir = make_path_posix( os.getenv('INTAKE_CONF_DIR', os.path.join(expanduser('~'), '.intake'))) defaults = { 'auth': {'cls': 'intake.auth.base.BaseAuth'}, 'port': 5000, 'cache_dir': posixpath.join(confdir, 'cache'), 'cache_disabled': False, 'cache_download_progress': True, 'logging': 'INFO', 'catalog_path': [], 'persist_path': posixpath.join(confdir, 'persisted') } conf = {} def cfile(): return make_path_posix( os.getenv('INTAKE_CONF_FILE', posixpath.join(confdir, 'conf.yaml'))) def reset_conf(): """Set conf values back to defaults""" conf.clear() conf.update(defaults) def save_conf(fn=None): """Save current configuration to file as YAML If not given, uses current config directory, ``confdir``, which can be set by INTAKE_CONF_DIR. """ if fn is None: fn = cfile() try: os.makedirs(os.path.dirname(fn)) except (OSError, IOError): pass with open(fn, 'w') as f: yaml.dump(conf, f) def load_conf(fn=None): """Update global config from YAML file If fn is None, looks in global config directory, which is either defined by the INTAKE_CONF_DIR env-var or is ~/.intake/ . """ if fn is None: fn = cfile() if os.path.isfile(fn): with open(fn) as f: try: conf.update(yaml_load(f)) except Exception as e: logger.warning('Failure to load config file "{fn}": {e}' ''.format(fn=fn, e=e)) def intake_path_dirs(path): """Return a list of directories from the intake path. If a string, perhaps taken from an environment variable, then the list of paths will be split on the character ":" for posix of ";" for windows. Protocol indicators ("protocol://") will be ignored. """ if isinstance(path, (list, tuple)): return path import re pattern = re.compile(";" if os.name == 'nt' else r"(?