Skip to content

Commit

Permalink
Merge pull request #8 from ccpgames/feature/output_filename_meta_tag
Browse files Browse the repository at this point in the history
Version 0.6.1 - SetFileName Fix
  • Loading branch information
CCP-Zeulix authored May 30, 2024
2 parents fe06f12 + 07c21e4 commit 77812f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
5 changes: 2 additions & 3 deletions _sandbox_template.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% setfilename %}
{{name}}_file.txt asd kasd
a dnlkjn .a?!289h$
{{colors.favorite}}_file.txt
{% endsetfilename %}
My name is {{name}} and I am {{age}} years old and my favorite color is NOT {{color}}
My name is {{name}} and I am {{age}} years old and my favorite color is NOT {{colors.weakness}}
2 changes: 1 addition & 1 deletion ccpstencil/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.6.0'
__version__ = '0.6.1'

__author__ = 'Thordur Matthiasson <thordurm@ccpgames.com>'
__license__ = 'MIT License'
Expand Down
4 changes: 3 additions & 1 deletion ccpstencil/cli/ccp_stencil/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def run(self):
rnd = self.get_renderer()
rnd.template = self.get_template()
rnd.context = self.get_context()
rnd.render()
res = rnd.render()
if self.output:
print(f'Wrote file: {res}')

def get_template(self) -> ITemplate:
if self.template:
Expand Down
2 changes: 1 addition & 1 deletion ccpstencil/cli/ccp_stencil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main():
parser.add_argument('-a', '--arg', action='append', help='Add additional Context arguments from the command line, e.g. -a foo=bar')

parser.add_argument('-o', '--output', help='Path or file to write the results to (otherwise its just printed to stdout).'
' If this is only a path, the name of the rendered file will be the same as the input template.',
' If this is a path (ends with /), the name of the rendered file will be the same as the input template.',
default='', nargs='?')
parser.add_argument('--no-overwrite', action="store_true", help='Makes sore existing output files are not overwritten')

Expand Down
24 changes: 17 additions & 7 deletions ccpstencil/renderer/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@
from ccpstencil.structs import *
from pathlib import Path
from ._string import *
import logging
log = logging.getLogger(__file__)


class FileRenderer(StringRenderer):
def __init__(self, output_path: Union[str, Path],
context: Optional[IContext] = None, template: Optional[ITemplate] = None,
overwrite: bool = True,
**kwargs):
self._overwrite = overwrite
super().__init__(context, template, **kwargs)
is_dir = False
log.debug(f'{output_path=}')
if isinstance(output_path, str):
if output_path.endswith('\\') or output_path.endswith('/'):
is_dir = True
output_path = Path(output_path)
self._output_path: Path = output_path
if not self._output_path.is_dir():
self.output_file_name = self._output_path.name
self._output_path = self._output_path.parent

self._overwrite = overwrite
super().__init__(context, template, **kwargs)
if not is_dir:
self.output_file_name = str(output_path.name)
self._output_path = output_path.parent
else:
self._output_path = output_path

log.debug(f'{self._output_path=}')
log.debug(f'{self.output_file_name=}')

def render(self) -> str:
return super().render()
Expand Down Expand Up @@ -60,4 +70,4 @@ def output_file_name(self) -> Optional[str]:

@output_file_name.setter
def output_file_name(self, value: str):
self._output_file_name = value
self._output_file_name = value

0 comments on commit 77812f0

Please sign in to comment.