"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/config.py" (27 Sep 2019, 2743 Bytes) of package /linux/misc/openstack/monasca-api-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "config.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.0_vs_3.0.0.

    1 # Copyright 2017 FUJITSU LIMITED
    2 # Copyright 2018 OP5 AB
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 # not use this file except in compliance with the License. You may obtain
    6 # a copy of the License at
    7 #
    8 #      http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 # License for the specific language governing permissions and limitations
   14 # under the License.
   15 
   16 import sys
   17 
   18 from oslo_config import cfg
   19 from oslo_log import log
   20 from oslo_policy import opts as policy_opts
   21 
   22 from monasca_api import conf
   23 from monasca_api import version
   24 
   25 CONF = conf.CONF
   26 LOG = log.getLogger(__name__)
   27 
   28 _CONF_LOADED = False
   29 _GUNICORN_MARKER = 'gunicorn'
   30 
   31 
   32 def parse_args(argv=None):
   33     """Loads application configuration.
   34 
   35     Loads entire application configuration just once.
   36 
   37     """
   38     global _CONF_LOADED
   39     if _CONF_LOADED:
   40         LOG.debug('Configuration has been already loaded')
   41         return
   42 
   43     log.set_defaults()
   44     log.register_options(CONF)
   45 
   46     argv = (argv if argv is not None else sys.argv[1:])
   47     args = ([] if _is_running_under_gunicorn() else argv or [])
   48 
   49     CONF(args=args,
   50          prog=sys.argv[1:],
   51          project='monasca',
   52          version=version.version_str,
   53          default_config_files=get_config_files(),
   54          description='RESTful API for alarming in the cloud')
   55 
   56     log.setup(CONF,
   57               product_name='monasca-api',
   58               version=version.version_str)
   59     conf.register_opts()
   60     policy_opts.set_defaults(CONF)
   61 
   62     _CONF_LOADED = True
   63 
   64 
   65 def get_config_files():
   66     """Get the possible configuration files accepted by oslo.config
   67 
   68     This also includes the deprecated ones
   69     """
   70     # default files
   71     conf_files = cfg.find_config_files(project='monasca', prog='monasca-api')
   72     # deprecated config files (only used if standard config files are not there)
   73     if len(conf_files) == 0:
   74         for prog_name in ['api', 'api-config']:
   75             old_conf_files = cfg.find_config_files(project='monasca', prog=prog_name)
   76             if len(old_conf_files) > 0:
   77                 LOG.warning('Found deprecated old location "{}" '
   78                             'of main configuration file'.format(old_conf_files))
   79                 conf_files += old_conf_files
   80     return conf_files
   81 
   82 
   83 def _is_running_under_gunicorn():
   84     """Evaluates if api runs under gunicorn."""
   85     content = filter(lambda x: x != sys.executable and _GUNICORN_MARKER in x,
   86                      sys.argv or [])
   87     return len(list(content) if not isinstance(content, list) else content) > 0