confighelper: add method to write the config to any folder

Signed-off-by:  Eric Callahan <arksine.cod@gmail.com>
This commit is contained in:
Eric Callahan 2022-08-09 12:14:27 -04:00
parent a4f9fbe298
commit 0dd8eee034
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 29 additions and 0 deletions

View File

@ -806,6 +806,35 @@ class FileSourceWrapper(ConfigSourceWrapper):
self.read_file(entry)
return True
def write_config(
self, dest_folder: Union[str, pathlib.Path]
) -> Awaitable[None]:
eventloop = self.server.get_event_loop()
if self.server.is_running():
fut = eventloop.run_in_thread(self._do_write, dest_folder)
else:
self._do_write(dest_folder)
fut = eventloop.create_future()
fut.set_result(None)
return fut
def _do_write(self, dest_folder: Union[str, pathlib.Path]) -> None:
with self.save_lock:
if isinstance(dest_folder, str):
dest_folder = pathlib.Path(dest_folder)
dest_folder = dest_folder.expanduser().resolve()
cfg_parent = self.files[0].parent
for i, path in enumerate(self.files):
try:
rel_path = path.relative_to(cfg_parent)
dest_file = dest_folder.joinpath(rel_path)
except ValueError:
dest_file = dest_folder.joinpath(
f"{path.parent.name}-{path.name}"
)
os.makedirs(str(dest_file.parent), exist_ok=True)
dest_file.write_text(self.raw_config_data[i])
def _find_section_info(
self, section: str, file_data: List[str], raise_error: bool = True
) -> Dict[str, Any]: