fabricius.generator package#

Submodules#

fabricius.generator.errors module#

exception fabricius.generator.errors.NoContentError(file_name: str)#

Bases: FabriciusError

The file does not have any content.

exception fabricius.generator.errors.NoDestinationError(file_name: str)#

Bases: FabriciusError

The file does not know where to go.

exception fabricius.generator.errors.AlreadyCommittedError(file_name: str)#

Bases: FabriciusError

The file has already been committed/persisted.

fabricius.generator.file module#

class fabricius.generator.file.GeneratorCommitResult#

Bases: TypedDict

A CommitResult is returned when a file was successfully saved. It returns its information after its creation.

name: str#

The name of the file.

state: Literal['pending', 'persisted', 'deleted']#

The state of the file. Should always be “persisted”.

destination: Path#

Where the file is located/has been saved.

data: Dict[str, Any]#

The data that has been passed during rendering.

template_content: str#

The original content of the template.

content: str#

The resulting content of the saved file.

class fabricius.generator.file.FileGenerator(name: str, extension: Optional[str] = None)#

Bases: object

template_content: Optional[str]#

The content of the base template, if set.

name: str#

The name of the file that will be generated.

state: Literal['pending', 'persisted', 'deleted']#

The state of the file.

destination: Optional[Path]#

The destination of the file, if set.

renderer: Type[Renderer]#

The renderer to use to generate the file.

data: Dict[str, Any]#

The data that will be passed to the renderer.

from_file(path: Union[str, PathLike[str], Path]) Self#

Read the content from a file template.

Raises:

FileNotFoundError – If the file was not found.

Parameters:

path (str or pathlib.Path) – The path of the file template.

from_content(content: str) Self#

Read the content from a string.

Parameters:

content (str) – The template you want to format.

to_directory(directory: Union[str, PathLike[str], Path]) Self#

Set the directory where the file will be saved.

Raises:

NotADirectory – The given path exists but is not a directory.

Parameters:

directory (str or pathlib.Path) – Where the file will be stored. Does not include the file’s name.

use_mustache() Self#

Use chevron (Mustache) to render the template.

use_string_template() Self#

Use string.Template to render the template.

with_renderer(renderer: Type[Renderer]) Self#

Use a custom renderer to render the template.

Parameters:

renderer (Type of fabricius.generator.renderer.Renderer) – The renderer to use to format the file. It must be not initialized.

with_data(data: Dict[str, Any], *, overwrite: bool = True) Self#

Add data to pass to the template.

Parameters:
  • data (fabricius.const.Data) – The data you want to pass to the template.

  • overwrite (bool) – If the data that already exists should be deleted. If False, the new data will be added on top of the already existing data. Default to True.

generate() str#

Generate the file’s content.

Raises:

fabricius.generator.errors.NoContentError – If no content to the file were added.

Returns:

The final content of the file.

Return type:

str

commit(*, overwrite: bool = False, dry_run: bool = False) GeneratorCommitResult#

Save the file to the disk.

Parameters:
  • overwrite (bool) – If a file exist at the given path, shall the overwrite parameter say if the file should be overwritten or not. Default to False.

  • dry_run (bool) – You should not use this. This is mostly used for Fabricius’s tests. This parameter indicate if files should be created.

Raises:
Returns:

A typed dict with information about the created file.

Return type:

fabricius.generator.file.CommitResult

fabricius.generator.generator module#

class fabricius.generator.generator.Generator#

Bases: SupportsPlugin[GeneratorPlugin]

files: List[FileGenerator]#

The list of files to generate with the generator.

add_file(name: str, extension: Optional[str] = None) FileGenerator#

Add a file to the generator.

Parameters:
  • name (str) – The name of the file

  • extension (Optional, str) – The extension of the file, can be optional. If none, no extension will be added.

Returns:

The generated file. You then have to set file’s options.

Return type:

fabricius.generator.file.FileGenerator

execute(*, allow_overwrite: bool = False, dry_run: bool = False) Dict[FileGenerator, Optional[GeneratorCommitResult]]#

Execute generator’s tasks.

Parameters:
  • allow_overwrite (bool) – If files exist at their set path, shall this parameter say if files should be overwritten or not.

  • dry_run (bool) – You should not use this. This is mostly used for Fabricius’s tests. This parameter indicate if files should be created.

Returns:

A dict containing a file generator and its commit result. In case the value is None, this mean that the file was not successfully saved to the disk (Already committed, file already exists, etc.).

Return type:

Dict[fabricius.generator.file.FileGenerator, fabricius.generator.file.CommitResult]

fabricius.generator.renderer module#

class fabricius.generator.renderer.Renderer(data: Dict[str, Any])#

Bases: ABC

data: Dict[str, Any]#

A dictionary that contains data passed by the users to pass inside the template.

abstract render(content: str) str#
class fabricius.generator.renderer.PythonFormatRenderer(data: Dict[str, Any])#

Bases: Renderer

render(content: str) str#
class fabricius.generator.renderer.ChevronRenderer(data: Dict[str, Any])#

Bases: Renderer

render(content: str) str#
class fabricius.generator.renderer.StringTemplateRenderer(data: Dict[str, Any])#

Bases: Renderer

render(content: str) str#

Module contents#