comparison sat/plugins/plugin_misc_app_manager.py @ 3565:d66a8453b02b

plugin app manager: add a way to create files: a `files` field can now be used to create files on the fly. Its value must be a dict where key are file names, and values are an other dict with file data. For now file data can only use the `content` key. Value of content will be written to the file. If a file already exists, it is not overwritten.
author Goffi <goffi@goffi.org>
date Sat, 12 Jun 2021 15:10:49 +0200
parents 2c9e95796371
children 402d31527af4
comparison
equal deleted inserted replaced
3564:2c9e95796371 3565:d66a8453b02b
546 del prepare[action] 546 del prepare[action]
547 547
548 if prepare: 548 if prepare:
549 raise exceptions.InternalError('"prepare" should be empty') 549 raise exceptions.InternalError('"prepare" should be empty')
550 550
551 async def _doCreateFiles(
552 self,
553 app_data: dict,
554 ) -> None:
555 dest_path = app_data['_instance_dir_path']
556 files = app_data.get('files')
557 if not files:
558 return
559 if not isinstance(files, dict):
560 raise ValueError('"files" must be a dictionary')
561 for filename, data in files.items():
562 path = dest_path / filename
563 if path.is_file():
564 log.info(f"{path} already exists, skipping")
565 with path.open("w") as f:
566 f.write(data.get("content", ""))
567 log.debug(f"{path} created")
568
551 async def startCommon(self, app_data: dict) -> None: 569 async def startCommon(self, app_data: dict) -> None:
552 """Method running common action when starting a manager 570 """Method running common action when starting a manager
553 571
554 It should be called by managers in "start" method. 572 It should be called by managers in "start" method.
555 """ 573 """
556 log.info(f"starting {app_data['name']!r}") 574 log.info(f"starting {app_data['name']!r}")
557 await self._doPrepare(app_data) 575 await self._doPrepare(app_data)
576 await self._doCreateFiles(app_data)