Coverage for pesummary/core/file/formats/ini.py: 64.7%
34 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
1# Licensed under an MIT style license -- see LICENSE.md
3import configparser
4from pesummary.utils.utils import check_filename, logger
5from pesummary.utils.decorators import open_config
6from pesummary.utils.dict import convert_value_to_string
8__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"]
11def save_config_dictionary_to_file(
12 config_dict, outdir="./", filename=None, overwrite=False, label=None,
13 _raise=True, **kwargs
14):
15 """Save a dictionary containing the configuration settings to a file
17 Parameters
18 ----------
19 config_dict: dict
20 dictionary containing the configuration settings
21 outdir: str, optional
22 path indicating where you would like to configuration file to be
23 saved. Default is current working directory
24 filename: str, optional
25 rhe name of the file you wish to write to
26 overwrite: Bool, optional
27 If True, an existing file of the same name will be overwritten
28 label: str, optional
29 The label of the analysis. This is used in the filename if a filename
30 if not specified
31 """
32 _filename = check_filename(
33 default_filename="pesummary_{}.ini", outdir=outdir, label=label,
34 filename=filename, overwrite=overwrite
35 )
36 config = configparser.ConfigParser()
37 config.optionxform = str
38 if config_dict is None:
39 if _raise:
40 raise ValueError("No config data found. Unable to write to file")
41 logger.warning("No config data found. Unable to write to file")
42 return
44 for key in config_dict.keys():
45 config[key] = convert_value_to_string(config_dict[key])
47 with open(_filename, "w") as configfile:
48 config.write(configfile)
49 return _filename
52@open_config(index=0)
53def read_ini(path):
54 """Return the config data as a dictionary
56 Parameters
57 ----------
58 path: str
59 path to the configuration file
60 """
61 config = path
62 if config.error:
63 raise ValueError(
64 "Unable to open %s with configparser because %s" % (
65 config.path_to_file, config.error
66 )
67 )
68 sections = config.sections()
69 data = {}
70 if sections != []:
71 for i in sections:
72 data[i] = {}
73 for key in config["%s" % (i)]:
74 data[i][key] = config["%s" % (i)]["%s" % (key)]
75 return data
78def write_ini(
79 config_dictionary, outdir="./", label=None, filename=None, overwrite=False,
80 **kwargs
81):
82 """Write dictonary containing the configuration settings to an ini file
84 Parameters
85 ----------
86 config_dict: dict
87 dictionary containing the configuration settings
88 outdir: str, optional
89 path indicating where you would like to configuration file to be
90 saved. Default is current working directory
91 label: str, optional
92 The label of the analysis. This is used in the filename if a filename
93 if not specified
94 filename: str, optional
95 rhe name of the file you wish to write to
96 overwrite: Bool, optional
97 If True, an existing file of the same name will be overwritten
98 """
99 return save_config_dictionary_to_file(
100 config_dictionary, outdir=outdir, label=label, filename=filename,
101 overwrite=overwrite, **kwargs
102 )