API

Dewar

class dewar.dewar.Site(path=None, create_backups=True)[source]

This is the root class of any dewar project, that encapsulates all the pages in a project.

Parameters:path – the path where the directories with site files can be found, such as the templates/ and static/ directories.
close()[source]

Remove a site from the global list of sites.

This functionality is rarely needed, as most applications will only involve one site. If you need it though, this will prevent this module’s site property from returning returning this site.

Note: The site property will still return this site if it is used in a page function registered to this site, or by a function that is called by a page function registered to this site.

register(path, validate=True)[source]

A decorator that registers a page function with a site.

Parameters:
  • path – The path to that page in the rendered site.
  • validate – If True, when the page function returns, it will raise an error if it doesn’t return a value that can create a page/pages.
render(path='./dist/')[source]

Write the site to a path.

Parameters:path – The path to write to.

Exceptions

exception dewar.exceptions.ValidationError[source]

Helpers

dewar.helpers.add_wrapper(content, wrapper='\n<!doctype html>\n\n<html lang="en"> <head> <meta charset="utf-8">\n</head>\n<body>\n{content}\n</body>\n</html>\n')[source]

Add a basic HTML template around some given text.

Parameters:
  • content (str) – The HTML to fill in.
  • wrapper (str) – A string that contains ‘{content}’.
Returns:

the wrapped content.

Return type:

str

dewar.helpers.freeze_func(arg=None)[source]

A decorator for a function, that causes it to return the same result every time it’s called (given the same arguments).

>>> @freeze_func()
>>> def f()
>>>     f.num += 1
>>>     return f.num
>>> f.num = 0
>>> f()
1
>>> f()
1
>>> f.num
2
dewar.helpers.load_data(path)[source]

Load the text of a data file at a path

Parameters:path (str or pathlib.Path) – the path of the data file, relative to the data directory of the site.
Returns:the text of the data.
Return type:str
dewar.helpers.load_data_dir(path)[source]

Load the text of every file in the data directory.

Parameters:path (str or pathlib.Path) – the path of the data directory, relative to the data directory of the site.
Returns:a dictionary of ‘file_name’: ‘text in file’
Return type:dict
dewar.helpers.load_json(data)[source]

Load json from text

Parameters:data (str) – the text of some data
Returns:a dictionary that matches the json found in the text.
Return type:dict
dewar.helpers.load_json_data(path)[source]

Load json from a path

Parameters:path – the path to some data (relative to a site’s data dir)
Returns:a dictionary that matches the json found at the path.
Return type:dict
dewar.helpers.load_md(data)[source]

Load markdown into html from text

Parameters:data (str) – the text of some data
Returns:a string with html that represents the markdown in a path.
Return type:dict
dewar.helpers.load_md_data(path)[source]

Load md into html from a path

Parameters:path – the path to some data (relative to a site’s data dir)
Returns:a string with html that represents the markdown in the file.
Return type:str
dewar.helpers.rel_url_to(path, start=None)[source]

Given a path, return a link from the current site to that path.

This uses get_closest_path() to find the starting point if start is None.

Parameters:
  • path (pathlib.Path or str) – the path to link to.
  • start (pathlib.Path or str) – The path to start from (a directory, not a file), or the closest path in the stack if None).
Returns:

A relative path from start to path

Return type:

str

dewar.helpers.render_template(template, **kwargs)[source]

Given a path to a template, and arguments to fill in, render that template.

Parameters:
  • template (path.Pathlib, str) – a path to a jinja template (relative to the template directory.)
  • keywords – the variables to be set as the context for the jinja template.
dewar.helpers.static_url(path, start=None)[source]

Given a path relative to the static folder, return a path relative to the current function

This will raise a warning if the static file doesn’t exist.

Parameters:
  • path (pathlib.Path or str) – the path to link to (relative to the static folder).
  • start (pathlib.Path or str) – The path to start from, (or the closest path in the the stack if None).
Returns:

A relative path from start to path

Return type:

str

dewar.helpers.url_for(function, start=None, **kwargs)[source]

Given a function (that takes kwargs as arguments), returns the relative path from start to the output of that function.

This uses get_closest_path() to find the starting point if start is None.

Parameters:
  • function (function, str) – The page function to link to, or its name.
  • start (pathlib.Path, str) – The path to start from, (or the closest path in the the stack if None).
  • kwargs – the arguments to function.
Returns:

A relative path from start to path

Return type:

str

Parser

dewar.parser.fill_path(path, params)[source]

Given a path formatted like /<thing>/<page>.html, and a list of params, return the filled path.

Parameters:
  • path (str) – a path containing “<variables>”.
  • params – a list of strings to fill into the path.
Return type:

list

dewar.parser.parse_path(path)[source]

Given a path formatted like /<category>/<page>.html, will return [‘category’, ‘page’].

Validator

dewar.validator.validate_page()

Internal Functions

class dewar._internal.InfoTuple(function_name, variables)
function_name

Alias for field number 0

variables

Alias for field number 1