github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/cli_config.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import logging 17 import os 18 19 import toml 20 21 22 LOGGER = logging.getLogger(__name__) 23 24 25 def _load_default_cli_config(): 26 return { 27 'url': 'http://localhost:8008' 28 } 29 30 31 def load_cli_config(args): 32 """Modifies ARGS in-place to have the attributes defined in the CLI 33 config file if it doesn't already have them. Certain default 34 values are given if they are not in ARGS or the config file. 35 """ 36 default_cli_config = _load_default_cli_config() 37 toml_config = _load_toml_cli_config() 38 39 for config in (toml_config, default_cli_config): 40 for key, val in config.items(): 41 if key in args and getattr(args, key) is not None: 42 pass 43 else: 44 setattr(args, key, val) 45 46 47 class CliConfigurationError(Exception): 48 pass 49 50 51 def _load_toml_cli_config(filename=None): 52 if filename is None: 53 filename = os.path.join( 54 _get_config_dir(), 55 'cli.toml') 56 57 if not os.path.exists(filename): 58 LOGGER.info( 59 "Skipping CLI config loading from non-existent config file: %s", 60 filename) 61 62 return {} 63 64 LOGGER.info("Loading CLI information from config: %s", filename) 65 66 try: 67 with open(filename) as fd: 68 raw_config = fd.read() 69 except IOError as e: 70 raise CliConfigurationError( 71 "Unable to load CLI configuration file: {}".format(str(e))) 72 73 return toml.loads(raw_config) 74 75 76 def _get_config_dir(): 77 if 'SAWTOOTH_HOME' in os.environ: 78 return os.path.join(os.environ['SAWTOOTH_HOME'], 'etc') 79 80 return '/etc/sawtooth'