Skip to content

Commit 467ab50

Browse files
committed
add rename service
1 parent 23c2508 commit 467ab50

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

pgserviceparser/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ def remove_service(service_name: str, conf_file_path: Optional[Path] = None) ->
9292
config.write(configfile, space_around_delimiters=False)
9393

9494

95+
def rename_service(old_name: str, new_name: str, conf_file_path: Optional[Path] = None) -> None:
96+
"""Rename a service in the service file.
97+
98+
The service settings are preserved under the new name and the old
99+
section is removed.
100+
101+
Args:
102+
old_name: current service name
103+
new_name: desired service name
104+
conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used,
105+
defaults to None
106+
107+
Raises:
108+
ServiceFileNotFound: when the service file is not found
109+
ServiceNotFound: when the old service is not found
110+
PermissionError: when the service file is read-only
111+
"""
112+
config = full_config(conf_file_path)
113+
if old_name not in config:
114+
raise ServiceNotFound(
115+
service_name=old_name,
116+
existing_service_names=service_names(),
117+
pg_service_filepath=conf_file_path or conf_path(),
118+
)
119+
120+
settings = dict(config[old_name])
121+
config.remove_section(old_name)
122+
config[new_name] = settings
123+
with open(conf_file_path or conf_path(), "w") as configfile:
124+
config.write(configfile, space_around_delimiters=False)
125+
126+
95127
def service_config(service_name: str, conf_file_path: Optional[Path] = None) -> dict:
96128
"""Returns the config from the given service name as a dict.
97129

test/test_lib.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
conf_path,
2424
full_config,
2525
remove_service,
26+
rename_service,
2627
service_config,
2728
service_names,
2829
write_service,
@@ -140,6 +141,28 @@ def test_create_pgservice_file(self):
140141

141142
new_service_file_path.unlink()
142143

144+
def test_rename_service(self):
145+
with self.assertRaises(ServiceNotFound):
146+
rename_service("non_existing_service", "new_name")
147+
148+
# Rename service_1 -> service_1_renamed
149+
original_config = service_config("service_1")
150+
rename_service("service_1", "service_1_renamed")
151+
152+
# Old name should be gone
153+
self.assertNotIn("service_1", service_names())
154+
155+
# New name should exist with the same settings
156+
self.assertIn("service_1_renamed", service_names())
157+
self.assertEqual(service_config("service_1_renamed"), original_config)
158+
159+
# No whitespace around delimiters
160+
self.assertEqual(
161+
open(self.service_file_path).read().find(" = "),
162+
-1,
163+
"Whitespaces between delimiters were found, but should not be present",
164+
)
165+
143166
def test_remove_service(self):
144167
with self.assertRaises(ServiceNotFound):
145168
remove_service("non_existing_service")

0 commit comments

Comments
 (0)