Commit f6ed019e authored by M. Huang's avatar M. Huang
Browse files

config dir

parent 73199ba1
......@@ -15,7 +15,7 @@ import pwd
import grp
import os
from os.path import isfile, isdir, join
from os import listdir, chown, chmod, environ, setuid, setgid
from os import listdir
from pathlib import Path
from subprocess import Popen, PIPE, TimeoutExpired, run as srun
from flask import abort, make_response, request, jsonify
......
......@@ -7,7 +7,7 @@ import time
import sys
import functools
import os
from os import listdir, chown, chmod, environ, setuid, setgid
from os import chown, chmod
from pathlib import Path
import logging
import types
......
# -*- coding: utf-8 -*-
from os.path import join, expanduser, expandvars
from os.path import join, expanduser, expandvars, is_dir
import functools
import sys
......@@ -29,26 +29,38 @@ class Instance():
# @functools.lru_cache(8)
def getConfig(name=None, conf='pns'):
""" Imports a dict named [conf]config defined in ~/.config/[conf]local.py to update defaults in pns.config.
""" Imports a dict named [conf]config.
The contents of the config are defined in the ``.config/[conf]local.py`` file. The contenss are used to update defaults in ``fdi.pns.config``.
Th config file is given by the environment variable ``CONF_DIR``, which if not given or pointing to an existing directly is process owner's ``~/.config`` directory.
name: if given the poolurl in ``poolurl_of`` is returned, else construct a poolurl from the contents in dict <conf>config.
conf: configuration ID. default 'pns', so the file is 'pnslocal.py'.
"""
# default configuration is provided. Copy pns/config.py to ~/.config/pnslocal.py
config = {}
env = expanduser(expandvars('$HOME'))
# apache wsgi will return '$HOME' with no expansion
env = '/root' if env == '$HOME' else env
confp = join(env, '.config')
epath = expandvars('$CONF_DIR')
if is_dir(epath):
confp = epath
else:
env = expanduser(epath)
# apache wsgi will return '$HOME' with no expansion
if env == '$HOME':
env = '/root'
confp = join(env, '.config')
sys.path.insert(0, confp)
# this is the stem part of filename and the name of the returned dict
stem = conf+'config'
logger.info('Reading from configuration file %s/%s.py' % (confp, stem))
# this is the var_name part of filename and the name of the returned dict
var_name = conf+'config'
module_name = conf+'local'
file_name = module_name + '.py'
logger.info('Reading from configuration file %s/%s.py' %
(confp, file_name))
try:
c = __import__(conf+'local', globals(), locals(), [stem], 0)
logger.debug('Reading %s/%s.py done.' % (confp, stem))
config.update(c.__dict__[stem])
c = __import__(module_name, globals(), locals(), [stem], 0)
logger.debug('Reading %s/%s.py done.' % (confp, file_name))
config.update(c.__dict__[var_name])
except ModuleNotFoundError as e:
logger.warning(str(
e) + '. Use default config in the package, such as fdi/pns/config.py. Copy it to ~/.config/[package]local.py and make persistent customization there.')
......
......@@ -358,3 +358,6 @@ def test_opt():
pass
else:
assert 0, 'failed to exit.'
def test_getConfig():
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment