pageit.render

pageit generator

class pageit.render.Pageit(path='.', ext='.mako', dry_run=False, noerr=False, ignore_mtime=False, watcher=None, tmpl=None, site=None, log=None)

Bases: object

Mako template renderer.

watcher pageit.tools.Watcher

underlying watcher for this path

Parameters:
  • path (str, optional) – path to traverse; default is current dir

  • ext (str, optional) – extension to look for

  • dry_run (bool, optional) – if True, print what would happen instead of rendering; default is False

  • noerr (bool, optional) – if True, don’t create error files; default is False

  • ignore_mtime (bool, optional) – if True, do not consider template modification times when rendering; default is False.

    Note

    pageit does not currently warn you if the output is newer than the template.

  • watcher (pageit.tools.Watcher, optional) – underlying watcher for the given path

    Note

    You may attach the watcher after this object has been constructed via the watcher attribute.

  • tmpl (mako.lookup.TemplateLookup, optioanl) – mako template lookup object

  • site (pageit.namespace.DeepNamespace, optional) –

    DeepNamespace passed to mako templates during rendering

  • log (logging.Logger, optional) – system logger

Changed in version 0.2.1: Added the site parameter.

clean()

Deletes pageit output files.

Note

This function only deletes files for which there is a corresponding template. If the template was moved or deleted, the output will not be touched.

Returns:Pageit – for method chaining
list()

Generates list of files to render / clean.

This function only lists files that end with the appropriate extension, will not enter directories that end with that extension.

Yields:str – next file to process
mako(path, dest=None)

Render a mako template.

This function injects two DeepNamespace variables into the mako template:

  • site: environment information passed into the constructor
  • page: information about the current template
    • path: relative path of this template
    • output: relative path to the output of the template
    • dirname: name of the directory containing the template
    • basedir: relative path back to the root directory
Parameters:
  • path (str) – template path
  • dest (str, optional) – output path; if not provided will be computed
Returns:

Pageit – for method chaining

Changed in version 0.2.2: Added more template information (output, dirname, basedir).

mako_deps(path)

Returns set of immediate dependency paths for a mako template.

Note

This function does not recursively compute dependencies.

Parameters:path (str) – path to a mako template
Returns:set – paths of dependencies

Examples

>>> Pageit().mako_deps('fake.mako')
set([])
mako_mtime(path, levels=5)

Returns the modification time of a mako template.

Note

This function considers a limited portion of the template’s inheritance tree to determine the latest modification time.

Parameters:
  • path (str) – template path
  • levels (int) – number of inheritance levels to traverse (default: 5)
Returns:

int – latest modification time; 0 if the file does not exist

Examples

>>> Pageit().mako_mtime('fake.mako')
0
>>> import os.path as osp
>>> path1 = osp.abspath('test/example1')
>>> path2 = osp.join(path1, 'subdir/test-page.html.mako')
>>> Pageit(path1).mako_mtime(path2) > 0
True
on_change(path=None)

React to a change in the directory.

Parameters:path (str) – path that changed
Returns:Pageit – for method chaining

Examples

>>> import os.path as osp
>>> runner = Pageit('test/example1')
>>> _ = runner.run()
>>> _ = runner.on_change('test/example1/index.html.mako')
>>> _ = runner.on_change(osp.abspath('test/example1/index.html'))
>>> _ = runner.clean()
>>> _ is runner
True
run()

Runs the renderer.

Returns:Pageit – for method chaining

Examples

>>> runner = Pageit('test/example1', dry_run=True)
>>> runner.run().clean() is runner
True
>>> runner = Pageit('test/example1', ignore_mtime=True)
>>> runner.run().clean() is runner
True
>>> runner = Pageit('test/example1')
>>> runner.run().clean() is runner
True
pageit.render.create_config(path='pageit.yml', env='default', log=None)

Constructs a DeepNamespace for attributes to pass to mako templates.

The configuration file should be a list of keys mapped to key/value pairs. The load process first loads an environment called “default” and then extends those keys to include those in the given environment.

Parameters:
  • path (str) – YAML configuration file
  • env (str, optional) – section to load
  • log (logging.Logger, optional) – system logger
Returns:

pageit.namespace.DeepNamespace

DeepNamespace of environment attributes

Examples

>>> create_config('file.yml', 'local') == DeepNamespace()
True
>>> conf = create_config('test/example1/pageit.yml', 'test')
>>> conf.debug == True
True
>>> conf = create_config('test/example1/pageit.yml', 'fakeenv')
>>> 'debug' not in conf
True

New in version 0.2.1.

pageit.render.create_logger(verbosity=1, log=None)

Constructs a logger.

Parameters:
  • verbosity (int) – level of verbosity
  • log (logging.Logger) – an existing logger to modify
Returns:

logging.Logger – logger configured based on verbosity.

Example

>>> log = create_logger()
>>> log.getEffectiveLevel() == logging.INFO
True
>>> log = create_logger(0, log)
>>> log.getEffectiveLevel() == logging.NOTSET
True
>>> log = create_logger(2, log)
>>> log.getEffectiveLevel() == logging.DEBUG
True
pageit.render.create_lookup(path='.', tmp=None)

Constructs a mako TemplateLookup object.

Parameters:
  • path (str) – top-level path to search for mako templates
  • tmp (str, optional) – directory to store generated modules
Returns:

mako.lookup.TemplateLookup – object to use for searching for templates

Example

>>> create_lookup() is not None
True
pageit.render.main()

Console entry point.

Constructs and dispatches the argument parser.

pageit.render.render(args)

Convenience method for Pageit.

Parameters:args (Namespace) – argh arguments

Changed in version 0.2.1: Added configuration loading.

pageit.render.strip_ext(path, ext)

Remove an extension from a path, if present.

Parameters:
  • path (str) – path from which to strip extension
  • ext (str) – extension to strip
Returns:

str – path with extension removed

Examples

>>> strip_ext('foo.bar', '.bar')
'foo'
>>> strip_ext('foo.bar', '.baz')
'foo.bar'