Created
February 14, 2012 20:45
-
-
Save ei-grad/1830236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imp | |
import os | |
import stat | |
import string | |
import sys | |
from default_settings import * | |
def get_arg(key): | |
return sys.argv[sys.argv.index(key) + 1] | |
def get_args(key): | |
args = [] | |
_ = 0 | |
while True: | |
try: | |
_ = sys.argv.index(key, _) + 1 | |
args.append(sys.argv[_]) | |
_ += 1 | |
except ValueError: | |
break | |
return args | |
def load_config(): | |
conf_file = '/etc/service/service.conf' | |
if '--config' in sys.argv: | |
conf_file = get_arg('--config') | |
if not os.path.exists(conf_file): | |
# XXX: need to configure logging before importing config | |
sys.stderr.write('Config file %s does not exist!\n' % conf_file) | |
return object() | |
if not bool(os.stat(conf_file).st_mode & stat.S_IREAD): | |
sys.stderr.write('Config file %s is not readable!\n' % conf_file) | |
return object() | |
# make module name suitable for logging | |
mod_name = '.'.join([ | |
__name__, | |
os.path.basename(conf_file).rpartition('.')[0] | |
]) | |
try: | |
# it is here for imports in conf_file | |
sys.path.insert(0, os.path.dirname(conf_file)) | |
# load it | |
ret = imp.load_source(mod_name, conf_file) | |
# remove its dir from path, it is not needed now | |
del sys.path[0] | |
return ret | |
except ImportError as exc: | |
sys.stderr.write("Can't import config file %s!" % conf_file) | |
raise exc | |
# LOAD CONFIG PARAMETERS | |
config = load_config() | |
# Update globals with config.[A-Z]* | |
globals().update((i, getattr(config, i)) for i in dir(config) | |
if i[0] in string.ascii_uppercase) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment