-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
46 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from ..exceptions import TemplateFileNotFoundError | ||
from .base import AbstractPrompt | ||
|
||
|
||
class FileBasedPrompt(AbstractPrompt): | ||
"""Base class for prompts supposed to read template content from a file. | ||
`_path_to_template` attribute has to be specified. | ||
""" | ||
|
||
_path_to_template: str | ||
|
||
def __init__(self, **kwargs): | ||
if (template_path := kwargs.pop("path_to_template", None)) is not None: | ||
self._path_to_template = template_path | ||
else: | ||
current_dir_path = Path(__file__).parent | ||
self._path_to_template = os.path.join( | ||
current_dir_path, "..", self._path_to_template | ||
) | ||
|
||
super().__init__(**kwargs) | ||
|
||
@property | ||
def template(self) -> str: | ||
try: | ||
with open(self._path_to_template) as fp: | ||
return fp.read() | ||
except FileNotFoundError: | ||
raise TemplateFileNotFoundError( | ||
self._path_to_template, self.__class__.__name__ | ||
) | ||
except IOError as exc: | ||
raise RuntimeError( | ||
f"Failed to read template file '{self._path_to_template}': {exc}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters