diff options
author | Dominik Holland <dominik.holland@pelagicore.com> | 2017-12-15 15:19:25 +0100 |
---|---|---|
committer | jryannel <juergen@ryannel.org> | 2017-12-15 15:19:25 +0100 |
commit | 9da793d6cbc63d617ad5e2ffd13e9f6d055c88e8 (patch) | |
tree | 38b01d6122a095050ef2769545ba31ca9b568dcb | |
parent | e97451f627a9cc7a5ac86cd60277a0ed87204513 (diff) | |
download | qtivi-qface-9da793d6cbc63d617ad5e2ffd13e9f6d055c88e8.tar.gz |
Add a option to force writing the content to a file (#67)
This is needed when the generator is used inside a build system like make
as this is checking timestamps and only calls the generator when needed.
-rw-r--r-- | qface/generator.py | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/qface/generator.py b/qface/generator.py index a0514a3..21abe0e 100644 --- a/qface/generator.py +++ b/qface/generator.py @@ -138,7 +138,7 @@ class Generator(object): """Return the rendered text of a template instance""" return self.env.from_string(template).render(context) - def write(self, file_path: Path, template: str, context: dict={}, preserve: bool = False): + def write(self, file_path: Path, template: str, context: dict={}, preserve: bool = False, force: bool = False): """Using a template file name it renders a template into a file given a context """ @@ -146,7 +146,7 @@ class Generator(object): context = self.context error = False try: - self._write(file_path, template, context, preserve) + self._write(file_path, template, context, preserve, force) except TemplateSyntaxError as exc: message = '{0}:{1}: error: {2}'.format(exc.filename, exc.lineno, exc.message) click.secho(message, fg='red', err=True) @@ -162,12 +162,12 @@ class Generator(object): if error and Generator.strict: sys.exit(1) - def _write(self, file_path: Path, template: str, context: dict, preserve: bool = False): + def _write(self, file_path: Path, template: str, context: dict, preserve: bool = False, force: bool = False): path = self.destination / Path(self.apply(file_path, context)) path.parent.makedirs_p() logger.info('write {0}'.format(path)) data = self.render(template, context) - if self._has_different_content(data, path): + if self._has_different_content(data, path) or force: if path.exists() and preserve: click.secho('preserve: {0}'.format(path), fg='blue') else: |