Skip to content

Commit 4b44400

Browse files
committed
Add write_service_to_text() to allow us to get a string formatted as if the service was written to a config file. That means, without spaces before/after '=', supporting '%' in values, with comments, etc.
1 parent 76e538e commit 4b44400

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pgserviceparser/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# standard library
22
import configparser
3+
import io
34
import platform
45
from os import getenv
56
from pathlib import Path
@@ -187,6 +188,27 @@ def write_service(
187188
return dict(config[service_name])
188189

189190

191+
def write_service_to_text(service_name: str, settings: dict) -> str:
192+
"""Returns the complete service settings as a string.
193+
194+
Args:
195+
service_name: service name
196+
settings: settings dict defining the service config
197+
198+
Returns:
199+
Service settings as a string
200+
"""
201+
config = configparser.ConfigParser(interpolation=None)
202+
config[service_name] = settings.copy()
203+
204+
config_stream = io.StringIO()
205+
config.write(config_stream, space_around_delimiters=False)
206+
res = config_stream.getvalue()
207+
config_stream.close()
208+
209+
return res.strip()
210+
211+
190212
def service_names(conf_file_path: Optional[Path] = None) -> list[str]:
191213
"""Returns all service names in a list.
192214

0 commit comments

Comments
 (0)