/usr/lib/python3/dist-packages/metaconfig/__init__.py is in python3-metaconfig 0.1.4a1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | # BSD Licence
# Copyright (c) 2010, Science & Technology Facilities Council (STFC)
# All rights reserved.
#
# See the LICENSE file in the source distribution of this software for
# the full license text.
"""
State global to the whole package.
"""
import sys, re, os
from configparser import ConfigParser
from metaconfig.mconf import MetaConfig, Error
import logging
log = logging.getLogger(__name__)
_metaconfig = None
def _default_search_path():
search_path = [
'./metaconfig.conf',
os.path.join(os.environ.get('HOME', ''), '.metaconfig.conf'),
os.path.join(sys.prefix, 'etc', 'metaconfig.conf')
]
if 'METACONFIG_CONF' in os.environ:
search_path[0:] = [(os.environ['METACONFIG_CONF'])]
return search_path
def init_from_config(config):
"""
Initialise metaconfig from a :mod:`ConfigParser.ConfigParser` instance.
An exception will be raised if metaconfig has already been initialised.
"""
global _metaconfig
if _metaconfig is not None:
raise Exception("Metaconfig is already initialised")
_metaconfig = MetaConfig.from_config(config)
def init_from_string(config_str):
"""
Initialise metaconfig from a string.
An exception will be raised if metaconfig has already been initialised.
"""
from io import StringIO
mconf = ConfigParser()
mconf.readfp(StringIO(config_str))
init_from_config(mconf)
def init(search_path=None):
"""
Initialise metaconfig.
An exception will be raised if metaconfig has already been initialised.
:param search_path: A sequence of file paths to search for a metaconfig
configuration.
If ``search_path`` is None it defaults to:
1. The value of the ``METACONFIG_CONF`` environment variable if set.
2. ``metaconfig.conf`` in the current directory.
3. ``$HOME/.metaconfig.conf``
4. ``<sys.prefix>/etc/metaconfig.conf``
"""
global _metaconfig
if _metaconfig is not None:
raise Exception("Metaconfig is already initialised")
if search_path is None:
search_path = _default_search_path()
for config in search_path:
if os.path.exists(config):
log.debug('Selected %s as metaconfig.conf' % config)
_metaconfig = MetaConfig.from_config_file(config)
return
else:
_metaconfig = MetaConfig()
def get_config(name, inherit=True):
"""
Returns the :mod:`ConfigParser.ConfigParser` for the given name.
:param name: The name of the config object to return. This is interpreted
as a '.'-separated hierarchical namespace.
:param inherit: If ``True`` and cofig ``name`` does not exist each config
above ``name`` in the hierarchy will be tried before returning an empty
config object. For instance ``get_config("x.y.z")`` would try to
return existing configs ``x.y.z``, ``x.y`` and ``x`` before returning a
new, empty config ``x.y.z``.
"""
if _metaconfig is None:
init()
return _metaconfig.get_config(name, inherit=inherit)
def add_config(name, config_parser):
"""
Add a config_parser object to metaconfig.
"""
if _metaconfig is None:
init()
return _metaconfig.add_config(name, config_parser)
def add_config_file(name, config, ConfigClass=None):
"""
Read a config file and add it to metaconfig.
"""
if _metaconfig is None:
init()
return _metaconfig.add_config_file(name, config, ConfigClass)
def reset():
global _metaconfig
log.warn("Reseting metaconfig. Existing configs will remain.")
_metaconfig = None
|