From afd332b2a93e6a32194e0a00a3ff978f0864e6ed Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 6 Nov 2022 17:37:38 +0100 Subject: [PATCH] add Rougail object --- src/rougail/__init__.py | 35 +++++++++++++++++++++++++++++++-- src/rougail/convert.py | 1 + src/rougail/template/base.py | 34 +++++++++++++++++++++++++++----- src/rougail/template/systemd.py | 21 +------------------- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/rougail/__init__.py b/src/rougail/__init__.py index afe927f3b..78800ccf0 100644 --- a/src/rougail/__init__.py +++ b/src/rougail/__init__.py @@ -28,8 +28,39 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ from .convert import RougailConvert +from .template.base import RougailBaseTemplate from .template.systemd import RougailSystemdTemplate from .config import RougailConfig -from rougail.update import RougailUpgrade +from tiramisu import Config +from .update import RougailUpgrade -__ALL__ = ('RougailConvert', 'RougailSystemdTemplate', 'RougailConfig', 'RougailUpgrade') + +class Rougail: + def __init__(self, + rougailconfig: RougailConfig=None, + ) -> None: + if rougailconfig is None: + rougailconfig = RougailConfig + self.rougailconfig = rougailconfig + self.converted = RougailConvert(self.rougailconfig) + self.config = None + + async def get_config(self): + if not self.config: + tiram_obj = self.converted.save(None) + optiondescription = {} + exec(tiram_obj, None, optiondescription) + self.config = await Config(optiondescription['option_0']) + return self.config + + async def template(self, + type: str='base', + ) -> None: + config = await self.get_config() + if type == 'base': + engine = RougailBaseTemplate(config, self.rougailconfig) + else: + engine = RougailSystemdTemplate(config, self.rougailconfig) + await engine.instance_files() + +__ALL__ = ('Rougail', 'RougailConvert', 'RougailBaseTemplate', 'RougailSystemdTemplate', 'RougailConfig', 'RougailUpgrade') diff --git a/src/rougail/convert.py b/src/rougail/convert.py index b482d7969..5c0ec5932 100644 --- a/src/rougail/convert.py +++ b/src/rougail/convert.py @@ -45,6 +45,7 @@ procedures. """ from typing import List +from tiramisu import Config from .i18n import _ from .config import RougailConfig diff --git a/src/rougail/template/base.py b/src/rougail/template/base.py index 212fcc891..bb37ecf5e 100644 --- a/src/rougail/template/base.py +++ b/src/rougail/template/base.py @@ -467,9 +467,20 @@ class RougailBaseTemplate: pass def process(self, - *args, - ): # pragma: no cover - raise NotImplementedError(_('cannot processed')) + filename: str, + destfilename: str, + mode: str, + owner: str, + group: str, + ) -> None: + if owner not in [None, self.rougailconfig['default_files_owner']]: + #FIXME + raise TemplateError(_(f'cannot change owner of file {destfilename}')) + if group not in [None, self.rougailconfig['default_files_group']]: + #FIXME + raise TemplateError(_(f'cannot change group of file {destfilename}')) + if mode not in [None, self.rougailconfig['default_files_mode']]: + chmod(destfilename, eval(f'0o{mode}')) def post_instance(self): # pragma: no cover pass @@ -480,9 +491,22 @@ class RougailBaseTemplate: raise NotImplementedError(_('cannot instanciate this service type ip')) def get_data_files(self, - *args, + filevar: Dict, + destfile: str, + service_name: str, + variable, + idx: int, ) -> None: # pragma: no cover - raise NotImplementedError(_('cannot instanciate this service type file')) + source = filevar['source'] + if not isfile(source): # pragma: no cover + raise FileNotFound(_(f'Source file "{source}" does not exist in {", ".join(self.templates_dir)}')) + tmp_file = join(self.tmp_dir, source) + #self.instance_file(fill, 'files') + if variable: + var = variable[idx] + else: + var = None + return tmp_file, None, destfile, var def get_data_service(self, *args, diff --git a/src/rougail/template/systemd.py b/src/rougail/template/systemd.py index 47e801784..b62915454 100644 --- a/src/rougail/template/systemd.py +++ b/src/rougail/template/systemd.py @@ -78,24 +78,6 @@ C %%filename %%file.mode %%file.owner %%file.group - {self.rougailconfig['tmpfil %end for """ - def get_data_files(self, - filevar: Dict, - destfile: str, - service_name: str, - variable, - idx: int, - ) -> tuple: - source = filevar['source'] - if not isfile(source): # pragma: no cover - raise FileNotFound(_(f'Source file "{source}" does not exist in {", ".join(self.templates_dir)}')) - tmp_file = join(self.tmp_dir, source) - #self.instance_file(fill, 'files') - if variable: - var = variable[idx] - else: - var = None - return tmp_file, None, destfile, var - def get_data_overrides(self, filevar: Dict, destfile, @@ -196,8 +178,7 @@ C %%filename %%file.mode %%file.owner %%file.group - {self.rougailconfig['tmpfil raise TemplateError(_(f'cannot change owner of file {destfilename}')) if group not in [None, self.rougailconfig['default_files_group']]: raise TemplateError(_(f'cannot change group of file {destfilename}')) - if mode not in [None, self.rougailconfig['default_files_mode']]: - chmod(destfilename, eval(f'0o{mode}')) + super().process(filename, destfilename, mode, owner, group) def post_instance(self): destfilename = join(self.destinations_dir, ROUGAIL_DEST_FILE[1:])