@@ -105,50 +105,67 @@ def _version(self, response) -> str:
105
105
@classmethod
106
106
def _from_config_file (cls , config_file : str = "config.ini" , debug = None , enable_gzip = False ):
107
107
config = configparser .ConfigParser ()
108
- config .read (config_file )
108
+ is_json = False
109
+ try :
110
+ config .read (config_file )
111
+ except configparser .ParsingError :
112
+ with open (config_file ) as json_file :
113
+ import json
114
+ config = json .load (json_file )
115
+ is_json = True
109
116
110
- def config_value (key : str ):
111
- return config ['influx2' ][key ].strip ('"' )
117
+ def _config_value (key : str ):
118
+ value = str (config [key ]) if is_json else config ['influx2' ][key ]
119
+ return value .strip ('"' )
112
120
113
- url = config_value ('url' )
114
- token = config_value ('token' )
121
+ def _has_option (key : str ):
122
+ return key in config if is_json else config .has_option ('influx2' , key )
123
+
124
+ def _has_section (key : str ):
125
+ return key in config if is_json else config .has_section (key )
126
+
127
+ url = _config_value ('url' )
128
+ token = _config_value ('token' )
115
129
116
130
timeout = None
117
- if config . has_option ( 'influx2' , 'timeout' ):
118
- timeout = config_value ('timeout' )
131
+ if _has_option ( 'timeout' ):
132
+ timeout = _config_value ('timeout' )
119
133
120
134
org = None
121
- if config . has_option ( 'influx2' , 'org' ):
122
- org = config_value ('org' )
135
+ if _has_option ( 'org' ):
136
+ org = _config_value ('org' )
123
137
124
138
verify_ssl = True
125
- if config . has_option ( 'influx2' , 'verify_ssl' ):
126
- verify_ssl = config_value ('verify_ssl' )
139
+ if _has_option ( 'verify_ssl' ):
140
+ verify_ssl = _config_value ('verify_ssl' )
127
141
128
142
ssl_ca_cert = None
129
- if config . has_option ( 'influx2' , 'ssl_ca_cert' ):
130
- ssl_ca_cert = config_value ('ssl_ca_cert' )
143
+ if _has_option ( 'ssl_ca_cert' ):
144
+ ssl_ca_cert = _config_value ('ssl_ca_cert' )
131
145
132
146
connection_pool_maxsize = None
133
- if config . has_option ( 'influx2' , 'connection_pool_maxsize' ):
134
- connection_pool_maxsize = config_value ('connection_pool_maxsize' )
147
+ if _has_option ( 'connection_pool_maxsize' ):
148
+ connection_pool_maxsize = _config_value ('connection_pool_maxsize' )
135
149
136
150
auth_basic = False
137
- if config . has_option ( 'influx2' , 'auth_basic' ):
138
- auth_basic = config_value ('auth_basic' )
151
+ if _has_option ( 'auth_basic' ):
152
+ auth_basic = _config_value ('auth_basic' )
139
153
140
154
default_tags = None
141
- if config .has_section ('tags' ):
142
- tags = {k : v .strip ('"' ) for k , v in config .items ('tags' )}
143
- default_tags = dict (tags )
155
+ if _has_section ('tags' ):
156
+ if is_json :
157
+ default_tags = config ['tags' ]
158
+ else :
159
+ tags = {k : v .strip ('"' ) for k , v in config .items ('tags' )}
160
+ default_tags = dict (tags )
144
161
145
162
profilers = None
146
- if config . has_option ( 'influx2' , 'profilers' ):
147
- profilers = [x .strip () for x in config_value ('profilers' ).split (',' )]
163
+ if _has_option ( 'profilers' ):
164
+ profilers = [x .strip () for x in _config_value ('profilers' ).split (',' )]
148
165
149
166
proxy = None
150
- if config . has_option ( 'influx2' , 'proxy' ):
151
- proxy = config_value ('proxy' )
167
+ if _has_option ( 'proxy' ):
168
+ proxy = _config_value ('proxy' )
152
169
153
170
return cls (url , token , debug = debug , timeout = _to_int (timeout ), org = org , default_tags = default_tags ,
154
171
enable_gzip = enable_gzip , verify_ssl = _to_bool (verify_ssl ), ssl_ca_cert = ssl_ca_cert ,
0 commit comments