github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/rest_api/sawtooth_rest_api/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 collections 17 import logging 18 import os 19 import toml 20 21 from sawtooth_rest_api.exceptions import RestApiConfigurationError 22 23 24 LOGGER = logging.getLogger(__name__) 25 26 27 def load_default_rest_api_config(): 28 return RestApiConfig( 29 bind=["127.0.0.1:8008"], 30 connect="tcp://localhost:4004", 31 timeout=300, 32 client_max_size=10485760) 33 34 35 def load_toml_rest_api_config(filename): 36 """Returns a RestApiConfig created by loading a TOML file from the 37 filesystem. 38 """ 39 if not os.path.exists(filename): 40 LOGGER.info( 41 "Skipping rest api loading from non-existent config file: %s", 42 filename) 43 return RestApiConfig() 44 45 LOGGER.info("Loading rest api information from config: %s", filename) 46 47 try: 48 with open(filename) as fd: 49 raw_config = fd.read() 50 except IOError as e: 51 raise RestApiConfigurationError( 52 "Unable to load rest api configuration file: {}".format(str(e))) 53 54 toml_config = toml.loads(raw_config) 55 56 invalid_keys = set(toml_config.keys()).difference( 57 ['bind', 'connect', 'timeout', 'opentsdb_db', 'opentsdb_url', 58 'opentsdb_username', 'opentsdb_password', 'client_max_size']) 59 if invalid_keys: 60 raise RestApiConfigurationError( 61 "Invalid keys in rest api config: {}".format( 62 ", ".join(sorted(list(invalid_keys))))) 63 config = RestApiConfig( 64 bind=toml_config.get("bind", None), 65 connect=toml_config.get('connect', None), 66 timeout=toml_config.get('timeout', None), 67 opentsdb_url=toml_config.get('opentsdb_url', None), 68 opentsdb_db=toml_config.get('opentsdb_db', None), 69 opentsdb_username=toml_config.get('opentsdb_username', None), 70 opentsdb_password=toml_config.get('opentsdb_password', None), 71 client_max_size=toml_config.get('client_max_size', None) 72 ) 73 74 return config 75 76 77 def merge_rest_api_config(configs): 78 """ 79 Given a list of PathConfig objects, merges them into a single PathConfig, 80 giving priority in the order of the configs (first has highest priority). 81 """ 82 bind = None 83 connect = None 84 timeout = None 85 opentsdb_url = None 86 opentsdb_db = None 87 opentsdb_username = None 88 opentsdb_password = None 89 client_max_size = None 90 91 for config in reversed(configs): 92 if config.bind is not None: 93 bind = config.bind 94 if config.connect is not None: 95 connect = config.connect 96 if config.timeout is not None: 97 timeout = config.timeout 98 if config.opentsdb_url is not None: 99 opentsdb_url = config.opentsdb_url 100 if config.opentsdb_db is not None: 101 opentsdb_db = config.opentsdb_db 102 if config.opentsdb_username is not None: 103 opentsdb_username = config.opentsdb_username 104 if config.opentsdb_password is not None: 105 opentsdb_password = config.opentsdb_password 106 if config.client_max_size is not None: 107 client_max_size = config.client_max_size 108 109 return RestApiConfig( 110 bind=bind, 111 connect=connect, 112 timeout=timeout, 113 opentsdb_url=opentsdb_url, 114 opentsdb_db=opentsdb_db, 115 opentsdb_username=opentsdb_username, 116 opentsdb_password=opentsdb_password, 117 client_max_size=client_max_size) 118 119 120 class RestApiConfig: 121 def __init__( 122 self, 123 bind=None, 124 connect=None, 125 timeout=None, 126 opentsdb_url=None, 127 opentsdb_db=None, 128 opentsdb_username=None, 129 opentsdb_password=None, 130 client_max_size=None): 131 self._bind = bind 132 self._connect = connect 133 self._timeout = timeout 134 self._opentsdb_url = opentsdb_url 135 self._opentsdb_db = opentsdb_db 136 self._opentsdb_username = opentsdb_username 137 self._opentsdb_password = opentsdb_password 138 self._client_max_size = client_max_size 139 140 @property 141 def bind(self): 142 return self._bind 143 144 @property 145 def connect(self): 146 return self._connect 147 148 @property 149 def timeout(self): 150 return self._timeout 151 152 @property 153 def opentsdb_url(self): 154 return self._opentsdb_url 155 156 @property 157 def opentsdb_db(self): 158 return self._opentsdb_db 159 160 @property 161 def opentsdb_username(self): 162 return self._opentsdb_username 163 164 @property 165 def opentsdb_password(self): 166 return self._opentsdb_password 167 168 @property 169 def client_max_size(self): 170 return self._client_max_size 171 172 def __repr__(self): 173 # skip opentsdb_db password 174 return \ 175 "{}(bind={}, connect={}, timeout={}," \ 176 "opentsdb_url={}, opentsdb_db={}, opentsdb_username={}," \ 177 "client_max_size={})" \ 178 .format( 179 self.__class__.__name__, 180 repr(self._bind), 181 repr(self._connect), 182 repr(self._timeout), 183 repr(self._opentsdb_url), 184 repr(self._opentsdb_db), 185 repr(self._opentsdb_username), 186 repr(self._client_max_size)) 187 188 def to_dict(self): 189 return collections.OrderedDict([ 190 ('bind', self._bind), 191 ('connect', self._connect), 192 ('timeout', self._timeout), 193 ('opentsdb_url', self._opentsdb_url), 194 ('opentsdb_db', self._opentsdb_db), 195 ('opentsdb_username', self._opentsdb_username), 196 ('opentsdb_password', self._opentsdb_password), 197 ('client_max_size', self._client_max_size) 198 ]) 199 200 def to_toml_string(self): 201 return str(toml.dumps(self.to_dict())).strip().split('\n')