Page building and views

class optimus.pages.registry.PageRegistry(elements={})[source]

Page registry

Index templates and memorize page destination that use them.

Keyword Arguments:
 elements (dict) – Initial element dictionnary. Default to an empty dict.
elements

Dictionnary indexed on template names which contain destinations using them.

Type:string
map_dest_to_page

Dictionnary indexed on destinations which contain their related page view.

Type:string
logger

Optimus logger.

Type:logging.Logger
add_page(page, templates)[source]

Add a page to registry.

Parameters:
  • page (optimus.pages.views.PageViewBase) – Page instance
  • templates (list) – List of templates names to link to given page instance.
get_pages_from_dependency(template_name)[source]

Get page list depending from a template.

This method is not safe out of the context of scanned pages, because it use an internal map builded from the scan use by the add_page method. In short, it will raise a KeyError exception for every destination that is unknowned from internal map.

Parameters:template_name (string) – Template name to search for.
Returns:List of page instances depending from given template name.
Return type:list
get_all_destinations()[source]

Return all registered destinations

Returns:List of all page destinations.
Return type:list
get_all_pages()[source]

Return all registered pages

Returns:List of all page instances.
Return type:list
class optimus.pages.builder.PageBuilder(settings, jinja_env=None, assets_env=None, dry_run=False)[source]

Builder class to init Jinja2 environment and build given pages.

Parameters:

settings (conf.model.SettingsModel) – Settings registry instance.

Keyword Arguments:
 
  • jinja_env (jinja2.Jinja2Environment) – Jinja2 environment. Default is None.
  • assets_env (webassets.Environment) – Webasset environment. Default is None.
  • dry_run (boolean) – Enable dry run mode. Default is False.
logger

Optimus logger.

Type:logging.Logger
settings

Settings registry instance.

Type:conf.model.SettingsModel
jinja_env

Jinja2 environment. Default is None.

Type:jinja2.Jinja2Environment
assets_env

Webasset environment. Default is None.

Type:webassets.Environment
internationalized

Indicate it internationalization is enabled. Will be automatically set to True if Jinja environment enable the i18n extension.

Type:boolean
translations

Dictionnary of translation catalog indexed on language identifier.

Type:dict
registry

Registry of all knowed page from scanning.

Type:optimus.pages.registry.PageRegistry
dry_run

Dry run mode.

Type:boolean
get_environnement(assets_env=None)[source]

Init and configure Jinja environment.

Automatically enable some extensions and link possible asset environment.

Keyword Arguments:
 assets_env (webassets.Environment) – Webasset environment. Default is None. If empty, webassets will not be available from page templates.
Returns:Configured Jinja2 environment.
Return type:jinja2.Jinja2Environment
serialize_settings()[source]

Get and return valid settings variables.

Valid settings means only variable names full uppercase.

Returns:Settings variables.
Return type:dict
get_globals()[source]

Get global context variables.

Returns:Shortcuts to some common settings like SITE options, static urls, Optimus version and finally all settings contained in _SETTINGS.
Return type:dict
get_translation_for_item(page_item)[source]

Try to load the translations for the page language if any, then install it in Jinja2.

It does not reload a language translations if a previous page has allready loaded it.

Parameters:page_item (optimus.pages.views.PageViewBase) – Page instance which its language identifier will be used to search for translation catalog.
Returns:Translations object to give to Jinja i18n extension.
Return type:babel.support.Translations
scan_item(page_item)[source]

Scan given page to retrieve template dependancies.

Possibly connect settings to page instance if not allready done.

Parameters:page_item (optimus.pages.views.PageViewBase) – Page instance.
Returns:All used templates from given page.
Return type:string
scan_bulk(page_list)[source]

Scan all given pages to set their dependancies

Parameters:page_list (list) – List of page instances.
Returns:Every template name involved in scanned page instances.
Return type:list
build_item(page_item)[source]

Build given page.

Possibly connect settings to page instance if not allready done.

Parameters:page_item (optimus.pages.views.PageViewBase) – Page instance.
Returns:Destination path from builded page.
Return type:string
build_bulk(page_list)[source]

Build all given pages.

Return all the effective builded pages

Parameters:page_list (list) – List of page instances.
Returns:List of destination paths from builded pages.
Return type:list
class optimus.pages.views.base.PageViewBase(**kwargs)[source]

Base view object for a page

You can set class attributes at the init if needed

The render method is responsible to rendering the HTML from the template and his context. Actually this is the only used method directly.

Only lang and context attributes are optional, so take care to set all the required ones because their default value is None. You should not use directly PageViewBase, inherit it in a common object with all attributes setted by default.

Template context will have the following variables :

page_title
Page title
page_destination
Page destination
page_lang
Given langage if any
page_template_name
Template name used to compile the page HTML

But you can add new variable if needed. The default context variables can not be overriden from the context class attribute, only from the get_context class method.

View need settings to be defined either as argument on instance init or later through attribute setter.

title

Page title.

Type:string
template_name

Page template file path relaive to templates directoy. Used as Python template string with optional non positional argument {{ language_code }} available for internationalized pages.

Type:string
destination

Page destionation path relative to build directory.

Type:string
lang

Language identifier or an instance of optimus.i18n.LangBase.

Type:string
context

Initial page template context.

Type:dict
logger

Optimus logger.

Type:logging.Logger
_used_templates

List of every used templates. Only filled when introspect() method is executed. Default to None.

Type:list
__settings

Settings registry instance when given in kwargs. Default to None.

Type:conf.model.SettingsModel
Parameters:**kwargs – Arbitrary keyword arguments. Will be added as object attribute.
validate()[source]

Validate every required attribute is set.

Returns:True if requirements are set.
Return type:boolean
settings

settings attribute getter, check settings have been correctly defined.

Returns:Settings registry instance when given in kwargs. Default to None.
Return type:conf.model.SettingsModel
get_title()[source]

Get page title.

Default behavior is to used page attribute title.

Returns:Page title.
Return type:string
get_lang()[source]

Get page language object.

Returns:Language object. If lang page attribute is None it will create a language object using default language identifier from setting LANGUAGE_CODE.
Return type:optimus.i18n.LangBase
get_destination()[source]

Get page destination path.

Returns:Page destination path relative to build directory.
Return type:string
get_relative_position()[source]

Get relative path position from the destination file to the root.

Returns:Either something like “../../” if the destination is in subdirectories or “./” if at the root. Won’t never return empty string.
Return type:string
get_template_name()[source]

Get template file path.

Returns:Template file path relative to templates directory.
Return type:string
get_context()[source]

Get template context.

Returns:Template context of variables.
Return type:dict
render(env)[source]

Take the Jinja2 environment as required argument.

Parameters:env (jinja2.Jinja2Environment) – Jinja environment.
Returns:HTML builded from page template with its context.
Return type:string

Load involved template sources from given template file path then find their template references.

Parameters:
  • env (jinja2.Jinja2Environment) – Jinja environment.
  • template_name (string) – Template file path.
Returns:

List of involved templates sources files.

Return type:

list

introspect(env)[source]

Take the Jinja2 environment as required argument to find every templates dependancies from page.

Parameters:env (jinja2.Jinja2Environment) – Jinja environment.
Returns:List of involved templates sources files.
Return type:list