flexget.utils.template module#

exception flexget.utils.template.RenderError[source]#

Bases: Exception

Error raised when there is a problem with jinja rendering.

class flexget.utils.template.CoercingDateTime[source]#

Bases: DateTime

Datetime with some features that make it better when used in our templates.

The features are:

  • Avoids crashing when comparing tz aware and naive datetimes. When this happens, it will assume the naive datetime is in the same timezone as the dt aware one.

  • Allows comparisons with plain dates, where the date is assumed to be at midnight in the same timezone.

  • Returns Interval with a nicer string representation for our templates

This allows us to introduce tz aware datetimes into entry fields without breaking old configs, or old plugins.

static _same_tz(first, second)[source]#
diff(dt=None, abs=True)[source]#

Return the difference between two DateTime objects represented as an Interval.

Parameters:
Return type:

Interval

class flexget.utils.template.FlexGetEnvironment(block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', comment_start_string='{#', comment_end_string='#}', line_statement_prefix=None, line_comment_prefix=None, trim_blocks=False, lstrip_blocks=False, newline_sequence='\n', keep_trailing_newline=False, extensions=(), optimized=True, undefined=<class 'jinja2.runtime.Undefined'>, finalize=None, autoescape=False, loader=None, cache_size=400, auto_reload=True, bytecode_cache=None, enable_async=False)[source]#

Bases: Environment

Environment with template_class support.

Parameters:
  • block_start_string (str)

  • block_end_string (str)

  • variable_start_string (str)

  • variable_end_string (str)

  • comment_start_string (str)

  • comment_end_string (str)

  • line_statement_prefix (str | None)

  • line_comment_prefix (str | None)

  • trim_blocks (bool)

  • lstrip_blocks (bool)

  • newline_sequence (te.Literal['\n', '\r\n', '\r'])

  • keep_trailing_newline (bool)

  • extensions (Sequence[str | Type[Extension]])

  • optimized (bool)

  • undefined (Type[Undefined])

  • finalize (Callable[[...], Any] | None)

  • autoescape (bool | Callable[[str | None], bool])

  • loader (BaseLoader | None)

  • cache_size (int)

  • auto_reload (bool)

  • bytecode_cache (BytecodeCache | None)

  • enable_async (bool)

class flexget.utils.template.FlexGetNativeTemplate(source, block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', comment_start_string='{#', comment_end_string='#}', line_statement_prefix=None, line_comment_prefix=None, trim_blocks=False, lstrip_blocks=False, newline_sequence='\n', keep_trailing_newline=False, extensions=(), optimized=True, undefined=<class 'jinja2.runtime.Undefined'>, finalize=None, autoescape=False, enable_async=False)[source]#

Bases: FlexGetTemplate, NativeTemplate

Lazy lookup support and native python return types.

Parameters:
  • source (str | Template)

  • block_start_string (str)

  • block_end_string (str)

  • variable_start_string (str)

  • variable_end_string (str)

  • comment_start_string (str)

  • comment_end_string (str)

  • line_statement_prefix (str | None)

  • line_comment_prefix (str | None)

  • trim_blocks (bool)

  • lstrip_blocks (bool)

  • newline_sequence (te.Literal['\n', '\r\n', '\r'])

  • keep_trailing_newline (bool)

  • extensions (Sequence[str | Type[Extension]])

  • optimized (bool)

  • undefined (Type[Undefined])

  • finalize (Callable[[...], Any] | None)

  • autoescape (bool | Callable[[str | None], bool])

  • enable_async (bool)

Return type:

Any

class flexget.utils.template.FlexGetTemplate(source, block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', comment_start_string='{#', comment_end_string='#}', line_statement_prefix=None, line_comment_prefix=None, trim_blocks=False, lstrip_blocks=False, newline_sequence='\n', keep_trailing_newline=False, extensions=(), optimized=True, undefined=<class 'jinja2.runtime.Undefined'>, finalize=None, autoescape=False, enable_async=False)[source]#

Bases: Template

Adds lazy lookup support when rendering templates.

Parameters:
  • source (str | Template)

  • block_start_string (str)

  • block_end_string (str)

  • variable_start_string (str)

  • variable_end_string (str)

  • comment_start_string (str)

  • comment_end_string (str)

  • line_statement_prefix (str | None)

  • line_comment_prefix (str | None)

  • trim_blocks (bool)

  • lstrip_blocks (bool)

  • newline_sequence (te.Literal['\n', '\r\n', '\r'])

  • keep_trailing_newline (bool)

  • extensions (Sequence[str | Type[Extension]])

  • optimized (bool)

  • undefined (Type[Undefined])

  • finalize (Callable[[...], Any] | None)

  • autoescape (bool | Callable[[str | None], bool])

  • enable_async (bool)

Return type:

Any

new_context(vars=None, shared=False, locals=None)[source]#

Create a new Context for this template. The vars provided will be passed to the template. Per default the globals are added to the context. If shared is set to True the data is passed as is to the context without adding the globals.

locals can be a dict of local variables for internal usage.

class flexget.utils.template.Interval(start, end, absolute=False)[source]#

Bases: Interval

Same as normal Interval, but gives a better string representation for our templates.

Parameters:
  • start (_T)

  • end (_T)

  • absolute (bool)

Return type:

Self

flexget.utils.template.evaluate_expression(expression, context)[source]#

Evaluate a jinja expression using a given context with support for LazyDict (Entry).

Parameters:
  • expression (str) – A jinja expression to evaluate

  • context (Mapping) – dictlike, supporting LazyDicts

Return type:

Any

flexget.utils.template.extra_vars()[source]#
Return type:

dict

flexget.utils.template.filter_asciify(text)[source]#

Simplify text.

Parameters:

text (str)

Return type:

str

flexget.utils.template.filter_d(value, default_value='', boolean=True)#

Override the built-in Jinja default filter to set the boolean param to True by default.

Parameters:
  • default_value (str)

  • boolean (bool)

Return type:

str

flexget.utils.template.filter_date_suffix(date_str)[source]#

Return a date suffix for a given date.

Parameters:

date_str (str)

flexget.utils.template.filter_default(value, default_value='', boolean=True)[source]#

Override the built-in Jinja default filter to set the boolean param to True by default.

Parameters:
  • default_value (str)

  • boolean (bool)

Return type:

str

flexget.utils.template.filter_format_number(val, places=None, grouping=True)[source]#

Format a number according to the user’s locale.

Parameters:
  • places (int | None)

  • grouping (bool)

Return type:

str

flexget.utils.template.filter_format_size(size, si=False, unit=None)[source]#
Parameters:

size (float)

flexget.utils.template.filter_formatdate(val, format_str)[source]#

Return a string representation of a datetime object according to format string.

flexget.utils.template.filter_get_year(name)[source]#
Parameters:

name (str)

Return type:

str

flexget.utils.template.filter_pad(val, width, fillchar='0')[source]#

Pad a number or string with fillchar to the specified width.

Parameters:
Return type:

str

flexget.utils.template.filter_parse_size(val, si=False, match_re=None)[source]#

Parse human-readable file size to bytes.

Parameters:
Return type:

int

flexget.utils.template.filter_parsedate(val)[source]#

Attempt to parse a date according to the rules in ISO 8601 and RFC 2822.

flexget.utils.template.filter_pathbase(val='')[source]#

Return base name of a path.

Parameters:

val (Path | str)

Return type:

str

flexget.utils.template.filter_pathdir(val='')[source]#

Directory containing the given path.

Parameters:

val (Path | str)

Return type:

PureWindowsPath | PurePosixPath | Path

flexget.utils.template.filter_pathext(val='')[source]#

Extension of a path (including the ‘.’).

Parameters:

val (Path | str)

Return type:

str

flexget.utils.template.filter_pathname(val='')[source]#

Return base name of a path, without its extension.

Parameters:

val (Path | str)

Return type:

str

flexget.utils.template.filter_pathscrub(val, os_mode=None)[source]#

Replace problematic characters in a path.

Parameters:
  • val (str)

  • os_mode (str | None)

Return type:

str

flexget.utils.template.filter_re_replace(val, pattern, repl)[source]#

Perform a regexp replacement on the given string.

Parameters:
  • val (AnyStr)

  • pattern (str)

  • repl (str)

Return type:

str

Perform a search for given regexp pattern, return the matching portion of the text.

Parameters:

pattern (str)

flexget.utils.template.filter_strip_symbols(text)[source]#

Strip Symbols text.

Parameters:

text (str)

Return type:

str

flexget.utils.template.filter_strip_year(name)[source]#
Parameters:

name (str)

Return type:

str

flexget.utils.template.filter_to_date(date_time_val)[source]#

Return the date from any date-time object.

flexget.utils.template.get_filters()[source]#

Return all built-in and custom Jinja filters in a dict.

The key is the name, and the value is the filter func

Return type:

dict

flexget.utils.template.get_template(template_name, scope='task')[source]#

Load a template from disk. Looks in both included plugins and users custom scope dir.

Parameters:
  • template_name (str)

  • scope (str | None)

Return type:

FlexGetTemplate

flexget.utils.template.is_fs_dir(pathname)[source]#

Test whether item is existing directory in filesystem.

Parameters:

pathname (Path | str)

Return type:

bool

flexget.utils.template.is_fs_file(pathname)[source]#

Test whether item is existing file in filesystem.

Parameters:

pathname (Path | str)

Return type:

bool

Test whether item is existing link in filesystem.

Parameters:

pathname (Path | str)

Return type:

bool

flexget.utils.template.list_templates(extensions=None)[source]#

Return all templates names that are configured under environment loader dirs.

Parameters:

extensions (list[str] | None)

Return type:

list[str]

flexget.utils.template.make_environment(manager)[source]#

Create our environment and add our custom filters.

Parameters:

manager (Manager)

Return type:

None

flexget.utils.template.render(template, context, native=False)[source]#

Render a Template with context as its context.

Parameters:
  • template (FlexGetTemplate | str) – Template or template string to render.

  • context (Mapping) – Context to render the template from.

  • native (bool) – If True, and the rendering result can be all native python types, not just strings.

Returns:

The rendered template text.

Return type:

str

flexget.utils.template.render_from_entry(template, entry, native=False)[source]#

Render a Template or template string with an Entry as its context.

Parameters:
Return type:

str

flexget.utils.template.render_from_task(template, task)[source]#

Render a Template with a task as its context.

Parameters:
  • template (FlexGetTemplate | str) – Template or template string to render.

  • task (Task) – Task to render the template from.

Returns:

The rendered template text.

Return type:

str