Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: possibility to precise which config name to load when loading config from file #586

Merged
merged 13 commits into from
Jun 29, 2023
8 changes: 5 additions & 3 deletions influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
pass

@classmethod
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def _from_config_file(cls, config_file: str = "config.ini", debug=None,
config_name: str = "influx2", enable_gzip=False, **kwargs):

config = configparser.ConfigParser()
is_json = False
try:
Expand All @@ -111,11 +113,11 @@ def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_g
is_json = True

def _config_value(key: str):
value = str(config[key]) if is_json else config['influx2'][key]
value = str(config[key]) if is_json else config[config_name][key]
return value.strip('"')

def _has_option(key: str):
return key in config if is_json else config.has_option('influx2', key)
return key in config if is_json else config.has_option(config_name, key)

def _has_section(key: str):
return key in config if is_json else config.has_section(key)
Expand Down
6 changes: 4 additions & 2 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def __exit__(self, exc_type, exc_value, traceback):
self.close()

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False,
config_name: str = "influx2", **kwargs):
"""
Configure client via configuration file. The configuration has to be under 'influx' section.

Expand Down Expand Up @@ -173,7 +174,8 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
}

"""
return InfluxDBClient._from_config_file(config_file=config_file, debug=debug, enable_gzip=enable_gzip, **kwargs)
return InfluxDBClient._from_config_file(config_file=config_file, config_name=config_name,
debug=debug, enable_gzip=enable_gzip, **kwargs)

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
Expand Down
7 changes: 4 additions & 3 deletions influxdb_client/client/influxdb_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ async def close(self):
self.api_client = None

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False,
config_name: str = "influx2", **kwargs):
"""
Configure client via configuration file. The configuration has to be under 'influx' section.

Expand Down Expand Up @@ -189,8 +190,8 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
}

"""
return InfluxDBClientAsync._from_config_file(config_file=config_file, debug=debug,
enable_gzip=enable_gzip, **kwargs)
return InfluxDBClientAsync._from_config_file(config_file=config_file, config_name=config_name,
debug=debug, enable_gzip=enable_gzip, **kwargs)

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions tests/config2.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[test_name]
url=http://localhost:8086
org=my-org
token=my-token
timeout=6000
connection_pool_maxsize=55
auth_basic=false
profilers=query, operator

[tags]
id = 132-987-655
customer = California Miner
data_center = ${env.data_center}
6 changes: 6 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def test_init_from_ini_file(self):

self.assertConfig()

def test_init_from_ini_file_custom_name(self):
self.client = InfluxDBClient.from_config_file(
f'{os.path.dirname(__file__)}/config2.ini', config_name='test_name')

self.assertConfig()

def test_init_from_toml_file(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.toml')

Expand Down