replace configure with attrdict

Change-Id: I226e9cd1fa44c9fe47490be7f781ab7030406cf3
This commit is contained in:
ahothan 2015-09-11 15:45:34 -07:00
parent 1d161d9c30
commit e03d1af38a
4 changed files with 61 additions and 33 deletions

View File

@ -5,7 +5,6 @@
pbr<2.0,>=1.3
Babel>=1.3
configure>=0.5
lxml>=3.4.0
paramiko>=1.14.0
prettytable>=0.7.2
@ -17,3 +16,4 @@ python-openstackclient>=0.4.1
python-keystoneclient>=1.0.0
scp>=0.8.0
tabulate>=0.7.3
attrdict>=2.0.0

49
vmtp/config.py Normal file
View File

@ -0,0 +1,49 @@
# Copyright 2015 Cisco Systems, Inc. All rights reserved.
#
# Licensed 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.
#
from attrdict import AttrDict
import yaml
with open('cfg.default.yaml') as fileobj:
settings = AttrDict(yaml.safe_load(fileobj))
def config_load(file_name, from_cfg=None):
'''Load a yaml file into a config dict, merge with from_cfg if not None
The config file content taking precedence in case of duplicate
'''
with open(file_name) as fileobj:
cfg = AttrDict(yaml.safe_load(fileobj))
if from_cfg:
return from_cfg + cfg
return cfg
def config_loads(cfg_text, from_cfg=None):
'''Same as config_load but load from a string
'''
try:
cfg = AttrDict(yaml.load(cfg_text))
except TypeError:
# empty string
cfg = AttrDict()
if from_cfg:
return from_cfg + cfg
return cfg
def test_config():
cfg = config_load('a1.yaml')
cfg = config_load('a2.yaml', cfg)
cfg = config_loads('color: 500', cfg)
config_loads('')
config_loads('#')

View File

@ -62,9 +62,9 @@ class Network(object):
break
if not self.ext_net:
self.ext_net = network
except KeyError:
except AttributeError:
###############################################
# A key error indicates, no user defined
# A attribute error indicates, no user defined
# external network defined, so use the first one
###############################################
self.ext_net = network

View File

@ -27,7 +27,8 @@ import traceback
from __init__ import __version__
import compute
import configure
from config import config_load
from config import config_loads
import credentials
from glanceclient.v2 import client as glanceclient
import iperf_tool
@ -660,7 +661,7 @@ def parse_opts_from_cli():
help='override default values with a config file',
metavar='<config_file>')
parser.add_argument('-sc', '--show-config', dest='show_cofig',
parser.add_argument('-sc', '--show-config', dest='show_config',
default=False,
action='store_true',
help='print the default config')
@ -794,29 +795,6 @@ def parse_opts_from_cli():
return parser.parse_known_args()[0]
def merge_opts_to_configs(opts):
def _merge_config(cfg_file, source_config, required=False):
'''
returns the merged config or exits if the file does not exist and is required
'''
dest_config = source_config
fullname = os.path.expanduser(cfg_file)
if os.path.isfile(fullname):
print('Loading ' + fullname + '...')
try:
alt_config = configure.Configuration.from_file(fullname).configure()
dest_config = source_config.merge(alt_config)
except configure.ConfigurationError:
# this is in most cases when the config file passed is empty
# configure.ConfigurationError: unconfigured
# in case of syntax error, another exception is thrown:
# TypeError: string indices must be integers, not str
pass
elif required:
print('Error: configration file %s does not exist' % (fullname))
sys.exit(1)
return dest_config
default_cfg_file = resource_string(__name__, "cfg.default.yaml")
# read the default configuration file and possibly an override config file
@ -824,17 +802,18 @@ def merge_opts_to_configs(opts):
# $HOME/.vmtp.yaml if exists
# -c <file> from command line if provided
# cfg.default.yaml
config = configure.Configuration.from_string(default_cfg_file).configure()
config = _merge_config('~/.vmtp.yaml', config)
config = config_loads(default_cfg_file)
local_cfg = os.path.expanduser('~/.vmtp.yaml')
if os.path.isfile(local_cfg):
config = config_load(local_cfg, config)
if opts.config:
config = _merge_config(opts.config, config, required=True)
config = config_load(opts.config, config)
if opts.show_cofig:
if opts.show_config:
print default_cfg_file
sys.exit(0)
if opts.version:
print(__version__)
sys.exit(0)