replace configure with attrdict
Change-Id: I226e9cd1fa44c9fe47490be7f781ab7030406cf3
This commit is contained in:
parent
1d161d9c30
commit
e03d1af38a
@ -5,7 +5,6 @@
|
|||||||
pbr<2.0,>=1.3
|
pbr<2.0,>=1.3
|
||||||
Babel>=1.3
|
Babel>=1.3
|
||||||
|
|
||||||
configure>=0.5
|
|
||||||
lxml>=3.4.0
|
lxml>=3.4.0
|
||||||
paramiko>=1.14.0
|
paramiko>=1.14.0
|
||||||
prettytable>=0.7.2
|
prettytable>=0.7.2
|
||||||
@ -17,3 +16,4 @@ python-openstackclient>=0.4.1
|
|||||||
python-keystoneclient>=1.0.0
|
python-keystoneclient>=1.0.0
|
||||||
scp>=0.8.0
|
scp>=0.8.0
|
||||||
tabulate>=0.7.3
|
tabulate>=0.7.3
|
||||||
|
attrdict>=2.0.0
|
||||||
|
49
vmtp/config.py
Normal file
49
vmtp/config.py
Normal 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('#')
|
@ -62,9 +62,9 @@ class Network(object):
|
|||||||
break
|
break
|
||||||
if not self.ext_net:
|
if not self.ext_net:
|
||||||
self.ext_net = network
|
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
|
# external network defined, so use the first one
|
||||||
###############################################
|
###############################################
|
||||||
self.ext_net = network
|
self.ext_net = network
|
||||||
|
39
vmtp/vmtp.py
39
vmtp/vmtp.py
@ -27,7 +27,8 @@ import traceback
|
|||||||
|
|
||||||
from __init__ import __version__
|
from __init__ import __version__
|
||||||
import compute
|
import compute
|
||||||
import configure
|
from config import config_load
|
||||||
|
from config import config_loads
|
||||||
import credentials
|
import credentials
|
||||||
from glanceclient.v2 import client as glanceclient
|
from glanceclient.v2 import client as glanceclient
|
||||||
import iperf_tool
|
import iperf_tool
|
||||||
@ -660,7 +661,7 @@ def parse_opts_from_cli():
|
|||||||
help='override default values with a config file',
|
help='override default values with a config file',
|
||||||
metavar='<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,
|
default=False,
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='print the default config')
|
help='print the default config')
|
||||||
@ -794,29 +795,6 @@ def parse_opts_from_cli():
|
|||||||
return parser.parse_known_args()[0]
|
return parser.parse_known_args()[0]
|
||||||
|
|
||||||
def merge_opts_to_configs(opts):
|
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")
|
default_cfg_file = resource_string(__name__, "cfg.default.yaml")
|
||||||
# read the default configuration file and possibly an override config file
|
# 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
|
# $HOME/.vmtp.yaml if exists
|
||||||
# -c <file> from command line if provided
|
# -c <file> from command line if provided
|
||||||
# cfg.default.yaml
|
# cfg.default.yaml
|
||||||
config = configure.Configuration.from_string(default_cfg_file).configure()
|
config = config_loads(default_cfg_file)
|
||||||
config = _merge_config('~/.vmtp.yaml', config)
|
local_cfg = os.path.expanduser('~/.vmtp.yaml')
|
||||||
|
if os.path.isfile(local_cfg):
|
||||||
|
config = config_load(local_cfg, config)
|
||||||
|
|
||||||
if opts.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
|
print default_cfg_file
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
if opts.version:
|
if opts.version:
|
||||||
print(__version__)
|
print(__version__)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user