2
2
Why configparser? Because its simple. There is a blockperf.ini file in the
3
3
contrib/ folder which has all options and a short explanation of what they do.
4
4
"""
5
- from pathlib import Path
6
- from configparser import ConfigParser
7
- import os
8
5
import json
6
+ import os
7
+ from configparser import ConfigParser
8
+ from pathlib import Path
9
+ from typing import Union
9
10
10
11
11
12
class ConfigError (Exception ):
@@ -15,9 +16,10 @@ class ConfigError(Exception):
15
16
class AppConfig :
16
17
config_parser : ConfigParser
17
18
18
- def __init__ (self , config : Path ):
19
+ def __init__ (self , config : Union [ Path , None ] ):
19
20
self .config_parser = ConfigParser ()
20
- self .config_parser .read (config )
21
+ if config :
22
+ self .config_parser .read (config )
21
23
22
24
# def validate_config(self):
23
25
# node_config_folder = node_config_path.parent
@@ -75,33 +77,48 @@ def network_magic(self) -> int:
75
77
76
78
@property
77
79
def relay_public_ip (self ) -> str :
78
- relay_public_ip = self .config_parser .get ("DEFAULT" , "relay_public_ip" )
80
+ relay_public_ip = os .getenv (
81
+ "BLOCKPERF_RELAY_PUBLIC_IP" ,
82
+ self .config_parser .get ("DEFAULT" , "relay_public_ip" , fallback = None )
83
+ )
79
84
if not relay_public_ip :
80
85
raise ConfigError ("'relay_public_ip' not set!" )
81
86
return relay_public_ip
82
87
83
88
@property
84
89
def relay_public_port (self ) -> int :
85
- relay_public_port = int (self .config_parser .get ("DEFAULT" , "relay_public_port" , fallback = 3001 ))
90
+ relay_public_port = int (os .getenv (
91
+ "BLOCKPERF_RELAY_PUBLIC_PORT" ,
92
+ self .config_parser .get ("DEFAULT" , "relay_public_port" , fallback = 3001 )
93
+ ))
86
94
return relay_public_port
87
95
88
96
@property
89
97
def client_cert (self ) -> str :
90
- client_cert = self .config_parser .get ("DEFAULT" , "client_cert" )
98
+ client_cert = os .getenv (
99
+ "BLOCKPERF_CLIENT_CERT" ,
100
+ self .config_parser .get ("DEFAULT" , "client_cert" , fallback = None )
101
+ )
91
102
if not client_cert :
92
103
raise ConfigError ("No client_cert set" )
93
104
return client_cert
94
105
95
106
@property
96
107
def client_key (self ) -> str :
97
- client_key = self .config_parser .get ("DEFAULT" , "client_key" )
108
+ client_key = os .getenv (
109
+ "BLOCKPERF_CLIENT_KEY" ,
110
+ self .config_parser .get ("DEFAULT" , "client_key" , fallback = None )
111
+ )
98
112
if not client_key :
99
113
raise ConfigError ("No client_key set" )
100
114
return client_key
101
115
102
116
@property
103
117
def operator (self ) -> str :
104
- operator = self .config_parser .get ("DEFAULT" , "operator" )
118
+ operator = os .getenv (
119
+ "BLOCKPERF_OPERATOR" ,
120
+ self .config_parser .get ("DEFAULT" , "operator" , fallback = None )
121
+ )
105
122
if not operator :
106
123
raise ConfigError ("No operator set" )
107
124
return operator
@@ -112,21 +129,31 @@ def lock_file(self) -> str:
112
129
113
130
@property
114
131
def topic_base (self ) -> str :
115
- return self .config_parser .get ("DEFAULT" , "topic_base" , fallback = "develop" )
132
+ topic_base = os .getenv (
133
+ "BLOCKPERF_TOPIC_BASE" ,
134
+ self .config_parser .get ("DEFAULT" , "topic_base" , fallback = "develop" )
135
+ )
136
+ return topic_base
116
137
117
138
@property
118
139
def mqtt_broker_url (self ) -> str :
119
- return str (
140
+ broker_url = os .getenv (
141
+ "BLOCKPERF_BROKER_URL" ,
120
142
self .config_parser .get (
121
143
"DEFAULT" ,
122
144
"mqtt_broker_url" ,
123
145
fallback = "a12j2zhynbsgdv-ats.iot.eu-central-1.amazonaws.com" ,
124
146
)
125
147
)
148
+ return broker_url
126
149
127
150
@property
128
151
def mqtt_broker_port (self ) -> int :
129
- return int (self .config_parser .get ("DEFAULT" , "mqtt_broker_port" , fallback = 8883 ))
152
+ broker_port = int (os .getenv (
153
+ "BLOCKPERF_BROKER_PORT" ,
154
+ self .config_parser .get ("DEFAULT" , "mqtt_broker_port" , fallback = 8883 )
155
+ ))
156
+ return broker_port
130
157
131
158
@property
132
159
def enable_tracelogs (self ) -> bool :
0 commit comments