page_tours.driver#

The shared bases of everything a page declares for Driver.js.

Module Contents#

Classes#

DriverObject

A declaration that mirrors one object of Driver.js.

PopoverContent

What a popover says and where it sits.

Declaration

What a page renders: a name, a version and Driver.js’s configuration.

Functions#

driver_value

Convert one value of a field to the form Driver.js takes.

forget

Drop the record of a collected declaration, and its name once none has it.

resolve_override

Resolve one entry of the PAGE_TOURS_OVERRIDES setting.

declaration_overrides

Resolve every entry of the PAGE_TOURS_OVERRIDES setting.

declarations

Return every declaration that exists, by its name.

API#

driver_value(value: Any) → Any[source]#

Convert one value of a field to the form Driver.js takes.

Parameters:

value – A declared object, a tuple or list, a lazy string or any other value.

Returns:

The object’s as_driver(), a list of the converted items, the string a lazy string stands for in the active language, or the value as it is.

class DriverObject[source]#

A declaration that mirrors one object of Driver.js.

The fields are named as Driver.js names them. A field left as None is left out, so the value Driver.js falls back to applies.

Variables:
  • package_fields – The fields that are the package’s own, which Driver.js never receives.

  • nested – The fields that Driver.js reads from an inner object, by the name of that object.

as_driver() → dict[str, Any][source]#

Convert the object to the form Driver.js takes.

Every inner object named in nested is created, even when nothing goes into it. Translated text is converted when this is called, so the active language applies.

Returns:

The fields that are neither package fields nor None, each converted, with the nested ones inside their inner object.

class PopoverContent[source]#

Bases: page_tours.driver.DriverObject

What a popover says and where it sits.

See https://driverjs.com/docs/popover for what each field does.

Variables:

popover_fields – The names of the fields below, which Driver.js reads from a popover.

Parameters:
  • title – The heading of the popover. Without one, the popover shows no heading.

  • description – The body of the popover.

  • side – Which side of the element the popover sits on.

  • align – How the popover lines up along that side.

  • popoverClass – A class added to the popover.

forget(name: str, ref: weakref.ref[Declaration]) → None[source]#

Drop the record of a collected declaration, and its name once none has it.

Parameters:
  • name – The name the declaration was declared with.

  • ref – The weak reference to the declaration, which the collector has cleared.

class Declaration[source]#

Bases: page_tours.driver.DriverObject

What a page renders: a name, a version and Driver.js’s configuration.

A subclass is one kind of declaration, such as a tour. It is recorded under its name when it is made, so the system checks can report a name declared twice or a PAGE_TOURS_OVERRIDES key that names nothing.

Variables:
  • kind – What the declaration is called in the browser, such as "tour".

  • content_field – The field that holds what the declaration shows, which the system checks compare to tell two declarations of one name apart.

Parameters:
  • name – The name the declaration is known by: a browser remembers what a visitor did under it, and PAGE_TOURS_OVERRIDES changes the declaration by it. Plain text, never empty.

  • version – A whole number of 1 or more. Raise it when the declaration has changed enough that what a browser remembers should be forgotten.

Raises:

ImproperlyConfigured – If name is not a string with something in it or version is not a whole number of 1 or more.

__post_init__()[source]#

Refuse a bad name or version and record the declaration under its name.

payload(name: str, version: int) → dict[str, Any][source]#

Return what the browser needs to play the declaration.

Parameters:
  • name – The name the browser remembers the declaration by.

  • version – The version it remembers it at.

Returns:

The kind, name, version, the classes of each part and the declaration as Driver.js takes it.

playing() → tuple[page_tours.driver.Declaration, int] | None[source]#

Return the declaration that plays under this one’s name, and its version.

The replacement the PAGE_TOURS_OVERRIDES setting names, at this declaration’s version plus the replacement’s, less one, so a browser that saw this one does not see the replacement until the replacement’s own version is raised. This declaration at its own version when the setting does not name it. The setting is read every time.

Returns:

The declaration to play and the version to play it at, or None when the setting turns this declaration off.

Raises:

ImproperlyConfigured – If the setting’s entry for this declaration is not valid or names a replacement of another class.

__html__() → django.utils.safestring.SafeString[source]#

Render the declaration as the element the package’s script plays it from.

This is what {{ declaration }} outputs in a template: a <script id="page-tours-<kind>" type="application/json"> element.

What plays is what playing() returns: the replacement the PAGE_TOURS_OVERRIDES setting names, under this declaration’s name, or nothing when the setting turns the declaration off.

Returns:

The element, marked safe so the template does not escape it, or an empty string when the declaration is turned off.

Raises:

ImproperlyConfigured – If the setting’s entry for this declaration is not valid or names a replacement of another class.

__str__() → django.utils.safestring.SafeString[source]#

Render the declaration, as __html__ does.

A template converts a value with str() before it escapes it, so this has to return the same safe string for the declaration to arrive unescaped.

Returns:

What __html__ returns.

resolve_override(name: str, value: Any, cls: type[page_tours.driver.Declaration] = Declaration) → page_tours.driver.Declaration | None[source]#

Resolve one entry of the PAGE_TOURS_OVERRIDES setting.

Parameters:
  • name – The name of the declaration the entry changes, for the error message.

  • value – A dotted path to a declaration, or None to turn it off.

  • cls – The class the replacement has to be.

Returns:

The replacement, or None when the declaration is turned off.

Raises:

ImproperlyConfigured – If value is neither a string nor None, the path does not import, or it names something that is not a cls. A module whose own declaration is invalid raises its own error.

declaration_overrides() → dict[str, page_tours.driver.Declaration | None][source]#

Resolve every entry of the PAGE_TOURS_OVERRIDES setting.

Returns:

The replacement, or None for a declaration turned off, by name.

Raises:

ImproperlyConfigured – If any entry is not valid.

declarations() → dict[str, list[page_tours.driver.Declaration]][source]#

Return every declaration that exists, by its name.

A declaration is declared when it is made, which for one in a module is when the module is imported. One that nothing refers to any more is left out.

Returns:

The declarations under each name, in the order they were made.