Template reference

class nusex.api.Template(name: str)

A class representing a template.

Parameters

name (str) – The name of the template.

build(files: set[Path], project_name: str, *, project_slug: str | None = None, blueprint: type[blueprints.Blueprint] | str | None = None, profile: Profile | None = None, store_profile: bool = False, only_replace_profile_data: bool = False) None

Build this template.

Parameters
Keyword Arguments
  • project_slug (str | None) – The name to use as the project slug. If this is None, a project slug is automatically created. In the vast majority of cases, it’s best to leave this alone.

  • blueprint (type [blueprints.Blueprint]) – The blueprint class to use when building this template. This must be either type [Blueprint] or a subclass thereof. If no blueprint is passed, nusex will build a static template.

  • profile (Profile) – A profile instance.

  • store_profile (bool) – Whether to store the given profile’s attributes within the template. This effectively provides a series of default profile attributes that can be used if a profile is not provided during deployment, or if the profile is missing some attributes. A profile must be passed to use this.

  • only_replace_profile_data (bool) – Whether to only replace variables explicitly set as attributes in the given profile. If this is True, profile attributes set to None will not be replaced with placeholder variables when building. This is only really useful when storing the profile in the template itself. A profile must be passed to use this.

copy(*, name: str | None = None) Template

Create a new instance of this template with the same data.

Keyword Arguments

name (str | None) – The name to attribute to the copy. If this is None, “_copy” will be appended to the name. Defaults to None.

Returns

Template

The copied template.

property dependencies: list[str]

The list of dependencies stored in this template. If there are none, this is an empty list.

Returns

This template’s dependencies.

Return type

list [str]

deploy(to_dir: Path | str, *, project_name: str | None = None, project_slug: str | None = None, profile: Profile | None = None, force: bool = False) None

Deploy this template.

Parameters

to_dir (pathlib.Path | str) – The directory to deploy this template into.

Keyword Arguments
  • project_name (str | None) – The name of the project. If this is None, the project name is set to the name of the directory the template is being deployed to.

  • project_slug (str | None) – The name to use as the project slug. If this is None, a project slug is automatically created. In the vast majority of cases, it’s best to leave this alone.

  • profile (Profile | None) – The profile to use when deploying this template. Attributes in this profile will override any profile attributes stored within the template.

  • force (bool) – Whether to forcibly deploy this template, overwriting any existing files.

property files: dict[str, bytes]

The files stored in this template. If there are none, this is an empty dict.

Returns

This template’s files, with the filename as the key, and the file’s contents as the value.

Return type

dict [str, bytes]

find_files(in_dir: Path | str) set[Path]

Recursively search the given directory for files.

Parameters

in_dir (pathlib.Path | str) – The directory to search through.

Returns

The set of all files found.

Return type

set [pathlib.Path]

classmethod from_disk(name: str, *, from_dir: Path | str) Template

Load a saved template.

Parameters

name (str) – The name of the template to load.

Keyword Arguments

from_dir (pathlib.Path | str) – The directory to load the template from.

Returns

A template instance.

Return type

Template

property language: str | None

The language this template is for, or None if it has not been set.

Returns

The language this template is for, or None if it has not been set.

Return type

str

property path: Path | None

The filepath to this template. This is None if the template has not been saved.

Returns

The filepath to this template, or None if it has not been saved.

Return type

pathlib.Path

process_excludes(files: set[Path], *, use_defaults: bool = False, sources: list[Path | str] | None = None, patterns: list[str] | None = None) set[Path]

Compile a series of files to exclude from all given sources and expressions.

Parameters

files (set [pathlib.Path]) – A list of files to compare against exclude patterns. This should generally be obtained through find_files.

Keyword Arguments
  • use_defaults (bool) – Whether to use the default set of exclude patterns employed by the command-line interface.

  • sources (list [pathlib.Path | str] | None) – A list of files containing glob exclude patterns.

  • patterns (list [str]) – A list of glob exclude patterns.

Returns

The files to exclude from the template.

Return type

set [pathlib.Path]

property profile_data: dict[str, str | None]

The profile data stored in this template. If there is none, this is an empty dict.

Warning

This returns the profile data, not a profile instance. To get a profile instance, use Profile.from_template.

Returns

This template’s profile data.

Return type

dict [str, str]

save(*, to_dir: Path | str, overwrite: bool = False) Path

Save this template to disk.

Keyword Arguments
  • to_dir (pathlib.Path, str) – The directory to save the template to.

  • overwrite (bool) – Whether to overwrite an existing template. If this is False, an FileExistsError error will be raised if a template with the same name already exists. Defaults to False.

Returns

A pathlib.Path object with the full path to the template.

Return type

pathlib.Path

set_dependencies(*args: str) None

Set the dependencies for this template.

Note

Dependency installations are only supported for Python projects.

Parameters

*args (str) – A sequence of libraries to store as dependencies.