Skip to content

Commit

Permalink
ReST export plugin should accept --template option
Browse files Browse the repository at this point in the history
The option was accepted by `template` plugin only, but it absolutely
makes sense for `-h rst` to accept it as well. There is a default story
template, and no other template can be used...
  • Loading branch information
happz committed Jul 25, 2023
1 parent 7af0a2b commit 3b3ae1a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ def tests_import(
# TODO: move to `template` export plugin options
@option(
'--template', metavar='PATH',
help="Path to a template to use for rendering the export. Used with '--how=template' only."
help="Path to a template to use for rendering the export. Used with '--how=rst|template' only."
)
def tests_export(
context: Context,
Expand Down Expand Up @@ -1004,7 +1004,7 @@ def plans_create(
# TODO: move to `template` export plugin options
@option(
'--template', metavar='PATH',
help="Path to a template to use for rendering the export. Used with '--how=template' only."
help="Path to a template to use for rendering the export. Used with '--how=rst|template' only."
)
def plans_export(
context: Context,
Expand Down Expand Up @@ -1260,7 +1260,7 @@ def headfoot(text: str) -> None:
# TODO: move to `template` export plugin options
@option(
'--template', metavar='PATH',
help="Path to a template to use for rendering the export. Used with '--how=template' only."
help="Path to a template to use for rendering the export. Used with '--how=rst|template' only."
)
def stories_export(
context: Context,
Expand Down
9 changes: 7 additions & 2 deletions tmt/export/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tmt.export
import tmt.export.template
import tmt.utils
from tmt.utils import Path


@tmt.base.Story.provides_export('rst')
Expand All @@ -12,8 +13,10 @@ class RestructuredExporter(tmt.export.ExportPlugin):
def export_story(cls,
story: tmt.base.Story,
keys: Optional[List[str]] = None,
template: Optional[Path] = None,
include_title: bool = True) -> str:
return tmt.export.template.TemplateExporter.render_template(
template_filepath=template,
default_template_filename='default-story.rst.j2',
keys=keys,
STORY=story,
Expand All @@ -23,7 +26,9 @@ def export_story(cls,
def export_story_collection(cls,
stories: List[tmt.base.Story],
keys: Optional[List[str]] = None,
template: Optional[Path] = None,
include_title: bool = True,
**kwargs: Any) -> str:
return '\n\n'.join([cls.export_story(story, keys=keys, include_title=include_title)
for story in stories])
return '\n\n'.join([
cls.export_story(story, keys=keys, template=template, include_title=include_title)
for story in stories])
4 changes: 4 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5142,6 +5142,10 @@ def render_template_file(

return template.render(**variables).strip()

except FileNotFoundError as exc:
raise GeneralError(
f"Could not open template '{template_filepath}'.") from exc

except jinja2.exceptions.TemplateSyntaxError as exc:
raise GeneralError(
f"Could not parse template '{template_filepath}' at line {exc.lineno}.") from exc
Expand Down

0 comments on commit 3b3ae1a

Please sign in to comment.