X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=azure%2Faria%2Faria-extension-cloudify%2Fsrc%2Faria%2Faria%2Fcli%2Fconfig%2Fconfig.py;fp=azure%2Faria%2Faria-extension-cloudify%2Fsrc%2Faria%2Faria%2Fcli%2Fconfig%2Fconfig.py;h=bbece801dc7aa0c6192e055c672f2e4e4ed88005;hb=7409dfb144cf2a06210400134d822a1393462b1f;hp=0000000000000000000000000000000000000000;hpb=9e65649dfff8f00dc0a0ef6b10d020ae0e2255ba;p=multicloud%2Fazure.git diff --git a/azure/aria/aria-extension-cloudify/src/aria/aria/cli/config/config.py b/azure/aria/aria-extension-cloudify/src/aria/aria/cli/config/config.py new file mode 100644 index 0000000..bbece80 --- /dev/null +++ b/azure/aria/aria-extension-cloudify/src/aria/aria/cli/config/config.py @@ -0,0 +1,93 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +CLI configuration mechanism. +""" + +import os +import pkg_resources +from ruamel import yaml + +from jinja2.environment import Template + + +CONFIG_FILE_NAME = 'config.yaml' + + +class CliConfig(object): + + def __init__(self, config_path): + with open(config_path) as f: + self._config = yaml.safe_load(f.read()) + + @classmethod + def create_config(cls, workdir): + config_path = os.path.join(workdir, CONFIG_FILE_NAME) + if not os.path.isfile(config_path): + config_template = pkg_resources.resource_string( + __package__, + 'config_template.yaml') + + default_values = { + 'log_path': os.path.join(workdir, 'cli.log'), + 'enable_colors': True + } + + template = Template(config_template) + rendered = template.render(**default_values) + with open(config_path, 'w') as f: + f.write(rendered) + f.write(os.linesep) + + return cls(config_path) + + @property + def logging(self): + return self.Logging(self._config.get('logging')) + + class Logging(object): + + def __init__(self, logging): + self._logging = logging or {} + + @property + def filename(self): + return self._logging.get('filename') + + @property + def loggers(self): + return self._logging.get('loggers', {}) + + @property + def execution(self): + return self.Execution(self._logging.get('execution')) + + class Execution(object): + + def __init__(self, execution_logging): + self._execution_logging = execution_logging + + @property + def colors_enabled(self): + return self.colors.get('enabled', False) + + @property + def colors(self): + return self._execution_logging.get('colors', {}) + + @property + def formats(self): + return self._execution_logging.get('formats', {})